annotate examples/MSP/xtract~.c @ 78:afb298ce1b4d

Fixes for MSP example, and changed the fundamental estimators so that if they don't get a samplerate 44100 is assumed (I'm not sure if this is a good idea!).
author Jamie Bullock <jamie@postlude.co.uk>
date Sun, 19 Aug 2007 16:54:25 +0000
parents 899c31350e8d
children 06c3ca76e007
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@77 43 t_int feature_type;
jamie@77 44 tracked_memory memory;
jamie@77 45 void *argv;
jamie@77 46 } t_xtract_tilde;
jamie@77 47
jamie@77 48 static t_int *xtract_perform(t_int *w) {
jamie@78 49 t_float *in = (t_float *)(w[1]);
jamie@77 50 t_xtract_tilde *x = (t_xtract_tilde *)(w[2]);
jamie@77 51 t_int N = (t_int)(w[3]);
jamie@77 52 t_int return_code = 0;
jamie@78 53 float result = 0.f;
jamie@77 54
jamie@77 55 return_code = xtract[x->feature]((float *)in, N, x->argv, &result);
jamie@77 56
jamie@77 57 if(return_code == XTRACT_FEATURE_NOT_IMPLEMENTED)
jamie@77 58 perror("Feature not implemented");
jamie@77 59
jamie@77 60 /* set nan, inf or -inf to 0 */
jamie@77 61 result = (isinf(result) || isnan(result) ? 0 : result);
jamie@77 62
jamie@77 63 outlet_float(x->outlet, result);
jamie@77 64 return (w+4);
jamie@77 65 }
jamie@77 66
jamie@77 67 static t_int *xtract_perform_vector(t_int *w) {
jamie@78 68 t_sample *in = (t_float *)(w[1]);
jamie@78 69 t_sample *out = (t_float *)(w[2]);
jamie@77 70 float *temp_in, *temp_out;
jamie@77 71 t_xtract_tilde *x = (t_xtract_tilde *)(w[3]);
jamie@77 72 t_int N = (t_int)(w[4]), n;
jamie@77 73 t_int return_code = 0;
jamie@77 74
jamie@77 75 if(x->feature == XTRACT_PEAK_SPECTRUM)
jamie@77 76 N >>= 1;
jamie@77 77
jamie@77 78 n = N;
jamie@77 79
jamie@77 80 temp_in = (float *)getbytes(N * sizeof(float));
jamie@77 81 temp_out= (float *)getbytes(N * sizeof(float));
jamie@77 82
jamie@77 83 while(n--)
jamie@77 84 temp_in[n] = in[n];
jamie@77 85
jamie@77 86 n = N;
jamie@77 87
jamie@77 88 return_code = xtract[x->feature](temp_in, N, x->argv, temp_out);
jamie@77 89
jamie@77 90 while(n--)
jamie@77 91 out[n] = temp_out[n];
jamie@77 92
jamie@77 93 if(return_code == XTRACT_FEATURE_NOT_IMPLEMENTED)
jamie@77 94 perror("Feature not implemented");
jamie@77 95
jamie@77 96 freebytes(temp_in, sizeof(float) * N);
jamie@77 97 freebytes(temp_out, sizeof(float) * N);
jamie@77 98
jamie@77 99 return (w+5);
jamie@77 100 }
jamie@77 101
jamie@77 102 static void xtract_tilde_dsp(t_xtract_tilde *x, t_signal **sp) {
jamie@77 103
jamie@77 104 if(x->feature_type == XTRACT_VECTOR)
jamie@77 105 dsp_add(xtract_perform_vector, 4,
jamie@77 106 sp[0]->s_vec, sp[1]->s_vec, x, sp[0]->s_n);
jamie@77 107
jamie@77 108 else dsp_add(xtract_perform, 3, sp[0]->s_vec, x, sp[0]->s_n);
jamie@77 109
jamie@77 110 }
jamie@77 111
jamie@77 112 static void *xtract_tilde_new(t_symbol *me, t_int argc, t_atom *argv) {
jamie@77 113
jamie@77 114 t_symbol *tmp;
jamie@77 115 t_xtract_tilde *x = (t_xtract_tilde *)newobject(xtract_tilde_class);
jamie@77 116 xtract_mel_filter *mf;
jamie@78 117 t_int n, N, f, F, n_args, type, blocksize;
jamie@77 118 t_float *argv_max;
jamie@77 119 xtract_function_descriptor_t *fd;
jamie@77 120 char *p_name, *p_desc, *author;
jamie@77 121 int year;
jamie@77 122
jamie@78 123
jamie@78 124 blocksize = BLOCKSIZE; /* Default */
jamie@78 125 tmp = NULL;
jamie@77 126 p_name = p_desc = author = NULL;
jamie@77 127
jamie@77 128 n_args = type = x->feature = 0;
jamie@77 129
jamie@77 130 f = F = XTRACT_FEATURES;
jamie@77 131
jamie@78 132 /* N = BLOCKSIZE;*/
jamie@77 133
jamie@77 134 x->argv = NULL;
jamie@77 135
jamie@78 136 if(argc)
jamie@78 137 tmp = argv[0].a_w.w_sym; /*atom_getsymbol(argv); */
jamie@78 138 if(argc > 1)
jamie@78 139 blocksize = (t_int)argv[1].a_w.w_long;
jamie@78 140
jamie@78 141 N = blocksize;
jamie@77 142
jamie@77 143 /* get function descriptors */
jamie@77 144 fd = (xtract_function_descriptor_t *)xtract_make_descriptors();
jamie@77 145
jamie@77 146 /* iterate over descriptors */
jamie@77 147 while(f--){
jamie@77 148 /* map creation arg to feature */
jamie@77 149 if(tmp == gensym(fd[f].algo.name)){
jamie@77 150 x->feature = f;
jamie@77 151 break;
jamie@77 152 }
jamie@77 153 }
jamie@77 154
jamie@77 155 /* allocate memory for feature arguments */
jamie@77 156 n_args = fd[f].argc;
jamie@77 157 type = fd[f].argv.type;
jamie@77 158
jamie@77 159 if(n_args){
jamie@77 160 for(n = 0; n < n_args; n++){
jamie@77 161 argv_max = &fd[f].argv.max[n];
jamie@77 162 //post("Argument %d, max: %.2f", n, *argv_max);
jamie@77 163 }
jamie@77 164 if(type == XTRACT_MEL_FILTER){
jamie@77 165 x->memory.argv = (size_t)(n_args * sizeof(xtract_mel_filter));
jamie@77 166 x->argv = (xtract_mel_filter *)getbytes(x->memory.argv);
jamie@77 167 }
jamie@77 168 else if(type == XTRACT_INT){
jamie@77 169 x->memory.argv = (size_t)(n_args * sizeof(t_int));
jamie@77 170 x->argv = (t_int *)getbytes(x->memory.argv);
jamie@77 171 }
jamie@77 172 else if (type == XTRACT_FLOAT){
jamie@77 173 x->memory.argv = (size_t)(n_args * sizeof(t_float));
jamie@77 174 x->argv = (t_float *)getbytes(x->memory.argv);
jamie@77 175 }
jamie@77 176 else
jamie@77 177 x->memory.argv = 0;
jamie@77 178 }
jamie@77 179
jamie@77 180
jamie@77 181 p_name = fd[f].algo.p_name;
jamie@77 182 p_desc = fd[f].algo.p_desc;
jamie@77 183 author = fd[f].algo.author;
jamie@77 184 year = fd[f].algo.year;
jamie@77 185
jamie@77 186 if(argc){
jamie@77 187 if(strcmp(p_name, ""))
jamie@77 188 post("xtract~: %s", p_name );
jamie@77 189 if(strcmp(p_desc, ""))
jamie@77 190 post("xtract~: %s", p_desc );
jamie@77 191 if(strcmp(author, "") && year)
jamie@77 192 post("xtract~: %s(%d)", author, year);
jamie@77 193 }
jamie@77 194 else
jamie@77 195 post("xtract~: No arguments given");
jamie@77 196
jamie@77 197 /* do init if needed */
jamie@77 198 if(x->feature == XTRACT_MFCC){
jamie@77 199
jamie@77 200 mf = x->argv;
jamie@77 201
jamie@77 202 mf->n_filters = 20;
jamie@77 203
jamie@77 204 post("xtract~: mfcc: filters = %d",
jamie@77 205 ((xtract_mel_filter *)x->argv)->n_filters);
jamie@77 206 mf->filters =
jamie@77 207 (t_float **)getbytes(mf->n_filters * sizeof(t_float *));
jamie@77 208 for(n = 0; n < mf->n_filters; n++)
jamie@77 209 mf->filters[n] = (float *)getbytes(N * sizeof(float));
jamie@77 210
jamie@77 211 xtract_init_mfcc(N, NYQUIST, XTRACT_EQUAL_GAIN, 80.0f,
jamie@77 212 18000.0f, mf->n_filters, mf->filters);
jamie@77 213 }
jamie@77 214 else if(x->feature == XTRACT_BARK_COEFFICIENTS)
jamie@77 215 xtract_init_bark(N, NYQUIST, x->argv);
jamie@77 216
jamie@77 217 if(x->feature == XTRACT_AUTOCORRELATION ||
jamie@77 218 x->feature == XTRACT_AUTOCORRELATION_FFT ||
jamie@77 219 x->feature == XTRACT_MFCC || x->feature == XTRACT_AMDF ||
jamie@77 220 x->feature == XTRACT_ASDF|| x->feature == XTRACT_DCT ||
jamie@77 221 x->feature == XTRACT_BARK_COEFFICIENTS ||
jamie@77 222 x->feature == XTRACT_SPECTRUM ||
jamie@77 223 x->feature == XTRACT_PEAK_SPECTRUM ||
jamie@77 224 x->feature == XTRACT_HARMONIC_SPECTRUM)
jamie@77 225 x->feature_type = XTRACT_VECTOR;
jamie@77 226
jamie@77 227 else if (x->feature == XTRACT_FLUX || x->feature == XTRACT_ATTACK_TIME ||
jamie@77 228 x->feature == XTRACT_DECAY_TIME || x->feature == XTRACT_DELTA)
jamie@77 229 x->feature_type = XTRACT_DELTA;
jamie@77 230
jamie@77 231 else x->feature_type = XTRACT_SCALAR;
jamie@77 232
jamie@77 233 /* argv through right inlet */
jamie@78 234 inlet_new((t_pxobject *)x, "list");
jamie@77 235
jamie@77 236 /* DSP inlet */
jamie@77 237 dsp_setup((t_pxobject *)x, 1);
jamie@77 238
jamie@77 239
jamie@77 240 /* if feature is vector, create signal out */
jamie@77 241 if(x->feature_type == XTRACT_VECTOR)
jamie@77 242 outlet_new((t_pxobject *)x, "signal");
jamie@77 243
jamie@77 244 /* otherwise: float */
jamie@77 245 else
jamie@77 246 x->outlet = floatout((t_pxobject *)x);
jamie@77 247
jamie@77 248
jamie@77 249 /* free the function descriptors */
jamie@77 250 xtract_free_descriptors(fd);
jamie@77 251
jamie@77 252 return (void *)x;
jamie@77 253 }
jamie@77 254
jamie@77 255 static void xtract_tilde_get_args(t_xtract_tilde *x, t_symbol *selector,
jamie@77 256 t_int argc, t_atom *argv) {
jamie@77 257 /*
jamie@77 258 if(argc > (t_int)sizeof(x->argv) /
jamie@77 259 (t_int)sizeof(t_float) || x->argv == NULL)
jamie@77 260 post("Too many parameters to right inlet");
jamie@77 261 else{*/
jamie@77 262
jamie@77 263 x->argv = getbytes(argc * sizeof(float));
jamie@77 264
jamie@77 265 while(argc--)
jamie@77 266 ((t_float *)x->argv)[argc] = atom_getfloatarg(1, argc, argv);
jamie@77 267 /* }*/
jamie@77 268 }
jamie@77 269
jamie@77 270 static void xtract_tilde_show_help(t_xtract_tilde *x, t_symbol *s){
jamie@77 271
jamie@77 272 int i;
jamie@77 273
jamie@77 274 i = XTRACT_FEATURES;
jamie@77 275 xtract_function_descriptor_t *fd, *d;
jamie@77 276
jamie@77 277 fd = (xtract_function_descriptor_t *)xtract_make_descriptors();
jamie@77 278 post("\nxtract~: Feature List\n");
jamie@77 279
jamie@77 280 while(i--){
jamie@77 281 d = &fd[i];
jamie@77 282 post("\t%s", d->algo.name);
jamie@77 283 }
jamie@77 284
jamie@77 285 xtract_free_descriptors(fd);
jamie@77 286 }
jamie@77 287
jamie@77 288 static void xtract_tilde_free(t_xtract_tilde *x) {
jamie@77 289
jamie@77 290 if(x->argv != NULL && x->memory.argv)
jamie@77 291 freebytes(x->argv, x->memory.argv);
jamie@77 292 }
jamie@77 293
jamie@77 294 int main(void) {
jamie@77 295
jamie@77 296 setup((t_messlist **)&xtract_tilde_class,
jamie@77 297 (method)xtract_tilde_new,
jamie@77 298 (method)xtract_tilde_free,
jamie@77 299 sizeof(t_xtract_tilde),
jamie@77 300 0L,
jamie@77 301 A_GIMME, 0);
jamie@77 302
jamie@77 303 addmess((method)xtract_tilde_dsp, "dsp", A_CANT, 0);
jamie@78 304 addmess((method)xtract_tilde_get_args, "list", A_GIMME, 0);
jamie@77 305 addmess((method)xtract_tilde_show_help, "help", A_DEFSYM, 0);
jamie@77 306 dsp_initclass();
jamie@77 307 //class_setname("xtract~", "xtract~");
jamie@77 308
jamie@77 309 return 0;
jamie@77 310
jamie@77 311 }