aboutsummaryrefslogtreecommitdiff
path: root/rhythm_ioi_histogram.c
blob: 8d9dc76450e088eda44f03fb5d812213152eda10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/* Rhythm estimation in real time
   Copyright (C) 2000 Jarno Seppänen and Piotr Majdak
   $Id: rhythm_ioi_histogram.c,v 1.1 2002-11-19 11:39:55 ggeiger Exp $

   This file is part of rhythm_estimator.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; either version 2
   of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */

#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#include "rhythm_ioi_histogram.h"


Rhythm_Ioi_Histogram*
rhythm_ioi_histogram_new (void)
{
    Rhythm_Ioi_Histogram* ge = NULL;

#if RHYTHM_ESTIMATOR_DEBUG_TEXT
    fputs ("rhythm_ioi_histogram_new\n", stderr);
#endif

    /* Allocate memory for our data structure */
    ge = calloc (1, sizeof (Rhythm_Ioi_Histogram));
    if (ge == NULL)
    {
	printf ("Not enough memory.");
	assert (0);
    }

    /* Initialize data members */
    ge->ioi_resolution = RHYTHM_ESTIMATOR_DEFAULT_IOI_RESOLUTION;
    ge->min_quantum = RHYTHM_ESTIMATOR_DEFAULT_MIN_QUANTUM;
    ge->max_quantum = RHYTHM_ESTIMATOR_DEFAULT_MAX_QUANTUM;
    ge->hist_cycles = RHYTHM_IOI_HISTOGRAM_DEFAULT_HIST_CYCLES;
    ge->hist_half_life = RHYTHM_IOI_HISTOGRAM_DEFAULT_HIST_HALF_LIFE;

    /* Initialize onset FIFO */
    ge->fifo_length = RHYTHM_IOI_HISTOGRAM_IOI_FIFO_LENGTH;
    ge->fifo_buffer = calloc (ge->fifo_length, sizeof (float));
    if (ge->fifo_buffer == NULL)
    {
	printf("Not enough memory.");
	assert (0);
    }
    
    /* Compute internal "helper" variables */
    assert (ge->ioi_resolution != 0);
    ge->hist_length = (unsigned)(ge->hist_cycles * ge->max_quantum
				 / ge->ioi_resolution);

    /* Initialize filling IOI histogram */
    ge->fill_ioi_histogram = calloc (ge->hist_length, sizeof (float));
    if (ge->fill_ioi_histogram == NULL)
    {
	printf("Not enough memory.");
	assert (0);
    }

    /* Initialize main IOI histogram */
    ge->main_ioi_histogram = calloc (ge->hist_length, sizeof (float));
    if (ge->main_ioi_histogram == NULL)
    {
	printf("Not enough memory.");
	assert (0);
    }
    
    return ge;
}

void
rhythm_ioi_histogram_delete (Rhythm_Ioi_Histogram* rhythm_ioi_histogram)
{
#if RHYTHM_ESTIMATOR_DEBUG_TEXT
    fputs ("rhythm_ioi_histogram_delete\n", stderr);
#endif

    if (rhythm_ioi_histogram->main_ioi_histogram != NULL)
    {
	free (rhythm_ioi_histogram->main_ioi_histogram);
	rhythm_ioi_histogram->main_ioi_histogram = NULL;
    }

    if (rhythm_ioi_histogram->fill_ioi_histogram != NULL)
    {
	free (rhythm_ioi_histogram->fill_ioi_histogram);
	rhythm_ioi_histogram->fill_ioi_histogram = NULL;
    }

    if (rhythm_ioi_histogram->fifo_buffer != NULL)
    {
	free (rhythm_ioi_histogram->fifo_buffer);
	rhythm_ioi_histogram->fifo_buffer = NULL;
    }
    rhythm_ioi_histogram->fifo_length = 0;
    rhythm_ioi_histogram->fifo_num_onsets = 0;

    if (rhythm_ioi_histogram != NULL)
    {
	free (rhythm_ioi_histogram);
	rhythm_ioi_histogram = NULL;
    }
}

void
rhythm_ioi_histogram_initialize (Rhythm_Ioi_Histogram* ge)
{
    unsigned i = 0;

    /* precondition(s) */
    assert (ge != NULL);

#if RHYTHM_ESTIMATOR_DEBUG_TEXT
    fputs ("rhythm_ioi_histogram_initialize\n", stderr);
#endif

    /* Clear FIFO */
    ge->fifo_num_onsets = 0;
    ge->fifo_current_index = 0;
   
    /* Clear IOI histograms */
    for (i = 0; i < ge->hist_length; i++)
    {
	ge->fill_ioi_histogram[i] = 0.0;
	ge->main_ioi_histogram[i] = 0.0;
    }

    /* Initialize histogram update time variable */
    ge->main_histogram_update_time = 0;
}

void
rhythm_ioi_histogram_finish (Rhythm_Ioi_Histogram* rhythm_ioi_histogram)
{
    /* precondition(s) */
    assert (rhythm_ioi_histogram != NULL);

#if RHYTHM_ESTIMATOR_DEBUG_TEXT
    fputs ("rhythm_ioi_histogram_finish\n", stderr);
#endif
}

int
rhythm_ioi_histogram_onset_event (Rhythm_Ioi_Histogram* ge, double time)
{
    unsigned i = 0;

    /* precondition(s) */
    assert (ge != NULL);

#if RHYTHM_ESTIMATOR_DEBUG_TEXT
    fprintf (stderr, "rhythm_ioi_histogram_onset_event: time %f ms\n", time);
#endif

    /* Compute inter-onset interval (IOI) histogram fill function */
    for (i = 0; i < ge->fifo_num_onsets; i++)
    {
	double ioi;
	unsigned ioi_index;

	/* First, compute the exact IOI */

	/* (a) When the FIFO is not full, the onset values are
	       positioned in FIFO begin from 0 to num_onsets - 1
	   (b) When the FIFO is full, it doesn't matter in
	       which order the onsets are inspected, therefore
	       we loop from 0 to num_onsets - 1 (end of FIFO) */
	ioi = time - ge->fifo_buffer[i];

	/* Then, the exact "continuous" IOI is discretized (quantized) */
	ioi_index = (unsigned)rint (ioi / ge->ioi_resolution);

	/* Next, increment the proper "fill" histogram bin by one */
	if (ioi_index < ge->hist_length)
	{
	    ge->fill_ioi_histogram[ioi_index]++;
	    /* Set a flag for the periodic_event function */
	    ge->fill_histogram_changed = 1;
	}
    }

    /* Shift onset FIFO and store new onset */
    ge->fifo_buffer[ge->fifo_current_index] = time;
    ge->fifo_current_index = (ge->fifo_current_index + 1) % ge->fifo_length;
    if (ge->fifo_num_onsets < ge->fifo_length)
    {
	ge->fifo_num_onsets++;
    }
    if(ge->fill_histogram_changed)
    {
	rhythm_ioi_histogram_periodic_event(ge, time);
	ge->fill_histogram_changed = 0;
	/* Update PD-array */
	return 1;
    }
    else
    {
	return 0;
    }
}

void
rhythm_ioi_histogram_periodic_event (Rhythm_Ioi_Histogram* ge, double time)
{
    unsigned i = 0;
    double time_passed = 0.0;

    /* precondition(s) */
    assert (ge != NULL);

#if RHYTHM_ESTIMATOR_DEBUG_TEXT
    fprintf (stderr, "rhythm_ioi_histogram_periodic_event: time %f ms\n", time);
#endif

    /* Get the amount of time since last histogram update */
    if (ge->main_histogram_update_time == 0)
    {
	/* Have the new IOI's get through to the histogram */
	time_passed = 1.0e10;
    }
    else
    {
	/* Compute time since last update */
	time_passed = time - ge->main_histogram_update_time;
    }

    /* Save time for next event */
    ge->main_histogram_update_time = time;

    /* Accumulate the main IOI histogram */
    rhythm_ioi_histogram_update_ioi_histogram (ge, time_passed);

#if RHYTHM_ESTIMATOR_DEBUG_TEXT
    /* Dump main IOI histogram for debugging */
    printf ("HIST: ");
    for (i = 0; i < ge->hist_length; i++)
    {
	printf ("%f ", (double)(ge->main_ioi_histogram[i]));
    }
    printf ("\n.\n");
#endif
	
    /* Clear fill IOI histogram */
    for (i = 0; i < ge->hist_length; i++)
    {
	ge->fill_ioi_histogram[i] = 0;
    }
}

static void
rhythm_ioi_histogram_update_ioi_histogram (Rhythm_Ioi_Histogram* ge, double time_passed)
{
    unsigned i = 0;
    double coeff_old = 0.0;
    double coeff_new = 0.0;

    /* Compute the coefficients needed for update */
    coeff_old = pow (0.5, time_passed / ge->hist_half_life);
    /*coeff_new = (1 - coeff_old) / coeff_old;*/
    coeff_new = 1 - coeff_old;

#if RHYTHM_ESTIMATOR_DEBUG_TEXT
    printf ("time_passed=%.2f, coeff_old=%.2f, coeff_new=%.2f\n",
	    time_passed, coeff_old, coeff_new);
#endif

    /* Update main_ioi_histogram now */
    for (i = 0; i < ge->hist_length; i++)
    {
	ge->main_ioi_histogram[i] =
	    coeff_old * ge->main_ioi_histogram[i]
	    + coeff_new * ge->fill_ioi_histogram[i];
    }
}


float
rhythm_ioi_histogram_get_ioi_resolution (Rhythm_Ioi_Histogram* rhythm_ioi_histogram)
{
    /* precondition(s) */
    assert (rhythm_ioi_histogram != NULL);

    /* get the parameter */
    return rhythm_ioi_histogram->ioi_resolution;
}

float
rhythm_ioi_histogram_get_min_quantum (Rhythm_Ioi_Histogram* rhythm_ioi_histogram)
{
    /* precondition(s) */
    assert (rhythm_ioi_histogram != NULL);

    /* get the parameter */
    return rhythm_ioi_histogram->min_quantum;
}

float
rhythm_ioi_histogram_get_max_quantum (Rhythm_Ioi_Histogram* rhythm_ioi_histogram)
{
    /* precondition(s) */
    assert (rhythm_ioi_histogram != NULL);

    /* get the parameter */
    return rhythm_ioi_histogram->max_quantum;
}

float
rhythm_ioi_histogram_get_hist_cycles (Rhythm_Ioi_Histogram* rhythm_ioi_histogram)
{
    /* precondition(s) */
    assert (rhythm_ioi_histogram != NULL);

    /* get the parameter */
    return rhythm_ioi_histogram->hist_cycles;
}

float
rhythm_ioi_histogram_get_hist_half_life (Rhythm_Ioi_Histogram* rhythm_ioi_histogram)
{
    /* precondition(s) */
    assert (rhythm_ioi_histogram != NULL);

    /* get the parameter */
    return rhythm_ioi_histogram->hist_half_life;
}

void
rhythm_ioi_histogram_set_ioi_resolution (Rhythm_Ioi_Histogram* rhythm_ioi_histogram, float v)
{
    /* precondition(s) */
    assert (rhythm_ioi_histogram != NULL);

    /* sanity check(s) */
    if (v < 1)
    {
	fprintf (stderr, "ERROR rhythm_ioi_histogram: ioi_resolution must not be less than 1 ms\n");
	v = 1;
    }

    /* set the parameter */
    rhythm_ioi_histogram->ioi_resolution = v;
}

void
rhythm_ioi_histogram_set_min_quantum (Rhythm_Ioi_Histogram* rhythm_ioi_histogram, float v)
{
    /* precondition(s) */
    assert (rhythm_ioi_histogram != NULL);

    /* sanity check(s) */
    if (v < 1)
    {
	fprintf (stderr, "ERROR rhythm_ioi_histogram: min_quantum must not be less than 1 ms\n");
	v = 1;
    }

    /* set the parameter */
    rhythm_ioi_histogram->min_quantum = v;
}

void
rhythm_ioi_histogram_set_max_quantum (Rhythm_Ioi_Histogram* rhythm_ioi_histogram, float v)
{
    /* precondition(s) */
    assert (rhythm_ioi_histogram != NULL);

    /* sanity check(s) */
    if (v < 1)
    {
	fprintf (stderr, "ERROR rhythm_ioi_histogram: max_quantum must not be less than 1 ms\n");
	v = 1;
    }

    /* set the parameter */
    rhythm_ioi_histogram->max_quantum = v;
}

void
rhythm_ioi_histogram_set_hist_cycles (Rhythm_Ioi_Histogram* rhythm_ioi_histogram, float v)
{
    /* precondition(s) */
    assert (rhythm_ioi_histogram != NULL);

    /* sanity check(s) */
    if (v <= 0)
    {
	fprintf (stderr, "ERROR rhythm_ioi_histogram: hist_cycles must be positive\n");
	v = 1;
    }

    /* set the parameter */
    rhythm_ioi_histogram->hist_cycles = v;
}

void
rhythm_ioi_histogram_set_hist_half_life (Rhythm_Ioi_Histogram* rhythm_ioi_histogram, float v)
{
    /* precondition(s) */
    assert (rhythm_ioi_histogram != NULL);

    /* sanity check(s) */
    if (v <= 0)
    {
	fprintf (stderr, "ERROR rhythm_ioi_histogram: hist_half_life must be positive\n");
	v = 1;
    }

    /* set the parameter */
    rhythm_ioi_histogram->hist_half_life = v;
}


/* EOF */