jamie@4
|
1 /* xtract~ - PD library for feature extraction
|
jamie@4
|
2 Copyright (C) 2006 Jamie Bullock
|
jamie@4
|
3
|
jamie@4
|
4 This program is free software; you can redistribute it and/or
|
jamie@4
|
5 modify it under the terms of the GNU General Public License
|
jamie@4
|
6 as published by the Free Software Foundation; either version 2
|
jamie@4
|
7 of the License, or (at your option) any later version.
|
jamie@4
|
8
|
jamie@4
|
9 This program is distributed in the hope that it will be useful,
|
jamie@4
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jamie@4
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jamie@4
|
12 GNU General Public License for more details.
|
jamie@4
|
13
|
jamie@4
|
14 You should have received a copy of the GNU General Public License
|
jamie@4
|
15 along with this program; if not, write to the Free Software
|
jamie@4
|
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
jamie@4
|
17 */
|
jamie@4
|
18
|
jamie@4
|
19 /* calculates the spectral xtract of one frame, given peak frequency and amplitude to first and second inputs respectively */
|
jamie@4
|
20
|
jamie@41
|
21 #include "m_pd.h"
|
jamie@42
|
22 #include <math.h>
|
jamie@52
|
23 #include <string.h>
|
jamie@41
|
24
|
jamie@4
|
25 #include "xtract/libxtract.h"
|
jamie@4
|
26
|
jamie@62
|
27 #define BLOCKSIZE 1024 /* FIX: this should be dynamic - somehow */
|
jamie@4
|
28 #define NYQUIST 22050.0f
|
jamie@4
|
29
|
jamie@85
|
30 #ifndef isnan
|
jamie@85
|
31 /* FIX: should probably try to handle signalling NaNs */
|
jamie@85
|
32 int isnan(x){ if(x == x) return 0; else return 1;}
|
jamie@85
|
33 #endif
|
jamie@85
|
34
|
jamie@85
|
35 #ifndef isinf
|
jamie@85
|
36 int isinf(x) {if(x == 1.0 / 0. || x == -1.0 / 0.) return 1; else return 0;}
|
jamie@85
|
37 #endif
|
jamie@85
|
38
|
jamie@4
|
39 static t_class *xtract_class;
|
jamie@4
|
40
|
jamie@41
|
41 /* Struct for keeping track of memory allocations */
|
jamie@41
|
42 typedef struct _tracked_memory {
|
jamie@41
|
43 char argv;
|
jamie@41
|
44 } tracked_memory;
|
jamie@41
|
45
|
jamie@4
|
46 typedef struct _xtract {
|
jamie@4
|
47 t_object x_obj;
|
jamie@4
|
48 t_float f;
|
jamie@4
|
49 t_int feature;
|
jamie@4
|
50 t_int feature_type;
|
jamie@41
|
51 tracked_memory memory;
|
jamie@4
|
52 void *argv;
|
jamie@4
|
53 } t_xtract_tilde;
|
jamie@4
|
54
|
jamie@4
|
55 static t_int *xtract_perform(t_int *w) {
|
jamie@4
|
56 t_sample *in = (t_sample *)(w[1]);
|
jamie@4
|
57 t_xtract_tilde *x = (t_xtract_tilde *)(w[2]);
|
jamie@4
|
58 t_int N = (t_int)(w[3]);
|
jamie@38
|
59 t_int return_code = 0;
|
jamie@4
|
60 float result = 0;
|
jamie@4
|
61
|
jamie@38
|
62 return_code = xtract[x->feature]((float *)in, N, x->argv, &result);
|
jamie@4
|
63
|
jamie@56
|
64 if(return_code == XTRACT_FEATURE_NOT_IMPLEMENTED)
|
jamie@38
|
65 pd_error(x, "Feature not implemented");
|
jamie@42
|
66
|
jamie@42
|
67 /* set nan, inf or -inf to 0 */
|
jamie@42
|
68 result = (isinf(result) || isnan(result) ? 0 : result);
|
jamie@38
|
69
|
jamie@4
|
70 outlet_float(x->x_obj.ob_outlet, result);
|
jamie@4
|
71 return (w+4);
|
jamie@4
|
72 }
|
jamie@4
|
73
|
jamie@4
|
74 static t_int *xtract_perform_vector(t_int *w) {
|
jamie@4
|
75 t_sample *in = (t_sample *)(w[1]);
|
jamie@4
|
76 t_sample *out = (t_sample *)(w[2]);
|
jamie@4
|
77 t_float *tmp_in, *tmp_out;
|
jamie@4
|
78 t_xtract_tilde *x = (t_xtract_tilde *)(w[3]);
|
jamie@4
|
79 t_int N = (t_int)(w[4]), n;
|
jamie@38
|
80 t_int return_code = 0;
|
jamie@4
|
81
|
jamie@62
|
82 n = N;
|
jamie@62
|
83
|
jamie@4
|
84 tmp_in = copybytes(in, N * sizeof(t_float));
|
jamie@4
|
85 tmp_out = getbytes(N * sizeof(t_float));
|
jamie@62
|
86
|
jamie@62
|
87 if(x->feature == XTRACT_PEAK_SPECTRUM)
|
jamie@62
|
88 N >>= 1;
|
jamie@4
|
89
|
jamie@38
|
90 return_code = xtract[x->feature](tmp_in, N, x->argv, tmp_out);
|
jamie@38
|
91
|
jamie@56
|
92 if(return_code == XTRACT_FEATURE_NOT_IMPLEMENTED)
|
jamie@38
|
93 pd_error(x, "Feature not implemented");
|
jamie@4
|
94
|
jamie@4
|
95 while(n--) out[n] = tmp_out[n];
|
jamie@4
|
96
|
jamie@4
|
97 freebytes(tmp_in, N * sizeof(t_float));
|
jamie@4
|
98 freebytes(tmp_out, N * sizeof(t_float));
|
jamie@4
|
99
|
jamie@4
|
100 return (w+5);
|
jamie@4
|
101 }
|
jamie@4
|
102
|
jamie@4
|
103 static void xtract_dsp(t_xtract_tilde *x, t_signal **sp) {
|
jamie@4
|
104
|
jamie@56
|
105 if(x->feature_type == XTRACT_VECTOR)
|
jamie@4
|
106 dsp_add(xtract_perform_vector, 4,
|
jamie@4
|
107 sp[0]->s_vec, sp[1]->s_vec, x, sp[0]->s_n);
|
jamie@4
|
108
|
jamie@4
|
109 else dsp_add(xtract_perform, 3, sp[0]->s_vec, x, sp[0]->s_n);
|
jamie@4
|
110
|
jamie@4
|
111 }
|
jamie@4
|
112
|
jamie@4
|
113 static void *xtract_new(t_symbol *me, t_int argc, t_atom *argv) {
|
jamie@4
|
114
|
jamie@4
|
115 t_symbol *tmp;
|
jamie@4
|
116 t_xtract_tilde *x = (t_xtract_tilde *)pd_new(xtract_class);
|
jamie@50
|
117 xtract_mel_filter *mf;
|
jamie@50
|
118 t_int n, N, f, F, n_args, type;
|
jamie@55
|
119 t_float *argv_max;
|
jamie@56
|
120 xtract_function_descriptor_t *fd;
|
jamie@52
|
121 char *p_name, *p_desc, *author;
|
jamie@62
|
122 int year;
|
jamie@52
|
123
|
jamie@52
|
124 p_name = p_desc = author = NULL;
|
jamie@4
|
125
|
jamie@51
|
126 n_args = type = x->feature = 0;
|
jamie@50
|
127
|
jamie@50
|
128 f = F = XTRACT_FEATURES;
|
jamie@50
|
129
|
jamie@4
|
130 N = BLOCKSIZE;
|
jamie@4
|
131
|
jamie@4
|
132 x->argv = NULL;
|
jamie@4
|
133
|
jamie@4
|
134 tmp = atom_getsymbol(argv);
|
jamie@4
|
135
|
jamie@50
|
136 /* get function descriptors */
|
jamie@56
|
137 fd = (xtract_function_descriptor_t *)xtract_make_descriptors();
|
jamie@50
|
138
|
jamie@50
|
139 /* iterate over descriptors */
|
jamie@50
|
140 while(f--){
|
jamie@50
|
141 /* map creation arg to feature */
|
jamie@50
|
142 if(tmp == gensym(fd[f].algo.name)){
|
jamie@50
|
143 x->feature = f;
|
jamie@50
|
144 break;
|
jamie@50
|
145 }
|
jamie@50
|
146 }
|
jamie@4
|
147
|
jamie@41
|
148 /* allocate memory for feature arguments */
|
jamie@51
|
149 n_args = fd[f].argc;
|
jamie@50
|
150 type = fd[f].argv.type;
|
jamie@50
|
151
|
jamie@50
|
152 if(n_args){
|
jamie@55
|
153 for(n = 0; n < n_args; n++){
|
jamie@55
|
154 argv_max = &fd[f].argv.max[n];
|
jamie@85
|
155 /*post("Argument %d, max: %.2f", n, *argv_max); */
|
jamie@55
|
156 }
|
jamie@56
|
157 if(type == XTRACT_MEL_FILTER){
|
jamie@50
|
158 x->memory.argv = (size_t)(n_args * sizeof(xtract_mel_filter));
|
jamie@50
|
159 x->argv = (xtract_mel_filter *)getbytes(x->memory.argv);
|
jamie@50
|
160 }
|
jamie@56
|
161 else if(type == XTRACT_INT){
|
jamie@50
|
162 x->memory.argv = (size_t)(n_args * sizeof(t_int));
|
jamie@50
|
163 x->argv = (t_int *)getbytes(x->memory.argv);
|
jamie@50
|
164 }
|
jamie@56
|
165 else if (type == XTRACT_FLOAT){
|
jamie@50
|
166 x->memory.argv = (size_t)(n_args * sizeof(t_float));
|
jamie@50
|
167 x->argv = (t_float *)getbytes(x->memory.argv);
|
jamie@50
|
168 }
|
jamie@50
|
169 else
|
jamie@50
|
170 x->memory.argv = 0;
|
jamie@41
|
171 }
|
jamie@52
|
172
|
jamie@52
|
173
|
jamie@52
|
174 p_name = fd[f].algo.p_name;
|
jamie@52
|
175 p_desc = fd[f].algo.p_desc;
|
jamie@52
|
176 author = fd[f].algo.author;
|
jamie@62
|
177 year = fd[f].algo.year;
|
jamie@52
|
178
|
jamie@52
|
179 if(argc){
|
jamie@52
|
180 if(strcmp(p_name, ""))
|
jamie@52
|
181 post("xtract~: %s", p_name );
|
jamie@52
|
182 if(strcmp(p_desc, ""))
|
jamie@52
|
183 post("xtract~: %s", p_desc );
|
jamie@52
|
184 if(strcmp(author, "") && year)
|
jamie@62
|
185 post("xtract~: %s(%d)", author, year);
|
jamie@52
|
186 }
|
jamie@52
|
187 else
|
jamie@52
|
188 post("xtract~: No arguments given");
|
jamie@50
|
189
|
jamie@41
|
190
|
jamie@41
|
191 /* do init if needed */
|
jamie@56
|
192 if(x->feature == XTRACT_MFCC){
|
jamie@41
|
193
|
jamie@50
|
194 mf = x->argv;
|
jamie@4
|
195
|
jamie@50
|
196 mf->n_filters = 20;
|
jamie@4
|
197
|
jamie@56
|
198 post("xtract~: mfcc: filters = %d",
|
jamie@56
|
199 ((xtract_mel_filter *)x->argv)->n_filters);
|
jamie@50
|
200 mf->filters =
|
jamie@50
|
201 (t_float **)getbytes(mf->n_filters * sizeof(t_float *));
|
jamie@50
|
202 for(n = 0; n < mf->n_filters; n++)
|
jamie@50
|
203 mf->filters[n] = (float *)getbytes(N * sizeof(float));
|
jamie@4
|
204
|
jamie@62
|
205 xtract_init_mfcc(N, NYQUIST, XTRACT_EQUAL_GAIN, 80.0f,
|
jamie@62
|
206 18000.0f, mf->n_filters, mf->filters);
|
jamie@4
|
207 }
|
jamie@56
|
208 else if(x->feature == XTRACT_BARK_COEFFICIENTS)
|
jamie@4
|
209 xtract_init_bark(N, NYQUIST, x->argv);
|
jamie@41
|
210
|
jamie@62
|
211 if(x->feature == XTRACT_AUTOCORRELATION ||
|
jamie@62
|
212 x->feature == XTRACT_AUTOCORRELATION_FFT ||
|
jamie@62
|
213 x->feature == XTRACT_MFCC || x->feature == XTRACT_AMDF ||
|
jamie@62
|
214 x->feature == XTRACT_ASDF|| x->feature == XTRACT_DCT ||
|
jamie@62
|
215 x->feature == XTRACT_BARK_COEFFICIENTS ||
|
jamie@62
|
216 x->feature == XTRACT_SPECTRUM ||
|
jamie@62
|
217 x->feature == XTRACT_PEAK_SPECTRUM ||
|
jamie@62
|
218 x->feature == XTRACT_HARMONIC_SPECTRUM)
|
jamie@62
|
219 x->feature_type = XTRACT_VECTOR;
|
jamie@4
|
220
|
jamie@56
|
221 else if (x->feature == XTRACT_FLUX || x->feature == XTRACT_ATTACK_TIME ||
|
jamie@56
|
222 x->feature == XTRACT_DECAY_TIME || x->feature == XTRACT_DELTA)
|
jamie@56
|
223 x->feature_type = XTRACT_DELTA;
|
jamie@4
|
224
|
jamie@56
|
225 else x->feature_type = XTRACT_SCALAR;
|
jamie@4
|
226
|
jamie@4
|
227 /* argv through right inlet */
|
jamie@4
|
228 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("list"), gensym("list"));
|
jamie@4
|
229
|
jamie@4
|
230 /* if feature is vector, create signal out */
|
jamie@56
|
231 if(x->feature_type == XTRACT_VECTOR) outlet_new(&x->x_obj, &s_signal);
|
jamie@4
|
232
|
jamie@4
|
233 /* otherwise: float */
|
jamie@4
|
234 else outlet_new(&x->x_obj, &s_float);
|
jamie@4
|
235
|
jamie@50
|
236 /* free the function descriptors */
|
jamie@50
|
237 xtract_free_descriptors(fd);
|
jamie@50
|
238
|
jamie@4
|
239 return (void *)x;
|
jamie@4
|
240 }
|
jamie@4
|
241
|
jamie@4
|
242 static void xtract_tilde_get_args(t_xtract_tilde *x, t_symbol *selector,
|
jamie@4
|
243 t_int argc, t_atom *argv) {
|
jamie@4
|
244 /*
|
jamie@4
|
245 if(argc > (t_int)sizeof(x->argv) /
|
jamie@4
|
246 (t_int)sizeof(t_float) || x->argv == NULL)
|
jamie@4
|
247 post("Too many parameters to right inlet");
|
jamie@4
|
248 else{*/
|
jamie@4
|
249
|
jamie@4
|
250 x->argv = getbytes(argc * sizeof(float));
|
jamie@4
|
251
|
jamie@4
|
252 while(argc--)
|
jamie@4
|
253 ((t_float *)x->argv)[argc] = atom_getfloat(&argv[argc]);
|
jamie@4
|
254 /* }*/
|
jamie@4
|
255 }
|
jamie@4
|
256
|
jamie@26
|
257 static void xtract_tilde_show_help(t_xtract_tilde *x, t_symbol *s){
|
jamie@26
|
258
|
jamie@26
|
259 int i;
|
jamie@26
|
260
|
jamie@85
|
261 xtract_function_descriptor_t *fd, *d;
|
jamie@85
|
262
|
jamie@26
|
263 i = XTRACT_FEATURES;
|
jamie@26
|
264
|
jamie@62
|
265 fd = (xtract_function_descriptor_t *)xtract_make_descriptors();
|
jamie@62
|
266 post("\nxtract~: Feature List\n");
|
jamie@62
|
267
|
jamie@26
|
268 while(i--){
|
jamie@62
|
269 d = &fd[i];
|
jamie@62
|
270 post("\t%s", d->algo.name);
|
jamie@62
|
271 }
|
jamie@62
|
272
|
jamie@62
|
273 xtract_free_descriptors(fd);
|
jamie@26
|
274 }
|
jamie@26
|
275
|
jamie@4
|
276 static void xtract_tilde_free(t_xtract_tilde *x) {
|
jamie@41
|
277
|
jamie@41
|
278 if(x->argv != NULL && x->memory.argv)
|
jamie@41
|
279 freebytes(x->argv, x->memory.argv);
|
jamie@4
|
280 }
|
jamie@4
|
281
|
jamie@4
|
282 void xtract_tilde_setup(void) {
|
jamie@4
|
283 xtract_class = class_new(gensym("xtract~"),
|
jamie@4
|
284 (t_newmethod)xtract_new,
|
jamie@4
|
285 (t_method)xtract_tilde_free,
|
jamie@4
|
286 sizeof(t_xtract_tilde),
|
jamie@4
|
287 CLASS_DEFAULT,
|
jamie@4
|
288 A_GIMME, 0);
|
jamie@4
|
289
|
jamie@4
|
290 class_addmethod(xtract_class,
|
jamie@26
|
291 (t_method)xtract_dsp, gensym("dsp"), 0);
|
jamie@4
|
292 class_addmethod(xtract_class,
|
jamie@26
|
293 (t_method)xtract_tilde_get_args, gensym("list"), A_GIMME, 0);
|
jamie@26
|
294 class_addmethod(xtract_class,
|
jamie@26
|
295 (t_method)xtract_tilde_show_help, gensym("help"), A_DEFSYMBOL, 0);
|
jamie@4
|
296 CLASS_MAINSIGNALIN(xtract_class, t_xtract_tilde, f);
|
jamie@33
|
297 class_sethelpsymbol(xtract_class, gensym("xtract-help"));
|
jamie@4
|
298 }
|