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