[650e39b] | 1 | /* |
---|
| 2 | Copyright (C) 2003 Paul Brossier |
---|
| 3 | |
---|
| 4 | This program is free software; you can redistribute it and/or modify |
---|
| 5 | it under the terms of the GNU General Public License as published by |
---|
| 6 | the Free Software Foundation; either version 2 of the License, or |
---|
| 7 | (at your option) any later version. |
---|
| 8 | |
---|
| 9 | This program is distributed in the hope that it will be useful, |
---|
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 12 | GNU General Public License for more details. |
---|
| 13 | |
---|
| 14 | You should have received a copy of the GNU General Public License |
---|
| 15 | along with this program; if not, write to the Free Software |
---|
| 16 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | #include "aubio_priv.h" |
---|
[6c7d49b] | 20 | #include "fvec.h" |
---|
| 21 | #include "cvec.h" |
---|
[650e39b] | 22 | #include "mathutils.h" |
---|
[32d6958] | 23 | #include "spectral/fft.h" |
---|
[2d8cffa] | 24 | #include "pitch/pitchyinfft.h" |
---|
[650e39b] | 25 | |
---|
[91879d9] | 26 | /** pitch yinfft structure */ |
---|
[650e39b] | 27 | struct _aubio_pitchyinfft_t { |
---|
[91879d9] | 28 | fvec_t * win; /**< temporal weighting window */ |
---|
| 29 | fvec_t * winput; /**< windowed spectrum */ |
---|
| 30 | cvec_t * res; /**< complex vector to compute square difference function */ |
---|
| 31 | fvec_t * sqrmag; /**< square difference function */ |
---|
| 32 | fvec_t * weight; /**< spectral weighting window (psychoacoustic model) */ |
---|
| 33 | cvec_t * fftout; /**< Fourier transform output */ |
---|
[679441e] | 34 | aubio_fft_t * fft; /**< fft object to compute square difference function */ |
---|
[91879d9] | 35 | fvec_t * yinfft; /**< Yin function */ |
---|
[650e39b] | 36 | }; |
---|
| 37 | |
---|
| 38 | static const smpl_t freqs[] = {0., 20., 25., 31.5, 40., 50., 63., 80., 100., |
---|
[10cf306] | 39 | 125., 160., 200., 250., 315., 400., 500., 630., 800., 1000., 1250., |
---|
| 40 | 1600., 2000., 2500., 3150., 4000., 5000., 6300., 8000., 9000., 10000., |
---|
| 41 | 12500., 15000., 20000., 25100}; |
---|
[650e39b] | 42 | |
---|
| 43 | static const smpl_t weight[] = {-75.8, -70.1, -60.8, -52.1, -44.2, -37.5, |
---|
[10cf306] | 44 | -31.3, -25.6, -20.9, -16.5, -12.6, -9.6, -7.0, -4.7, -3.0, -1.8, -0.8, |
---|
| 45 | -0.2, -0.0, 0.5, 1.6, 3.2, 5.4, 7.8, 8.1, 5.3, -2.4, -11.1, -12.8, |
---|
| 46 | -12.2, -7.4, -17.8, -17.8, -17.8}; |
---|
[650e39b] | 47 | |
---|
| 48 | aubio_pitchyinfft_t * new_aubio_pitchyinfft (uint_t bufsize) |
---|
| 49 | { |
---|
| 50 | aubio_pitchyinfft_t * p = AUBIO_NEW(aubio_pitchyinfft_t); |
---|
[10cf306] | 51 | p->winput = new_fvec(bufsize,1); |
---|
| 52 | p->fft = new_aubio_fft(bufsize, 1); |
---|
| 53 | p->fftout = new_cvec(bufsize,1); |
---|
| 54 | p->sqrmag = new_fvec(bufsize,1); |
---|
| 55 | p->res = new_cvec(bufsize,1); |
---|
| 56 | p->yinfft = new_fvec(bufsize/2+1,1); |
---|
| 57 | p->win = new_fvec(bufsize,1); |
---|
[650e39b] | 58 | aubio_window(p->win->data[0], bufsize, aubio_win_hanningz); |
---|
| 59 | p->weight = new_fvec(bufsize/2+1,1); |
---|
| 60 | { |
---|
[10cf306] | 61 | uint_t i = 0, j = 1; |
---|
| 62 | smpl_t freq = 0, a0 = 0, a1 = 0, f0 = 0, f1 = 0; |
---|
| 63 | for (i=0; i<p->weight->length; i++) { |
---|
| 64 | freq = (smpl_t)i/(smpl_t)bufsize*(smpl_t)44100.; |
---|
| 65 | while (freq > freqs[j]) { |
---|
| 66 | j +=1; |
---|
| 67 | } |
---|
| 68 | a0 = weight[j-1]; |
---|
| 69 | f0 = freqs[j-1]; |
---|
| 70 | a1 = weight[j]; |
---|
| 71 | f1 = freqs[j]; |
---|
| 72 | if (f0 == f1) { // just in case |
---|
| 73 | p->weight->data[0][i] = a0; |
---|
| 74 | } else if (f0 == 0) { // y = ax+b |
---|
| 75 | p->weight->data[0][i] = (a1-a0)/f1*freq + a0; |
---|
| 76 | } else { |
---|
| 77 | p->weight->data[0][i] = (a1-a0)/(f1-f0)*freq + |
---|
| 78 | (a0 - (a1 - a0)/(f1/f0 - 1.)); |
---|
| 79 | } |
---|
| 80 | while (freq > freqs[j]) { |
---|
| 81 | j +=1; |
---|
| 82 | } |
---|
| 83 | //AUBIO_DBG("%f\n",p->weight->data[0][i]); |
---|
| 84 | p->weight->data[0][i] = DB2LIN(p->weight->data[0][i]); |
---|
| 85 | //p->weight->data[0][i] = SQRT(DB2LIN(p->weight->data[0][i])); |
---|
| 86 | } |
---|
[650e39b] | 87 | } |
---|
| 88 | return p; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | smpl_t aubio_pitchyinfft_detect(aubio_pitchyinfft_t * p, fvec_t * input, smpl_t tol) { |
---|
| 92 | uint_t tau, l = 0; |
---|
| 93 | uint_t halfperiod; |
---|
| 94 | smpl_t tmp = 0, sum = 0; |
---|
| 95 | cvec_t * res = (cvec_t *)p->res; |
---|
| 96 | fvec_t * yin = (fvec_t *)p->yinfft; |
---|
| 97 | for (l=0; l < input->length; l++){ |
---|
[10cf306] | 98 | p->winput->data[0][l] = p->win->data[0][l] * input->data[0][l]; |
---|
[650e39b] | 99 | } |
---|
[8b2dc90] | 100 | aubio_fft_do(p->fft,p->winput,p->fftout); |
---|
[650e39b] | 101 | for (l=0; l < p->fftout->length; l++){ |
---|
[10cf306] | 102 | p->sqrmag->data[0][l] = SQR(p->fftout->norm[0][l]); |
---|
| 103 | p->sqrmag->data[0][l] *= p->weight->data[0][l]; |
---|
[283d93f] | 104 | } |
---|
| 105 | for (l=1; l < p->fftout->length; l++){ |
---|
[10cf306] | 106 | p->sqrmag->data[0][(p->fftout->length-1)*2-l] = |
---|
| 107 | SQR(p->fftout->norm[0][l]); |
---|
| 108 | p->sqrmag->data[0][(p->fftout->length-1)*2-l] *= |
---|
| 109 | p->weight->data[0][l]; |
---|
[650e39b] | 110 | } |
---|
| 111 | for (l=0; l < p->sqrmag->length/2+1; l++) { |
---|
[10cf306] | 112 | sum += p->sqrmag->data[0][l]; |
---|
[650e39b] | 113 | } |
---|
| 114 | sum *= 2.; |
---|
[8b2dc90] | 115 | aubio_fft_do(p->fft,p->sqrmag,res); |
---|
[10cf306] | 116 | yin->data[0][0] = 1.; |
---|
[650e39b] | 117 | for (tau=1; tau < yin->length; tau++) { |
---|
[10cf306] | 118 | yin->data[0][tau] = sum - |
---|
| 119 | res->norm[0][tau]*COS(res->phas[0][tau]); |
---|
| 120 | tmp += yin->data[0][tau]; |
---|
| 121 | yin->data[0][tau] *= tau/tmp; |
---|
[650e39b] | 122 | } |
---|
[10cf306] | 123 | tau = vec_min_elem(yin); |
---|
[650e39b] | 124 | if (yin->data[0][tau] < tol) { |
---|
[10cf306] | 125 | /* no interpolation */ |
---|
| 126 | //return tau; |
---|
| 127 | /* 3 point quadratic interpolation */ |
---|
| 128 | //return vec_quadint_min(yin,tau,1); |
---|
| 129 | /* additional check for (unlikely) octave doubling in higher frequencies */ |
---|
| 130 | if (tau>35) { |
---|
[c5c0c98] | 131 | return vec_quadint(yin,tau,1); |
---|
[10cf306] | 132 | } else { |
---|
| 133 | /* should compare the minimum value of each interpolated peaks */ |
---|
| 134 | halfperiod = FLOOR(tau/2+.5); |
---|
| 135 | if (yin->data[0][halfperiod] < tol) |
---|
[c5c0c98] | 136 | return vec_quadint(yin,halfperiod,1); |
---|
[10cf306] | 137 | else |
---|
[c5c0c98] | 138 | return vec_quadint(yin,tau,1); |
---|
[10cf306] | 139 | } |
---|
[650e39b] | 140 | } else |
---|
[15b97d9] | 141 | return 0.; |
---|
[650e39b] | 142 | } |
---|
| 143 | |
---|
| 144 | void del_aubio_pitchyinfft(aubio_pitchyinfft_t *p){ |
---|
[10cf306] | 145 | del_fvec(p->win); |
---|
| 146 | del_aubio_fft(p->fft); |
---|
| 147 | del_fvec(p->yinfft); |
---|
| 148 | del_fvec(p->sqrmag); |
---|
| 149 | del_cvec(p->res); |
---|
| 150 | del_cvec(p->fftout); |
---|
| 151 | del_fvec(p->winput); |
---|
| 152 | del_fvec(p->weight); |
---|
| 153 | AUBIO_FREE(p); |
---|
[650e39b] | 154 | } |
---|