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@9
|
21 #define XTRACT
|
jamie@4
|
22 #include "xtract/libxtract.h"
|
jamie@4
|
23 #include "m_pd.h"
|
jamie@4
|
24
|
jamie@4
|
25 #define BLOCKSIZE 1024
|
jamie@4
|
26 #define NYQUIST 22050.0f
|
jamie@4
|
27
|
jamie@4
|
28 static t_class *xtract_class;
|
jamie@4
|
29
|
jamie@4
|
30 typedef struct _xtract {
|
jamie@4
|
31 t_object x_obj;
|
jamie@4
|
32 t_float f;
|
jamie@4
|
33 t_int feature;
|
jamie@4
|
34 t_int feature_type;
|
jamie@4
|
35 void *argv;
|
jamie@4
|
36 } t_xtract_tilde;
|
jamie@4
|
37
|
jamie@4
|
38 static t_int *xtract_perform(t_int *w) {
|
jamie@4
|
39 t_sample *in = (t_sample *)(w[1]);
|
jamie@4
|
40 t_xtract_tilde *x = (t_xtract_tilde *)(w[2]);
|
jamie@4
|
41 t_int N = (t_int)(w[3]);
|
jamie@4
|
42 float result = 0;
|
jamie@4
|
43
|
jamie@4
|
44 xtract[x->feature]((float *)in, N, x->argv, &result);
|
jamie@4
|
45
|
jamie@4
|
46 outlet_float(x->x_obj.ob_outlet, result);
|
jamie@4
|
47 return (w+4);
|
jamie@4
|
48 }
|
jamie@4
|
49
|
jamie@4
|
50 static t_int *xtract_perform_vector(t_int *w) {
|
jamie@4
|
51 t_sample *in = (t_sample *)(w[1]);
|
jamie@4
|
52 t_sample *out = (t_sample *)(w[2]);
|
jamie@4
|
53 t_float *tmp_in, *tmp_out;
|
jamie@4
|
54 t_xtract_tilde *x = (t_xtract_tilde *)(w[3]);
|
jamie@4
|
55 t_int N = (t_int)(w[4]), n;
|
jamie@4
|
56
|
jamie@4
|
57 tmp_in = copybytes(in, N * sizeof(t_float));
|
jamie@4
|
58 tmp_out = getbytes(N * sizeof(t_float));
|
jamie@4
|
59
|
jamie@4
|
60 xtract[x->feature](tmp_in, N, x->argv, tmp_out);
|
jamie@4
|
61
|
jamie@4
|
62 n = N;
|
jamie@4
|
63
|
jamie@4
|
64 while(n--) out[n] = tmp_out[n];
|
jamie@4
|
65
|
jamie@4
|
66 freebytes(tmp_in, N * sizeof(t_float));
|
jamie@4
|
67 freebytes(tmp_out, N * sizeof(t_float));
|
jamie@4
|
68
|
jamie@4
|
69 return (w+5);
|
jamie@4
|
70 }
|
jamie@4
|
71
|
jamie@4
|
72 static void xtract_dsp(t_xtract_tilde *x, t_signal **sp) {
|
jamie@4
|
73
|
jamie@4
|
74 if(x->feature_type == VECTOR)
|
jamie@4
|
75 dsp_add(xtract_perform_vector, 4,
|
jamie@4
|
76 sp[0]->s_vec, sp[1]->s_vec, x, sp[0]->s_n);
|
jamie@4
|
77
|
jamie@4
|
78 else dsp_add(xtract_perform, 3, sp[0]->s_vec, x, sp[0]->s_n);
|
jamie@4
|
79
|
jamie@4
|
80 }
|
jamie@4
|
81
|
jamie@4
|
82 static void *xtract_new(t_symbol *me, t_int argc, t_atom *argv) {
|
jamie@4
|
83
|
jamie@4
|
84 t_symbol *tmp;
|
jamie@4
|
85 t_xtract_tilde *x = (t_xtract_tilde *)pd_new(xtract_class);
|
jamie@4
|
86 xtract_mel_filter *f;
|
jamie@4
|
87 t_int n, N;
|
jamie@4
|
88
|
jamie@4
|
89 N = BLOCKSIZE;
|
jamie@4
|
90
|
jamie@4
|
91 x->argv = NULL;
|
jamie@4
|
92
|
jamie@4
|
93 tmp = atom_getsymbol(argv);
|
jamie@4
|
94
|
jamie@4
|
95 if(tmp == gensym("mean")) x->feature = MEAN;
|
jamie@4
|
96 else if(tmp == gensym("variance")) {
|
jamie@4
|
97 x->feature = VARIANCE;
|
jamie@4
|
98 x->argv = getbytes(sizeof(t_float));
|
jamie@4
|
99 }
|
jamie@4
|
100 else if(tmp == gensym("standard_deviation")) x->feature = STANDARD_DEVIATION;
|
jamie@4
|
101 else if(tmp == gensym("average_deviation")) x->feature = AVERAGE_DEVIATION;
|
jamie@4
|
102 else if(tmp == gensym("skewness")) x->feature = SKEWNESS;
|
jamie@4
|
103 else if(tmp == gensym("kurtosis")) x->feature = KURTOSIS;
|
jamie@11
|
104 else if(tmp == gensym("centroid")) x->feature = CENTROID;
|
jamie@4
|
105 else if(tmp == gensym("irregularity_k")) x->feature = IRREGULARITY_K;
|
jamie@4
|
106 else if(tmp == gensym("irregularity_j")) x->feature = IRREGULARITY_J;
|
jamie@4
|
107 else if(tmp == gensym("tristimulus_1")) x->feature = TRISTIMULUS_1;
|
jamie@4
|
108 else if(tmp == gensym("tristimulus_2")) x->feature = TRISTIMULUS_2;
|
jamie@4
|
109 else if(tmp == gensym("tristimulus_3")) x->feature = TRISTIMULUS_3;
|
jamie@4
|
110 else if(tmp == gensym("smoothness")) x->feature = SMOOTHNESS;
|
jamie@4
|
111 else if(tmp == gensym("spread")) x->feature = SPREAD;
|
jamie@4
|
112 else if(tmp == gensym("zcr")) x->feature = ZCR;
|
jamie@4
|
113 else if(tmp == gensym("rolloff")) x->feature = ROLLOFF;
|
jamie@4
|
114 else if(tmp == gensym("loudness")) x->feature = LOUDNESS;
|
jamie@4
|
115 else if(tmp == gensym("flatness")) x->feature = FLATNESS;
|
jamie@4
|
116 else if(tmp == gensym("tonality")) x->feature = TONALITY;
|
jamie@4
|
117 else if(tmp == gensym("crest")) x->feature = CREST;
|
jamie@4
|
118 else if(tmp == gensym("noisiness")) x->feature = NOISINESS;
|
jamie@4
|
119 else if(tmp == gensym("rms_amplitude")) x->feature = RMS_AMPLITUDE;
|
jamie@4
|
120 else if(tmp == gensym("inharmonicity")) x->feature = INHARMONICITY;
|
jamie@4
|
121 else if(tmp == gensym("power")) x->feature = POWER;
|
jamie@4
|
122 else if(tmp == gensym("odd_even_ratio")) x->feature = ODD_EVEN_RATIO;
|
jamie@4
|
123 else if(tmp == gensym("sharpness")) x->feature = SHARPNESS;
|
jamie@4
|
124 else if(tmp == gensym("slope")) x->feature = SLOPE;
|
jamie@4
|
125 else if(tmp == gensym("f0")){
|
jamie@12
|
126 x->feature = F0;
|
jamie@26
|
127 x->argv = getbytes(3 * sizeof(t_float));
|
jamie@12
|
128 }
|
jamie@12
|
129 else if(tmp == gensym("hps"))x->feature = HPS;
|
jamie@12
|
130 else if(tmp == gensym("lowest_match")){
|
jamie@12
|
131 x->feature = LOWEST_MATCH;
|
jamie@4
|
132 x->argv = getbytes(sizeof(t_float));
|
jamie@12
|
133 }
|
jamie@4
|
134 else if(tmp == gensym("magnitude_spectrum"))
|
jamie@4
|
135 x->feature = MAGNITUDE_SPECTRUM;
|
jamie@4
|
136 else if(tmp == gensym("autocorrelation")) x->feature = AUTOCORRELATION;
|
jamie@4
|
137 else if(tmp == gensym("autocorrelation_fft"))
|
jamie@4
|
138 x->feature = AUTOCORRELATION_FFT;
|
jamie@4
|
139 else if(tmp == gensym("amdf")) x->feature = AMDF;
|
jamie@4
|
140 else if(tmp == gensym("asdf")) x->feature = ASDF;
|
jamie@4
|
141 else if(tmp == gensym("mfcc")){
|
jamie@4
|
142
|
jamie@4
|
143 x->argv = (xtract_mel_filter *)getbytes(sizeof(xtract_mel_filter));
|
jamie@4
|
144 /* Change! can use x->argv because it is a pointer to void */
|
jamie@4
|
145 /* put the malloc here */
|
jamie@4
|
146 x->feature = MFCC;
|
jamie@4
|
147 f = x->argv;
|
jamie@4
|
148
|
jamie@4
|
149 f->n_filters = 20;
|
jamie@4
|
150
|
jamie@4
|
151 post("filters = %d", ((xtract_mel_filter *)x->argv)->n_filters);
|
jamie@4
|
152 f->filters =
|
jamie@4
|
153 (t_float **)getbytes(f->n_filters * sizeof(t_float *));
|
jamie@4
|
154 for(n = 0; n < f->n_filters; n++)
|
jamie@4
|
155 f->filters[n] = (float *)getbytes(N * sizeof(float));
|
jamie@4
|
156
|
jamie@4
|
157 xtract_init_mfcc(N, NYQUIST, EQUAL_GAIN, 18000.0f,
|
jamie@4
|
158 80.0f, f->n_filters, f->filters);
|
jamie@4
|
159 }
|
jamie@4
|
160 else if(tmp == gensym("dct")) x->feature = DCT;
|
jamie@4
|
161 else if(tmp == gensym("bark_coefficients")){
|
jamie@4
|
162 x->feature = BARK_COEFFICIENTS;
|
jamie@4
|
163 x->argv = (t_int *)getbytes(BARK_BANDS * sizeof(t_int));
|
jamie@4
|
164 xtract_init_bark(N, NYQUIST, x->argv);
|
jamie@4
|
165 }
|
jamie@4
|
166 else if(tmp == gensym("peaks")) x->feature = PEAKS;
|
jamie@4
|
167 else if(tmp == gensym("flux")) x->feature = FLUX;
|
jamie@4
|
168 else if(tmp == gensym("attack_time")) x->feature = ATTACK_TIME;
|
jamie@4
|
169 else if(tmp == gensym("decay_time")) x->feature = DECAY_TIME;
|
jamie@4
|
170 else if(tmp == gensym("delta")) x->feature = DELTA_FEATURE;
|
jamie@4
|
171 else post("xtract~: No feature selected");
|
jamie@4
|
172
|
jamie@4
|
173 if(x->feature == AUTOCORRELATION || x->feature == AUTOCORRELATION_FFT ||
|
jamie@4
|
174 x->feature == MFCC || x->feature == AMDF || x->feature == ASDF||
|
jamie@4
|
175 x->feature == DCT || x->feature == BARK_COEFFICIENTS ||
|
jamie@4
|
176 x->feature == MAGNITUDE_SPECTRUM || x->feature == PEAKS)
|
jamie@4
|
177 x->feature_type = VECTOR;
|
jamie@4
|
178
|
jamie@4
|
179 else if (x->feature == FLUX || x->feature == ATTACK_TIME ||
|
jamie@4
|
180 x->feature == DECAY_TIME || x->feature == DELTA)
|
jamie@4
|
181 x->feature_type = DELTA;
|
jamie@4
|
182
|
jamie@4
|
183 else x->feature_type = SCALAR;
|
jamie@4
|
184
|
jamie@4
|
185 /* argv through right inlet */
|
jamie@4
|
186 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("list"), gensym("list"));
|
jamie@4
|
187
|
jamie@4
|
188
|
jamie@4
|
189 /* if feature is vector, create signal out */
|
jamie@4
|
190 if(x->feature_type == VECTOR) outlet_new(&x->x_obj, &s_signal);
|
jamie@4
|
191
|
jamie@4
|
192 /* otherwise: float */
|
jamie@4
|
193 else outlet_new(&x->x_obj, &s_float);
|
jamie@4
|
194
|
jamie@4
|
195 return (void *)x;
|
jamie@4
|
196 }
|
jamie@4
|
197
|
jamie@4
|
198 static void xtract_tilde_get_args(t_xtract_tilde *x, t_symbol *selector,
|
jamie@4
|
199 t_int argc, t_atom *argv) {
|
jamie@4
|
200 /*
|
jamie@4
|
201 if(argc > (t_int)sizeof(x->argv) /
|
jamie@4
|
202 (t_int)sizeof(t_float) || x->argv == NULL)
|
jamie@4
|
203 post("Too many parameters to right inlet");
|
jamie@4
|
204 else{*/
|
jamie@4
|
205
|
jamie@4
|
206 x->argv = getbytes(argc * sizeof(float));
|
jamie@4
|
207
|
jamie@4
|
208 while(argc--)
|
jamie@4
|
209 ((t_float *)x->argv)[argc] = atom_getfloat(&argv[argc]);
|
jamie@4
|
210 /* }*/
|
jamie@4
|
211 }
|
jamie@4
|
212
|
jamie@26
|
213 static void xtract_tilde_show_help(t_xtract_tilde *x, t_symbol *s){
|
jamie@26
|
214
|
jamie@26
|
215 int i;
|
jamie@26
|
216
|
jamie@26
|
217 i = XTRACT_FEATURES;
|
jamie@26
|
218
|
jamie@26
|
219 post("\n\txtract~: Feature List\n");
|
jamie@26
|
220
|
jamie@26
|
221 while(i--){
|
jamie@26
|
222 post("\t%s", xtract_help_strings[i]+7);
|
jamie@26
|
223 }
|
jamie@26
|
224 }
|
jamie@26
|
225
|
jamie@4
|
226 static void xtract_tilde_free(t_xtract_tilde *x) {
|
jamie@4
|
227 /*FIX */
|
jamie@4
|
228 if(x->argv != NULL)
|
jamie@4
|
229 freebytes(x->argv, 0);
|
jamie@4
|
230 }
|
jamie@4
|
231
|
jamie@4
|
232 void xtract_tilde_setup(void) {
|
jamie@4
|
233 xtract_class = class_new(gensym("xtract~"),
|
jamie@4
|
234 (t_newmethod)xtract_new,
|
jamie@4
|
235 (t_method)xtract_tilde_free,
|
jamie@4
|
236 sizeof(t_xtract_tilde),
|
jamie@4
|
237 CLASS_DEFAULT,
|
jamie@4
|
238 A_GIMME, 0);
|
jamie@4
|
239
|
jamie@4
|
240 class_addmethod(xtract_class,
|
jamie@26
|
241 (t_method)xtract_dsp, gensym("dsp"), 0);
|
jamie@4
|
242 class_addmethod(xtract_class,
|
jamie@26
|
243 (t_method)xtract_tilde_get_args, gensym("list"), A_GIMME, 0);
|
jamie@26
|
244 class_addmethod(xtract_class,
|
jamie@26
|
245 (t_method)xtract_tilde_show_help, gensym("help"), A_DEFSYMBOL, 0);
|
jamie@4
|
246 CLASS_MAINSIGNALIN(xtract_class, t_xtract_tilde, f);
|
jamie@4
|
247 class_sethelpsymbol(xtract_class, gensym("help-flib"));
|
jamie@4
|
248 }
|