aboutsummaryrefslogtreecommitdiff
path: root/pd_rhythm_quantum.c
blob: 28cdfe3fd57cfa5c59696f0b94315a0416997c37 (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
/* Rhythm estimation in real time -- PD wrapper
   Copyright (C) 2000 Jarno Seppänen and Piotr Majdak
   $Id: pd_rhythm_quantum.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_quantum.h"
#include "rhythm_quantum.h"

static t_class* pd_rhythm_quantum_class = NULL;

void
rhythm_quantum_setup (void)
{
    pd_rhythm_quantum_class = class_new (gensym ("rhythm_quantum"),
					(t_newmethod)pd_rhythm_quantum_new,
					(t_method)pd_rhythm_quantum_free,
					sizeof (pd_t_rhythm_quantum),
					0,
					A_DEFSYM, 0);
    assert (pd_rhythm_quantum_class != NULL);

    /* Set the callback for bang messages */
    class_addbang (pd_rhythm_quantum_class, pd_rhythm_quantum_bang);

    /* SET-Message */
    class_addmethod(pd_rhythm_quantum_class,
		    (t_method)pd_rhythm_quantum_set,
		    gensym("set"),
		    A_SYMBOL, A_FLOAT, 0);
    /* PRINT-Message */
    class_addmethod(pd_rhythm_quantum_class,
		    (t_method)pd_rhythm_quantum_print,
		    gensym("print"),
		    0);
}

static void*
pd_rhythm_quantum_new (t_symbol* s)
{
    pd_t_rhythm_quantum* x = NULL;

    /* Allocate object struct */
    x = (pd_t_rhythm_quantum*)pd_new (pd_rhythm_quantum_class);
    assert (x != NULL);

    /* Construct rhythm_quantum */
    x->rhythm_quantum = rhythm_quantum_new ();
    assert (x->rhythm_quantum != NULL);
    rhythm_quantum_initialize (x->rhythm_quantum);

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

    /* Create outlet(s) */

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

    return x;
}

static void
pd_rhythm_quantum_free (pd_t_rhythm_quantum* x)
{
    /* precondition(s) */
    assert (x != NULL);

    pd_rhythm_quantum_array_finish (x);

    /* Destruct rhythm_quantum */
    rhythm_quantum_finish (x->rhythm_quantum);
    rhythm_quantum_delete (x->rhythm_quantum);
    x->rhythm_quantum = NULL;
}

static void
pd_rhythm_quantum_bang (pd_t_rhythm_quantum* x)
{
    float *vector;
    unsigned len;
    unsigned i;
#if RHYTHM_ESTIMATOR_DEBUG_PROFILE
    double time;
#endif

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

    /* Invoke rhythm_quantum onset event */

    pd_rhythm_quantum_array_read(x, &vector, &len);
#if RHYTHM_ESTIMATOR_DEBUG_PROFILE
    time = sys_getrealtime();
#endif
    outlet_float(x->x_obj.ob_outlet,
		 rhythm_quantum_compute_quantum (x->rhythm_quantum, vector, len));

#if RHYTHM_ESTIMATOR_DEBUG_PROFILE
    printf("quantum: Used time: %f\n",  (sys_getrealtime()- time)*1000.);
#endif

    /* FIXME - Only for debugging: copy error function to histogram array */
#if RHYTHM_ESTIMATOR_DEBUG_QUANTUM
    for(i=0; i<x->rhythm_quantum->gcd_re_length; i++)
	vector[i]=x->rhythm_quantum->gcd_remainder_error[i];
    garray_redraw ((t_garray*)pd_findbyclass (x->array_symbol, garray_class));
#endif
}

static void
pd_rhythm_quantum_set (pd_t_rhythm_quantum* x,
		       t_symbol* s,
		       t_float value)
{

    /*  Format: s 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)))
	    {   
		/*		rhythm_ioi_quantum_set_ioi_resolution(x, value);*/
		x->rhythm_quantum->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_quantum->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_quantum->max_quantum =
		    ParameterCheck(RHYTHM_ESTIMATOR_MAX_QUANTUM_STR, 
				   value, 
				   RHYTHM_ESTIMATOR_MAX_QUANTUM_MIN,
				   RHYTHM_ESTIMATOR_MAX_QUANTUM_MAX);
		return;
	    }
	    else	    /* Check for GCD_PERC */
	    if (!strncmp(s->s_name,RHYTHM_QUANTUM_GCD_PERC_STR
		, strlen(RHYTHM_QUANTUM_GCD_PERC_STR)))
	    {	
		x->rhythm_quantum->gcd_perc =
		    ParameterCheck(RHYTHM_QUANTUM_GCD_PERC_STR, 
				   value, 
				   RHYTHM_QUANTUM_GCD_PERC_MIN,
				   RHYTHM_QUANTUM_GCD_PERC_MAX);
		return;
	    }
    }
    printf("Error, use format like: 'parameter value'\n");
    printf("Valid 'parameter' for rhythm_quantum:\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_QUANTUM_GCD_PERC_STR);
    printf("\n\n");
}

static void
pd_rhythm_quantum_print (pd_t_rhythm_quantum* x)
{
    
    printf("QUANTUM-Paramater:\n    %s: %0.1f",
	   RHYTHM_ESTIMATOR_IOI_RESOLUTION_STR, x->rhythm_quantum->ioi_resolution);
    printf("\n    ");
    printf("%s: %0.1f",RHYTHM_ESTIMATOR_MIN_QUANTUM_STR, x->rhythm_quantum->min_quantum);
    printf("\n    ");
    printf("%s: %0.1f",RHYTHM_ESTIMATOR_MAX_QUANTUM_STR, x->rhythm_quantum->max_quantum);
    printf("\n    ");
    printf("%s: %0.1f",RHYTHM_QUANTUM_GCD_PERC_STR, 
	   x->rhythm_quantum->gcd_perc);
    printf("\n\n");					 
}

static void
pd_rhythm_quantum_array_initialize (pd_t_rhythm_quantum* 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_quantum: %s: no such array",
		     x->array_symbol->s_name);
	}
    	x->array_vec = 0;
    }

    /* Get array buffer length and pointer */

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

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

static void
pd_rhythm_quantum_array_read (pd_t_rhythm_quantum* 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_quantum: %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_quantum: %s: bad array", x->array_symbol->s_name);
    	x->array_vec = 0;
    }
    else
    {
	garray_usedindsp (a);
	*vector = x->array_vec;
	*length = x->array_nsampsintab;
    }
}

static void
pd_rhythm_quantum_array_finish (pd_t_rhythm_quantum* x)
{
}

/* EOF */