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