[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 | #include "aubio_priv.h" |
---|
[6c7d49b] | 21 | #include "fvec.h" |
---|
| 22 | #include "cvec.h" |
---|
[7a04950] | 23 | #include "mathutils.h" |
---|
[83963b3] | 24 | #include "musicutils.h" |
---|
[32d6958] | 25 | #include "spectral/fft.h" |
---|
[2d8cffa] | 26 | #include "pitch/pitchfcomb.h" |
---|
[7a04950] | 27 | |
---|
| 28 | #define MAX_PEAKS 8 |
---|
| 29 | |
---|
| 30 | typedef struct { |
---|
[8040cca] | 31 | smpl_t bin; |
---|
[7a04950] | 32 | smpl_t db; |
---|
| 33 | } aubio_fpeak_t; |
---|
| 34 | |
---|
| 35 | struct _aubio_pitchfcomb_t { |
---|
[c8cbf3c] | 36 | uint_t fftSize; |
---|
| 37 | uint_t stepSize; |
---|
| 38 | uint_t rate; |
---|
| 39 | fvec_t * winput; |
---|
| 40 | fvec_t * win; |
---|
| 41 | cvec_t * fftOut; |
---|
| 42 | fvec_t * fftLastPhase; |
---|
| 43 | aubio_fft_t * fft; |
---|
[7a04950] | 44 | }; |
---|
| 45 | |
---|
[8040cca] | 46 | aubio_pitchfcomb_t * new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize, uint_t channels) |
---|
[7a04950] | 47 | { |
---|
| 48 | aubio_pitchfcomb_t * p = AUBIO_NEW(aubio_pitchfcomb_t); |
---|
[fbd3de6] | 49 | p->fftSize = bufsize; |
---|
[c8cbf3c] | 50 | p->stepSize = hopsize; |
---|
[fbd3de6] | 51 | p->winput = new_fvec(bufsize,1); |
---|
| 52 | p->fftOut = new_cvec(bufsize,1); |
---|
[8040cca] | 53 | p->fftLastPhase = new_fvec(bufsize, channels); |
---|
[8b2dc90] | 54 | p->fft = new_aubio_fft(bufsize, 1); |
---|
[407bba9] | 55 | p->win = new_aubio_window("hanning", bufsize); |
---|
[7a04950] | 56 | return p; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /* input must be stepsize long */ |
---|
[8040cca] | 60 | void aubio_pitchfcomb_do (aubio_pitchfcomb_t * p, fvec_t * input, fvec_t * output) |
---|
[7a04950] | 61 | { |
---|
[8040cca] | 62 | uint_t i, k, l, maxharm = 0; |
---|
| 63 | smpl_t phaseDifference = TWO_PI*(smpl_t)p->stepSize/(smpl_t)p->fftSize; |
---|
[7a04950] | 64 | aubio_fpeak_t peaks[MAX_PEAKS]; |
---|
| 65 | |
---|
[8040cca] | 66 | for (i = 0; i < input->channels; i++) { |
---|
| 67 | |
---|
[7a04950] | 68 | for (k=0; k<MAX_PEAKS; k++) { |
---|
| 69 | peaks[k].db = -200.; |
---|
[8040cca] | 70 | peaks[k].bin = 0.; |
---|
[7a04950] | 71 | } |
---|
| 72 | |
---|
[fbd3de6] | 73 | for (k=0; k < input->length; k++){ |
---|
[8040cca] | 74 | p->winput->data[0][k] = p->win->data[0][k] * input->data[i][k]; |
---|
[fbd3de6] | 75 | } |
---|
[8b2dc90] | 76 | aubio_fft_do(p->fft,p->winput,p->fftOut); |
---|
[7a04950] | 77 | |
---|
[fbd3de6] | 78 | for (k=0; k<=p->fftSize/2; k++) { |
---|
[7a04950] | 79 | smpl_t |
---|
| 80 | magnitude = 20.*LOG10(2.*p->fftOut->norm[0][k]/(smpl_t)p->fftSize), |
---|
| 81 | phase = p->fftOut->phas[0][k], |
---|
[8040cca] | 82 | tmp, bin; |
---|
[7a04950] | 83 | |
---|
| 84 | /* compute phase difference */ |
---|
[8040cca] | 85 | tmp = phase - p->fftLastPhase->data[i][k]; |
---|
| 86 | p->fftLastPhase->data[i][k] = phase; |
---|
[7a04950] | 87 | |
---|
| 88 | /* subtract expected phase difference */ |
---|
| 89 | tmp -= (smpl_t)k*phaseDifference; |
---|
| 90 | |
---|
| 91 | /* map delta phase into +/- Pi interval */ |
---|
[1d4fc4a] | 92 | tmp = aubio_unwrap2pi(tmp); |
---|
[7a04950] | 93 | |
---|
| 94 | /* get deviation from bin frequency from the +/- Pi interval */ |
---|
[1d4fc4a] | 95 | tmp = p->fftSize/(smpl_t)p->stepSize*tmp/(TWO_PI); |
---|
[7a04950] | 96 | |
---|
[8040cca] | 97 | /* compute the k-th partials' true bin */ |
---|
| 98 | bin = (smpl_t)k + tmp; |
---|
[7a04950] | 99 | |
---|
[8040cca] | 100 | if (bin > 0.0 && magnitude > peaks[0].db) { // && magnitude < 0) { |
---|
[7a04950] | 101 | memmove(peaks+1, peaks, sizeof(aubio_fpeak_t)*(MAX_PEAKS-1)); |
---|
[8040cca] | 102 | peaks[0].bin = bin; |
---|
[7a04950] | 103 | peaks[0].db = magnitude; |
---|
| 104 | } |
---|
| 105 | } |
---|
[c8cbf3c] | 106 | |
---|
[7a04950] | 107 | k = 0; |
---|
[8040cca] | 108 | for (l=1; l<MAX_PEAKS && peaks[l].bin > 0.0; l++) { |
---|
[7a04950] | 109 | sint_t harmonic; |
---|
| 110 | for (harmonic=5; harmonic>1; harmonic--) { |
---|
[8040cca] | 111 | if (peaks[0].bin / peaks[l].bin < harmonic+.02 && |
---|
| 112 | peaks[0].bin / peaks[l].bin > harmonic-.02) { |
---|
[b33e355] | 113 | if (harmonic > (sint_t)maxharm && |
---|
[c8cbf3c] | 114 | peaks[0].db < peaks[l].db/2) { |
---|
[7a04950] | 115 | maxharm = harmonic; |
---|
[c8cbf3c] | 116 | k = l; |
---|
[7a04950] | 117 | } |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | } |
---|
[8040cca] | 121 | output->data[i][0] = peaks[k].bin; |
---|
[8d84c0b] | 122 | /* quick hack to clean output a bit */ |
---|
[8040cca] | 123 | if (peaks[k].bin > 5000.) output->data[i][0] = 0.; |
---|
| 124 | } |
---|
[7a04950] | 125 | } |
---|
| 126 | |
---|
| 127 | void del_aubio_pitchfcomb (aubio_pitchfcomb_t * p) |
---|
| 128 | { |
---|
| 129 | del_cvec(p->fftOut); |
---|
| 130 | del_fvec(p->fftLastPhase); |
---|
[028c8e9] | 131 | del_fvec(p->win); |
---|
| 132 | del_fvec(p->winput); |
---|
[8b2dc90] | 133 | del_aubio_fft(p->fft); |
---|
[7a04950] | 134 | AUBIO_FREE(p); |
---|
| 135 | } |
---|
| 136 | |
---|