annotate examples/puredata/xtract~.c @ 113:72a9a393d5bd

- Fixed bugs in xtract_flatness(), or at least added necessary documentation and error checking to avoid problems - Added xtract_is_denormal() helper function and XTRACT_DENORMAL_FOUND return code - Replaced all instances of log, sqrt, exp etc. with respective floating point counterparts (logf etc.) - Added check for architecture endianness to configure script - Bug fix to PD example, now no longer crashes if no arguments are given - Minor documentation updates
author Jamie Bullock <jamie@postlude.co.uk>
date Fri, 15 Feb 2008 12:43:13 +0000
parents e6354b0137d3
children 6c5ece9cba3a
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@113 171 else {
jamie@113 172 post("xtract~: No arguments given");
jamie@113 173 return (void *)x;
jamie@113 174 }
jamie@108 175 if(argc > 1){
jamie@108 176 if(x->is_subframe)
jamie@108 177 x->feature_name = atom_getsymbol(argv+1);
jamie@108 178 else
jamie@108 179 N = atom_getint(argv+1);
jamie@108 180 }
jamie@108 181 if(argc > 2)
jamie@108 182 N = atom_getint(argv+2);
jamie@98 183
jamie@98 184 x->init_blocksize = N;
jamie@108 185 M = N >> 1;
jamie@4 186
jamie@50 187 /* get function descriptors */
jamie@56 188 fd = (xtract_function_descriptor_t *)xtract_make_descriptors();
jamie@50 189
jamie@50 190 /* iterate over descriptors */
jamie@50 191 while(f--){
jamie@50 192 /* map creation arg to feature */
jamie@98 193 if(x->feature_name == gensym(fd[f].algo.name)){
jamie@50 194 x->feature = f;
jamie@50 195 break;
jamie@50 196 }
jamie@50 197 }
jamie@4 198
jamie@108 199 if(x->feature == -1)
jamie@108 200 post("xtract~: feature not found: %s", x->feature_name->s_name);
jamie@108 201
jamie@41 202 /* allocate memory for feature arguments */
jamie@51 203 n_args = fd[f].argc;
jamie@50 204 type = fd[f].argv.type;
jamie@50 205
jamie@50 206 if(n_args){
jamie@55 207 for(n = 0; n < n_args; n++){
jamie@55 208 argv_max = &fd[f].argv.max[n];
jamie@85 209 /*post("Argument %d, max: %.2f", n, *argv_max); */
jamie@55 210 }
jamie@56 211 if(type == XTRACT_MEL_FILTER){
jamie@50 212 x->memory.argv = (size_t)(n_args * sizeof(xtract_mel_filter));
jamie@50 213 x->argv = (xtract_mel_filter *)getbytes(x->memory.argv);
jamie@50 214 }
jamie@56 215 else if(type == XTRACT_INT){
jamie@50 216 x->memory.argv = (size_t)(n_args * sizeof(t_int));
jamie@50 217 x->argv = (t_int *)getbytes(x->memory.argv);
jamie@50 218 }
jamie@56 219 else if (type == XTRACT_FLOAT){
jamie@50 220 x->memory.argv = (size_t)(n_args * sizeof(t_float));
jamie@50 221 x->argv = (t_float *)getbytes(x->memory.argv);
jamie@50 222 }
jamie@50 223 else
jamie@50 224 x->memory.argv = 0;
jamie@41 225 }
jamie@52 226
jamie@52 227 p_name = fd[f].algo.p_name;
jamie@52 228 p_desc = fd[f].algo.p_desc;
jamie@52 229 author = fd[f].algo.author;
jamie@62 230 year = fd[f].algo.year;
jamie@52 231
jamie@52 232 if(argc){
jamie@52 233 if(strcmp(p_name, ""))
jamie@52 234 post("xtract~: %s", p_name );
jamie@52 235 if(strcmp(p_desc, ""))
jamie@52 236 post("xtract~: %s", p_desc );
jamie@52 237 if(strcmp(author, "") && year)
jamie@62 238 post("xtract~: %s(%d)", author, year);
jamie@52 239 }
jamie@50 240
jamie@108 241 /* Adjust frame size if we are using subframe features */
jamie@108 242 if(x->is_subframe)
jamie@108 243 N = M;
jamie@108 244
jamie@108 245 post("xtract~: window size: %d", N);
jamie@41 246
jamie@41 247 /* do init if needed */
jamie@56 248 if(x->feature == XTRACT_MFCC){
jamie@41 249
jamie@50 250 mf = x->argv;
jamie@4 251
jamie@50 252 mf->n_filters = 20;
jamie@4 253
jamie@56 254 post("xtract~: mfcc: filters = %d",
jamie@56 255 ((xtract_mel_filter *)x->argv)->n_filters);
jamie@50 256 mf->filters =
jamie@50 257 (t_float **)getbytes(mf->n_filters * sizeof(t_float *));
jamie@50 258 for(n = 0; n < mf->n_filters; n++)
jamie@50 259 mf->filters[n] = (float *)getbytes(N * sizeof(float));
jamie@4 260
jamie@62 261 xtract_init_mfcc(N, NYQUIST, XTRACT_EQUAL_GAIN, 80.0f,
jamie@98 262 18000.0f, mf->n_filters, mf->filters);
jamie@98 263 x->done_init = 1;
jamie@4 264 }
jamie@98 265 else if(x->feature == XTRACT_BARK_COEFFICIENTS){
jamie@4 266 xtract_init_bark(N, NYQUIST, x->argv);
jamie@98 267 x->done_init = 1;
jamie@98 268 }
jamie@108 269 else if(x->feature == XTRACT_WINDOWED){
jamie@108 270 x->window = xtract_init_window(N, XTRACT_HANN);
jamie@108 271 x->argv = x->window;
jamie@108 272 x->done_init = 1;
jamie@108 273 }
jamie@98 274
jamie@98 275 /* Initialise fft_plan if required */
jamie@98 276 if(x->feature == XTRACT_AUTOCORRELATION_FFT ||
jamie@98 277 x->feature == XTRACT_SPECTRUM ||
jamie@98 278 x->feature == XTRACT_DCT){
jamie@98 279 xtract_init_fft(N, x->feature);
jamie@98 280 x->done_init = 1;
jamie@98 281 }
jamie@41 282
jamie@108 283 if(fd[f].is_scalar)
jamie@108 284 x->is_scalar = 1;
jamie@108 285
jamie@108 286 /*
jamie@62 287 if(x->feature == XTRACT_AUTOCORRELATION ||
jamie@62 288 x->feature == XTRACT_AUTOCORRELATION_FFT ||
jamie@62 289 x->feature == XTRACT_MFCC || x->feature == XTRACT_AMDF ||
jamie@62 290 x->feature == XTRACT_ASDF|| x->feature == XTRACT_DCT ||
jamie@62 291 x->feature == XTRACT_BARK_COEFFICIENTS ||
jamie@62 292 x->feature == XTRACT_SPECTRUM ||
jamie@62 293 x->feature == XTRACT_PEAK_SPECTRUM ||
jamie@104 294 x->feature == XTRACT_HARMONIC_SPECTRUM ||
jamie@104 295 x->feature == XTRACT_LPC ||
jamie@108 296 x->feature == XTRACT_LPCC ||
jamie@108 297 x->feature == XTRACT_WINDOWED)
jamie@62 298 x->feature_type = XTRACT_VECTOR;
jamie@108 299 */
jamie@108 300 /* else if (x->feature == XTRACT_FLUX || x->feature == XTRACT_ATTACK_TIME ||
jamie@108 301 x->feature == XTRACT_DECAY_TIME || x->feature == XTRACT_DIFFERENCE_VECTOR)
jamie@108 302 x->feature_type = XTRACT_DELTA; */
jamie@108 303 /*
jamie@56 304 else x->feature_type = XTRACT_SCALAR;
jamie@108 305 */
jamie@4 306
jamie@4 307 /* argv through right inlet */
jamie@4 308 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("list"), gensym("list"));
jamie@4 309
jamie@4 310 /* if feature is vector, create signal out */
jamie@108 311 if(!x->is_scalar)
jamie@108 312 outlet_new(&x->x_obj, &s_signal);
jamie@4 313
jamie@4 314 /* otherwise: float */
jamie@108 315 else
jamie@108 316 outlet_new(&x->x_obj, &s_float);
jamie@108 317
jamie@108 318 if(x->is_scalar && x->is_subframe)
jamie@108 319 post(
jamie@108 320 "xtract~: warning: subframes not yet supported for scalar features");
jamie@4 321
jamie@50 322 /* free the function descriptors */
jamie@50 323 xtract_free_descriptors(fd);
jamie@50 324
jamie@4 325 return (void *)x;
jamie@4 326 }
jamie@4 327
jamie@4 328 static void xtract_tilde_get_args(t_xtract_tilde *x, t_symbol *selector,
jamie@4 329 t_int argc, t_atom *argv) {
jamie@4 330 /*
jamie@4 331 if(argc > (t_int)sizeof(x->argv) /
jamie@4 332 (t_int)sizeof(t_float) || x->argv == NULL)
jamie@4 333 post("Too many parameters to right inlet");
jamie@4 334 else{*/
jamie@4 335
jamie@4 336 x->argv = getbytes(argc * sizeof(float));
jamie@4 337
jamie@104 338 while(argc--)
jamie@104 339 ((t_float *)x->argv)[argc] = atom_getfloat(&argv[argc]);
jamie@4 340 /* }*/
jamie@4 341 }
jamie@4 342
jamie@26 343 static void xtract_tilde_show_help(t_xtract_tilde *x, t_symbol *s){
jamie@26 344
jamie@26 345 int i;
jamie@26 346
jamie@85 347 xtract_function_descriptor_t *fd, *d;
jamie@85 348
jamie@26 349 i = XTRACT_FEATURES;
jamie@26 350
jamie@62 351 fd = (xtract_function_descriptor_t *)xtract_make_descriptors();
jamie@62 352 post("\nxtract~: Feature List\n");
jamie@62 353
jamie@26 354 while(i--){
jamie@62 355 d = &fd[i];
jamie@62 356 post("\t%s", d->algo.name);
jamie@62 357 }
jamie@62 358
jamie@62 359 xtract_free_descriptors(fd);
jamie@26 360 }
jamie@26 361
jamie@4 362 static void xtract_tilde_free(t_xtract_tilde *x) {
jamie@41 363
jamie@41 364 if(x->argv != NULL && x->memory.argv)
jamie@41 365 freebytes(x->argv, x->memory.argv);
jamie@108 366
jamie@108 367 if(x->window != NULL)
jamie@108 368 xtract_free_window(x->window);
jamie@4 369 }
jamie@4 370
jamie@4 371 void xtract_tilde_setup(void) {
jamie@4 372 xtract_class = class_new(gensym("xtract~"),
jamie@4 373 (t_newmethod)xtract_new,
jamie@4 374 (t_method)xtract_tilde_free,
jamie@4 375 sizeof(t_xtract_tilde),
jamie@4 376 CLASS_DEFAULT,
jamie@4 377 A_GIMME, 0);
jamie@4 378
jamie@4 379 class_addmethod(xtract_class,
jamie@26 380 (t_method)xtract_dsp, gensym("dsp"), 0);
jamie@4 381 class_addmethod(xtract_class,
jamie@26 382 (t_method)xtract_tilde_get_args, gensym("list"), A_GIMME, 0);
jamie@26 383 class_addmethod(xtract_class,
jamie@26 384 (t_method)xtract_tilde_show_help, gensym("help"), A_DEFSYMBOL, 0);
jamie@4 385 CLASS_MAINSIGNALIN(xtract_class, t_xtract_tilde, f);
jamie@33 386 class_sethelpsymbol(xtract_class, gensym("xtract-help"));
jamie@4 387 }