[7a04950] | 1 | /* |
---|
| 2 | Copyright (C) 2004, 2005 Mario Lang <mlang@delysid.org> |
---|
| 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 | |
---|
| 20 | /* |
---|
| 21 | |
---|
| 22 | This file was taken from the tuneit project, in the file |
---|
| 23 | tuneit.c -- Detect fundamental frequency of a sound |
---|
| 24 | see http://delysid.org/tuneit.html |
---|
| 25 | |
---|
| 26 | a fast harmonic comb filter algorithm for pitch tracking |
---|
| 27 | |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | #include "aubio_priv.h" |
---|
| 31 | #include "sample.h" |
---|
| 32 | #include "mathutils.h" |
---|
[fbd3de6] | 33 | #include "fft.h" |
---|
[7a04950] | 34 | #include "pitchfcomb.h" |
---|
| 35 | |
---|
| 36 | #define MAX_PEAKS 8 |
---|
| 37 | |
---|
| 38 | typedef struct { |
---|
| 39 | smpl_t freq; |
---|
| 40 | smpl_t db; |
---|
| 41 | } aubio_fpeak_t; |
---|
| 42 | |
---|
| 43 | struct _aubio_pitchfcomb_t { |
---|
| 44 | uint_t fftSize; |
---|
[fbd3de6] | 45 | uint_t stepSize; |
---|
[7a04950] | 46 | uint_t rate; |
---|
[fbd3de6] | 47 | fvec_t * winput; |
---|
| 48 | fvec_t * win; |
---|
[7a04950] | 49 | cvec_t * fftOut; |
---|
| 50 | fvec_t * fftLastPhase; |
---|
[fbd3de6] | 51 | aubio_mfft_t * fft; |
---|
| 52 | //aubio_pvoc_t * pvoc; |
---|
[7a04950] | 53 | }; |
---|
| 54 | |
---|
[fbd3de6] | 55 | aubio_pitchfcomb_t * new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize, uint_t samplerate) |
---|
[7a04950] | 56 | { |
---|
| 57 | aubio_pitchfcomb_t * p = AUBIO_NEW(aubio_pitchfcomb_t); |
---|
| 58 | p->rate = samplerate; |
---|
[fbd3de6] | 59 | p->fftSize = bufsize; |
---|
| 60 | p->stepSize = hopsize; |
---|
| 61 | p->winput = new_fvec(bufsize,1); |
---|
| 62 | p->fftOut = new_cvec(bufsize,1); |
---|
| 63 | p->fftLastPhase = new_fvec(bufsize,1); |
---|
| 64 | p->fft = new_aubio_mfft(bufsize, 1); |
---|
| 65 | p->win = new_fvec(bufsize,1); |
---|
| 66 | aubio_window(p->win->data[0], bufsize, aubio_win_hanning); |
---|
[7a04950] | 67 | return p; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | /* input must be stepsize long */ |
---|
| 71 | smpl_t aubio_pitchfcomb_detect (aubio_pitchfcomb_t * p, fvec_t * input) |
---|
| 72 | { |
---|
[fbd3de6] | 73 | uint_t k, l, maxharm = 0; |
---|
[7a04950] | 74 | smpl_t freqPerBin = p->rate/(smpl_t)p->fftSize, |
---|
[fbd3de6] | 75 | phaseDifference = TWO_PI*(smpl_t)p->stepSize/(smpl_t)p->fftSize; |
---|
[7a04950] | 76 | aubio_fpeak_t peaks[MAX_PEAKS]; |
---|
| 77 | |
---|
| 78 | for (k=0; k<MAX_PEAKS; k++) { |
---|
| 79 | peaks[k].db = -200.; |
---|
| 80 | peaks[k].freq = 0.; |
---|
| 81 | } |
---|
| 82 | |
---|
[fbd3de6] | 83 | for (k=0; k < input->length; k++){ |
---|
| 84 | p->winput->data[0][k] = p->win->data[0][k] * input->data[0][k]; |
---|
| 85 | } |
---|
[1d4fc4a] | 86 | aubio_mfft_do(p->fft,p->winput,p->fftOut); |
---|
[7a04950] | 87 | |
---|
[fbd3de6] | 88 | for (k=0; k<=p->fftSize/2; k++) { |
---|
[7a04950] | 89 | smpl_t |
---|
| 90 | magnitude = 20.*LOG10(2.*p->fftOut->norm[0][k]/(smpl_t)p->fftSize), |
---|
| 91 | phase = p->fftOut->phas[0][k], |
---|
| 92 | tmp, freq; |
---|
| 93 | |
---|
| 94 | /* compute phase difference */ |
---|
| 95 | tmp = phase - p->fftLastPhase->data[0][k]; |
---|
| 96 | p->fftLastPhase->data[0][k] = phase; |
---|
| 97 | |
---|
| 98 | /* subtract expected phase difference */ |
---|
| 99 | tmp -= (smpl_t)k*phaseDifference; |
---|
| 100 | |
---|
| 101 | /* map delta phase into +/- Pi interval */ |
---|
[1d4fc4a] | 102 | tmp = aubio_unwrap2pi(tmp); |
---|
[7a04950] | 103 | |
---|
| 104 | /* get deviation from bin frequency from the +/- Pi interval */ |
---|
[1d4fc4a] | 105 | tmp = p->fftSize/(smpl_t)p->stepSize*tmp/(TWO_PI); |
---|
[7a04950] | 106 | |
---|
| 107 | /* compute the k-th partials' true frequency */ |
---|
| 108 | freq = (smpl_t)k*freqPerBin + tmp*freqPerBin; |
---|
| 109 | |
---|
[fbd3de6] | 110 | if (freq > 0.0 && magnitude > peaks[0].db) { // && magnitude < 0) { |
---|
[7a04950] | 111 | memmove(peaks+1, peaks, sizeof(aubio_fpeak_t)*(MAX_PEAKS-1)); |
---|
| 112 | peaks[0].freq = freq; |
---|
| 113 | peaks[0].db = magnitude; |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | k = 0; |
---|
| 118 | for (l=1; l<MAX_PEAKS && peaks[l].freq > 0.0; l++) { |
---|
| 119 | sint_t harmonic; |
---|
| 120 | for (harmonic=5; harmonic>1; harmonic--) { |
---|
| 121 | if (peaks[0].freq / peaks[l].freq < harmonic+.02 && |
---|
| 122 | peaks[0].freq / peaks[l].freq > harmonic-.02) { |
---|
| 123 | if (harmonic > maxharm && |
---|
| 124 | peaks[0].db < peaks[l].db/2) { |
---|
| 125 | maxharm = harmonic; |
---|
| 126 | k = l; |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | } |
---|
[8d84c0b] | 131 | /* quick hack to clean output a bit */ |
---|
[f97445c] | 132 | if (peaks[k].freq > 5000.) return 0.; |
---|
[7a04950] | 133 | return peaks[k].freq; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | void del_aubio_pitchfcomb (aubio_pitchfcomb_t * p) |
---|
| 137 | { |
---|
| 138 | del_cvec(p->fftOut); |
---|
| 139 | del_fvec(p->fftLastPhase); |
---|
[fbd3de6] | 140 | del_aubio_mfft(p->fft); |
---|
[7a04950] | 141 | AUBIO_FREE(p); |
---|
| 142 | } |
---|
| 143 | |
---|