jamie@141
|
1 /*
|
jamie@141
|
2 * Copyright (C) 2012 Jamie Bullock
|
jamie@141
|
3 *
|
jamie@141
|
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
|
jamie@141
|
5 * of this software and associated documentation files (the "Software"), to
|
jamie@141
|
6 * deal in the Software without restriction, including without limitation the
|
jamie@141
|
7 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
jamie@141
|
8 * sell copies of the Software, and to permit persons to whom the Software is
|
jamie@141
|
9 * furnished to do so, subject to the following conditions:
|
jamie@141
|
10 *
|
jamie@141
|
11 * The above copyright notice and this permission notice shall be included in
|
jamie@141
|
12 * all copies or substantial portions of the Software.
|
jamie@141
|
13 *
|
jamie@141
|
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
jamie@141
|
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
jamie@141
|
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
jamie@141
|
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
jamie@141
|
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
jamie@141
|
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
jamie@141
|
20 * IN THE SOFTWARE.
|
jamie@141
|
21 *
|
jamie@141
|
22 */
|
jamie@77
|
23
|
jamie@77
|
24 /* calculates the spectral xtract of one frame, given peak frequency and amplitude to first and second inputs respectively */
|
jamie@77
|
25
|
jamie@77
|
26 #include "ext.h"
|
jamie@77
|
27 #include "z_dsp.h"
|
jamie@77
|
28 #include <math.h>
|
jamie@77
|
29 #include <string.h>
|
jamie@77
|
30
|
jamie@77
|
31 #include "xtract/libxtract.h"
|
jamie@77
|
32
|
jamie@77
|
33 #define BLOCKSIZE 1024 /* FIX: this should be dynamic - somehow */
|
jamie@77
|
34 #define NYQUIST 22050.0f
|
jamie@77
|
35
|
jamie@77
|
36 void *xtract_tilde_class;
|
jamie@77
|
37
|
jamie@77
|
38 /* Struct for keeping track of memory allocations */
|
jamie@77
|
39 typedef struct _tracked_memory {
|
jamie@77
|
40 char argv;
|
jamie@77
|
41 } tracked_memory;
|
jamie@77
|
42
|
jamie@77
|
43 typedef struct _xtract {
|
jamie@77
|
44 t_pxobject x_obj;
|
jamie@77
|
45 void *outlet; /*Float outlet */
|
jamie@77
|
46 t_float f;
|
jamie@77
|
47 t_int feature;
|
jamie@98
|
48 t_symbol *feature_name;
|
jamie@98
|
49 t_int init_blocksize;
|
jamie@98
|
50 t_int done_init;
|
jamie@77
|
51 t_int feature_type;
|
jamie@77
|
52 tracked_memory memory;
|
jamie@77
|
53 void *argv;
|
jamie@146
|
54 double *data;
|
jamie@146
|
55 double *result;
|
jamie@77
|
56 } t_xtract_tilde;
|
jamie@77
|
57
|
jamie@77
|
58 static t_int *xtract_perform(t_int *w) {
|
jamie@78
|
59 t_float *in = (t_float *)(w[1]);
|
jamie@77
|
60 t_xtract_tilde *x = (t_xtract_tilde *)(w[2]);
|
jamie@77
|
61 t_int N = (t_int)(w[3]);
|
jamie@77
|
62 t_int return_code = 0;
|
jamie@146
|
63 double result = 0.f;
|
jamie@77
|
64
|
jamie@146
|
65 for(n = 0; n < N; ++n){
|
jamie@146
|
66 x->data[n] = (double)in[n];
|
jamie@146
|
67 }
|
jamie@146
|
68
|
jamie@146
|
69 return_code = xtract[x->feature](x->data, N, x->argv, &result);
|
jamie@77
|
70
|
jamie@77
|
71 if(return_code == XTRACT_FEATURE_NOT_IMPLEMENTED)
|
jamie@77
|
72 perror("Feature not implemented");
|
jamie@77
|
73
|
jamie@77
|
74 /* set nan, inf or -inf to 0 */
|
jamie@77
|
75 result = (isinf(result) || isnan(result) ? 0 : result);
|
jamie@77
|
76
|
jamie@146
|
77 outlet_float(x->outlet, (float)result);
|
jamie@77
|
78 return (w+4);
|
jamie@77
|
79 }
|
jamie@77
|
80
|
jamie@77
|
81 static t_int *xtract_perform_vector(t_int *w) {
|
jamie@78
|
82 t_sample *in = (t_float *)(w[1]);
|
jamie@78
|
83 t_sample *out = (t_float *)(w[2]);
|
jamie@77
|
84 t_xtract_tilde *x = (t_xtract_tilde *)(w[3]);
|
jamie@77
|
85 t_int N = (t_int)(w[4]), n;
|
jamie@77
|
86 t_int return_code = 0;
|
jamie@77
|
87
|
jamie@77
|
88 if(x->feature == XTRACT_PEAK_SPECTRUM)
|
jamie@77
|
89 N >>= 1;
|
jamie@77
|
90
|
jamie@98
|
91 if(N != x->init_blocksize && x->done_init){
|
jamie@98
|
92 post("xtract~ %s: Blocksize mismatch, try specifying the blocksize as a second argument", x->feature_name->s_name);
|
jamie@98
|
93 return (w+5);
|
jamie@98
|
94 }
|
jamie@146
|
95
|
jamie@146
|
96 for(n = 0; n < N; ++n){
|
jamie@146
|
97 x->data[n] = (double)in[n];
|
jamie@146
|
98 }
|
jamie@98
|
99
|
jamie@146
|
100 return_code = xtract[x->feature](x->data, N, x->argv, x->result);
|
jamie@77
|
101
|
jamie@146
|
102 for(n = 0; n < N; ++n){
|
jamie@146
|
103 out[n] = (float)x->result[n];
|
jamie@146
|
104 }
|
jamie@77
|
105
|
jamie@77
|
106 if(return_code == XTRACT_FEATURE_NOT_IMPLEMENTED)
|
jamie@77
|
107 perror("Feature not implemented");
|
jamie@77
|
108
|
jamie@77
|
109 return (w+5);
|
jamie@77
|
110 }
|
jamie@77
|
111
|
jamie@77
|
112 static void xtract_tilde_dsp(t_xtract_tilde *x, t_signal **sp) {
|
jamie@77
|
113
|
jamie@77
|
114 if(x->feature_type == XTRACT_VECTOR)
|
jamie@77
|
115 dsp_add(xtract_perform_vector, 4,
|
jamie@77
|
116 sp[0]->s_vec, sp[1]->s_vec, x, sp[0]->s_n);
|
jamie@77
|
117
|
jamie@77
|
118 else dsp_add(xtract_perform, 3, sp[0]->s_vec, x, sp[0]->s_n);
|
jamie@77
|
119
|
jamie@77
|
120 }
|
jamie@77
|
121
|
jamie@77
|
122 static void *xtract_tilde_new(t_symbol *me, t_int argc, t_atom *argv) {
|
jamie@77
|
123
|
jamie@77
|
124 t_symbol *tmp;
|
jamie@77
|
125 t_xtract_tilde *x = (t_xtract_tilde *)newobject(xtract_tilde_class);
|
jamie@77
|
126 xtract_mel_filter *mf;
|
jamie@98
|
127 t_int n, N, f, F, n_args, type;
|
jamie@77
|
128 t_float *argv_max;
|
jamie@77
|
129 xtract_function_descriptor_t *fd;
|
jamie@77
|
130 char *p_name, *p_desc, *author;
|
jamie@77
|
131 int year;
|
jamie@77
|
132
|
jamie@78
|
133 tmp = NULL;
|
jamie@77
|
134 p_name = p_desc = author = NULL;
|
jamie@77
|
135
|
jamie@77
|
136 n_args = type = x->feature = 0;
|
jamie@77
|
137
|
jamie@77
|
138 f = F = XTRACT_FEATURES;
|
jamie@77
|
139
|
jamie@98
|
140 N = BLOCKSIZE;
|
jamie@77
|
141
|
jamie@77
|
142 x->argv = NULL;
|
jamie@98
|
143 x->done_init = 0;
|
jamie@146
|
144
|
jamie@146
|
145 x->data = (double *)getbytes(N * sizeof(double));
|
jamie@146
|
146 x->result = (double *)getbytes(N * sizeof(double));
|
jamie@146
|
147
|
jamie@77
|
148
|
jamie@78
|
149 if(argc)
|
jamie@78
|
150 tmp = argv[0].a_w.w_sym; /*atom_getsymbol(argv); */
|
jamie@78
|
151 if(argc > 1)
|
jamie@98
|
152 N = (t_int)argv[1].a_w.w_long;
|
jamie@78
|
153
|
jamie@98
|
154 x->init_blocksize = N;
|
jamie@77
|
155
|
jamie@77
|
156 /* get function descriptors */
|
jamie@77
|
157 fd = (xtract_function_descriptor_t *)xtract_make_descriptors();
|
jamie@77
|
158
|
jamie@77
|
159 /* iterate over descriptors */
|
jamie@77
|
160 while(f--){
|
jamie@77
|
161 /* map creation arg to feature */
|
jamie@77
|
162 if(tmp == gensym(fd[f].algo.name)){
|
jamie@77
|
163 x->feature = f;
|
jamie@77
|
164 break;
|
jamie@77
|
165 }
|
jamie@77
|
166 }
|
jamie@77
|
167
|
jamie@77
|
168 /* allocate memory for feature arguments */
|
jamie@77
|
169 n_args = fd[f].argc;
|
jamie@77
|
170 type = fd[f].argv.type;
|
jamie@77
|
171
|
jamie@77
|
172 if(n_args){
|
jamie@77
|
173 for(n = 0; n < n_args; n++){
|
jamie@77
|
174 argv_max = &fd[f].argv.max[n];
|
jamie@77
|
175 //post("Argument %d, max: %.2f", n, *argv_max);
|
jamie@77
|
176 }
|
jamie@77
|
177 if(type == XTRACT_MEL_FILTER){
|
jamie@77
|
178 x->memory.argv = (size_t)(n_args * sizeof(xtract_mel_filter));
|
jamie@77
|
179 x->argv = (xtract_mel_filter *)getbytes(x->memory.argv);
|
jamie@77
|
180 }
|
jamie@77
|
181 else if(type == XTRACT_INT){
|
jamie@77
|
182 x->memory.argv = (size_t)(n_args * sizeof(t_int));
|
jamie@77
|
183 x->argv = (t_int *)getbytes(x->memory.argv);
|
jamie@77
|
184 }
|
jamie@77
|
185 else if (type == XTRACT_FLOAT){
|
jamie@77
|
186 x->memory.argv = (size_t)(n_args * sizeof(t_float));
|
jamie@77
|
187 x->argv = (t_float *)getbytes(x->memory.argv);
|
jamie@77
|
188 }
|
jamie@77
|
189 else
|
jamie@77
|
190 x->memory.argv = 0;
|
jamie@77
|
191 }
|
jamie@77
|
192
|
jamie@77
|
193
|
jamie@77
|
194 p_name = fd[f].algo.p_name;
|
jamie@77
|
195 p_desc = fd[f].algo.p_desc;
|
jamie@77
|
196 author = fd[f].algo.author;
|
jamie@77
|
197 year = fd[f].algo.year;
|
jamie@77
|
198
|
jamie@77
|
199 if(argc){
|
jamie@77
|
200 if(strcmp(p_name, ""))
|
jamie@77
|
201 post("xtract~: %s", p_name );
|
jamie@77
|
202 if(strcmp(p_desc, ""))
|
jamie@77
|
203 post("xtract~: %s", p_desc );
|
jamie@77
|
204 if(strcmp(author, "") && year)
|
jamie@77
|
205 post("xtract~: %s(%d)", author, year);
|
jamie@77
|
206 }
|
jamie@77
|
207 else
|
jamie@77
|
208 post("xtract~: No arguments given");
|
jamie@77
|
209
|
jamie@77
|
210 /* do init if needed */
|
jamie@77
|
211 if(x->feature == XTRACT_MFCC){
|
jamie@77
|
212
|
jamie@77
|
213 mf = x->argv;
|
jamie@77
|
214
|
jamie@77
|
215 mf->n_filters = 20;
|
jamie@77
|
216
|
jamie@77
|
217 post("xtract~: mfcc: filters = %d",
|
jamie@77
|
218 ((xtract_mel_filter *)x->argv)->n_filters);
|
jamie@77
|
219 mf->filters =
|
jamie@77
|
220 (t_float **)getbytes(mf->n_filters * sizeof(t_float *));
|
jamie@77
|
221 for(n = 0; n < mf->n_filters; n++)
|
jamie@77
|
222 mf->filters[n] = (float *)getbytes(N * sizeof(float));
|
jamie@77
|
223
|
jamie@77
|
224 xtract_init_mfcc(N, NYQUIST, XTRACT_EQUAL_GAIN, 80.0f,
|
jamie@77
|
225 18000.0f, mf->n_filters, mf->filters);
|
jamie@77
|
226 }
|
jamie@77
|
227 else if(x->feature == XTRACT_BARK_COEFFICIENTS)
|
jamie@77
|
228 xtract_init_bark(N, NYQUIST, x->argv);
|
jamie@77
|
229
|
jamie@98
|
230 /* Initialise fft_plan if required */
|
jamie@98
|
231 if(x->feature == XTRACT_AUTOCORRELATION_FFT ||
|
jamie@98
|
232 x->feature == XTRACT_SPECTRUM ||
|
jamie@98
|
233 x->feature == XTRACT_DCT){
|
jamie@98
|
234 xtract_init_fft(N, x->feature);
|
jamie@98
|
235 x->done_init = 1;
|
jamie@98
|
236 }
|
jamie@98
|
237
|
jamie@77
|
238 if(x->feature == XTRACT_AUTOCORRELATION ||
|
jamie@77
|
239 x->feature == XTRACT_AUTOCORRELATION_FFT ||
|
jamie@77
|
240 x->feature == XTRACT_MFCC || x->feature == XTRACT_AMDF ||
|
jamie@77
|
241 x->feature == XTRACT_ASDF|| x->feature == XTRACT_DCT ||
|
jamie@77
|
242 x->feature == XTRACT_BARK_COEFFICIENTS ||
|
jamie@77
|
243 x->feature == XTRACT_SPECTRUM ||
|
jamie@77
|
244 x->feature == XTRACT_PEAK_SPECTRUM ||
|
jamie@77
|
245 x->feature == XTRACT_HARMONIC_SPECTRUM)
|
jamie@77
|
246 x->feature_type = XTRACT_VECTOR;
|
jamie@77
|
247
|
jamie@77
|
248 else if (x->feature == XTRACT_FLUX || x->feature == XTRACT_ATTACK_TIME ||
|
jamie@77
|
249 x->feature == XTRACT_DECAY_TIME || x->feature == XTRACT_DELTA)
|
jamie@77
|
250 x->feature_type = XTRACT_DELTA;
|
jamie@77
|
251
|
jamie@77
|
252 else x->feature_type = XTRACT_SCALAR;
|
jamie@77
|
253
|
jamie@77
|
254 /* argv through right inlet */
|
jamie@78
|
255 inlet_new((t_pxobject *)x, "list");
|
jamie@77
|
256
|
jamie@77
|
257 /* DSP inlet */
|
jamie@77
|
258 dsp_setup((t_pxobject *)x, 1);
|
jamie@77
|
259
|
jamie@77
|
260
|
jamie@77
|
261 /* if feature is vector, create signal out */
|
jamie@77
|
262 if(x->feature_type == XTRACT_VECTOR)
|
jamie@77
|
263 outlet_new((t_pxobject *)x, "signal");
|
jamie@77
|
264
|
jamie@77
|
265 /* otherwise: float */
|
jamie@77
|
266 else
|
jamie@77
|
267 x->outlet = floatout((t_pxobject *)x);
|
jamie@77
|
268
|
jamie@77
|
269
|
jamie@77
|
270 /* free the function descriptors */
|
jamie@77
|
271 xtract_free_descriptors(fd);
|
jamie@77
|
272
|
jamie@77
|
273 return (void *)x;
|
jamie@77
|
274 }
|
jamie@77
|
275
|
jamie@77
|
276 static void xtract_tilde_get_args(t_xtract_tilde *x, t_symbol *selector,
|
jamie@77
|
277 t_int argc, t_atom *argv) {
|
jamie@77
|
278 /*
|
jamie@77
|
279 if(argc > (t_int)sizeof(x->argv) /
|
jamie@77
|
280 (t_int)sizeof(t_float) || x->argv == NULL)
|
jamie@77
|
281 post("Too many parameters to right inlet");
|
jamie@77
|
282 else{*/
|
jamie@77
|
283
|
jamie@79
|
284 t_float temp = 0.f;
|
jamie@77
|
285 x->argv = getbytes(argc * sizeof(float));
|
jamie@77
|
286
|
jamie@79
|
287 while(argc--){
|
jamie@79
|
288 temp = argv[argc].a_w.w_float;
|
jamie@79
|
289 ((t_float *)x->argv)[argc] = temp;
|
jamie@79
|
290 }
|
jamie@77
|
291 /* }*/
|
jamie@77
|
292 }
|
jamie@77
|
293
|
jamie@77
|
294 static void xtract_tilde_show_help(t_xtract_tilde *x, t_symbol *s){
|
jamie@77
|
295
|
jamie@77
|
296 int i;
|
jamie@77
|
297
|
jamie@77
|
298 i = XTRACT_FEATURES;
|
jamie@77
|
299 xtract_function_descriptor_t *fd, *d;
|
jamie@77
|
300
|
jamie@77
|
301 fd = (xtract_function_descriptor_t *)xtract_make_descriptors();
|
jamie@77
|
302 post("\nxtract~: Feature List\n");
|
jamie@77
|
303
|
jamie@77
|
304 while(i--){
|
jamie@77
|
305 d = &fd[i];
|
jamie@77
|
306 post("\t%s", d->algo.name);
|
jamie@77
|
307 }
|
jamie@77
|
308
|
jamie@77
|
309 xtract_free_descriptors(fd);
|
jamie@77
|
310 }
|
jamie@77
|
311
|
jamie@77
|
312 static void xtract_tilde_free(t_xtract_tilde *x) {
|
jamie@77
|
313
|
jamie@77
|
314 if(x->argv != NULL && x->memory.argv)
|
jamie@77
|
315 freebytes(x->argv, x->memory.argv);
|
jamie@146
|
316 freebytes(x->data);
|
jamie@146
|
317 freebytes(x->result);
|
jamie@77
|
318 }
|
jamie@77
|
319
|
jamie@77
|
320 int main(void) {
|
jamie@77
|
321
|
jamie@77
|
322 setup((t_messlist **)&xtract_tilde_class,
|
jamie@77
|
323 (method)xtract_tilde_new,
|
jamie@77
|
324 (method)xtract_tilde_free,
|
jamie@77
|
325 sizeof(t_xtract_tilde),
|
jamie@77
|
326 0L,
|
jamie@77
|
327 A_GIMME, 0);
|
jamie@77
|
328
|
jamie@77
|
329 addmess((method)xtract_tilde_dsp, "dsp", A_CANT, 0);
|
jamie@78
|
330 addmess((method)xtract_tilde_get_args, "list", A_GIMME, 0);
|
jamie@77
|
331 addmess((method)xtract_tilde_show_help, "help", A_DEFSYM, 0);
|
jamie@77
|
332 dsp_initclass();
|
jamie@77
|
333 //class_setname("xtract~", "xtract~");
|
jamie@77
|
334
|
jamie@77
|
335 return 0;
|
jamie@77
|
336
|
jamie@77
|
337 }
|