2 Copyright (C) 2003-2014 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
6 aubio is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 aubio is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with aubio. If not, see <http://www.gnu.org/licenses/>.
21 /* see in mathutils.h for doc */
23 #include "aubio_priv.h"
25 #include "mathutils.h"
26 #include "musicutils.h"
29 #ifdef HAVE_ACCELERATE
30 #include <Accelerate/Accelerate.h>
41 aubio_win_blackman_harris,
45 aubio_win_default = aubio_win_hanningz,
49 new_aubio_window (char_t * window_type, uint_t length)
51 fvec_t * win = new_fvec (length);
56 err = fvec_set_window (win, window_type);
64 uint_t fvec_set_window (fvec_t *win, char_t *window_type) {
65 smpl_t * w = win->data;
66 uint_t i, size = win->length;
67 aubio_window_type wintype;
68 if (window_type == NULL) {
69 AUBIO_ERR ("window type can not be null.\n");
71 } else if (strcmp (window_type, "rectangle") == 0)
72 wintype = aubio_win_rectangle;
73 else if (strcmp (window_type, "hamming") == 0)
74 wintype = aubio_win_hamming;
75 else if (strcmp (window_type, "hanning") == 0)
76 wintype = aubio_win_hanning;
77 else if (strcmp (window_type, "hanningz") == 0)
78 wintype = aubio_win_hanningz;
79 else if (strcmp (window_type, "blackman") == 0)
80 wintype = aubio_win_blackman;
81 else if (strcmp (window_type, "blackman_harris") == 0)
82 wintype = aubio_win_blackman_harris;
83 else if (strcmp (window_type, "gaussian") == 0)
84 wintype = aubio_win_gaussian;
85 else if (strcmp (window_type, "welch") == 0)
86 wintype = aubio_win_welch;
87 else if (strcmp (window_type, "parzen") == 0)
88 wintype = aubio_win_parzen;
89 else if (strcmp (window_type, "default") == 0)
90 wintype = aubio_win_default;
92 AUBIO_ERR ("unknown window type `%s`.\n", window_type);
96 case aubio_win_rectangle:
100 case aubio_win_hamming:
102 w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size));
104 case aubio_win_hanning:
106 w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size)));
108 case aubio_win_hanningz:
110 w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size)));
112 case aubio_win_blackman:
115 - 0.50 * COS( TWO_PI*i/(size-1.0))
116 + 0.08 * COS(2.0*TWO_PI*i/(size-1.0));
118 case aubio_win_blackman_harris:
121 - 0.48829 * COS( TWO_PI*i/(size-1.0))
122 + 0.14128 * COS(2.0*TWO_PI*i/(size-1.0))
123 - 0.01168 * COS(3.0*TWO_PI*i/(size-1.0));
125 case aubio_win_gaussian:
127 lsmp_t a, b, c = 0.5;
129 for (n = 0; n < size; n++)
131 a = (n-c*(size-1))/(SQR(c)*(size-1));
137 case aubio_win_welch:
139 w[i] = 1.0 - SQR((2.*i-size)/(size+1.0));
141 case aubio_win_parzen:
143 w[i] = 1.0 - ABS((2.f*i-size)/(size+1.0f));
152 aubio_unwrap2pi (smpl_t phase)
154 /* mod(phase+pi,-2pi)+pi */
155 return phase + TWO_PI * (1. + FLOOR (-(phase + PI) / TWO_PI));
159 fvec_mean (fvec_t * s)
162 #ifndef HAVE_ACCELERATE
164 for (j = 0; j < s->length; j++) {
167 return tmp / (smpl_t) (s->length);
169 #if !HAVE_AUBIO_DOUBLE
170 vDSP_meanv(s->data, 1, &tmp, s->length);
171 #else /* HAVE_AUBIO_DOUBLE */
172 vDSP_meanvD(s->data, 1, &tmp, s->length);
173 #endif /* HAVE_AUBIO_DOUBLE */
175 #endif /* HAVE_ACCELERATE */
179 fvec_sum (fvec_t * s)
182 #ifndef HAVE_ACCELERATE
184 for (j = 0; j < s->length; j++) {
188 #if !HAVE_AUBIO_DOUBLE
189 vDSP_sve(s->data, 1, &tmp, s->length);
190 #else /* HAVE_AUBIO_DOUBLE */
191 vDSP_sveD(s->data, 1, &tmp, s->length);
192 #endif /* HAVE_AUBIO_DOUBLE */
193 #endif /* HAVE_ACCELERATE */
198 fvec_max (fvec_t * s)
200 #ifndef HAVE_ACCELERATE
203 for (j = 0; j < s->length; j++) {
204 tmp = (tmp > s->data[j]) ? tmp : s->data[j];
208 #if !HAVE_AUBIO_DOUBLE
209 vDSP_maxv(s->data, 1, &tmp, s->length);
211 vDSP_maxvD(s->data, 1, &tmp, s->length);
218 fvec_min (fvec_t * s)
220 #ifndef HAVE_ACCELERATE
222 smpl_t tmp = s->data[0];
223 for (j = 0; j < s->length; j++) {
224 tmp = (tmp < s->data[j]) ? tmp : s->data[j];
228 #if !HAVE_AUBIO_DOUBLE
229 vDSP_minv(s->data, 1, &tmp, s->length);
231 vDSP_minvD(s->data, 1, &tmp, s->length);
238 fvec_min_elem (fvec_t * s)
240 #ifndef HAVE_ACCELERATE
242 smpl_t tmp = s->data[0];
243 for (j = 0; j < s->length; j++) {
244 pos = (tmp < s->data[j]) ? pos : j;
245 tmp = (tmp < s->data[j]) ? tmp : s->data[j];
250 #if !HAVE_AUBIO_DOUBLE
251 vDSP_minvi(s->data, 1, &tmp, (vDSP_Length *)&pos, s->length);
253 vDSP_minviD(s->data, 1, &tmp, (vDSP_Length *)&pos, s->length);
260 fvec_max_elem (fvec_t * s)
262 #ifndef HAVE_ACCELERATE
265 for (j = 0; j < s->length; j++) {
266 pos = (tmp > s->data[j]) ? pos : j;
267 tmp = (tmp > s->data[j]) ? tmp : s->data[j];
272 #if !HAVE_AUBIO_DOUBLE
273 vDSP_maxvi(s->data, 1, &tmp, (vDSP_Length *)&pos, s->length);
275 vDSP_maxviD(s->data, 1, &tmp, (vDSP_Length *)&pos, s->length);
282 fvec_shift (fvec_t * s)
285 for (j = 0; j < s->length / 2; j++) {
286 ELEM_SWAP (s->data[j], s->data[j + s->length / 2]);
291 aubio_level_lin (fvec_t * f)
295 for (j = 0; j < f->length; j++) {
296 energy += SQR (f->data[j]);
298 return energy / f->length;
302 fvec_local_hfc (fvec_t * v)
306 for (j = 0; j < v->length; j++) {
307 hfc += (j + 1) * v->data[j];
313 fvec_min_removal (fvec_t * v)
315 smpl_t v_min = fvec_min (v);
316 fvec_add (v, - v_min );
320 fvec_alpha_norm (fvec_t * o, smpl_t alpha)
324 for (j = 0; j < o->length; j++) {
325 tmp += POW (ABS (o->data[j]), alpha);
327 return POW (tmp / o->length, 1. / alpha);
331 fvec_alpha_normalise (fvec_t * o, smpl_t alpha)
334 smpl_t norm = fvec_alpha_norm (o, alpha);
335 for (j = 0; j < o->length; j++) {
341 fvec_add (fvec_t * o, smpl_t val)
344 for (j = 0; j < o->length; j++) {
349 void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp,
350 uint_t post, uint_t pre) {
351 uint_t length = vec->length, j;
352 for (j=0;j<length;j++) {
353 vec->data[j] -= fvec_moving_thres(vec, tmp, post, pre, j);
358 fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec,
359 uint_t post, uint_t pre, uint_t pos)
362 smpl_t *medar = (smpl_t *) tmpvec->data;
363 uint_t win_length = post + pre + 1;
364 uint_t length = vec->length;
365 /* post part of the buffer does not exist */
366 if (pos < post + 1) {
367 for (k = 0; k < post + 1 - pos; k++)
368 medar[k] = 0.; /* 0-padding at the beginning */
369 for (k = post + 1 - pos; k < win_length; k++)
370 medar[k] = vec->data[k + pos - post];
371 /* the buffer is fully defined */
372 } else if (pos + pre < length) {
373 for (k = 0; k < win_length; k++)
374 medar[k] = vec->data[k + pos - post];
375 /* pre part of the buffer does not exist */
377 for (k = 0; k < length - pos + post; k++)
378 medar[k] = vec->data[k + pos - post];
379 for (k = length - pos + post; k < win_length; k++)
380 medar[k] = 0.; /* 0-padding at the end */
382 return fvec_median (tmpvec);
385 smpl_t fvec_median (fvec_t * input) {
386 uint_t n = input->length;
387 smpl_t * arr = (smpl_t *) input->data;
390 uint_t middle, ll, hh;
392 low = 0 ; high = n-1 ; median = (low + high) / 2;
394 if (high <= low) /* One element only */
397 if (high == low + 1) { /* Two elements only */
398 if (arr[low] > arr[high])
399 ELEM_SWAP(arr[low], arr[high]) ;
403 /* Find median of low, middle and high items; swap into position low */
404 middle = (low + high) / 2;
405 if (arr[middle] > arr[high]) ELEM_SWAP(arr[middle], arr[high]);
406 if (arr[low] > arr[high]) ELEM_SWAP(arr[low], arr[high]);
407 if (arr[middle] > arr[low]) ELEM_SWAP(arr[middle], arr[low]) ;
409 /* Swap low item (now in position middle) into position (low+1) */
410 ELEM_SWAP(arr[middle], arr[low+1]) ;
412 /* Nibble from each end towards middle, swapping items when stuck */
416 do ll++; while (arr[low] > arr[ll]) ;
417 do hh--; while (arr[hh] > arr[low]) ;
422 ELEM_SWAP(arr[ll], arr[hh]) ;
425 /* Swap middle item (in position low) back into correct position */
426 ELEM_SWAP(arr[low], arr[hh]) ;
428 /* Re-set active partition */
436 smpl_t fvec_quadratic_peak_pos (fvec_t * x, uint_t pos) {
437 smpl_t s0, s1, s2; uint_t x0, x2;
438 if (pos == 0 || pos == x->length - 1) return pos;
439 x0 = (pos < 1) ? pos : pos - 1;
440 x2 = (pos + 1 < x->length) ? pos + 1 : pos;
441 if (x0 == pos) return (x->data[pos] <= x->data[x2]) ? pos : x2;
442 if (x2 == pos) return (x->data[pos] <= x->data[x0]) ? pos : x0;
446 return pos + 0.5 * (s0 - s2 ) / (s0 - 2.* s1 + s2);
449 smpl_t fvec_quadratic_peak_mag (fvec_t *x, smpl_t pos) {
451 uint_t index = (uint_t)(pos - .5) + 1;
452 if (pos >= x->length || pos < 0.) return 0.;
453 if ((smpl_t)index == pos) return x->data[index];
454 x0 = x->data[index - 1];
456 x2 = x->data[index + 1];
457 return x1 - .25 * (x0 - x2) * (pos - index);
460 uint_t fvec_peakpick(fvec_t * onset, uint_t pos) {
462 tmp = (onset->data[pos] > onset->data[pos-1]
463 && onset->data[pos] > onset->data[pos+1]
464 && onset->data[pos] > 0.);
469 aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf)
472 s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2);
477 aubio_freqtomidi (smpl_t freq)
480 if (freq < 2. || freq > 100000.) return 0.; // avoid nans and infs
481 /* log(freq/A-2)/log(2) */
483 midi = LOG (midi) / 0.69314718055995;
490 aubio_miditofreq (smpl_t midi)
493 if (midi > 140.) return 0.; // avoid infs
494 freq = (midi + 3.) / 12.;
495 freq = EXP (freq * 0.69314718055995);
501 aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
503 smpl_t freq = samplerate / fftsize;
504 return freq * MAX(bin, 0);
508 aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize)
510 smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize);
511 return aubio_freqtomidi (midi);
515 aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize)
517 smpl_t bin = fftsize / samplerate;
518 return MAX(freq, 0) * bin;
522 aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize)
524 smpl_t freq = aubio_miditofreq (midi);
525 return aubio_freqtobin (freq, samplerate, fftsize);
529 aubio_is_power_of_two (uint_t a)
531 if ((a & (a - 1)) == 0) {
539 aubio_next_power_of_two (uint_t a)
542 while (i < a) i <<= 1;
547 aubio_db_spl (fvec_t * o)
549 return 10. * LOG10 (aubio_level_lin (o));
553 aubio_silence_detection (fvec_t * o, smpl_t threshold)
555 return (aubio_db_spl (o) < threshold);
559 aubio_level_detection (fvec_t * o, smpl_t threshold)
561 smpl_t db_spl = aubio_db_spl (o);
562 if (db_spl < threshold) {
570 aubio_zero_crossing_rate (fvec_t * input)
574 for (j = 1; j < input->length; j++) {
575 // previous was strictly negative
576 if (input->data[j - 1] < 0.) {
577 // current is positive or null
578 if (input->data[j] >= 0.) {
581 // previous was positive or null
583 // current is strictly negative
584 if (input->data[j] < 0.) {
589 return zcr / (smpl_t) input->length;
593 aubio_autocorr (fvec_t * input, fvec_t * output)
595 uint_t i, j, length = input->length;
600 for (i = 0; i < length; i++) {
602 for (j = i; j < length; j++) {
603 tmp += data[j - i] * data[j];
605 acf[i] = tmp / (smpl_t) (length - i);