aboutsummaryrefslogtreecommitdiff
path: root/pd_rhythm_ioi_histogram.c
blob: 15dadf7e64dc964ba275d216b5222b39c7ae2cc3 (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
/* Rhythm estimation in real time -- PD wrapper
   Copyright (C) 2000 Jarno Seppänen and Piotr Majdak
   $Id: pd_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 <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "m_pd.h"

#include "pd_rhythm_estimator.h"
#include "pd_rhythm_ioi_histogram.h"
#include "rhythm_ioi_histogram.h"


static t_class* pd_rhythm_ioi_histogram_class = NULL;

/* We need a reference systime value */
static double pd_rhythm_ioi_histogram_setup_systime = 0;

void
rhythm_ioi_histogram_setup (void)
{
    pd_rhythm_ioi_histogram_class = class_new (gensym ("rhythm_ioi_histogram"),
					(t_newmethod)pd_rhythm_ioi_histogram_new,
					(t_method)pd_rhythm_ioi_histogram_free,
					sizeof (pd_t_rhythm_ioi_histogram),
					0,
					A_DEFSYM, 0);
    assert (pd_rhythm_ioi_histogram_class != NULL);

    /* Initialize setup time variable */
    pd_rhythm_ioi_histogram_setup_systime = clock_getsystime ();

    /* Set the callback for bang messages */
    class_addbang (pd_rhythm_ioi_histogram_class, pd_rhythm_ioi_histogram_bang);
    /* Set the callback for float messages */
    class_addfloat (pd_rhythm_ioi_histogram_class,
		    pd_rhythm_ioi_histogram_float);
    /* SET-Message */
    class_addmethod (pd_rhythm_ioi_histogram_class,
		     (t_method)pd_rhythm_ioi_histogram_set,
		     gensym("set"),
		     A_SYMBOL, A_FLOAT, 0);
    /* PRINT-Message */
    class_addmethod (pd_rhythm_ioi_histogram_class,
		     (t_method)pd_rhythm_ioi_histogram_print,
		     gensym("print"),
		     0);
}

static void*
pd_rhythm_ioi_histogram_new (t_symbol* s)
{
    pd_t_rhythm_ioi_histogram* x = NULL;

    /* Allocate object struct */
    x = (pd_t_rhythm_ioi_histogram*)pd_new (pd_rhythm_ioi_histogram_class);
    assert (x != NULL);

    /* Construct rhythm_ioi_histogram */
    x->rhythm_ioi_histogram = rhythm_ioi_histogram_new ();
    assert (x->rhythm_ioi_histogram != NULL);
    rhythm_ioi_histogram_initialize (x->rhythm_ioi_histogram);

    /* Initialize debug array */
    pd_rhythm_ioi_histogram_array_initialize (x, s);

    /* Inlet(s) already created? */

    /* Create outlet(s) */

    /* Bang message outlet */
    outlet_new (&x->x_obj, gensym ("bang"));

    return x;
}

static void
pd_rhythm_ioi_histogram_free (pd_t_rhythm_ioi_histogram* x)
{
    /* precondition(s) */
    assert (x != NULL);

    pd_rhythm_ioi_histogram_array_finish (x);

    /* Destruct rhythm_ioi_histogram */
    rhythm_ioi_histogram_finish (x->rhythm_ioi_histogram);
    rhythm_ioi_histogram_delete (x->rhythm_ioi_histogram);
    x->rhythm_ioi_histogram = NULL;
}

static void
pd_rhythm_ioi_histogram_bang (pd_t_rhythm_ioi_histogram* x)
{
    /* precondition(s) */
    assert (x != NULL);

    /* a "bang" message is equal to a float "0" message */
    pd_rhythm_ioi_histogram_float (x, 0);
}

static void
pd_rhythm_ioi_histogram_float (pd_t_rhythm_ioi_histogram* x, t_floatarg f)
{
#if RHYTHM_ESTIMATOR_DEBUG_PROFILE
    double time; /* for debugging */
#endif

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

    /* Invoke rhythm_ioi_histogram onset event */
#if RHYTHM_ESTIMATOR_DEBUG_PROFILE
    time = sys_getrealtime ();
#endif
    if (rhythm_ioi_histogram_onset_event (x->rhythm_ioi_histogram,
					  pd_rhythm_ioi_histogram_get_time ())
	!= 0)
    {
      /* Update our histogram array */
#if RHYTHM_ESTIMATOR_DEBUG_PROFILE
      printf("ioi_histo: B'4 write time: %f\n",  (sys_getrealtime()- time)*1000.);
#endif
	pd_rhythm_ioi_histogram_array_write (x,
					     x->rhythm_ioi_histogram->main_ioi_histogram,
					     x->rhythm_ioi_histogram->hist_length);	
	/* Send a "bang" message to the output */
#if RHYTHM_ESTIMATOR_DEBUG_PROFILE
	printf("ioi_histo: after write time: %f\n",  (sys_getrealtime()- time)*1000.);
#endif
	outlet_bang (x->x_obj.ob_outlet);
    }
#if RHYTHM_ESTIMATOR_DEBUG_PROFILE
    printf("ioi_histo: Used time: %f\n",  (sys_getrealtime()- time)*1000.);
#endif
}

static void
pd_rhythm_ioi_histogram_set (pd_t_rhythm_ioi_histogram* x,
			     t_symbol* s,
			     t_float value)
{

    /*  Format: 'parameter value' */

    if(strlen(s->s_name))
    {	
			/* Check for IOI_RESOLUTION */
	    if (!strncmp( s->s_name, RHYTHM_ESTIMATOR_IOI_RESOLUTION_STR
		      , strlen(RHYTHM_ESTIMATOR_IOI_RESOLUTION_STR)))
	    {   
		x->rhythm_ioi_histogram->ioi_resolution =
		    ParameterCheck(RHYTHM_ESTIMATOR_IOI_RESOLUTION_STR, 
				   value, 
				   RHYTHM_ESTIMATOR_IOI_RESOLUTION_MIN,
				   RHYTHM_ESTIMATOR_IOI_RESOLUTION_MAX);
		return;
	    }
	    else	    /* Check for MIN_QUANTUM */
	    if (!strncmp(s->s_name, RHYTHM_ESTIMATOR_MIN_QUANTUM_STR
		, strlen(RHYTHM_ESTIMATOR_MIN_QUANTUM_STR)))
	    {   
		x->rhythm_ioi_histogram->min_quantum =
		    ParameterCheck(RHYTHM_ESTIMATOR_MIN_QUANTUM_STR, 
				   value, 
				   RHYTHM_ESTIMATOR_MIN_QUANTUM_MIN,
				   RHYTHM_ESTIMATOR_MIN_QUANTUM_MAX);
		return;
	    }
	    else	    /* Check for MAX_QUANTUM */
	    if (!strncmp(s->s_name, RHYTHM_ESTIMATOR_MAX_QUANTUM_STR
		, strlen(RHYTHM_ESTIMATOR_MAX_QUANTUM_STR)))
	    {	
		x->rhythm_ioi_histogram->max_quantum =
		    ParameterCheck(RHYTHM_ESTIMATOR_MAX_QUANTUM_STR, 
				   value, 
				   RHYTHM_ESTIMATOR_MAX_QUANTUM_MIN,
				   RHYTHM_ESTIMATOR_MAX_QUANTUM_MAX);
		return;
	    }
	    else	    /* Check for HIST_HALF_LIFE */
	    if (!strncmp(s->s_name,RHYTHM_IOI_HISTOGRAM_HIST_HALF_LIFE_STR
		, strlen(RHYTHM_IOI_HISTOGRAM_HIST_HALF_LIFE_STR)))
	    {	
		x->rhythm_ioi_histogram->hist_half_life =
		    ParameterCheck(RHYTHM_IOI_HISTOGRAM_HIST_HALF_LIFE_STR, 
				   value, 
				   RHYTHM_IOI_HISTOGRAM_HIST_HALF_LIFE_MIN,
				   RHYTHM_IOI_HISTOGRAM_HIST_HALF_LIFE_MAX);
		return;
	    }
	    else	    /* Check for _HIST_CYCLES */
	    if (!strncmp(s->s_name, RHYTHM_IOI_HISTOGRAM_HIST_CYCLES_STR
		, strlen(RHYTHM_IOI_HISTOGRAM_HIST_CYCLES_STR)))
	    {	
		x->rhythm_ioi_histogram->hist_cycles =
		    ParameterCheck(RHYTHM_IOI_HISTOGRAM_HIST_CYCLES_STR, 
				   value, 
				   RHYTHM_IOI_HISTOGRAM_HIST_CYCLES_MIN,
				   RHYTHM_IOI_HISTOGRAM_HIST_CYCLES_MAX);
		return;
	    }
    }
    printf("Error, use format like: 'parameter value'\n");
    printf("Valid 'parameter' for rhythm_ioi_resolution:\n    ");
    printf(RHYTHM_ESTIMATOR_IOI_RESOLUTION_STR);
    printf("\n    ");
    printf(RHYTHM_ESTIMATOR_MIN_QUANTUM_STR);
    printf("\n    ");
    printf(RHYTHM_ESTIMATOR_MAX_QUANTUM_STR);
    printf("\n    ");
    printf(RHYTHM_IOI_HISTOGRAM_HIST_HALF_LIFE_STR);
    printf("\n    ");
    printf(RHYTHM_IOI_HISTOGRAM_HIST_CYCLES_STR);
    printf("\n\n");
}

static void
pd_rhythm_ioi_histogram_print (pd_t_rhythm_ioi_histogram* x)
{
    
    printf("HISTOGRAM:\n    %s: %0.1f",
	   RHYTHM_ESTIMATOR_IOI_RESOLUTION_STR, x->rhythm_ioi_histogram->ioi_resolution);
    printf("\n    ");
    printf("%s: %0.1f",RHYTHM_ESTIMATOR_MIN_QUANTUM_STR, x->rhythm_ioi_histogram->min_quantum);
    printf("\n    ");
    printf("%s: %0.1f",RHYTHM_ESTIMATOR_MAX_QUANTUM_STR, x->rhythm_ioi_histogram->max_quantum);
    printf("\n    ");
    printf("%s: %0.1f",RHYTHM_IOI_HISTOGRAM_HIST_HALF_LIFE_STR, 
	   x->rhythm_ioi_histogram->hist_half_life);
    printf("\n    ");
    printf("%s: %0.1f",RHYTHM_IOI_HISTOGRAM_HIST_CYCLES_STR, 
	   x->rhythm_ioi_histogram->hist_cycles);
    printf("\n\n");					 
}

static double
pd_rhythm_ioi_histogram_get_time (void)
{
    return clock_gettimesince (pd_rhythm_ioi_histogram_setup_systime);
}

static void
pd_rhythm_ioi_histogram_array_initialize (pd_t_rhythm_ioi_histogram* x,
				   t_symbol* s)
{
    t_garray *a;
    x->array_symbol = s;

    /* Find the array by name */

    a = (t_garray*)pd_findbyclass (x->array_symbol, garray_class);
    if (a == 0)
    {
    	if (s->s_name != NULL)
	{
	    pd_error(x, "rhythm_ioi_histogram: %s: no such array",
		     x->array_symbol->s_name);
	}
    	x->array_vec = 0;
    }

    /* Initialize array buffer length and pointer */

    else if (!garray_getfloatarray (a, &x->array_nsampsintab, &x->array_vec))
    {
    	error("rhythm_ioi_histogram: %s: bad array", x->array_symbol->s_name);
    	x->array_vec = 0;
    }
    else garray_usedindsp (a);

    if (x->array_vec == NULL)
    {
	printf ("rhythm_ioi_histogram: array %s not given\n", x->array_symbol->s_name);
    }
}

void
pd_rhythm_ioi_histogram_array_write (pd_t_rhythm_ioi_histogram* x, float* vector,
			      unsigned length)
{
    t_garray *a;

    /* Find the array by name and test if we still have an array */

    a = (t_garray*)pd_findbyclass (x->array_symbol, garray_class);
    if (a == 0)
    {
    	if (x->array_symbol->s_name != NULL)
	{
	    pd_error(x, "rhythm_ioi_histogram: %s: no such array",
		     x->array_symbol->s_name);
	}
    	x->array_vec = 0;
    }

    /* Initialize array buffer length and pointer */

    else if (!garray_getfloatarray (a, &x->array_nsampsintab, &x->array_vec))
    {
    	error("rhythm_ioi_histogram: %s: bad array", x->array_symbol->s_name);
    	x->array_vec = 0;
    }
    else
    {
	unsigned i = 0;

	garray_usedindsp (a);

	/* Copy histogram content to array */

	if (length > x->array_nsampsintab)
	{
	    printf ("warning: array (%d) is shorter than histogram (%d)\n",
		    x->array_nsampsintab, length);
	    length = x->array_nsampsintab;
	}
	for (i = 0; i < length; i++)
	{
	    x->array_vec[i] = vector[i];
	}

	/* Redraw array on screen, if needed -- FIXME don't */
	/*garray_redraw (a);*/
    }
}

static void
pd_rhythm_ioi_histogram_array_finish (pd_t_rhythm_ioi_histogram* x)
{
}

/* EOF */