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