annotate examples/puredata/xtract~.c @ 62:1222cd753029

Improvements an fixes to PD example and help files
author Jamie Bullock <jamie@postlude.co.uk>
date Mon, 12 Mar 2007 18:20:43 +0000
parents 8fd7088c8ff6
children 89b516adb5df
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@4 30 static t_class *xtract_class;
jamie@4 31
jamie@41 32 /* Struct for keeping track of memory allocations */
jamie@41 33 typedef struct _tracked_memory {
jamie@41 34 char argv;
jamie@41 35 } tracked_memory;
jamie@41 36
jamie@4 37 typedef struct _xtract {
jamie@4 38 t_object x_obj;
jamie@4 39 t_float f;
jamie@4 40 t_int feature;
jamie@4 41 t_int feature_type;
jamie@41 42 tracked_memory memory;
jamie@4 43 void *argv;
jamie@4 44 } t_xtract_tilde;
jamie@4 45
jamie@4 46 static t_int *xtract_perform(t_int *w) {
jamie@4 47 t_sample *in = (t_sample *)(w[1]);
jamie@4 48 t_xtract_tilde *x = (t_xtract_tilde *)(w[2]);
jamie@4 49 t_int N = (t_int)(w[3]);
jamie@38 50 t_int return_code = 0;
jamie@4 51 float result = 0;
jamie@4 52
jamie@38 53 return_code = xtract[x->feature]((float *)in, N, x->argv, &result);
jamie@4 54
jamie@56 55 if(return_code == XTRACT_FEATURE_NOT_IMPLEMENTED)
jamie@38 56 pd_error(x, "Feature not implemented");
jamie@42 57
jamie@42 58 /* set nan, inf or -inf to 0 */
jamie@42 59 result = (isinf(result) || isnan(result) ? 0 : result);
jamie@38 60
jamie@4 61 outlet_float(x->x_obj.ob_outlet, result);
jamie@4 62 return (w+4);
jamie@4 63 }
jamie@4 64
jamie@4 65 static t_int *xtract_perform_vector(t_int *w) {
jamie@4 66 t_sample *in = (t_sample *)(w[1]);
jamie@4 67 t_sample *out = (t_sample *)(w[2]);
jamie@4 68 t_float *tmp_in, *tmp_out;
jamie@4 69 t_xtract_tilde *x = (t_xtract_tilde *)(w[3]);
jamie@4 70 t_int N = (t_int)(w[4]), n;
jamie@38 71 t_int return_code = 0;
jamie@4 72
jamie@62 73 n = N;
jamie@62 74
jamie@4 75 tmp_in = copybytes(in, N * sizeof(t_float));
jamie@4 76 tmp_out = getbytes(N * sizeof(t_float));
jamie@62 77
jamie@62 78 if(x->feature == XTRACT_PEAK_SPECTRUM)
jamie@62 79 N >>= 1;
jamie@4 80
jamie@38 81 return_code = xtract[x->feature](tmp_in, N, x->argv, tmp_out);
jamie@38 82
jamie@56 83 if(return_code == XTRACT_FEATURE_NOT_IMPLEMENTED)
jamie@38 84 pd_error(x, "Feature not implemented");
jamie@4 85
jamie@4 86 while(n--) out[n] = tmp_out[n];
jamie@4 87
jamie@4 88 freebytes(tmp_in, N * sizeof(t_float));
jamie@4 89 freebytes(tmp_out, N * sizeof(t_float));
jamie@4 90
jamie@4 91 return (w+5);
jamie@4 92 }
jamie@4 93
jamie@4 94 static void xtract_dsp(t_xtract_tilde *x, t_signal **sp) {
jamie@4 95
jamie@56 96 if(x->feature_type == XTRACT_VECTOR)
jamie@4 97 dsp_add(xtract_perform_vector, 4,
jamie@4 98 sp[0]->s_vec, sp[1]->s_vec, x, sp[0]->s_n);
jamie@4 99
jamie@4 100 else dsp_add(xtract_perform, 3, sp[0]->s_vec, x, sp[0]->s_n);
jamie@4 101
jamie@4 102 }
jamie@4 103
jamie@4 104 static void *xtract_new(t_symbol *me, t_int argc, t_atom *argv) {
jamie@4 105
jamie@4 106 t_symbol *tmp;
jamie@4 107 t_xtract_tilde *x = (t_xtract_tilde *)pd_new(xtract_class);
jamie@50 108 xtract_mel_filter *mf;
jamie@50 109 t_int n, N, f, F, n_args, type;
jamie@55 110 t_float *argv_max;
jamie@56 111 xtract_function_descriptor_t *fd;
jamie@52 112 char *p_name, *p_desc, *author;
jamie@62 113 int year;
jamie@52 114
jamie@52 115 p_name = p_desc = author = NULL;
jamie@4 116
jamie@51 117 n_args = type = x->feature = 0;
jamie@50 118
jamie@50 119 f = F = XTRACT_FEATURES;
jamie@50 120
jamie@4 121 N = BLOCKSIZE;
jamie@4 122
jamie@4 123 x->argv = NULL;
jamie@4 124
jamie@4 125 tmp = atom_getsymbol(argv);
jamie@4 126
jamie@50 127 /* get function descriptors */
jamie@56 128 fd = (xtract_function_descriptor_t *)xtract_make_descriptors();
jamie@50 129
jamie@50 130 /* iterate over descriptors */
jamie@50 131 while(f--){
jamie@50 132 /* map creation arg to feature */
jamie@50 133 if(tmp == gensym(fd[f].algo.name)){
jamie@50 134 x->feature = f;
jamie@50 135 break;
jamie@50 136 }
jamie@50 137 }
jamie@4 138
jamie@41 139 /* allocate memory for feature arguments */
jamie@51 140 n_args = fd[f].argc;
jamie@50 141 type = fd[f].argv.type;
jamie@50 142
jamie@50 143 if(n_args){
jamie@55 144 for(n = 0; n < n_args; n++){
jamie@55 145 argv_max = &fd[f].argv.max[n];
jamie@59 146 //post("Argument %d, max: %.2f", n, *argv_max);
jamie@55 147 }
jamie@56 148 if(type == XTRACT_MEL_FILTER){
jamie@50 149 x->memory.argv = (size_t)(n_args * sizeof(xtract_mel_filter));
jamie@50 150 x->argv = (xtract_mel_filter *)getbytes(x->memory.argv);
jamie@50 151 }
jamie@56 152 else if(type == XTRACT_INT){
jamie@50 153 x->memory.argv = (size_t)(n_args * sizeof(t_int));
jamie@50 154 x->argv = (t_int *)getbytes(x->memory.argv);
jamie@50 155 }
jamie@56 156 else if (type == XTRACT_FLOAT){
jamie@50 157 x->memory.argv = (size_t)(n_args * sizeof(t_float));
jamie@50 158 x->argv = (t_float *)getbytes(x->memory.argv);
jamie@50 159 }
jamie@50 160 else
jamie@50 161 x->memory.argv = 0;
jamie@41 162 }
jamie@52 163
jamie@52 164
jamie@52 165 p_name = fd[f].algo.p_name;
jamie@52 166 p_desc = fd[f].algo.p_desc;
jamie@52 167 author = fd[f].algo.author;
jamie@62 168 year = fd[f].algo.year;
jamie@52 169
jamie@52 170 if(argc){
jamie@52 171 if(strcmp(p_name, ""))
jamie@52 172 post("xtract~: %s", p_name );
jamie@52 173 if(strcmp(p_desc, ""))
jamie@52 174 post("xtract~: %s", p_desc );
jamie@52 175 if(strcmp(author, "") && year)
jamie@62 176 post("xtract~: %s(%d)", author, year);
jamie@52 177 }
jamie@52 178 else
jamie@52 179 post("xtract~: No arguments given");
jamie@50 180
jamie@41 181
jamie@41 182 /* do init if needed */
jamie@56 183 if(x->feature == XTRACT_MFCC){
jamie@41 184
jamie@50 185 mf = x->argv;
jamie@4 186
jamie@50 187 mf->n_filters = 20;
jamie@4 188
jamie@56 189 post("xtract~: mfcc: filters = %d",
jamie@56 190 ((xtract_mel_filter *)x->argv)->n_filters);
jamie@50 191 mf->filters =
jamie@50 192 (t_float **)getbytes(mf->n_filters * sizeof(t_float *));
jamie@50 193 for(n = 0; n < mf->n_filters; n++)
jamie@50 194 mf->filters[n] = (float *)getbytes(N * sizeof(float));
jamie@4 195
jamie@62 196 xtract_init_mfcc(N, NYQUIST, XTRACT_EQUAL_GAIN, 80.0f,
jamie@62 197 18000.0f, mf->n_filters, mf->filters);
jamie@4 198 }
jamie@56 199 else if(x->feature == XTRACT_BARK_COEFFICIENTS)
jamie@4 200 xtract_init_bark(N, NYQUIST, x->argv);
jamie@41 201
jamie@62 202 if(x->feature == XTRACT_AUTOCORRELATION ||
jamie@62 203 x->feature == XTRACT_AUTOCORRELATION_FFT ||
jamie@62 204 x->feature == XTRACT_MFCC || x->feature == XTRACT_AMDF ||
jamie@62 205 x->feature == XTRACT_ASDF|| x->feature == XTRACT_DCT ||
jamie@62 206 x->feature == XTRACT_BARK_COEFFICIENTS ||
jamie@62 207 x->feature == XTRACT_SPECTRUM ||
jamie@62 208 x->feature == XTRACT_PEAK_SPECTRUM ||
jamie@62 209 x->feature == XTRACT_HARMONIC_SPECTRUM)
jamie@62 210 x->feature_type = XTRACT_VECTOR;
jamie@4 211
jamie@56 212 else if (x->feature == XTRACT_FLUX || x->feature == XTRACT_ATTACK_TIME ||
jamie@56 213 x->feature == XTRACT_DECAY_TIME || x->feature == XTRACT_DELTA)
jamie@56 214 x->feature_type = XTRACT_DELTA;
jamie@4 215
jamie@56 216 else x->feature_type = XTRACT_SCALAR;
jamie@4 217
jamie@4 218 /* argv through right inlet */
jamie@4 219 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("list"), gensym("list"));
jamie@4 220
jamie@4 221 /* if feature is vector, create signal out */
jamie@56 222 if(x->feature_type == XTRACT_VECTOR) outlet_new(&x->x_obj, &s_signal);
jamie@4 223
jamie@4 224 /* otherwise: float */
jamie@4 225 else outlet_new(&x->x_obj, &s_float);
jamie@4 226
jamie@50 227 /* free the function descriptors */
jamie@50 228 xtract_free_descriptors(fd);
jamie@50 229
jamie@4 230 return (void *)x;
jamie@4 231 }
jamie@4 232
jamie@4 233 static void xtract_tilde_get_args(t_xtract_tilde *x, t_symbol *selector,
jamie@4 234 t_int argc, t_atom *argv) {
jamie@4 235 /*
jamie@4 236 if(argc > (t_int)sizeof(x->argv) /
jamie@4 237 (t_int)sizeof(t_float) || x->argv == NULL)
jamie@4 238 post("Too many parameters to right inlet");
jamie@4 239 else{*/
jamie@4 240
jamie@4 241 x->argv = getbytes(argc * sizeof(float));
jamie@4 242
jamie@4 243 while(argc--)
jamie@4 244 ((t_float *)x->argv)[argc] = atom_getfloat(&argv[argc]);
jamie@4 245 /* }*/
jamie@4 246 }
jamie@4 247
jamie@26 248 static void xtract_tilde_show_help(t_xtract_tilde *x, t_symbol *s){
jamie@26 249
jamie@26 250 int i;
jamie@26 251
jamie@26 252 i = XTRACT_FEATURES;
jamie@62 253 xtract_function_descriptor_t *fd, *d;
jamie@26 254
jamie@62 255 fd = (xtract_function_descriptor_t *)xtract_make_descriptors();
jamie@62 256 post("\nxtract~: Feature List\n");
jamie@62 257
jamie@26 258 while(i--){
jamie@62 259 d = &fd[i];
jamie@62 260 post("\t%s", d->algo.name);
jamie@62 261 }
jamie@62 262
jamie@62 263 xtract_free_descriptors(fd);
jamie@26 264 }
jamie@26 265
jamie@4 266 static void xtract_tilde_free(t_xtract_tilde *x) {
jamie@41 267
jamie@41 268 if(x->argv != NULL && x->memory.argv)
jamie@41 269 freebytes(x->argv, x->memory.argv);
jamie@4 270 }
jamie@4 271
jamie@4 272 void xtract_tilde_setup(void) {
jamie@4 273 xtract_class = class_new(gensym("xtract~"),
jamie@4 274 (t_newmethod)xtract_new,
jamie@4 275 (t_method)xtract_tilde_free,
jamie@4 276 sizeof(t_xtract_tilde),
jamie@4 277 CLASS_DEFAULT,
jamie@4 278 A_GIMME, 0);
jamie@4 279
jamie@4 280 class_addmethod(xtract_class,
jamie@26 281 (t_method)xtract_dsp, gensym("dsp"), 0);
jamie@4 282 class_addmethod(xtract_class,
jamie@26 283 (t_method)xtract_tilde_get_args, gensym("list"), A_GIMME, 0);
jamie@26 284 class_addmethod(xtract_class,
jamie@26 285 (t_method)xtract_tilde_show_help, gensym("help"), A_DEFSYMBOL, 0);
jamie@4 286 CLASS_MAINSIGNALIN(xtract_class, t_xtract_tilde, f);
jamie@33 287 class_sethelpsymbol(xtract_class, gensym("xtract-help"));
jamie@4 288 }