jamie@141: /* jamie@141: * Copyright (C) 2012 Jamie Bullock jamie@141: * jamie@141: * Permission is hereby granted, free of charge, to any person obtaining a copy jamie@141: * of this software and associated documentation files (the "Software"), to jamie@141: * deal in the Software without restriction, including without limitation the jamie@141: * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or jamie@141: * sell copies of the Software, and to permit persons to whom the Software is jamie@141: * furnished to do so, subject to the following conditions: jamie@141: * jamie@141: * The above copyright notice and this permission notice shall be included in jamie@141: * all copies or substantial portions of the Software. jamie@141: * jamie@141: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR jamie@141: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, jamie@141: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE jamie@141: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER jamie@141: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING jamie@141: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS jamie@141: * IN THE SOFTWARE. jamie@141: * jamie@141: */ jamie@77: jamie@77: /* calculates the spectral xtract of one frame, given peak frequency and amplitude to first and second inputs respectively */ jamie@77: jamie@77: #include "ext.h" jamie@77: #include "z_dsp.h" jamie@77: #include jamie@77: #include jamie@77: jamie@77: #include "xtract/libxtract.h" jamie@77: jamie@77: #define BLOCKSIZE 1024 /* FIX: this should be dynamic - somehow */ jamie@77: #define NYQUIST 22050.0f jamie@77: jamie@77: void *xtract_tilde_class; jamie@77: jamie@77: /* Struct for keeping track of memory allocations */ jamie@77: typedef struct _tracked_memory { jamie@77: char argv; jamie@77: } tracked_memory; jamie@77: jamie@77: typedef struct _xtract { jamie@77: t_pxobject x_obj; jamie@77: void *outlet; /*Float outlet */ jamie@77: t_float f; jamie@77: t_int feature; jamie@98: t_symbol *feature_name; jamie@98: t_int init_blocksize; jamie@98: t_int done_init; jamie@77: t_int feature_type; jamie@77: tracked_memory memory; jamie@77: void *argv; jamie@146: double *data; jamie@146: double *result; jamie@77: } t_xtract_tilde; jamie@77: jamie@77: static t_int *xtract_perform(t_int *w) { jamie@78: t_float *in = (t_float *)(w[1]); jamie@77: t_xtract_tilde *x = (t_xtract_tilde *)(w[2]); jamie@77: t_int N = (t_int)(w[3]); jamie@77: t_int return_code = 0; jamie@146: double result = 0.f; jamie@77: jamie@146: for(n = 0; n < N; ++n){ jamie@146: x->data[n] = (double)in[n]; jamie@146: } jamie@146: jamie@146: return_code = xtract[x->feature](x->data, N, x->argv, &result); jamie@77: jamie@77: if(return_code == XTRACT_FEATURE_NOT_IMPLEMENTED) jamie@77: perror("Feature not implemented"); jamie@77: jamie@77: /* set nan, inf or -inf to 0 */ jamie@77: result = (isinf(result) || isnan(result) ? 0 : result); jamie@77: jamie@146: outlet_float(x->outlet, (float)result); jamie@77: return (w+4); jamie@77: } jamie@77: jamie@77: static t_int *xtract_perform_vector(t_int *w) { jamie@78: t_sample *in = (t_float *)(w[1]); jamie@78: t_sample *out = (t_float *)(w[2]); jamie@77: t_xtract_tilde *x = (t_xtract_tilde *)(w[3]); jamie@77: t_int N = (t_int)(w[4]), n; jamie@77: t_int return_code = 0; jamie@77: jamie@77: if(x->feature == XTRACT_PEAK_SPECTRUM) jamie@77: N >>= 1; jamie@77: jamie@98: if(N != x->init_blocksize && x->done_init){ jamie@98: post("xtract~ %s: Blocksize mismatch, try specifying the blocksize as a second argument", x->feature_name->s_name); jamie@98: return (w+5); jamie@98: } jamie@146: jamie@146: for(n = 0; n < N; ++n){ jamie@146: x->data[n] = (double)in[n]; jamie@146: } jamie@98: jamie@146: return_code = xtract[x->feature](x->data, N, x->argv, x->result); jamie@77: jamie@146: for(n = 0; n < N; ++n){ jamie@146: out[n] = (float)x->result[n]; jamie@146: } jamie@77: jamie@77: if(return_code == XTRACT_FEATURE_NOT_IMPLEMENTED) jamie@77: perror("Feature not implemented"); jamie@77: jamie@77: return (w+5); jamie@77: } jamie@77: jamie@77: static void xtract_tilde_dsp(t_xtract_tilde *x, t_signal **sp) { jamie@77: jamie@77: if(x->feature_type == XTRACT_VECTOR) jamie@77: dsp_add(xtract_perform_vector, 4, jamie@77: sp[0]->s_vec, sp[1]->s_vec, x, sp[0]->s_n); jamie@77: jamie@77: else dsp_add(xtract_perform, 3, sp[0]->s_vec, x, sp[0]->s_n); jamie@77: jamie@77: } jamie@77: jamie@77: static void *xtract_tilde_new(t_symbol *me, t_int argc, t_atom *argv) { jamie@77: jamie@77: t_symbol *tmp; jamie@77: t_xtract_tilde *x = (t_xtract_tilde *)newobject(xtract_tilde_class); jamie@77: xtract_mel_filter *mf; jamie@98: t_int n, N, f, F, n_args, type; jamie@77: t_float *argv_max; jamie@77: xtract_function_descriptor_t *fd; jamie@77: char *p_name, *p_desc, *author; jamie@77: int year; jamie@77: jamie@78: tmp = NULL; jamie@77: p_name = p_desc = author = NULL; jamie@77: jamie@77: n_args = type = x->feature = 0; jamie@77: jamie@77: f = F = XTRACT_FEATURES; jamie@77: jamie@98: N = BLOCKSIZE; jamie@77: jamie@77: x->argv = NULL; jamie@98: x->done_init = 0; jamie@146: jamie@146: x->data = (double *)getbytes(N * sizeof(double)); jamie@146: x->result = (double *)getbytes(N * sizeof(double)); jamie@146: jamie@77: jamie@78: if(argc) jamie@78: tmp = argv[0].a_w.w_sym; /*atom_getsymbol(argv); */ jamie@78: if(argc > 1) jamie@98: N = (t_int)argv[1].a_w.w_long; jamie@78: jamie@98: x->init_blocksize = N; jamie@77: jamie@77: /* get function descriptors */ jamie@77: fd = (xtract_function_descriptor_t *)xtract_make_descriptors(); jamie@77: jamie@77: /* iterate over descriptors */ jamie@77: while(f--){ jamie@77: /* map creation arg to feature */ jamie@77: if(tmp == gensym(fd[f].algo.name)){ jamie@77: x->feature = f; jamie@77: break; jamie@77: } jamie@77: } jamie@77: jamie@77: /* allocate memory for feature arguments */ jamie@77: n_args = fd[f].argc; jamie@77: type = fd[f].argv.type; jamie@77: jamie@77: if(n_args){ jamie@77: for(n = 0; n < n_args; n++){ jamie@77: argv_max = &fd[f].argv.max[n]; jamie@77: //post("Argument %d, max: %.2f", n, *argv_max); jamie@77: } jamie@77: if(type == XTRACT_MEL_FILTER){ jamie@77: x->memory.argv = (size_t)(n_args * sizeof(xtract_mel_filter)); jamie@77: x->argv = (xtract_mel_filter *)getbytes(x->memory.argv); jamie@77: } jamie@77: else if(type == XTRACT_INT){ jamie@77: x->memory.argv = (size_t)(n_args * sizeof(t_int)); jamie@77: x->argv = (t_int *)getbytes(x->memory.argv); jamie@77: } jamie@77: else if (type == XTRACT_FLOAT){ jamie@77: x->memory.argv = (size_t)(n_args * sizeof(t_float)); jamie@77: x->argv = (t_float *)getbytes(x->memory.argv); jamie@77: } jamie@77: else jamie@77: x->memory.argv = 0; jamie@77: } jamie@77: jamie@77: jamie@77: p_name = fd[f].algo.p_name; jamie@77: p_desc = fd[f].algo.p_desc; jamie@77: author = fd[f].algo.author; jamie@77: year = fd[f].algo.year; jamie@77: jamie@77: if(argc){ jamie@77: if(strcmp(p_name, "")) jamie@77: post("xtract~: %s", p_name ); jamie@77: if(strcmp(p_desc, "")) jamie@77: post("xtract~: %s", p_desc ); jamie@77: if(strcmp(author, "") && year) jamie@77: post("xtract~: %s(%d)", author, year); jamie@77: } jamie@77: else jamie@77: post("xtract~: No arguments given"); jamie@77: jamie@77: /* do init if needed */ jamie@77: if(x->feature == XTRACT_MFCC){ jamie@77: jamie@77: mf = x->argv; jamie@77: jamie@77: mf->n_filters = 20; jamie@77: jamie@77: post("xtract~: mfcc: filters = %d", jamie@77: ((xtract_mel_filter *)x->argv)->n_filters); jamie@77: mf->filters = jamie@77: (t_float **)getbytes(mf->n_filters * sizeof(t_float *)); jamie@77: for(n = 0; n < mf->n_filters; n++) jamie@77: mf->filters[n] = (float *)getbytes(N * sizeof(float)); jamie@77: jamie@77: xtract_init_mfcc(N, NYQUIST, XTRACT_EQUAL_GAIN, 80.0f, jamie@77: 18000.0f, mf->n_filters, mf->filters); jamie@77: } jamie@77: else if(x->feature == XTRACT_BARK_COEFFICIENTS) jamie@77: xtract_init_bark(N, NYQUIST, x->argv); jamie@77: jamie@98: /* Initialise fft_plan if required */ jamie@98: if(x->feature == XTRACT_AUTOCORRELATION_FFT || jamie@98: x->feature == XTRACT_SPECTRUM || jamie@98: x->feature == XTRACT_DCT){ jamie@98: xtract_init_fft(N, x->feature); jamie@98: x->done_init = 1; jamie@98: } jamie@98: jamie@77: if(x->feature == XTRACT_AUTOCORRELATION || jamie@77: x->feature == XTRACT_AUTOCORRELATION_FFT || jamie@77: x->feature == XTRACT_MFCC || x->feature == XTRACT_AMDF || jamie@77: x->feature == XTRACT_ASDF|| x->feature == XTRACT_DCT || jamie@77: x->feature == XTRACT_BARK_COEFFICIENTS || jamie@77: x->feature == XTRACT_SPECTRUM || jamie@77: x->feature == XTRACT_PEAK_SPECTRUM || jamie@77: x->feature == XTRACT_HARMONIC_SPECTRUM) jamie@77: x->feature_type = XTRACT_VECTOR; jamie@77: jamie@77: else if (x->feature == XTRACT_FLUX || x->feature == XTRACT_ATTACK_TIME || jamie@77: x->feature == XTRACT_DECAY_TIME || x->feature == XTRACT_DELTA) jamie@77: x->feature_type = XTRACT_DELTA; jamie@77: jamie@77: else x->feature_type = XTRACT_SCALAR; jamie@77: jamie@77: /* argv through right inlet */ jamie@78: inlet_new((t_pxobject *)x, "list"); jamie@77: jamie@77: /* DSP inlet */ jamie@77: dsp_setup((t_pxobject *)x, 1); jamie@77: jamie@77: jamie@77: /* if feature is vector, create signal out */ jamie@77: if(x->feature_type == XTRACT_VECTOR) jamie@77: outlet_new((t_pxobject *)x, "signal"); jamie@77: jamie@77: /* otherwise: float */ jamie@77: else jamie@77: x->outlet = floatout((t_pxobject *)x); jamie@77: jamie@77: jamie@77: /* free the function descriptors */ jamie@77: xtract_free_descriptors(fd); jamie@77: jamie@77: return (void *)x; jamie@77: } jamie@77: jamie@77: static void xtract_tilde_get_args(t_xtract_tilde *x, t_symbol *selector, jamie@77: t_int argc, t_atom *argv) { jamie@77: /* jamie@77: if(argc > (t_int)sizeof(x->argv) / jamie@77: (t_int)sizeof(t_float) || x->argv == NULL) jamie@77: post("Too many parameters to right inlet"); jamie@77: else{*/ jamie@77: jamie@79: t_float temp = 0.f; jamie@77: x->argv = getbytes(argc * sizeof(float)); jamie@77: jamie@79: while(argc--){ jamie@79: temp = argv[argc].a_w.w_float; jamie@79: ((t_float *)x->argv)[argc] = temp; jamie@79: } jamie@77: /* }*/ jamie@77: } jamie@77: jamie@77: static void xtract_tilde_show_help(t_xtract_tilde *x, t_symbol *s){ jamie@77: jamie@77: int i; jamie@77: jamie@77: i = XTRACT_FEATURES; jamie@77: xtract_function_descriptor_t *fd, *d; jamie@77: jamie@77: fd = (xtract_function_descriptor_t *)xtract_make_descriptors(); jamie@77: post("\nxtract~: Feature List\n"); jamie@77: jamie@77: while(i--){ jamie@77: d = &fd[i]; jamie@77: post("\t%s", d->algo.name); jamie@77: } jamie@77: jamie@77: xtract_free_descriptors(fd); jamie@77: } jamie@77: jamie@77: static void xtract_tilde_free(t_xtract_tilde *x) { jamie@77: jamie@77: if(x->argv != NULL && x->memory.argv) jamie@77: freebytes(x->argv, x->memory.argv); jamie@146: freebytes(x->data); jamie@146: freebytes(x->result); jamie@77: } jamie@77: jamie@77: int main(void) { jamie@77: jamie@77: setup((t_messlist **)&xtract_tilde_class, jamie@77: (method)xtract_tilde_new, jamie@77: (method)xtract_tilde_free, jamie@77: sizeof(t_xtract_tilde), jamie@77: 0L, jamie@77: A_GIMME, 0); jamie@77: jamie@77: addmess((method)xtract_tilde_dsp, "dsp", A_CANT, 0); jamie@78: addmess((method)xtract_tilde_get_args, "list", A_GIMME, 0); jamie@77: addmess((method)xtract_tilde_show_help, "help", A_DEFSYM, 0); jamie@77: dsp_initclass(); jamie@77: //class_setname("xtract~", "xtract~"); jamie@77: jamie@77: return 0; jamie@77: jamie@77: }