annotate examples/puredata/xtract~.c @ 108:e6354b0137d3

- PD example brought in line with new delta features and subframe function - subframe-test.pd added - fix to a_blockswap~.pd
author Jamie Bullock <jamie@postlude.co.uk>
date Sat, 29 Dec 2007 17:33:17 +0000
parents a32738e9d955
children 72a9a393d5bd
rev   line source
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@108 49 t_float *window;
jamie@108 50 t_int feature,
jamie@108 51 is_scalar,
jamie@108 52 is_subframe,
jamie@108 53 init_blocksize,
jamie@108 54 done_init;
jamie@98 55 t_symbol *feature_name;
jamie@41 56 tracked_memory memory;
jamie@4 57 void *argv;
jamie@4 58 } t_xtract_tilde;
jamie@4 59
jamie@4 60 static t_int *xtract_perform(t_int *w) {
jamie@4 61 t_sample *in = (t_sample *)(w[1]);
jamie@4 62 t_xtract_tilde *x = (t_xtract_tilde *)(w[2]);
jamie@4 63 t_int N = (t_int)(w[3]);
jamie@108 64 t_int rv = 0;
jamie@4 65 float result = 0;
jamie@4 66
jamie@108 67 rv = xtract[x->feature]((float *)in, N, x->argv, &result);
jamie@4 68
jamie@108 69 if(rv == XTRACT_FEATURE_NOT_IMPLEMENTED)
jamie@38 70 pd_error(x, "Feature not implemented");
jamie@42 71
jamie@42 72 /* set nan, inf or -inf to 0 */
jamie@42 73 result = (isinf(result) || isnan(result) ? 0 : result);
jamie@38 74
jamie@4 75 outlet_float(x->x_obj.ob_outlet, result);
jamie@4 76 return (w+4);
jamie@4 77 }
jamie@4 78
jamie@4 79 static t_int *xtract_perform_vector(t_int *w) {
jamie@98 80
jamie@4 81 t_sample *in = (t_sample *)(w[1]);
jamie@4 82 t_sample *out = (t_sample *)(w[2]);
jamie@4 83 t_float *tmp_in, *tmp_out;
jamie@4 84 t_xtract_tilde *x = (t_xtract_tilde *)(w[3]);
jamie@4 85 t_int N = (t_int)(w[4]), n;
jamie@108 86 t_int rv = 0;
jamie@4 87
jamie@98 88 if(N != x->init_blocksize && x->done_init){
jamie@98 89 error("xtract~ %s: Blocksize mismatch, try specifying the blocksize as a second argument", x->feature_name->s_name);
jamie@98 90 return (w+5);
jamie@98 91 }
jamie@98 92
jamie@62 93 n = N;
jamie@62 94
jamie@4 95 tmp_in = copybytes(in, N * sizeof(t_float));
jamie@4 96 tmp_out = getbytes(N * sizeof(t_float));
jamie@62 97
jamie@104 98 if(x->feature == XTRACT_PEAK_SPECTRUM || x->feature == XTRACT_LPC)
jamie@62 99 N >>= 1;
jamie@4 100
jamie@108 101 if(x->is_subframe){
jamie@108 102
jamie@108 103 rv = xtract_features_from_subframes(tmp_in, N, x->feature,
jamie@108 104 x->argv, tmp_out);
jamie@108 105 }
jamie@108 106 else{
jamie@108 107
jamie@108 108 rv = xtract[x->feature](tmp_in, N, x->argv, tmp_out);
jamie@38 109
jamie@108 110 }
jamie@108 111
jamie@108 112 if(rv == XTRACT_FEATURE_NOT_IMPLEMENTED)
jamie@38 113 pd_error(x, "Feature not implemented");
jamie@4 114
jamie@108 115 while(n--)
jamie@108 116 out[n] = tmp_out[n];
jamie@4 117
jamie@4 118 freebytes(tmp_in, N * sizeof(t_float));
jamie@4 119 freebytes(tmp_out, N * sizeof(t_float));
jamie@4 120
jamie@4 121 return (w+5);
jamie@4 122 }
jamie@4 123
jamie@4 124 static void xtract_dsp(t_xtract_tilde *x, t_signal **sp) {
jamie@4 125
jamie@108 126 if(!x->is_scalar)
jamie@4 127 dsp_add(xtract_perform_vector, 4,
jamie@4 128 sp[0]->s_vec, sp[1]->s_vec, x, sp[0]->s_n);
jamie@4 129
jamie@4 130 else dsp_add(xtract_perform, 3, sp[0]->s_vec, x, sp[0]->s_n);
jamie@4 131
jamie@4 132 }
jamie@4 133
jamie@4 134 static void *xtract_new(t_symbol *me, t_int argc, t_atom *argv) {
jamie@4 135
jamie@4 136 t_xtract_tilde *x = (t_xtract_tilde *)pd_new(xtract_class);
jamie@50 137 xtract_mel_filter *mf;
jamie@108 138 t_int n, N, M, f, F,
jamie@108 139 n_args,
jamie@108 140 type;
jamie@55 141 t_float *argv_max;
jamie@108 142 t_symbol *arg1;
jamie@56 143 xtract_function_descriptor_t *fd;
jamie@108 144 char *p_name,
jamie@108 145 *p_desc,
jamie@108 146 *author;
jamie@62 147 int year;
jamie@52 148
jamie@52 149 p_name = p_desc = author = NULL;
jamie@4 150
jamie@108 151 n_args = type = 0;
jamie@50 152
jamie@50 153 f = F = XTRACT_FEATURES;
jamie@50 154
jamie@4 155 N = BLOCKSIZE;
jamie@4 156
jamie@4 157 x->argv = NULL;
jamie@98 158 x->done_init = 0;
jamie@108 159 x->is_scalar = 0;
jamie@108 160 x->is_subframe = 0;
jamie@108 161 x->feature = -1;
jamie@4 162
jamie@108 163 /* Parse arguments */
jamie@108 164 if(argc){
jamie@108 165 arg1 = atom_getsymbol(argv);
jamie@108 166 if(arg1 == gensym("subframe"))
jamie@108 167 x->is_subframe = 1;
jamie@108 168 else
jamie@108 169 x->feature_name = atom_getsymbol(argv);
jamie@108 170 }
jamie@108 171 if(argc > 1){
jamie@108 172 if(x->is_subframe)
jamie@108 173 x->feature_name = atom_getsymbol(argv+1);
jamie@108 174 else
jamie@108 175 N = atom_getint(argv+1);
jamie@108 176 }
jamie@108 177 if(argc > 2)
jamie@108 178 N = atom_getint(argv+2);
jamie@98 179
jamie@98 180 x->init_blocksize = N;
jamie@108 181 M = N >> 1;
jamie@4 182
jamie@50 183 /* get function descriptors */
jamie@56 184 fd = (xtract_function_descriptor_t *)xtract_make_descriptors();
jamie@50 185
jamie@50 186 /* iterate over descriptors */
jamie@50 187 while(f--){
jamie@50 188 /* map creation arg to feature */
jamie@98 189 if(x->feature_name == gensym(fd[f].algo.name)){
jamie@50 190 x->feature = f;
jamie@50 191 break;
jamie@50 192 }
jamie@50 193 }
jamie@4 194
jamie@108 195 if(x->feature == -1)
jamie@108 196 post("xtract~: feature not found: %s", x->feature_name->s_name);
jamie@108 197
jamie@41 198 /* allocate memory for feature arguments */
jamie@51 199 n_args = fd[f].argc;
jamie@50 200 type = fd[f].argv.type;
jamie@50 201
jamie@50 202 if(n_args){
jamie@55 203 for(n = 0; n < n_args; n++){
jamie@55 204 argv_max = &fd[f].argv.max[n];
jamie@85 205 /*post("Argument %d, max: %.2f", n, *argv_max); */
jamie@55 206 }
jamie@56 207 if(type == XTRACT_MEL_FILTER){
jamie@50 208 x->memory.argv = (size_t)(n_args * sizeof(xtract_mel_filter));
jamie@50 209 x->argv = (xtract_mel_filter *)getbytes(x->memory.argv);
jamie@50 210 }
jamie@56 211 else if(type == XTRACT_INT){
jamie@50 212 x->memory.argv = (size_t)(n_args * sizeof(t_int));
jamie@50 213 x->argv = (t_int *)getbytes(x->memory.argv);
jamie@50 214 }
jamie@56 215 else if (type == XTRACT_FLOAT){
jamie@50 216 x->memory.argv = (size_t)(n_args * sizeof(t_float));
jamie@50 217 x->argv = (t_float *)getbytes(x->memory.argv);
jamie@50 218 }
jamie@50 219 else
jamie@50 220 x->memory.argv = 0;
jamie@41 221 }
jamie@52 222
jamie@52 223 p_name = fd[f].algo.p_name;
jamie@52 224 p_desc = fd[f].algo.p_desc;
jamie@52 225 author = fd[f].algo.author;
jamie@62 226 year = fd[f].algo.year;
jamie@52 227
jamie@52 228 if(argc){
jamie@52 229 if(strcmp(p_name, ""))
jamie@52 230 post("xtract~: %s", p_name );
jamie@52 231 if(strcmp(p_desc, ""))
jamie@52 232 post("xtract~: %s", p_desc );
jamie@52 233 if(strcmp(author, "") && year)
jamie@62 234 post("xtract~: %s(%d)", author, year);
jamie@52 235 }
jamie@52 236 else
jamie@52 237 post("xtract~: No arguments given");
jamie@50 238
jamie@108 239 /* Adjust frame size if we are using subframe features */
jamie@108 240 if(x->is_subframe)
jamie@108 241 N = M;
jamie@108 242
jamie@108 243 post("xtract~: window size: %d", N);
jamie@41 244
jamie@41 245 /* do init if needed */
jamie@56 246 if(x->feature == XTRACT_MFCC){
jamie@41 247
jamie@50 248 mf = x->argv;
jamie@4 249
jamie@50 250 mf->n_filters = 20;
jamie@4 251
jamie@56 252 post("xtract~: mfcc: filters = %d",
jamie@56 253 ((xtract_mel_filter *)x->argv)->n_filters);
jamie@50 254 mf->filters =
jamie@50 255 (t_float **)getbytes(mf->n_filters * sizeof(t_float *));
jamie@50 256 for(n = 0; n < mf->n_filters; n++)
jamie@50 257 mf->filters[n] = (float *)getbytes(N * sizeof(float));
jamie@4 258
jamie@62 259 xtract_init_mfcc(N, NYQUIST, XTRACT_EQUAL_GAIN, 80.0f,
jamie@98 260 18000.0f, mf->n_filters, mf->filters);
jamie@98 261 x->done_init = 1;
jamie@4 262 }
jamie@98 263 else if(x->feature == XTRACT_BARK_COEFFICIENTS){
jamie@4 264 xtract_init_bark(N, NYQUIST, x->argv);
jamie@98 265 x->done_init = 1;
jamie@98 266 }
jamie@108 267 else if(x->feature == XTRACT_WINDOWED){
jamie@108 268 x->window = xtract_init_window(N, XTRACT_HANN);
jamie@108 269 x->argv = x->window;
jamie@108 270 x->done_init = 1;
jamie@108 271 }
jamie@98 272
jamie@98 273 /* Initialise fft_plan if required */
jamie@98 274 if(x->feature == XTRACT_AUTOCORRELATION_FFT ||
jamie@98 275 x->feature == XTRACT_SPECTRUM ||
jamie@98 276 x->feature == XTRACT_DCT){
jamie@98 277 xtract_init_fft(N, x->feature);
jamie@98 278 x->done_init = 1;
jamie@98 279 }
jamie@41 280
jamie@108 281 if(fd[f].is_scalar)
jamie@108 282 x->is_scalar = 1;
jamie@108 283
jamie@108 284 /*
jamie@62 285 if(x->feature == XTRACT_AUTOCORRELATION ||
jamie@62 286 x->feature == XTRACT_AUTOCORRELATION_FFT ||
jamie@62 287 x->feature == XTRACT_MFCC || x->feature == XTRACT_AMDF ||
jamie@62 288 x->feature == XTRACT_ASDF|| x->feature == XTRACT_DCT ||
jamie@62 289 x->feature == XTRACT_BARK_COEFFICIENTS ||
jamie@62 290 x->feature == XTRACT_SPECTRUM ||
jamie@62 291 x->feature == XTRACT_PEAK_SPECTRUM ||
jamie@104 292 x->feature == XTRACT_HARMONIC_SPECTRUM ||
jamie@104 293 x->feature == XTRACT_LPC ||
jamie@108 294 x->feature == XTRACT_LPCC ||
jamie@108 295 x->feature == XTRACT_WINDOWED)
jamie@62 296 x->feature_type = XTRACT_VECTOR;
jamie@108 297 */
jamie@108 298 /* else if (x->feature == XTRACT_FLUX || x->feature == XTRACT_ATTACK_TIME ||
jamie@108 299 x->feature == XTRACT_DECAY_TIME || x->feature == XTRACT_DIFFERENCE_VECTOR)
jamie@108 300 x->feature_type = XTRACT_DELTA; */
jamie@108 301 /*
jamie@56 302 else x->feature_type = XTRACT_SCALAR;
jamie@108 303 */
jamie@4 304
jamie@4 305 /* argv through right inlet */
jamie@4 306 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("list"), gensym("list"));
jamie@4 307
jamie@4 308 /* if feature is vector, create signal out */
jamie@108 309 if(!x->is_scalar)
jamie@108 310 outlet_new(&x->x_obj, &s_signal);
jamie@4 311
jamie@4 312 /* otherwise: float */
jamie@108 313 else
jamie@108 314 outlet_new(&x->x_obj, &s_float);
jamie@108 315
jamie@108 316 if(x->is_scalar && x->is_subframe)
jamie@108 317 post(
jamie@108 318 "xtract~: warning: subframes not yet supported for scalar features");
jamie@4 319
jamie@50 320 /* free the function descriptors */
jamie@50 321 xtract_free_descriptors(fd);
jamie@50 322
jamie@4 323 return (void *)x;
jamie@4 324 }
jamie@4 325
jamie@4 326 static void xtract_tilde_get_args(t_xtract_tilde *x, t_symbol *selector,
jamie@4 327 t_int argc, t_atom *argv) {
jamie@4 328 /*
jamie@4 329 if(argc > (t_int)sizeof(x->argv) /
jamie@4 330 (t_int)sizeof(t_float) || x->argv == NULL)
jamie@4 331 post("Too many parameters to right inlet");
jamie@4 332 else{*/
jamie@4 333
jamie@4 334 x->argv = getbytes(argc * sizeof(float));
jamie@4 335
jamie@104 336 while(argc--)
jamie@104 337 ((t_float *)x->argv)[argc] = atom_getfloat(&argv[argc]);
jamie@4 338 /* }*/
jamie@4 339 }
jamie@4 340
jamie@26 341 static void xtract_tilde_show_help(t_xtract_tilde *x, t_symbol *s){
jamie@26 342
jamie@26 343 int i;
jamie@26 344
jamie@85 345 xtract_function_descriptor_t *fd, *d;
jamie@85 346
jamie@26 347 i = XTRACT_FEATURES;
jamie@26 348
jamie@62 349 fd = (xtract_function_descriptor_t *)xtract_make_descriptors();
jamie@62 350 post("\nxtract~: Feature List\n");
jamie@62 351
jamie@26 352 while(i--){
jamie@62 353 d = &fd[i];
jamie@62 354 post("\t%s", d->algo.name);
jamie@62 355 }
jamie@62 356
jamie@62 357 xtract_free_descriptors(fd);
jamie@26 358 }
jamie@26 359
jamie@4 360 static void xtract_tilde_free(t_xtract_tilde *x) {
jamie@41 361
jamie@41 362 if(x->argv != NULL && x->memory.argv)
jamie@41 363 freebytes(x->argv, x->memory.argv);
jamie@108 364
jamie@108 365 if(x->window != NULL)
jamie@108 366 xtract_free_window(x->window);
jamie@4 367 }
jamie@4 368
jamie@4 369 void xtract_tilde_setup(void) {
jamie@4 370 xtract_class = class_new(gensym("xtract~"),
jamie@4 371 (t_newmethod)xtract_new,
jamie@4 372 (t_method)xtract_tilde_free,
jamie@4 373 sizeof(t_xtract_tilde),
jamie@4 374 CLASS_DEFAULT,
jamie@4 375 A_GIMME, 0);
jamie@4 376
jamie@4 377 class_addmethod(xtract_class,
jamie@26 378 (t_method)xtract_dsp, gensym("dsp"), 0);
jamie@4 379 class_addmethod(xtract_class,
jamie@26 380 (t_method)xtract_tilde_get_args, gensym("list"), A_GIMME, 0);
jamie@26 381 class_addmethod(xtract_class,
jamie@26 382 (t_method)xtract_tilde_show_help, gensym("help"), A_DEFSYMBOL, 0);
jamie@4 383 CLASS_MAINSIGNALIN(xtract_class, t_xtract_tilde, f);
jamie@33 384 class_sethelpsymbol(xtract_class, gensym("xtract-help"));
jamie@4 385 }