-
Notifications
You must be signed in to change notification settings - Fork 0
/
specFlatness.c
496 lines (389 loc) · 13.3 KB
/
specFlatness.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
specFlatness - A non-real-time spectral flatness analysis external.
Copyright 2009 William Brent
This file is part of timbreID.
timbreID is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
timbreID is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
version 0.0.5, December 23, 2011
¥ 0.0.5 incorporates the tIDLib.h header
¥ 0.0.4 changed windowing functions so that they are computed ahead of time, this required considerable changes to the windowing stuff since 0.0.3 and before. changed _bang() to remove needless end_samp calculation and pass length_samp rather than window to _analyze() so that windowing will not cover the zero padded section. made power spectrum computation the first step, and changed the squaring function to a magnitude function instead. in the case that power spectrum is used, this saves needless computation of sqrt and subsequent squaring. wherever possible, using getbytes() directly instead of getting 0 bytes and resizing.
¥Ê0.0.3 added an ifndef M_PI for guaranteed windows compilation
¥ 0.0.2 adds a #define M_PI for windows compilation, and declares all functions except _setup static
*/
#include "tIDLib.h"
static t_class *specFlatness_class;
typedef struct _specFlatness
{
t_object x_obj;
t_float sr;
t_float window;
int powerSpectrum;
int windowFunction;
int windowFuncSize;
int maxWindowSize;
int *powersOfTwo;
int powTwoArrSize;
t_sample *signal_R;
t_float *nthRoots;
t_float *blackman;
t_float *cosine;
t_float *hamming;
t_float *hann;
t_word *x_vec;
t_symbol *x_arrayname;
int x_arrayPoints;
t_outlet *x_flatness;
} t_specFlatness;
/* ------------------------ specFlatness -------------------------------- */
static void specFlatness_analyze(t_specFlatness *x, t_floatarg start, t_floatarg n)
{
int i, j, oldWindow, window, windowHalf, startSamp, endSamp, lengthSamp;
t_float windowHalfPlusOneRecip, dividend, divisor, flatness, *windowFuncPtr;
t_sample *signal_I;
t_garray *a;
if(!(a = (t_garray *)pd_findbyclass(x->x_arrayname, garray_class)))
pd_error(x, "%s: no such array", x->x_arrayname->s_name);
else if(!garray_getfloatwords(a, &x->x_arrayPoints, &x->x_vec))
pd_error(x, "%s: bad template for specFlatness", x->x_arrayname->s_name);
else
{
startSamp = start;
startSamp = (startSamp<0)?0:startSamp;
if(n)
endSamp = startSamp + n-1;
else
endSamp = startSamp + x->window-1;
if(endSamp > x->x_arrayPoints)
endSamp = x->x_arrayPoints-1;
lengthSamp = endSamp-startSamp+1;
if(endSamp <= startSamp)
{
error("bad range of samples.");
return;
}
if(lengthSamp > x->powersOfTwo[x->powTwoArrSize-1])
{
post("WARNING: specFlatness: window truncated because requested size is larger than the current max_window setting. Use the max_window method to allow larger windows.");
lengthSamp = x->powersOfTwo[x->powTwoArrSize-1];
window = lengthSamp;
endSamp = startSamp + window - 1;
}
else
{
i=0;
while(lengthSamp > x->powersOfTwo[i])
i++;
window = x->powersOfTwo[i];
}
windowHalf = window * 0.5;
windowHalfPlusOneRecip = 1.0/(t_float)(windowHalf+1);
if(x->window != window)
{
oldWindow = x->window;
x->window = window;
x->signal_R = (t_sample *)t_resizebytes(x->signal_R, oldWindow*sizeof(t_sample), window*sizeof(t_sample));
x->nthRoots = (t_float *)t_resizebytes(x->nthRoots, (oldWindow*0.5+1)*sizeof(t_float), (windowHalf+1)*sizeof(t_float));
for(i=0; i<=windowHalf; i++)
x->nthRoots[i] = 0.0;
}
if(x->windowFuncSize != lengthSamp)
{
x->blackman = (t_float *)t_resizebytes(x->blackman, x->windowFuncSize*sizeof(t_float), lengthSamp*sizeof(t_float));
x->cosine = (t_float *)t_resizebytes(x->cosine, x->windowFuncSize*sizeof(t_float), lengthSamp*sizeof(t_float));
x->hamming = (t_float *)t_resizebytes(x->hamming, x->windowFuncSize*sizeof(t_float), lengthSamp*sizeof(t_float));
x->hann = (t_float *)t_resizebytes(x->hann, x->windowFuncSize*sizeof(t_float), lengthSamp*sizeof(t_float));
x->windowFuncSize = lengthSamp;
tIDLib_blackmanWindow(x->blackman, x->windowFuncSize);
tIDLib_cosineWindow(x->cosine, x->windowFuncSize);
tIDLib_hammingWindow(x->hamming, x->windowFuncSize);
tIDLib_hannWindow(x->hann, x->windowFuncSize);
}
// create local memory
signal_I = (t_sample *)t_getbytes((windowHalf+1)*sizeof(t_sample));
// construct analysis window
for(i=0, j=startSamp; j<=endSamp; i++, j++)
x->signal_R[i] = x->x_vec[j].w_float;
// set window function
windowFuncPtr = x->hann; //default case to get rid of compile warning
switch(x->windowFunction)
{
case 0:
break;
case 1:
windowFuncPtr = x->blackman;
break;
case 2:
windowFuncPtr = x->cosine;
break;
case 3:
windowFuncPtr = x->hamming;
break;
case 4:
windowFuncPtr = x->hann;
break;
default:
break;
};
// if windowFunction == 0, skip the windowing (rectangular)
if(x->windowFunction>0)
for(i=0; i<lengthSamp; i++, windowFuncPtr++)
x->signal_R[i] *= *windowFuncPtr;
// then zero pad the end
for(; i<window; i++)
x->signal_R[i] = 0.0;
mayer_realfft(window, x->signal_R);
tIDLib_realfftUnpack(window, windowHalf, x->signal_R, signal_I);
tIDLib_power(windowHalf+1, x->signal_R, signal_I);
// power spectrum sometimes generates lower scores than magnitude. make it optional.
if(!x->powerSpectrum)
tIDLib_mag(windowHalf+1, x->signal_R);
dividend=1; // to get the product of all terms for geometric mean
divisor=0;
flatness=0;
// geometric mean
// take the nth roots first so as not to lose data.
for(i=0; i<=windowHalf; i++)
x->nthRoots[i] = pow(x->signal_R[i], windowHalfPlusOneRecip);
// take the product of nth roots
// what to do with values that are zero? for now, ignoring them.
for(i=0; i<=windowHalf; i++)
if(x->nthRoots[i] != 0)
dividend *= x->nthRoots[i];
for(i=0; i<=windowHalf; i++)
divisor += x->signal_R[i];
divisor *= windowHalfPlusOneRecip; // arithmetic mean
divisor = (divisor==0)?1.0:divisor;
flatness = dividend/divisor;
outlet_float(x->x_flatness, flatness);
// free local memory
t_freebytes(signal_I, (windowHalf+1)*sizeof(t_sample));
}
}
// analyze the whole damn array
static void specFlatness_bang(t_specFlatness *x)
{
int window, startSamp, lengthSamp;
t_garray *a;
if(!(a = (t_garray *)pd_findbyclass(x->x_arrayname, garray_class)))
pd_error(x, "%s: no such array", x->x_arrayname->s_name);
else if(!garray_getfloatwords(a, &x->x_arrayPoints, &x->x_vec))
pd_error(x, "%s: bad template for specFlatness", x->x_arrayname->s_name);
else
{
startSamp = 0;
lengthSamp = x->x_arrayPoints;
if(lengthSamp > x->powersOfTwo[x->powTwoArrSize-1])
{
post("WARNING: specFlatness: window truncated because requested size is larger than the current max_window setting. Use the max_window method to allow larger windows. Sizes of more than 131072 may produce unreliable results.");
lengthSamp = x->powersOfTwo[x->powTwoArrSize-1];
window = lengthSamp;
}
specFlatness_analyze(x, startSamp, lengthSamp);
}
}
static void specFlatness_set(t_specFlatness *x, t_symbol *s)
{
t_garray *a;
if(!(a = (t_garray *)pd_findbyclass(s, garray_class)))
pd_error(x, "%s: no such array", s->s_name);
else if(!garray_getfloatwords(a, &x->x_arrayPoints, &x->x_vec))
pd_error(x, "%s: bad template for specFlatness", s->s_name);
else
x->x_arrayname = s;
}
static void specFlatness_print(t_specFlatness *x)
{
post("samplerate: %f", x->sr);
post("window: %f", x->window);
post("power spectrum: %i", x->powerSpectrum);
post("window function: %i", x->windowFunction);
}
static void specFlatness_samplerate(t_specFlatness *x, t_floatarg sr)
{
if(sr<64)
x->sr = 64;
else
x->sr = sr;
}
static void specFlatness_max_window(t_specFlatness *x, t_floatarg w)
{
int i;
if(w<64)
x->maxWindowSize = 64;
else
x->maxWindowSize = w;
x->powersOfTwo = (int *)t_resizebytes(x->powersOfTwo, x->powTwoArrSize*sizeof(int), sizeof(int));
x->powersOfTwo[0] = 64; // must have at least this large of a window
i=1;
while(x->powersOfTwo[i-1] < x->maxWindowSize)
{
x->powersOfTwo = (int *)t_resizebytes(x->powersOfTwo, (i)*sizeof(int), (i+1)*sizeof(int));
x->powersOfTwo[i] = pow(2, i+6); // +6 because we're starting at 2**6
i++;
}
x->powTwoArrSize = i;
post("maximum window size: %i", x->maxWindowSize);
}
static void specFlatness_windowFunction(t_specFlatness *x, t_floatarg f)
{
f = (f<0)?0:f;
f = (f>4)?4:f;
x->windowFunction = f;
switch(x->windowFunction)
{
case 0:
post("window function: rectangular.");
break;
case 1:
post("window function: blackman.");
break;
case 2:
post("window function: cosine.");
break;
case 3:
post("window function: hamming.");
break;
case 4:
post("window function: hann.");
break;
default:
break;
};
}
// magnitude spectrum == 0, power spectrum == 1
static void specFlatness_powerSpectrum(t_specFlatness *x, t_floatarg spec)
{
spec = (spec<0)?0:spec;
spec = (spec>1)?1:spec;
x->powerSpectrum = spec;
if(x->powerSpectrum)
post("using power spectrum for specFlatness computation.");
else
post("using magnitude spectrum for specFlatness computation.");
}
static void *specFlatness_new(t_symbol *s)
{
t_specFlatness *x = (t_specFlatness *)pd_new(specFlatness_class);
int i;
t_garray *a;
x->x_flatness = outlet_new(&x->x_obj, &s_float);
if(s)
{
x->x_arrayname = s;
if(!(a = (t_garray *)pd_findbyclass(x->x_arrayname, garray_class)))
;
else if(!garray_getfloatwords(a, &x->x_arrayPoints, &x->x_vec))
pd_error(x, "%s: bad template for specFlatness", x->x_arrayname->s_name);
}
else
error("specFlatness: no array specified.");
x->sr = 44100.0;
x->window = 1; // should be a bogus size initially to force the proper resizes when a real _analyze request comes through
x->windowFuncSize = 1;
x->windowFunction = 4; // 4 is hann window
x->powerSpectrum = 0; // choose mag (0) or power (1) spec in the specFlatness computation
x->maxWindowSize = MAXWINDOWSIZE; // this seems to be the maximum size allowable by mayer_realfft();
x->powersOfTwo = (int *)t_getbytes(sizeof(int));
x->powersOfTwo[0] = 64; // must have at least this large of a window
i=1;
while(x->powersOfTwo[i-1] < x->maxWindowSize)
{
x->powersOfTwo = (int *)t_resizebytes(x->powersOfTwo, i*sizeof(int), (i+1)*sizeof(int));
x->powersOfTwo[i] = pow(2, i+6); // +6 because we're starting at 2**6
i++;
}
x->powTwoArrSize = i;
x->signal_R = (t_sample *)t_getbytes(x->window*sizeof(t_sample));
x->nthRoots = (t_float *)t_getbytes((x->window*0.5+1)*sizeof(t_float));
for(i=0; i<x->window; i++)
x->signal_R[i] = 0.0;
for(i=0; i<=x->window*0.5; i++)
x->nthRoots[i] = 0.0;
x->blackman = (t_float *)t_getbytes(x->windowFuncSize*sizeof(t_float));
x->cosine = (t_float *)t_getbytes(x->windowFuncSize*sizeof(t_float));
x->hamming = (t_float *)t_getbytes(x->windowFuncSize*sizeof(t_float));
x->hann = (t_float *)t_getbytes(x->windowFuncSize*sizeof(t_float));
// initialize signal windowing functions
tIDLib_blackmanWindow(x->blackman, x->windowFuncSize);
tIDLib_cosineWindow(x->cosine, x->windowFuncSize);
tIDLib_hammingWindow(x->hamming, x->windowFuncSize);
tIDLib_hannWindow(x->hann, x->windowFuncSize);
return (x);
}
static void specFlatness_free(t_specFlatness *x)
{
// free the input buffer memory
t_freebytes(x->signal_R, x->window*sizeof(t_sample));
// free the window memory
t_freebytes(x->blackman, x->windowFuncSize*sizeof(t_float));
t_freebytes(x->cosine, x->windowFuncSize*sizeof(t_float));
t_freebytes(x->hamming, x->windowFuncSize*sizeof(t_float));
t_freebytes(x->hann, x->windowFuncSize*sizeof(t_float));
t_freebytes(x->nthRoots, (x->window*0.5+1)*sizeof(t_float));
// free the powers of two table
t_freebytes(x->powersOfTwo, x->powTwoArrSize*sizeof(int));
}
void specFlatness_setup(void)
{
specFlatness_class =
class_new(
gensym("specFlatness"),
(t_newmethod)specFlatness_new,
(t_method)specFlatness_free,
sizeof(t_specFlatness),
CLASS_DEFAULT,
A_DEFSYM,
0
);
class_addbang(specFlatness_class, specFlatness_bang);
class_addmethod(
specFlatness_class,
(t_method)specFlatness_analyze,
gensym("analyze"),
A_DEFFLOAT,
A_DEFFLOAT,
0
);
class_addmethod(
specFlatness_class,
(t_method)specFlatness_set,
gensym("set"),
A_SYMBOL,
0
);
class_addmethod(
specFlatness_class,
(t_method)specFlatness_print,
gensym("print"),
0
);
class_addmethod(
specFlatness_class,
(t_method)specFlatness_samplerate,
gensym("samplerate"),
A_DEFFLOAT,
0
);
class_addmethod(
specFlatness_class,
(t_method)specFlatness_max_window,
gensym("max_window"),
A_DEFFLOAT,
0
);
class_addmethod(
specFlatness_class,
(t_method)specFlatness_windowFunction,
gensym("window_function"),
A_DEFFLOAT,
0
);
class_addmethod(
specFlatness_class,
(t_method)specFlatness_powerSpectrum,
gensym("power_spectrum"),
A_DEFFLOAT,
0
);
}