jamie@4: /* xtract~ - PD library for feature extraction jamie@4: Copyright (C) 2006 Jamie Bullock jamie@4: jamie@4: This program is free software; you can redistribute it and/or jamie@4: modify it under the terms of the GNU General Public License jamie@4: as published by the Free Software Foundation; either version 2 jamie@4: of the License, or (at your option) any later version. jamie@4: jamie@4: This program is distributed in the hope that it will be useful, jamie@4: but WITHOUT ANY WARRANTY; without even the implied warranty of jamie@4: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jamie@4: GNU General Public License for more details. jamie@4: jamie@4: You should have received a copy of the GNU General Public License jamie@4: along with this program; if not, write to the Free Software jamie@4: Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. jamie@4: */ jamie@4: jamie@4: /* calculates the spectral xtract of one frame, given peak frequency and amplitude to first and second inputs respectively */ jamie@4: jamie@41: #include "m_pd.h" jamie@42: #include jamie@41: jamie@9: #define XTRACT jamie@4: #include "xtract/libxtract.h" jamie@4: jamie@4: #define BLOCKSIZE 1024 jamie@4: #define NYQUIST 22050.0f jamie@4: jamie@4: static t_class *xtract_class; jamie@4: jamie@41: /* Struct for keeping track of memory allocations */ jamie@41: typedef struct _tracked_memory { jamie@41: char argv; jamie@41: } tracked_memory; jamie@41: jamie@4: typedef struct _xtract { jamie@4: t_object x_obj; jamie@4: t_float f; jamie@4: t_int feature; jamie@4: t_int feature_type; jamie@41: tracked_memory memory; jamie@4: void *argv; jamie@4: } t_xtract_tilde; jamie@4: jamie@4: static t_int *xtract_perform(t_int *w) { jamie@4: t_sample *in = (t_sample *)(w[1]); jamie@4: t_xtract_tilde *x = (t_xtract_tilde *)(w[2]); jamie@4: t_int N = (t_int)(w[3]); jamie@38: t_int return_code = 0; jamie@4: float result = 0; jamie@4: jamie@38: return_code = xtract[x->feature]((float *)in, N, x->argv, &result); jamie@4: jamie@38: if(return_code == FEATURE_NOT_IMPLEMENTED) jamie@38: pd_error(x, "Feature not implemented"); jamie@42: jamie@42: /* set nan, inf or -inf to 0 */ jamie@42: result = (isinf(result) || isnan(result) ? 0 : result); jamie@38: jamie@4: outlet_float(x->x_obj.ob_outlet, result); jamie@4: return (w+4); jamie@4: } jamie@4: jamie@4: static t_int *xtract_perform_vector(t_int *w) { jamie@4: t_sample *in = (t_sample *)(w[1]); jamie@4: t_sample *out = (t_sample *)(w[2]); jamie@4: t_float *tmp_in, *tmp_out; jamie@4: t_xtract_tilde *x = (t_xtract_tilde *)(w[3]); jamie@4: t_int N = (t_int)(w[4]), n; jamie@38: t_int return_code = 0; jamie@4: jamie@4: tmp_in = copybytes(in, N * sizeof(t_float)); jamie@4: tmp_out = getbytes(N * sizeof(t_float)); jamie@4: jamie@38: return_code = xtract[x->feature](tmp_in, N, x->argv, tmp_out); jamie@38: jamie@38: if(return_code == FEATURE_NOT_IMPLEMENTED) jamie@38: pd_error(x, "Feature not implemented"); jamie@4: jamie@4: n = N; jamie@4: jamie@4: while(n--) out[n] = tmp_out[n]; jamie@4: jamie@4: freebytes(tmp_in, N * sizeof(t_float)); jamie@4: freebytes(tmp_out, N * sizeof(t_float)); jamie@4: jamie@4: return (w+5); jamie@4: } jamie@4: jamie@4: static void xtract_dsp(t_xtract_tilde *x, t_signal **sp) { jamie@4: jamie@4: if(x->feature_type == VECTOR) jamie@4: dsp_add(xtract_perform_vector, 4, jamie@4: sp[0]->s_vec, sp[1]->s_vec, x, sp[0]->s_n); jamie@4: jamie@4: else dsp_add(xtract_perform, 3, sp[0]->s_vec, x, sp[0]->s_n); jamie@4: jamie@4: } jamie@4: jamie@4: static void *xtract_new(t_symbol *me, t_int argc, t_atom *argv) { jamie@4: jamie@4: t_symbol *tmp; jamie@4: t_xtract_tilde *x = (t_xtract_tilde *)pd_new(xtract_class); jamie@4: xtract_mel_filter *f; jamie@41: t_int n, N, floatargs = 0; jamie@4: jamie@4: N = BLOCKSIZE; jamie@4: jamie@4: x->argv = NULL; jamie@4: jamie@4: tmp = atom_getsymbol(argv); jamie@4: jamie@41: /* map creation args to features */ jamie@4: if(tmp == gensym("mean")) x->feature = MEAN; jamie@41: else if(tmp == gensym("variance")) x->feature = VARIANCE; jamie@41: else if(tmp == gensym("standard_deviation"))x->feature = STANDARD_DEVIATION; jamie@4: else if(tmp == gensym("average_deviation")) x->feature = AVERAGE_DEVIATION; jamie@4: else if(tmp == gensym("skewness")) x->feature = SKEWNESS; jamie@4: else if(tmp == gensym("kurtosis")) x->feature = KURTOSIS; jamie@11: else if(tmp == gensym("centroid")) x->feature = CENTROID; jamie@4: else if(tmp == gensym("irregularity_k")) x->feature = IRREGULARITY_K; jamie@4: else if(tmp == gensym("irregularity_j")) x->feature = IRREGULARITY_J; jamie@4: else if(tmp == gensym("tristimulus_1")) x->feature = TRISTIMULUS_1; jamie@4: else if(tmp == gensym("tristimulus_2")) x->feature = TRISTIMULUS_2; jamie@4: else if(tmp == gensym("tristimulus_3")) x->feature = TRISTIMULUS_3; jamie@4: else if(tmp == gensym("smoothness")) x->feature = SMOOTHNESS; jamie@4: else if(tmp == gensym("spread")) x->feature = SPREAD; jamie@4: else if(tmp == gensym("zcr")) x->feature = ZCR; jamie@4: else if(tmp == gensym("rolloff")) x->feature = ROLLOFF; jamie@4: else if(tmp == gensym("loudness")) x->feature = LOUDNESS; jamie@4: else if(tmp == gensym("flatness")) x->feature = FLATNESS; jamie@4: else if(tmp == gensym("tonality")) x->feature = TONALITY; jamie@4: else if(tmp == gensym("crest")) x->feature = CREST; jamie@4: else if(tmp == gensym("noisiness")) x->feature = NOISINESS; jamie@4: else if(tmp == gensym("rms_amplitude")) x->feature = RMS_AMPLITUDE; jamie@4: else if(tmp == gensym("inharmonicity")) x->feature = INHARMONICITY; jamie@4: else if(tmp == gensym("power")) x->feature = POWER; jamie@4: else if(tmp == gensym("odd_even_ratio")) x->feature = ODD_EVEN_RATIO; jamie@4: else if(tmp == gensym("sharpness")) x->feature = SHARPNESS; jamie@4: else if(tmp == gensym("slope")) x->feature = SLOPE; jamie@41: else if(tmp == gensym("f0")) x->feature = F0; jamie@12: else if(tmp == gensym("hps"))x->feature = HPS; jamie@41: else if(tmp == gensym("lowest_match"))x->feature = LOWEST_MATCH; jamie@41: else if(tmp == gensym("dct")) x->feature = DCT; jamie@4: else if(tmp == gensym("magnitude_spectrum")) jamie@4: x->feature = MAGNITUDE_SPECTRUM; jamie@4: else if(tmp == gensym("autocorrelation")) x->feature = AUTOCORRELATION; jamie@4: else if(tmp == gensym("autocorrelation_fft")) jamie@4: x->feature = AUTOCORRELATION_FFT; jamie@4: else if(tmp == gensym("amdf")) x->feature = AMDF; jamie@4: else if(tmp == gensym("asdf")) x->feature = ASDF; jamie@41: else if(tmp == gensym("peaks")) x->feature = PEAKS; jamie@41: else if(tmp == gensym("flux")) x->feature = FLUX; jamie@41: else if(tmp == gensym("attack_time")) x->feature = ATTACK_TIME; jamie@41: else if(tmp == gensym("decay_time")) x->feature = DECAY_TIME; jamie@41: else if(tmp == gensym("delta")) x->feature = DELTA_FEATURE; jamie@41: else if(tmp == gensym("mfcc")) x->feature = MFCC; jamie@41: else if(tmp == gensym("harmonics")) x->feature = HARMONICS; jamie@41: else if(tmp == gensym("bark_coefficients")) x->feature = BARK_COEFFICIENTS; jamie@41: else post("xtract~: No feature selected"); jamie@4: jamie@41: /* allocate memory for feature arguments */ jamie@41: switch(x->feature){ jamie@41: case MEAN: jamie@41: case VARIANCE: jamie@41: case STANDARD_DEVIATION: jamie@41: case AVERAGE_DEVIATION: jamie@41: case ROLLOFF: jamie@41: case INHARMONICITY: jamie@41: case LOWEST_MATCH: jamie@41: case F0: jamie@42: case TONALITY: jamie@41: floatargs = 1; jamie@41: break; jamie@41: case SKEWNESS: jamie@41: case KURTOSIS: jamie@41: case PEAKS: jamie@41: case HARMONICS: jamie@41: floatargs = 2; jamie@41: break; jamie@41: case CENTROID: jamie@41: case IRREGULARITY_K: jamie@41: case IRREGULARITY_J: jamie@41: case TRISTIMULUS_1: jamie@41: case TRISTIMULUS_2: jamie@41: case TRISTIMULUS_3: jamie@41: case SMOOTHNESS: jamie@41: case SPREAD: jamie@41: case ZCR: jamie@41: case LOUDNESS: jamie@41: case FLATNESS: jamie@41: case CREST: jamie@41: case NOISINESS: jamie@41: case RMS_AMPLITUDE: jamie@41: case POWER: jamie@41: case ODD_EVEN_RATIO: jamie@41: case SHARPNESS: jamie@41: case SLOPE: jamie@41: case HPS: jamie@41: case FLUX: /*not implemented */ jamie@41: case ATTACK_TIME: /*not implemented */ jamie@41: case DECAY_TIME: /*not implemented */ jamie@41: case DELTA_FEATURE: /*not implemented */ jamie@41: case AUTOCORRELATION_FFT: jamie@41: case MAGNITUDE_SPECTRUM: jamie@41: case MFCC: jamie@41: case DCT: jamie@41: case AUTOCORRELATION: jamie@41: case AMDF: jamie@41: case ASDF: jamie@41: case BARK_COEFFICIENTS: jamie@41: floatargs = 0; jamie@41: break; jamie@41: default: jamie@41: floatargs = 0; jamie@41: break; jamie@41: } jamie@41: jamie@41: if(x->feature == MFCC){ jamie@41: x->memory.argv = (size_t)(sizeof(xtract_mel_filter)); jamie@41: x->argv = (xtract_mel_filter *)getbytes(x->memory.argv); jamie@41: } jamie@41: else if(x->feature == BARK_COEFFICIENTS){ jamie@42: x->memory.argv = (size_t)(BARK_BANDS * sizeof(t_int)); jamie@41: x->argv = (t_int *)getbytes(x->memory.argv); jamie@41: } jamie@41: else if (floatargs){ jamie@41: x->memory.argv = (size_t)(floatargs * sizeof(t_float)); jamie@41: x->argv = (t_float *)getbytes(x->memory.argv); jamie@41: } jamie@41: else jamie@41: x->memory.argv = 0; jamie@41: jamie@41: /* do init if needed */ jamie@41: if(x->feature == MFCC){ jamie@41: jamie@4: f = x->argv; jamie@4: jamie@4: f->n_filters = 20; jamie@4: jamie@41: post("xtract~: mfcc: filters = %d", ((xtract_mel_filter *)x->argv)->n_filters); jamie@4: f->filters = jamie@4: (t_float **)getbytes(f->n_filters * sizeof(t_float *)); jamie@4: for(n = 0; n < f->n_filters; n++) jamie@4: f->filters[n] = (float *)getbytes(N * sizeof(float)); jamie@4: jamie@4: xtract_init_mfcc(N, NYQUIST, EQUAL_GAIN, 18000.0f, jamie@4: 80.0f, f->n_filters, f->filters); jamie@4: } jamie@41: else if(x->feature == BARK_COEFFICIENTS) jamie@4: xtract_init_bark(N, NYQUIST, x->argv); jamie@41: jamie@4: if(x->feature == AUTOCORRELATION || x->feature == AUTOCORRELATION_FFT || jamie@4: x->feature == MFCC || x->feature == AMDF || x->feature == ASDF|| jamie@4: x->feature == DCT || x->feature == BARK_COEFFICIENTS || jamie@38: x->feature == MAGNITUDE_SPECTRUM || x->feature == PEAKS || jamie@38: x->feature == HARMONICS) jamie@4: x->feature_type = VECTOR; jamie@4: jamie@4: else if (x->feature == FLUX || x->feature == ATTACK_TIME || jamie@4: x->feature == DECAY_TIME || x->feature == DELTA) jamie@4: x->feature_type = DELTA; jamie@4: jamie@4: else x->feature_type = SCALAR; jamie@4: jamie@4: /* argv through right inlet */ jamie@4: inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("list"), gensym("list")); jamie@4: jamie@4: /* if feature is vector, create signal out */ jamie@4: if(x->feature_type == VECTOR) outlet_new(&x->x_obj, &s_signal); jamie@4: jamie@4: /* otherwise: float */ jamie@4: else outlet_new(&x->x_obj, &s_float); jamie@4: jamie@4: return (void *)x; jamie@4: } jamie@4: jamie@4: static void xtract_tilde_get_args(t_xtract_tilde *x, t_symbol *selector, jamie@4: t_int argc, t_atom *argv) { jamie@4: /* jamie@4: if(argc > (t_int)sizeof(x->argv) / jamie@4: (t_int)sizeof(t_float) || x->argv == NULL) jamie@4: post("Too many parameters to right inlet"); jamie@4: else{*/ jamie@4: jamie@4: x->argv = getbytes(argc * sizeof(float)); jamie@4: jamie@4: while(argc--) jamie@4: ((t_float *)x->argv)[argc] = atom_getfloat(&argv[argc]); jamie@4: /* }*/ jamie@4: } jamie@4: jamie@26: static void xtract_tilde_show_help(t_xtract_tilde *x, t_symbol *s){ jamie@26: jamie@26: int i; jamie@26: jamie@26: i = XTRACT_FEATURES; jamie@26: jamie@26: post("\n\txtract~: Feature List\n"); jamie@26: jamie@26: while(i--){ jamie@26: post("\t%s", xtract_help_strings[i]+7); jamie@26: } jamie@26: } jamie@26: jamie@4: static void xtract_tilde_free(t_xtract_tilde *x) { jamie@41: jamie@41: if(x->argv != NULL && x->memory.argv) jamie@41: freebytes(x->argv, x->memory.argv); jamie@4: } jamie@4: jamie@4: void xtract_tilde_setup(void) { jamie@4: xtract_class = class_new(gensym("xtract~"), jamie@4: (t_newmethod)xtract_new, jamie@4: (t_method)xtract_tilde_free, jamie@4: sizeof(t_xtract_tilde), jamie@4: CLASS_DEFAULT, jamie@4: A_GIMME, 0); jamie@4: jamie@4: class_addmethod(xtract_class, jamie@26: (t_method)xtract_dsp, gensym("dsp"), 0); jamie@4: class_addmethod(xtract_class, jamie@26: (t_method)xtract_tilde_get_args, gensym("list"), A_GIMME, 0); jamie@26: class_addmethod(xtract_class, jamie@26: (t_method)xtract_tilde_show_help, gensym("help"), A_DEFSYMBOL, 0); jamie@4: CLASS_MAINSIGNALIN(xtract_class, t_xtract_tilde, f); jamie@33: class_sethelpsymbol(xtract_class, gensym("xtract-help")); jamie@4: }