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