comparison examples/puredata/xtract~.c @ 4:46efa5536d04

Added Pure Data example
author Jamie Bullock <jamie@postlude.co.uk>
date Thu, 05 Oct 2006 16:48:38 +0000
parents
children 90b3a3a25d99
comparison
equal deleted inserted replaced
3:0c9a6c2c3e06 4:46efa5536d04
1 /* xtract~ - PD library for feature extraction
2 Copyright (C) 2006 Jamie Bullock
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19 /* calculates the spectral xtract of one frame, given peak frequency and amplitude to first and second inputs respectively */
20
21 #include "xtract/libxtract.h"
22 #include "m_pd.h"
23
24 #define BLOCKSIZE 1024
25 #define NYQUIST 22050.0f
26
27 static t_class *xtract_class;
28
29 typedef struct _xtract {
30 t_object x_obj;
31 t_float f;
32 t_int feature;
33 t_int feature_type;
34 void *argv;
35 } t_xtract_tilde;
36
37 static t_int *xtract_perform(t_int *w) {
38 t_sample *in = (t_sample *)(w[1]);
39 t_xtract_tilde *x = (t_xtract_tilde *)(w[2]);
40 t_int N = (t_int)(w[3]);
41 float result = 0;
42
43 xtract[x->feature]((float *)in, N, x->argv, &result);
44
45 outlet_float(x->x_obj.ob_outlet, result);
46 return (w+4);
47 }
48
49 static t_int *xtract_perform_vector(t_int *w) {
50 t_sample *in = (t_sample *)(w[1]);
51 t_sample *out = (t_sample *)(w[2]);
52 t_float *tmp_in, *tmp_out;
53 t_xtract_tilde *x = (t_xtract_tilde *)(w[3]);
54 t_int N = (t_int)(w[4]), n;
55
56 tmp_in = copybytes(in, N * sizeof(t_float));
57 tmp_out = getbytes(N * sizeof(t_float));
58
59 xtract[x->feature](tmp_in, N, x->argv, tmp_out);
60
61 n = N;
62
63 while(n--) out[n] = tmp_out[n];
64
65 freebytes(tmp_in, N * sizeof(t_float));
66 freebytes(tmp_out, N * sizeof(t_float));
67
68 return (w+5);
69 }
70
71 static void xtract_dsp(t_xtract_tilde *x, t_signal **sp) {
72
73 if(x->feature_type == VECTOR)
74 dsp_add(xtract_perform_vector, 4,
75 sp[0]->s_vec, sp[1]->s_vec, x, sp[0]->s_n);
76
77 else dsp_add(xtract_perform, 3, sp[0]->s_vec, x, sp[0]->s_n);
78
79 }
80
81 static void *xtract_new(t_symbol *me, t_int argc, t_atom *argv) {
82
83 t_symbol *tmp;
84 t_xtract_tilde *x = (t_xtract_tilde *)pd_new(xtract_class);
85 xtract_mel_filter *f;
86 t_int n, N;
87
88 N = BLOCKSIZE;
89
90 x->argv = NULL;
91
92 tmp = atom_getsymbol(argv);
93
94 if(tmp == gensym("mean")) x->feature = MEAN;
95 else if(tmp == gensym("variance")) {
96 x->feature = VARIANCE;
97 x->argv = getbytes(sizeof(t_float));
98 }
99 else if(tmp == gensym("standard_deviation")) x->feature = STANDARD_DEVIATION;
100 else if(tmp == gensym("average_deviation")) x->feature = AVERAGE_DEVIATION;
101 else if(tmp == gensym("skewness")) x->feature = SKEWNESS;
102 else if(tmp == gensym("kurtosis")) x->feature = KURTOSIS;
103 else if(tmp == gensym("irregularity_k")) x->feature = IRREGULARITY_K;
104 else if(tmp == gensym("irregularity_j")) x->feature = IRREGULARITY_J;
105 else if(tmp == gensym("tristimulus_1")) x->feature = TRISTIMULUS_1;
106 else if(tmp == gensym("tristimulus_2")) x->feature = TRISTIMULUS_2;
107 else if(tmp == gensym("tristimulus_3")) x->feature = TRISTIMULUS_3;
108 else if(tmp == gensym("smoothness")) x->feature = SMOOTHNESS;
109 else if(tmp == gensym("spread")) x->feature = SPREAD;
110 else if(tmp == gensym("zcr")) x->feature = ZCR;
111 else if(tmp == gensym("rolloff")) x->feature = ROLLOFF;
112 else if(tmp == gensym("loudness")) x->feature = LOUDNESS;
113 else if(tmp == gensym("flatness")) x->feature = FLATNESS;
114 else if(tmp == gensym("tonality")) x->feature = TONALITY;
115 else if(tmp == gensym("crest")) x->feature = CREST;
116 else if(tmp == gensym("noisiness")) x->feature = NOISINESS;
117 else if(tmp == gensym("rms_amplitude")) x->feature = RMS_AMPLITUDE;
118 else if(tmp == gensym("inharmonicity")) x->feature = INHARMONICITY;
119 else if(tmp == gensym("power")) x->feature = POWER;
120 else if(tmp == gensym("odd_even_ratio")) x->feature = ODD_EVEN_RATIO;
121 else if(tmp == gensym("sharpness")) x->feature = SHARPNESS;
122 else if(tmp == gensym("slope")) x->feature = SLOPE;
123 else if(tmp == gensym("f0")){
124 x->feature = F0;
125 x->argv = getbytes(sizeof(t_float));
126 }
127 else if(tmp == gensym("hps"))x->feature = HPS;
128 else if(tmp == gensym("lowest_match"))x->feature = LOWEST_MATCH;
129 else if(tmp == gensym("magnitude_spectrum"))
130 x->feature = MAGNITUDE_SPECTRUM;
131 else if(tmp == gensym("autocorrelation")) x->feature = AUTOCORRELATION;
132 else if(tmp == gensym("autocorrelation_fft"))
133 x->feature = AUTOCORRELATION_FFT;
134 else if(tmp == gensym("amdf")) x->feature = AMDF;
135 else if(tmp == gensym("asdf")) x->feature = ASDF;
136 else if(tmp == gensym("mfcc")){
137
138 x->argv = (xtract_mel_filter *)getbytes(sizeof(xtract_mel_filter));
139 /* Change! can use x->argv because it is a pointer to void */
140 /* put the malloc here */
141 x->feature = MFCC;
142 f = x->argv;
143
144 f->n_filters = 20;
145
146 post("filters = %d", ((xtract_mel_filter *)x->argv)->n_filters);
147 f->filters =
148 (t_float **)getbytes(f->n_filters * sizeof(t_float *));
149 for(n = 0; n < f->n_filters; n++)
150 f->filters[n] = (float *)getbytes(N * sizeof(float));
151
152 xtract_init_mfcc(N, NYQUIST, EQUAL_GAIN, 18000.0f,
153 80.0f, f->n_filters, f->filters);
154 }
155 else if(tmp == gensym("dct")) x->feature = DCT;
156 else if(tmp == gensym("bark_coefficients")){
157 x->feature = BARK_COEFFICIENTS;
158 x->argv = (t_int *)getbytes(BARK_BANDS * sizeof(t_int));
159 xtract_init_bark(N, NYQUIST, x->argv);
160 }
161 else if(tmp == gensym("peaks")) x->feature = PEAKS;
162 else if(tmp == gensym("flux")) x->feature = FLUX;
163 else if(tmp == gensym("attack_time")) x->feature = ATTACK_TIME;
164 else if(tmp == gensym("decay_time")) x->feature = DECAY_TIME;
165 else if(tmp == gensym("delta")) x->feature = DELTA_FEATURE;
166 else post("xtract~: No feature selected");
167
168 if(x->feature == AUTOCORRELATION || x->feature == AUTOCORRELATION_FFT ||
169 x->feature == MFCC || x->feature == AMDF || x->feature == ASDF||
170 x->feature == DCT || x->feature == BARK_COEFFICIENTS ||
171 x->feature == MAGNITUDE_SPECTRUM || x->feature == PEAKS)
172 x->feature_type = VECTOR;
173
174 else if (x->feature == FLUX || x->feature == ATTACK_TIME ||
175 x->feature == DECAY_TIME || x->feature == DELTA)
176 x->feature_type = DELTA;
177
178 else x->feature_type = SCALAR;
179
180 post("Type: %d", x->feature);
181
182 /* argv through right inlet */
183 inlet_new(&x->x_obj, &x->x_obj.ob_pd, gensym("list"), gensym("list"));
184
185
186 /* if feature is vector, create signal out */
187 if(x->feature_type == VECTOR) outlet_new(&x->x_obj, &s_signal);
188
189 /* otherwise: float */
190 else outlet_new(&x->x_obj, &s_float);
191
192 return (void *)x;
193 }
194
195 static void xtract_tilde_get_args(t_xtract_tilde *x, t_symbol *selector,
196 t_int argc, t_atom *argv) {
197 /*
198 if(argc > (t_int)sizeof(x->argv) /
199 (t_int)sizeof(t_float) || x->argv == NULL)
200 post("Too many parameters to right inlet");
201 else{*/
202
203 x->argv = getbytes(argc * sizeof(float));
204
205 while(argc--)
206 ((t_float *)x->argv)[argc] = atom_getfloat(&argv[argc]);
207 /* }*/
208 }
209
210 static void xtract_tilde_free(t_xtract_tilde *x) {
211 /*FIX */
212 if(x->argv != NULL)
213 freebytes(x->argv, 0);
214 }
215
216 void xtract_tilde_setup(void) {
217 xtract_class = class_new(gensym("xtract~"),
218 (t_newmethod)xtract_new,
219 (t_method)xtract_tilde_free,
220 sizeof(t_xtract_tilde),
221 CLASS_DEFAULT,
222 A_GIMME, 0);
223
224 class_addmethod(xtract_class,
225 (t_method)xtract_dsp, gensym("dsp"), 0);
226 class_addmethod(xtract_class,
227 (t_method)xtract_tilde_get_args, gensym("list"), A_GIMME, 0);
228 CLASS_MAINSIGNALIN(xtract_class, t_xtract_tilde, f);
229 class_sethelpsymbol(xtract_class, gensym("help-flib"));
230 }