annotate examples/MSP/xtract~.c @ 116:859495925633

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