[96fb8ad] | 1 | /* |
---|
[9499eefb] | 2 | Copyright (C) 2003-2013 Paul Brossier <piem@aubio.org> |
---|
[96fb8ad] | 3 | |
---|
[a6db140] | 4 | This file is part of aubio. |
---|
[96fb8ad] | 5 | |
---|
[a6db140] | 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. |
---|
[96fb8ad] | 10 | |
---|
[a6db140] | 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. |
---|
| 15 | |
---|
| 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/>. |
---|
[96fb8ad] | 18 | |
---|
| 19 | */ |
---|
| 20 | |
---|
| 21 | /* see in mathutils.h for doc */ |
---|
| 22 | |
---|
| 23 | #include "aubio_priv.h" |
---|
[6c7d49b] | 24 | #include "fvec.h" |
---|
[96fb8ad] | 25 | #include "mathutils.h" |
---|
[83963b3] | 26 | #include "musicutils.h" |
---|
[714380d] | 27 | #include "config.h" |
---|
[96fb8ad] | 28 | |
---|
[407bba9] | 29 | |
---|
| 30 | /** Window types */ |
---|
| 31 | typedef enum |
---|
| 32 | { |
---|
| 33 | aubio_win_rectangle, |
---|
| 34 | aubio_win_hamming, |
---|
| 35 | aubio_win_hanning, |
---|
| 36 | aubio_win_hanningz, |
---|
| 37 | aubio_win_blackman, |
---|
| 38 | aubio_win_blackman_harris, |
---|
| 39 | aubio_win_gaussian, |
---|
| 40 | aubio_win_welch, |
---|
| 41 | aubio_win_parzen, |
---|
| 42 | aubio_win_default = aubio_win_hanningz, |
---|
| 43 | } aubio_window_type; |
---|
| 44 | |
---|
[eb7f743] | 45 | fvec_t * |
---|
[33cd81f] | 46 | new_aubio_window (char_t * window_type, uint_t length) |
---|
[eb7f743] | 47 | { |
---|
[33cd81f] | 48 | fvec_t * win = new_fvec (length); |
---|
| 49 | fvec_set_window (win, window_type); |
---|
| 50 | return win; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | uint_t fvec_set_window (fvec_t *win, char_t *window_type) { |
---|
[8e5c051] | 54 | smpl_t * w = win->data; |
---|
[33cd81f] | 55 | uint_t i, size = win->length; |
---|
[407bba9] | 56 | aubio_window_type wintype; |
---|
[33cd81f] | 57 | if (window_type == NULL) { |
---|
| 58 | AUBIO_ERR ("window type can not be null.\n"); |
---|
| 59 | return 1; |
---|
| 60 | } else if (strcmp (window_type, "rectangle") == 0) |
---|
[407bba9] | 61 | wintype = aubio_win_rectangle; |
---|
| 62 | else if (strcmp (window_type, "hamming") == 0) |
---|
| 63 | wintype = aubio_win_hamming; |
---|
| 64 | else if (strcmp (window_type, "hanning") == 0) |
---|
| 65 | wintype = aubio_win_hanning; |
---|
| 66 | else if (strcmp (window_type, "hanningz") == 0) |
---|
| 67 | wintype = aubio_win_hanningz; |
---|
| 68 | else if (strcmp (window_type, "blackman") == 0) |
---|
| 69 | wintype = aubio_win_blackman; |
---|
| 70 | else if (strcmp (window_type, "blackman_harris") == 0) |
---|
| 71 | wintype = aubio_win_blackman_harris; |
---|
| 72 | else if (strcmp (window_type, "gaussian") == 0) |
---|
| 73 | wintype = aubio_win_gaussian; |
---|
| 74 | else if (strcmp (window_type, "welch") == 0) |
---|
| 75 | wintype = aubio_win_welch; |
---|
| 76 | else if (strcmp (window_type, "parzen") == 0) |
---|
| 77 | wintype = aubio_win_parzen; |
---|
| 78 | else if (strcmp (window_type, "default") == 0) |
---|
| 79 | wintype = aubio_win_default; |
---|
| 80 | else { |
---|
[33cd81f] | 81 | AUBIO_ERR ("unknown window type `%s`.\n", window_type); |
---|
| 82 | return 1; |
---|
[407bba9] | 83 | } |
---|
[96fb8ad] | 84 | switch(wintype) { |
---|
[b4b0324] | 85 | case aubio_win_rectangle: |
---|
[96fb8ad] | 86 | for (i=0;i<size;i++) |
---|
[ade9afe] | 87 | w[i] = 0.5; |
---|
[96fb8ad] | 88 | break; |
---|
[b4b0324] | 89 | case aubio_win_hamming: |
---|
[96fb8ad] | 90 | for (i=0;i<size;i++) |
---|
| 91 | w[i] = 0.54 - 0.46 * COS(TWO_PI * i / (size)); |
---|
| 92 | break; |
---|
[b4b0324] | 93 | case aubio_win_hanning: |
---|
[96fb8ad] | 94 | for (i=0;i<size;i++) |
---|
| 95 | w[i] = 0.5 - (0.5 * COS(TWO_PI * i / (size))); |
---|
| 96 | break; |
---|
[b4b0324] | 97 | case aubio_win_hanningz: |
---|
[96fb8ad] | 98 | for (i=0;i<size;i++) |
---|
| 99 | w[i] = 0.5 * (1.0 - COS(TWO_PI * i / (size))); |
---|
| 100 | break; |
---|
[b4b0324] | 101 | case aubio_win_blackman: |
---|
[96fb8ad] | 102 | for (i=0;i<size;i++) |
---|
| 103 | w[i] = 0.42 |
---|
| 104 | - 0.50 * COS( TWO_PI*i/(size-1.0)) |
---|
[ade9afe] | 105 | + 0.08 * COS(2.0*TWO_PI*i/(size-1.0)); |
---|
[96fb8ad] | 106 | break; |
---|
[b4b0324] | 107 | case aubio_win_blackman_harris: |
---|
[96fb8ad] | 108 | for (i=0;i<size;i++) |
---|
[ade9afe] | 109 | w[i] = 0.35875 |
---|
[96fb8ad] | 110 | - 0.48829 * COS( TWO_PI*i/(size-1.0)) |
---|
| 111 | + 0.14128 * COS(2.0*TWO_PI*i/(size-1.0)) |
---|
| 112 | - 0.01168 * COS(3.0*TWO_PI*i/(size-1.0)); |
---|
| 113 | break; |
---|
[b4b0324] | 114 | case aubio_win_gaussian: |
---|
[9e56228] | 115 | { |
---|
| 116 | lsmp_t a, b, c = 0.5; |
---|
| 117 | uint_t n; |
---|
| 118 | for (n = 0; n < size; n++) |
---|
| 119 | { |
---|
| 120 | a = (n-c*(size-1))/(SQR(c)*(size-1)); |
---|
| 121 | b = -c*SQR(a); |
---|
| 122 | w[n] = EXP(b); |
---|
| 123 | } |
---|
| 124 | } |
---|
[96fb8ad] | 125 | break; |
---|
[b4b0324] | 126 | case aubio_win_welch: |
---|
[96fb8ad] | 127 | for (i=0;i<size;i++) |
---|
[f9d5346] | 128 | w[i] = 1.0 - SQR((2.*i-size)/(size+1.0)); |
---|
[96fb8ad] | 129 | break; |
---|
[b4b0324] | 130 | case aubio_win_parzen: |
---|
[96fb8ad] | 131 | for (i=0;i<size;i++) |
---|
[f9d5346] | 132 | w[i] = 1.0 - ABS((2.*i-size)/(size+1.0)); |
---|
[96fb8ad] | 133 | break; |
---|
| 134 | default: |
---|
| 135 | break; |
---|
| 136 | } |
---|
[33cd81f] | 137 | return 0; |
---|
[96fb8ad] | 138 | } |
---|
| 139 | |
---|
[eb7f743] | 140 | smpl_t |
---|
| 141 | aubio_unwrap2pi (smpl_t phase) |
---|
| 142 | { |
---|
[96fb8ad] | 143 | /* mod(phase+pi,-2pi)+pi */ |
---|
[eb7f743] | 144 | return phase + TWO_PI * (1. + FLOOR (-(phase + PI) / TWO_PI)); |
---|
[96fb8ad] | 145 | } |
---|
| 146 | |
---|
[eb7f743] | 147 | smpl_t |
---|
| 148 | fvec_mean (fvec_t * s) |
---|
| 149 | { |
---|
[56ef7e1] | 150 | uint_t j; |
---|
| 151 | smpl_t tmp = 0.0; |
---|
[8e5c051] | 152 | for (j = 0; j < s->length; j++) { |
---|
| 153 | tmp += s->data[j]; |
---|
| 154 | } |
---|
[56ef7e1] | 155 | return tmp / (smpl_t) (s->length); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | smpl_t |
---|
[eb7f743] | 159 | fvec_sum (fvec_t * s) |
---|
| 160 | { |
---|
[8e5c051] | 161 | uint_t j; |
---|
[acf7d30] | 162 | smpl_t tmp = 0.0; |
---|
[8e5c051] | 163 | for (j = 0; j < s->length; j++) { |
---|
| 164 | tmp += s->data[j]; |
---|
[eb7f743] | 165 | } |
---|
[96fb8ad] | 166 | return tmp; |
---|
| 167 | } |
---|
| 168 | |
---|
[eb7f743] | 169 | smpl_t |
---|
| 170 | fvec_max (fvec_t * s) |
---|
| 171 | { |
---|
[8e5c051] | 172 | uint_t j; |
---|
[acf7d30] | 173 | smpl_t tmp = 0.0; |
---|
[8e5c051] | 174 | for (j = 0; j < s->length; j++) { |
---|
| 175 | tmp = (tmp > s->data[j]) ? tmp : s->data[j]; |
---|
[eb7f743] | 176 | } |
---|
[96fb8ad] | 177 | return tmp; |
---|
| 178 | } |
---|
| 179 | |
---|
[eb7f743] | 180 | smpl_t |
---|
| 181 | fvec_min (fvec_t * s) |
---|
| 182 | { |
---|
[8e5c051] | 183 | uint_t j; |
---|
| 184 | smpl_t tmp = s->data[0]; |
---|
| 185 | for (j = 0; j < s->length; j++) { |
---|
| 186 | tmp = (tmp < s->data[j]) ? tmp : s->data[j]; |
---|
[eb7f743] | 187 | } |
---|
[96fb8ad] | 188 | return tmp; |
---|
| 189 | } |
---|
| 190 | |
---|
[eb7f743] | 191 | uint_t |
---|
| 192 | fvec_min_elem (fvec_t * s) |
---|
| 193 | { |
---|
[8e5c051] | 194 | uint_t j, pos = 0.; |
---|
| 195 | smpl_t tmp = s->data[0]; |
---|
| 196 | for (j = 0; j < s->length; j++) { |
---|
| 197 | pos = (tmp < s->data[j]) ? pos : j; |
---|
| 198 | tmp = (tmp < s->data[j]) ? tmp : s->data[j]; |
---|
[eb7f743] | 199 | } |
---|
[96fb8ad] | 200 | return pos; |
---|
| 201 | } |
---|
| 202 | |
---|
[eb7f743] | 203 | uint_t |
---|
| 204 | fvec_max_elem (fvec_t * s) |
---|
| 205 | { |
---|
[8e5c051] | 206 | uint_t j, pos = 0; |
---|
[acf7d30] | 207 | smpl_t tmp = 0.0; |
---|
[8e5c051] | 208 | for (j = 0; j < s->length; j++) { |
---|
| 209 | pos = (tmp > s->data[j]) ? pos : j; |
---|
| 210 | tmp = (tmp > s->data[j]) ? tmp : s->data[j]; |
---|
[eb7f743] | 211 | } |
---|
[96fb8ad] | 212 | return pos; |
---|
| 213 | } |
---|
| 214 | |
---|
[eb7f743] | 215 | void |
---|
| 216 | fvec_shift (fvec_t * s) |
---|
| 217 | { |
---|
[8e5c051] | 218 | uint_t j; |
---|
| 219 | for (j = 0; j < s->length / 2; j++) { |
---|
| 220 | ELEM_SWAP (s->data[j], s->data[j + s->length / 2]); |
---|
[eb7f743] | 221 | } |
---|
[96fb8ad] | 222 | } |
---|
| 223 | |
---|
[eb7f743] | 224 | smpl_t |
---|
| 225 | fvec_local_energy (fvec_t * f) |
---|
| 226 | { |
---|
| 227 | smpl_t energy = 0.; |
---|
[8e5c051] | 228 | uint_t j; |
---|
| 229 | for (j = 0; j < f->length; j++) { |
---|
| 230 | energy += SQR (f->data[j]); |
---|
[eb7f743] | 231 | } |
---|
[1a74ac3] | 232 | return energy / f->length; |
---|
[96fb8ad] | 233 | } |
---|
| 234 | |
---|
[eb7f743] | 235 | smpl_t |
---|
| 236 | fvec_local_hfc (fvec_t * v) |
---|
| 237 | { |
---|
| 238 | smpl_t hfc = 0.; |
---|
[8e5c051] | 239 | uint_t j; |
---|
| 240 | for (j = 0; j < v->length; j++) { |
---|
| 241 | hfc += (j + 1) * v->data[j]; |
---|
[eb7f743] | 242 | } |
---|
| 243 | return hfc; |
---|
[96fb8ad] | 244 | } |
---|
| 245 | |
---|
[eb7f743] | 246 | void |
---|
| 247 | fvec_min_removal (fvec_t * v) |
---|
| 248 | { |
---|
| 249 | smpl_t v_min = fvec_min (v); |
---|
| 250 | fvec_add (v, - v_min ); |
---|
[96fb8ad] | 251 | } |
---|
| 252 | |
---|
[eb7f743] | 253 | smpl_t |
---|
| 254 | fvec_alpha_norm (fvec_t * o, smpl_t alpha) |
---|
[c0b295c] | 255 | { |
---|
[8e5c051] | 256 | uint_t j; |
---|
[eb7f743] | 257 | smpl_t tmp = 0.; |
---|
[8e5c051] | 258 | for (j = 0; j < o->length; j++) { |
---|
| 259 | tmp += POW (ABS (o->data[j]), alpha); |
---|
[c0b295c] | 260 | } |
---|
[eb7f743] | 261 | return POW (tmp / o->length, 1. / alpha); |
---|
[96fb8ad] | 262 | } |
---|
| 263 | |
---|
[eb7f743] | 264 | void |
---|
| 265 | fvec_alpha_normalise (fvec_t * o, smpl_t alpha) |
---|
| 266 | { |
---|
[8e5c051] | 267 | uint_t j; |
---|
[eb7f743] | 268 | smpl_t norm = fvec_alpha_norm (o, alpha); |
---|
[8e5c051] | 269 | for (j = 0; j < o->length; j++) { |
---|
| 270 | o->data[j] /= norm; |
---|
[96fb8ad] | 271 | } |
---|
| 272 | } |
---|
| 273 | |
---|
[eb7f743] | 274 | void |
---|
| 275 | fvec_add (fvec_t * o, smpl_t val) |
---|
| 276 | { |
---|
[8e5c051] | 277 | uint_t j; |
---|
| 278 | for (j = 0; j < o->length; j++) { |
---|
| 279 | o->data[j] += val; |
---|
[96fb8ad] | 280 | } |
---|
| 281 | } |
---|
| 282 | |
---|
[5c4ec3c] | 283 | void fvec_adapt_thres(fvec_t * vec, fvec_t * tmp, |
---|
[8e5c051] | 284 | uint_t post, uint_t pre) { |
---|
| 285 | uint_t length = vec->length, j; |
---|
[96fb8ad] | 286 | for (j=0;j<length;j++) { |
---|
[8e5c051] | 287 | vec->data[j] -= fvec_moving_thres(vec, tmp, post, pre, j); |
---|
[96fb8ad] | 288 | } |
---|
| 289 | } |
---|
| 290 | |
---|
[eb7f743] | 291 | smpl_t |
---|
| 292 | fvec_moving_thres (fvec_t * vec, fvec_t * tmpvec, |
---|
[8e5c051] | 293 | uint_t post, uint_t pre, uint_t pos) |
---|
[eb7f743] | 294 | { |
---|
[8e5c051] | 295 | uint_t k; |
---|
| 296 | smpl_t *medar = (smpl_t *) tmpvec->data; |
---|
[eb7f743] | 297 | uint_t win_length = post + pre + 1; |
---|
| 298 | uint_t length = vec->length; |
---|
[96fb8ad] | 299 | /* post part of the buffer does not exist */ |
---|
[eb7f743] | 300 | if (pos < post + 1) { |
---|
| 301 | for (k = 0; k < post + 1 - pos; k++) |
---|
| 302 | medar[k] = 0.; /* 0-padding at the beginning */ |
---|
| 303 | for (k = post + 1 - pos; k < win_length; k++) |
---|
[8e5c051] | 304 | medar[k] = vec->data[k + pos - post]; |
---|
[eb7f743] | 305 | /* the buffer is fully defined */ |
---|
| 306 | } else if (pos + pre < length) { |
---|
| 307 | for (k = 0; k < win_length; k++) |
---|
[8e5c051] | 308 | medar[k] = vec->data[k + pos - post]; |
---|
[eb7f743] | 309 | /* pre part of the buffer does not exist */ |
---|
[96fb8ad] | 310 | } else { |
---|
[eb7f743] | 311 | for (k = 0; k < length - pos + post; k++) |
---|
[8e5c051] | 312 | medar[k] = vec->data[k + pos - post]; |
---|
[eb7f743] | 313 | for (k = length - pos + post; k < win_length; k++) |
---|
| 314 | medar[k] = 0.; /* 0-padding at the end */ |
---|
[ade9afe] | 315 | } |
---|
[8e5c051] | 316 | return fvec_median (tmpvec); |
---|
[96fb8ad] | 317 | } |
---|
| 318 | |
---|
[8e5c051] | 319 | smpl_t fvec_median (fvec_t * input) { |
---|
[96fb8ad] | 320 | uint_t n = input->length; |
---|
[8e5c051] | 321 | smpl_t * arr = (smpl_t *) input->data; |
---|
[96fb8ad] | 322 | uint_t low, high ; |
---|
| 323 | uint_t median; |
---|
| 324 | uint_t middle, ll, hh; |
---|
| 325 | |
---|
| 326 | low = 0 ; high = n-1 ; median = (low + high) / 2; |
---|
| 327 | for (;;) { |
---|
| 328 | if (high <= low) /* One element only */ |
---|
| 329 | return arr[median] ; |
---|
| 330 | |
---|
| 331 | if (high == low + 1) { /* Two elements only */ |
---|
| 332 | if (arr[low] > arr[high]) |
---|
| 333 | ELEM_SWAP(arr[low], arr[high]) ; |
---|
| 334 | return arr[median] ; |
---|
| 335 | } |
---|
| 336 | |
---|
| 337 | /* Find median of low, middle and high items; swap into position low */ |
---|
| 338 | middle = (low + high) / 2; |
---|
| 339 | if (arr[middle] > arr[high]) ELEM_SWAP(arr[middle], arr[high]); |
---|
| 340 | if (arr[low] > arr[high]) ELEM_SWAP(arr[low], arr[high]); |
---|
| 341 | if (arr[middle] > arr[low]) ELEM_SWAP(arr[middle], arr[low]) ; |
---|
| 342 | |
---|
| 343 | /* Swap low item (now in position middle) into position (low+1) */ |
---|
| 344 | ELEM_SWAP(arr[middle], arr[low+1]) ; |
---|
| 345 | |
---|
| 346 | /* Nibble from each end towards middle, swapping items when stuck */ |
---|
| 347 | ll = low + 1; |
---|
| 348 | hh = high; |
---|
| 349 | for (;;) { |
---|
| 350 | do ll++; while (arr[low] > arr[ll]) ; |
---|
| 351 | do hh--; while (arr[hh] > arr[low]) ; |
---|
| 352 | |
---|
| 353 | if (hh < ll) |
---|
| 354 | break; |
---|
| 355 | |
---|
| 356 | ELEM_SWAP(arr[ll], arr[hh]) ; |
---|
| 357 | } |
---|
| 358 | |
---|
| 359 | /* Swap middle item (in position low) back into correct position */ |
---|
| 360 | ELEM_SWAP(arr[low], arr[hh]) ; |
---|
| 361 | |
---|
| 362 | /* Re-set active partition */ |
---|
| 363 | if (hh <= median) |
---|
| 364 | low = ll; |
---|
| 365 | if (hh >= median) |
---|
| 366 | high = hh - 1; |
---|
| 367 | } |
---|
| 368 | } |
---|
| 369 | |
---|
[9499eefb] | 370 | smpl_t fvec_quadratic_peak_pos (fvec_t * x, uint_t pos) { |
---|
| 371 | smpl_t s0, s1, s2; |
---|
[acd97d1] | 372 | if (pos == 0 || pos == x->length - 1) return pos; |
---|
[9499eefb] | 373 | uint_t x0 = (pos < 1) ? pos : pos - 1; |
---|
| 374 | uint_t x2 = (pos + 1 < x->length) ? pos + 1 : pos; |
---|
| 375 | if (x0 == pos) return (x->data[pos] <= x->data[x2]) ? pos : x2; |
---|
| 376 | if (x2 == pos) return (x->data[pos] <= x->data[x0]) ? pos : x0; |
---|
| 377 | s0 = x->data[x0]; |
---|
| 378 | s1 = x->data[pos]; |
---|
| 379 | s2 = x->data[x2]; |
---|
| 380 | return pos + 0.5 * (s0 - s2 ) / (s0 - 2.* s1 + s2); |
---|
| 381 | } |
---|
| 382 | |
---|
[5c4ec3c] | 383 | uint_t fvec_peakpick(fvec_t * onset, uint_t pos) { |
---|
[8e5c051] | 384 | uint_t tmp=0; |
---|
| 385 | tmp = (onset->data[pos] > onset->data[pos-1] |
---|
| 386 | && onset->data[pos] > onset->data[pos+1] |
---|
| 387 | && onset->data[pos] > 0.); |
---|
[ade9afe] | 388 | return tmp; |
---|
[96fb8ad] | 389 | } |
---|
| 390 | |
---|
[eb7f743] | 391 | smpl_t |
---|
| 392 | aubio_quadfrac (smpl_t s0, smpl_t s1, smpl_t s2, smpl_t pf) |
---|
| 393 | { |
---|
| 394 | smpl_t tmp = |
---|
| 395 | s0 + (pf / 2.) * (pf * (s0 - 2. * s1 + s2) - 3. * s0 + 4. * s1 - s2); |
---|
| 396 | return tmp; |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | smpl_t |
---|
| 400 | aubio_freqtomidi (smpl_t freq) |
---|
| 401 | { |
---|
[037319a] | 402 | if (freq < 2. || freq > 100000.) return 0.; // avoid nans and infs |
---|
[ade9afe] | 403 | /* log(freq/A-2)/log(2) */ |
---|
[eb7f743] | 404 | smpl_t midi = freq / 6.875; |
---|
| 405 | midi = LOG (midi) / 0.69314718055995; |
---|
[ade9afe] | 406 | midi *= 12; |
---|
| 407 | midi -= 3; |
---|
| 408 | return midi; |
---|
[79c2e52] | 409 | } |
---|
| 410 | |
---|
[eb7f743] | 411 | smpl_t |
---|
| 412 | aubio_miditofreq (smpl_t midi) |
---|
| 413 | { |
---|
[037319a] | 414 | if (midi > 140.) return 0.; // avoid infs |
---|
[eb7f743] | 415 | smpl_t freq = (midi + 3.) / 12.; |
---|
| 416 | freq = EXP (freq * 0.69314718055995); |
---|
[ade9afe] | 417 | freq *= 6.875; |
---|
| 418 | return freq; |
---|
[96fb8ad] | 419 | } |
---|
| 420 | |
---|
[eb7f743] | 421 | smpl_t |
---|
| 422 | aubio_bintofreq (smpl_t bin, smpl_t samplerate, smpl_t fftsize) |
---|
| 423 | { |
---|
| 424 | smpl_t freq = samplerate / fftsize; |
---|
[c965b33] | 425 | return freq * MAX(bin, 0); |
---|
[96fb8ad] | 426 | } |
---|
| 427 | |
---|
[eb7f743] | 428 | smpl_t |
---|
| 429 | aubio_bintomidi (smpl_t bin, smpl_t samplerate, smpl_t fftsize) |
---|
| 430 | { |
---|
| 431 | smpl_t midi = aubio_bintofreq (bin, samplerate, fftsize); |
---|
| 432 | return aubio_freqtomidi (midi); |
---|
[96fb8ad] | 433 | } |
---|
| 434 | |
---|
[eb7f743] | 435 | smpl_t |
---|
| 436 | aubio_freqtobin (smpl_t freq, smpl_t samplerate, smpl_t fftsize) |
---|
| 437 | { |
---|
| 438 | smpl_t bin = fftsize / samplerate; |
---|
[c965b33] | 439 | return MAX(freq, 0) * bin; |
---|
[79c2e52] | 440 | } |
---|
| 441 | |
---|
[eb7f743] | 442 | smpl_t |
---|
| 443 | aubio_miditobin (smpl_t midi, smpl_t samplerate, smpl_t fftsize) |
---|
| 444 | { |
---|
| 445 | smpl_t freq = aubio_miditofreq (midi); |
---|
| 446 | return aubio_freqtobin (freq, samplerate, fftsize); |
---|
[79c2e52] | 447 | } |
---|
| 448 | |
---|
[10a5413] | 449 | uint_t |
---|
[41f4c5b] | 450 | aubio_is_power_of_two (uint_t a) |
---|
| 451 | { |
---|
| 452 | if ((a & (a - 1)) == 0) { |
---|
[10a5413] | 453 | return 1; |
---|
| 454 | } else { |
---|
[41f4c5b] | 455 | return 0; |
---|
[10a5413] | 456 | } |
---|
| 457 | } |
---|
| 458 | |
---|
| 459 | uint_t |
---|
[41f4c5b] | 460 | aubio_next_power_of_two (uint_t a) |
---|
| 461 | { |
---|
[7581185] | 462 | uint_t i = 1; |
---|
| 463 | while (i < a) i <<= 1; |
---|
| 464 | return i; |
---|
[10a5413] | 465 | } |
---|
| 466 | |
---|
[eb7f743] | 467 | smpl_t |
---|
| 468 | aubio_db_spl (fvec_t * o) |
---|
| 469 | { |
---|
[1a74ac3] | 470 | return 10. * LOG10 (fvec_local_energy (o)); |
---|
[96fb8ad] | 471 | } |
---|
| 472 | |
---|
[eb7f743] | 473 | uint_t |
---|
| 474 | aubio_silence_detection (fvec_t * o, smpl_t threshold) |
---|
| 475 | { |
---|
| 476 | return (aubio_db_spl (o) < threshold); |
---|
| 477 | } |
---|
[96fb8ad] | 478 | |
---|
[eb7f743] | 479 | smpl_t |
---|
| 480 | aubio_level_detection (fvec_t * o, smpl_t threshold) |
---|
| 481 | { |
---|
| 482 | smpl_t db_spl = aubio_db_spl (o); |
---|
| 483 | if (db_spl < threshold) { |
---|
[ade9afe] | 484 | return 1.; |
---|
[eb7f743] | 485 | } else { |
---|
| 486 | return db_spl; |
---|
| 487 | } |
---|
[96fb8ad] | 488 | } |
---|
[a0fd4e4] | 489 | |
---|
[eb7f743] | 490 | smpl_t |
---|
| 491 | aubio_zero_crossing_rate (fvec_t * input) |
---|
| 492 | { |
---|
[8e5c051] | 493 | uint_t j; |
---|
[fff2bee] | 494 | uint_t zcr = 0; |
---|
[eb7f743] | 495 | for (j = 1; j < input->length; j++) { |
---|
[7e204d01] | 496 | // previous was strictly negative |
---|
[8e5c051] | 497 | if (input->data[j - 1] < 0.) { |
---|
[7e204d01] | 498 | // current is positive or null |
---|
[8e5c051] | 499 | if (input->data[j] >= 0.) { |
---|
[7e204d01] | 500 | zcr += 1; |
---|
| 501 | } |
---|
[eb7f743] | 502 | // previous was positive or null |
---|
[7e204d01] | 503 | } else { |
---|
| 504 | // current is strictly negative |
---|
[8e5c051] | 505 | if (input->data[j] < 0.) { |
---|
[fff2bee] | 506 | zcr += 1; |
---|
| 507 | } |
---|
| 508 | } |
---|
| 509 | } |
---|
[eb7f743] | 510 | return zcr / (smpl_t) input->length; |
---|
[fff2bee] | 511 | } |
---|
| 512 | |
---|
[eb7f743] | 513 | void |
---|
| 514 | aubio_autocorr (fvec_t * input, fvec_t * output) |
---|
| 515 | { |
---|
[8e5c051] | 516 | uint_t i, j, length = input->length; |
---|
[eb7f743] | 517 | smpl_t *data, *acf; |
---|
| 518 | smpl_t tmp = 0; |
---|
[8e5c051] | 519 | data = input->data; |
---|
| 520 | acf = output->data; |
---|
| 521 | for (i = 0; i < length; i++) { |
---|
| 522 | tmp = 0.; |
---|
| 523 | for (j = i; j < length; j++) { |
---|
| 524 | tmp += data[j - i] * data[j]; |
---|
[ade9afe] | 525 | } |
---|
[8e5c051] | 526 | acf[i] = tmp / (smpl_t) (length - i); |
---|
[ade9afe] | 527 | } |
---|
[a0fd4e4] | 528 | } |
---|
| 529 | |
---|
[eb7f743] | 530 | void |
---|
| 531 | aubio_cleanup (void) |
---|
| 532 | { |
---|
[729a3c0] | 533 | #ifdef HAVE_FFTW3F |
---|
[eb7f743] | 534 | fftwf_cleanup (); |
---|
[729a3c0] | 535 | #else |
---|
| 536 | #ifdef HAVE_FFTW3 |
---|
| 537 | fftw_cleanup (); |
---|
[714380d] | 538 | #endif |
---|
| 539 | #endif |
---|
| 540 | } |
---|