annotate examples/puredata/xtract~.c @ 52:45c585bb7996

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