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