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