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