annotate examples/puredata/xtract~.c @ 115:6c5ece9cba3a

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