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" |
---|
20 | #include "fvec.h" |
---|
21 | #include "cvec.h" |
---|
22 | #include "lvec.h" |
---|
23 | #include "spectral/phasevoc.h" |
---|
24 | #include "mathutils.h" |
---|
25 | #include "temporal/filter.h" |
---|
26 | #include "temporal/c_weighting.h" |
---|
27 | #include "pitch/pitchmcomb.h" |
---|
28 | #include "pitch/pitchyin.h" |
---|
29 | #include "pitch/pitchfcomb.h" |
---|
30 | #include "pitch/pitchschmitt.h" |
---|
31 | #include "pitch/pitchyinfft.h" |
---|
32 | #include "pitch/pitch.h" |
---|
33 | |
---|
34 | /** pitch detection algorithm */ |
---|
35 | typedef enum { |
---|
36 | aubio_pitcht_yin, /**< YIN algorithm */ |
---|
37 | aubio_pitcht_mcomb, /**< Multi-comb filter */ |
---|
38 | aubio_pitcht_schmitt, /**< Schmitt trigger */ |
---|
39 | aubio_pitcht_fcomb, /**< Fast comb filter */ |
---|
40 | aubio_pitcht_yinfft, /**< Spectral YIN */ |
---|
41 | aubio_pitcht_default = aubio_pitcht_yinfft, /**< the one used when "default" is asked */ |
---|
42 | } aubio_pitch_type; |
---|
43 | |
---|
44 | /** pitch detection output mode */ |
---|
45 | typedef enum { |
---|
46 | aubio_pitchm_freq, /**< Frequency (Hz) */ |
---|
47 | aubio_pitchm_midi, /**< MIDI note (0.,127) */ |
---|
48 | aubio_pitchm_cent, /**< Cent */ |
---|
49 | aubio_pitchm_bin, /**< Frequency bin (0,bufsize) */ |
---|
50 | aubio_pitchm_default = aubio_pitchm_freq, /**< the one used when "default" is asked */ |
---|
51 | } aubio_pitch_mode; |
---|
52 | |
---|
53 | typedef void (*aubio_pitch_func_t) |
---|
54 | (aubio_pitch_t *p, fvec_t * ibuf, fvec_t *obuf); |
---|
55 | typedef smpl_t (*aubio_pitch_conv_t) |
---|
56 | (smpl_t value, uint_t srate, uint_t bufsize); |
---|
57 | |
---|
58 | void aubio_pitch_slideblock(aubio_pitch_t *p, fvec_t *ibuf); |
---|
59 | |
---|
60 | void aubio_pitch_do_mcomb (aubio_pitch_t *p, fvec_t *ibuf, fvec_t *obuf); |
---|
61 | void aubio_pitch_do_yin (aubio_pitch_t *p, fvec_t *ibuf, fvec_t *obuf); |
---|
62 | void aubio_pitch_do_schmitt (aubio_pitch_t *p, fvec_t *ibuf, fvec_t *obuf); |
---|
63 | void aubio_pitch_do_fcomb (aubio_pitch_t *p, fvec_t *ibuf, fvec_t *obuf); |
---|
64 | void aubio_pitch_do_yinfft (aubio_pitch_t *p, fvec_t *ibuf, fvec_t *obuf); |
---|
65 | |
---|
66 | /** generic pitch detection structure */ |
---|
67 | struct _aubio_pitch_t { |
---|
68 | aubio_pitch_type type; /**< pitch detection mode */ |
---|
69 | aubio_pitch_mode mode; /**< pitch detection output mode */ |
---|
70 | uint_t srate; /**< samplerate */ |
---|
71 | uint_t bufsize; /**< buffer size */ |
---|
72 | aubio_pitchmcomb_t * mcomb; /**< mcomb object */ |
---|
73 | aubio_pitchfcomb_t * fcomb; /**< fcomb object */ |
---|
74 | aubio_pitchschmitt_t * schmitt; /**< schmitt object */ |
---|
75 | aubio_pitchyinfft_t * yinfft; /**< yinfft object */ |
---|
76 | aubio_pitchyin_t * yin; /**< yinfft object */ |
---|
77 | aubio_filter_t * filter; /**< filter */ |
---|
78 | aubio_pvoc_t * pv; /**< phase vocoder for mcomb */ |
---|
79 | cvec_t * fftgrain; /**< spectral frame for mcomb */ |
---|
80 | fvec_t * buf; /**< temporary buffer for yin */ |
---|
81 | aubio_pitch_func_t callback; /**< pointer to current pitch detection method */ |
---|
82 | aubio_pitch_conv_t freqconv; /**< pointer to current pitch conversion method */ |
---|
83 | }; |
---|
84 | |
---|
85 | /* convenience wrapper function for frequency unit conversions |
---|
86 | * should probably be rewritten with #defines */ |
---|
87 | smpl_t freqconvbin(smpl_t f,uint_t srate,uint_t bufsize); |
---|
88 | smpl_t freqconvbin(smpl_t f,uint_t srate,uint_t bufsize){ |
---|
89 | return aubio_freqtobin(f,srate,bufsize); |
---|
90 | } |
---|
91 | |
---|
92 | smpl_t freqconvmidi(smpl_t f,uint_t srate,uint_t bufsize); |
---|
93 | smpl_t freqconvmidi(smpl_t f,uint_t srate UNUSED,uint_t bufsize UNUSED){ |
---|
94 | return aubio_freqtomidi(f); |
---|
95 | } |
---|
96 | |
---|
97 | smpl_t freqconvpass(smpl_t f,uint_t srate,uint_t bufsize); |
---|
98 | smpl_t freqconvpass(smpl_t f,uint_t srate UNUSED,uint_t bufsize UNUSED){ |
---|
99 | return f; |
---|
100 | } |
---|
101 | |
---|
102 | aubio_pitch_t * |
---|
103 | new_aubio_pitch (char_t * pitch_mode, |
---|
104 | uint_t bufsize, uint_t hopsize, uint_t channels, uint_t samplerate) |
---|
105 | { |
---|
106 | aubio_pitch_t *p = AUBIO_NEW(aubio_pitch_t); |
---|
107 | aubio_pitch_type pitch_type; |
---|
108 | if (strcmp (pitch_mode, "mcomb") == 0) |
---|
109 | pitch_type = aubio_pitcht_mcomb; |
---|
110 | else if (strcmp (pitch_mode, "yinfft") == 0) |
---|
111 | pitch_type = aubio_pitcht_yin; |
---|
112 | else if (strcmp (pitch_mode, "yin") == 0) |
---|
113 | pitch_type = aubio_pitcht_yin; |
---|
114 | else if (strcmp (pitch_mode, "schmitt") == 0) |
---|
115 | pitch_type = aubio_pitcht_schmitt; |
---|
116 | else if (strcmp (pitch_mode, "fcomb") == 0) |
---|
117 | pitch_type = aubio_pitcht_fcomb; |
---|
118 | else if (strcmp (pitch_mode, "default") == 0) |
---|
119 | pitch_type = aubio_pitcht_default; |
---|
120 | else { |
---|
121 | AUBIO_ERR ("unknown pitch detection method %s, using default.\n", pitch_mode); |
---|
122 | pitch_type = aubio_pitcht_default; |
---|
123 | return NULL; |
---|
124 | } |
---|
125 | p->srate = samplerate; |
---|
126 | p->type = pitch_type; |
---|
127 | aubio_pitch_set_unit (p, "default"); |
---|
128 | p->bufsize = bufsize; |
---|
129 | switch(p->type) { |
---|
130 | case aubio_pitcht_yin: |
---|
131 | p->buf = new_fvec(bufsize,channels); |
---|
132 | p->yin = new_aubio_pitchyin(bufsize); |
---|
133 | p->callback = aubio_pitch_do_yin; |
---|
134 | aubio_pitchyin_set_tolerance (p->yin, 0.15); |
---|
135 | break; |
---|
136 | case aubio_pitcht_mcomb: |
---|
137 | p->pv = new_aubio_pvoc(bufsize, hopsize, channels); |
---|
138 | p->fftgrain = new_cvec(bufsize, channels); |
---|
139 | p->mcomb = new_aubio_pitchmcomb(bufsize,hopsize,channels); |
---|
140 | p->filter = new_aubio_filter_c_weighting (samplerate, channels); |
---|
141 | p->callback = aubio_pitch_do_mcomb; |
---|
142 | break; |
---|
143 | case aubio_pitcht_fcomb: |
---|
144 | p->buf = new_fvec(bufsize,channels); |
---|
145 | p->fcomb = new_aubio_pitchfcomb(bufsize,hopsize,channels); |
---|
146 | p->callback = aubio_pitch_do_fcomb; |
---|
147 | break; |
---|
148 | case aubio_pitcht_schmitt: |
---|
149 | p->buf = new_fvec(bufsize,channels); |
---|
150 | p->schmitt = new_aubio_pitchschmitt(bufsize); |
---|
151 | p->callback = aubio_pitch_do_schmitt; |
---|
152 | break; |
---|
153 | case aubio_pitcht_yinfft: |
---|
154 | p->buf = new_fvec(bufsize,channels); |
---|
155 | p->yinfft = new_aubio_pitchyinfft(bufsize); |
---|
156 | p->callback = aubio_pitch_do_yinfft; |
---|
157 | aubio_pitchyinfft_set_tolerance (p->yinfft, 0.85); |
---|
158 | break; |
---|
159 | default: |
---|
160 | break; |
---|
161 | } |
---|
162 | return p; |
---|
163 | } |
---|
164 | |
---|
165 | void del_aubio_pitch(aubio_pitch_t * p) { |
---|
166 | switch(p->type) { |
---|
167 | case aubio_pitcht_yin: |
---|
168 | del_fvec(p->buf); |
---|
169 | del_aubio_pitchyin(p->yin); |
---|
170 | break; |
---|
171 | case aubio_pitcht_mcomb: |
---|
172 | del_aubio_pvoc(p->pv); |
---|
173 | del_cvec(p->fftgrain); |
---|
174 | del_aubio_filter(p->filter); |
---|
175 | del_aubio_pitchmcomb(p->mcomb); |
---|
176 | break; |
---|
177 | case aubio_pitcht_schmitt: |
---|
178 | del_fvec(p->buf); |
---|
179 | del_aubio_pitchschmitt(p->schmitt); |
---|
180 | break; |
---|
181 | case aubio_pitcht_fcomb: |
---|
182 | del_fvec(p->buf); |
---|
183 | del_aubio_pitchfcomb(p->fcomb); |
---|
184 | break; |
---|
185 | case aubio_pitcht_yinfft: |
---|
186 | del_fvec(p->buf); |
---|
187 | del_aubio_pitchyinfft(p->yinfft); |
---|
188 | break; |
---|
189 | default: |
---|
190 | break; |
---|
191 | } |
---|
192 | AUBIO_FREE(p); |
---|
193 | } |
---|
194 | |
---|
195 | void aubio_pitch_slideblock(aubio_pitch_t *p, fvec_t *ibuf){ |
---|
196 | uint_t i,j = 0, overlap_size = 0; |
---|
197 | overlap_size = p->buf->length-ibuf->length; |
---|
198 | for (i=0;i<p->buf->channels;i++){ |
---|
199 | for (j=0;j<overlap_size;j++){ |
---|
200 | p->buf->data[i][j] = p->buf->data[i][j+ibuf->length]; |
---|
201 | } |
---|
202 | } |
---|
203 | for (i=0;i<ibuf->channels;i++){ |
---|
204 | for (j=0;j<ibuf->length;j++){ |
---|
205 | p->buf->data[i][j+overlap_size] = ibuf->data[i][j]; |
---|
206 | } |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | uint_t aubio_pitch_set_unit (aubio_pitch_t *p, char_t * pitch_unit) { |
---|
211 | aubio_pitch_mode pitch_mode; |
---|
212 | if (strcmp (pitch_unit, "freq") == 0) |
---|
213 | pitch_mode = aubio_pitchm_freq; |
---|
214 | else if (strcmp (pitch_unit, "midi") == 0) |
---|
215 | pitch_mode = aubio_pitchm_midi; |
---|
216 | else if (strcmp (pitch_unit, "cent") == 0) |
---|
217 | pitch_mode = aubio_pitchm_cent; |
---|
218 | else if (strcmp (pitch_unit, "bin") == 0) |
---|
219 | pitch_mode = aubio_pitchm_bin; |
---|
220 | else if (strcmp (pitch_unit, "default") == 0) |
---|
221 | pitch_mode = aubio_pitchm_default; |
---|
222 | else { |
---|
223 | AUBIO_ERR ("unknown pitch detection unit %s, using default\n", pitch_unit); |
---|
224 | pitch_mode = aubio_pitchm_default; |
---|
225 | } |
---|
226 | p->mode = pitch_mode; |
---|
227 | switch(p->mode) { |
---|
228 | case aubio_pitchm_freq: |
---|
229 | p->freqconv = freqconvpass; |
---|
230 | break; |
---|
231 | case aubio_pitchm_midi: |
---|
232 | p->freqconv = freqconvmidi; |
---|
233 | break; |
---|
234 | case aubio_pitchm_cent: |
---|
235 | /* bug: not implemented */ |
---|
236 | p->freqconv = freqconvmidi; |
---|
237 | break; |
---|
238 | case aubio_pitchm_bin: |
---|
239 | p->freqconv = freqconvbin; |
---|
240 | break; |
---|
241 | default: |
---|
242 | break; |
---|
243 | } |
---|
244 | return AUBIO_OK; |
---|
245 | } |
---|
246 | |
---|
247 | uint_t aubio_pitch_set_tolerance(aubio_pitch_t *p, smpl_t tol) { |
---|
248 | switch(p->type) { |
---|
249 | case aubio_pitcht_yin: |
---|
250 | aubio_pitchyin_set_tolerance (p->yin, tol); |
---|
251 | break; |
---|
252 | case aubio_pitcht_yinfft: |
---|
253 | aubio_pitchyinfft_set_tolerance (p->yinfft, tol); |
---|
254 | break; |
---|
255 | default: |
---|
256 | break; |
---|
257 | } |
---|
258 | return AUBIO_OK; |
---|
259 | } |
---|
260 | |
---|
261 | void aubio_pitch_do (aubio_pitch_t *p, fvec_t * ibuf, fvec_t *obuf) { |
---|
262 | uint_t i; |
---|
263 | p->callback(p, ibuf, obuf); |
---|
264 | for (i = 0; i < obuf->channels; i++) { |
---|
265 | p->freqconv(obuf->data[i][0],p->srate,p->bufsize); |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | void aubio_pitch_do_mcomb(aubio_pitch_t *p, fvec_t *ibuf, fvec_t * obuf) { |
---|
270 | uint_t i; |
---|
271 | aubio_filter_do(p->filter,ibuf); |
---|
272 | aubio_pvoc_do(p->pv,ibuf,p->fftgrain); |
---|
273 | aubio_pitchmcomb_do(p->mcomb,p->fftgrain, obuf); |
---|
274 | for (i = 0; i < obuf->channels; i++) { |
---|
275 | obuf->data[i][0] = aubio_bintofreq (obuf->data[i][0], p->srate, p->bufsize); |
---|
276 | } |
---|
277 | } |
---|
278 | |
---|
279 | void aubio_pitch_do_yin(aubio_pitch_t *p, fvec_t *ibuf, fvec_t * obuf) { |
---|
280 | smpl_t pitch = 0.; |
---|
281 | uint_t i; |
---|
282 | aubio_pitch_slideblock(p,ibuf); |
---|
283 | aubio_pitchyin_do(p->yin,p->buf, obuf); |
---|
284 | for (i = 0; i < obuf->channels; i++) { |
---|
285 | pitch = obuf->data[i][0]; |
---|
286 | if (pitch>0) { |
---|
287 | pitch = p->srate/(pitch+0.); |
---|
288 | } else { |
---|
289 | pitch = 0.; |
---|
290 | } |
---|
291 | obuf->data[i][0] = pitch; |
---|
292 | } |
---|
293 | } |
---|
294 | |
---|
295 | |
---|
296 | void aubio_pitch_do_yinfft(aubio_pitch_t *p, fvec_t *ibuf, fvec_t * obuf){ |
---|
297 | smpl_t pitch = 0.; |
---|
298 | uint_t i; |
---|
299 | aubio_pitch_slideblock(p,ibuf); |
---|
300 | aubio_pitchyinfft_do(p->yinfft,p->buf,obuf); |
---|
301 | for (i = 0; i < obuf->channels; i++) { |
---|
302 | pitch = obuf->data[i][0]; |
---|
303 | if (pitch>0) { |
---|
304 | pitch = p->srate/(pitch+0.); |
---|
305 | } else { |
---|
306 | pitch = 0.; |
---|
307 | } |
---|
308 | obuf->data[i][0] = pitch; |
---|
309 | } |
---|
310 | } |
---|
311 | |
---|
312 | void aubio_pitch_do_fcomb(aubio_pitch_t *p, fvec_t *ibuf, fvec_t * out){ |
---|
313 | uint_t i; |
---|
314 | aubio_pitch_slideblock(p,ibuf); |
---|
315 | aubio_pitchfcomb_do(p->fcomb,p->buf, out); |
---|
316 | for (i = 0; i < out->channels; i++) { |
---|
317 | out->data[i][0] = aubio_bintofreq (out->data[i][0], p->srate, p->bufsize); |
---|
318 | } |
---|
319 | } |
---|
320 | |
---|
321 | void aubio_pitch_do_schmitt(aubio_pitch_t *p, fvec_t *ibuf, fvec_t *out){ |
---|
322 | smpl_t period, pitch = 0.; |
---|
323 | uint_t i; |
---|
324 | aubio_pitch_slideblock(p,ibuf); |
---|
325 | aubio_pitchschmitt_do(p->schmitt,p->buf, out); |
---|
326 | for (i = 0; i < out->channels; i++) { |
---|
327 | period = out->data[i][0]; |
---|
328 | if (period>0) { |
---|
329 | pitch = p->srate/period; |
---|
330 | } else { |
---|
331 | pitch = 0.; |
---|
332 | } |
---|
333 | out->data[i][0] = pitch; |
---|
334 | } |
---|
335 | } |
---|