1 | /* |
---|
2 | Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org> |
---|
3 | |
---|
4 | This file is part of aubio. |
---|
5 | |
---|
6 | aubio is free software: you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation, either version 3 of the License, or |
---|
9 | (at your option) any later version. |
---|
10 | |
---|
11 | aubio is distributed in the hope that it will be useful, |
---|
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | GNU General Public License for more details. |
---|
15 | |
---|
16 | You should have received a copy of the GNU General Public License |
---|
17 | along with aubio. If not, see <http://www.gnu.org/licenses/>. |
---|
18 | |
---|
19 | */ |
---|
20 | |
---|
21 | #include "aubio_priv.h" |
---|
22 | #include "fvec.h" |
---|
23 | #include "cvec.h" |
---|
24 | #include "lvec.h" |
---|
25 | #include "mathutils.h" |
---|
26 | #include "musicutils.h" |
---|
27 | #include "spectral/phasevoc.h" |
---|
28 | #include "temporal/filter.h" |
---|
29 | #include "temporal/c_weighting.h" |
---|
30 | #include "pitch/pitchmcomb.h" |
---|
31 | #include "pitch/pitchyin.h" |
---|
32 | #include "pitch/pitchfcomb.h" |
---|
33 | #include "pitch/pitchschmitt.h" |
---|
34 | #include "pitch/pitchyinfft.h" |
---|
35 | #include "pitch/pitchspecacf.h" |
---|
36 | #include "pitch/pitch.h" |
---|
37 | |
---|
38 | #define DEFAULT_PITCH_SILENCE -50. |
---|
39 | |
---|
40 | /** pitch detection algorithms */ |
---|
41 | typedef enum |
---|
42 | { |
---|
43 | aubio_pitcht_yin, /**< `yin`, YIN algorithm */ |
---|
44 | aubio_pitcht_mcomb, /**< `mcomb`, Multi-comb filter */ |
---|
45 | aubio_pitcht_schmitt, /**< `schmitt`, Schmitt trigger */ |
---|
46 | aubio_pitcht_fcomb, /**< `fcomb`, Fast comb filter */ |
---|
47 | aubio_pitcht_yinfft, /**< `yinfft`, Spectral YIN */ |
---|
48 | aubio_pitcht_specacf, /**< `specacf`, Spectral autocorrelation */ |
---|
49 | aubio_pitcht_default |
---|
50 | = aubio_pitcht_yinfft, /**< `default` */ |
---|
51 | } aubio_pitch_type; |
---|
52 | |
---|
53 | /** pitch detection output modes */ |
---|
54 | typedef enum |
---|
55 | { |
---|
56 | aubio_pitchm_freq, /**< Frequency (Hz) */ |
---|
57 | aubio_pitchm_midi, /**< MIDI note (0.,127) */ |
---|
58 | aubio_pitchm_cent, /**< Cent */ |
---|
59 | aubio_pitchm_bin, /**< Frequency bin (0,bufsize) */ |
---|
60 | aubio_pitchm_default = aubio_pitchm_freq, /**< the one used when "default" is asked */ |
---|
61 | } aubio_pitch_mode; |
---|
62 | |
---|
63 | /** callback to get pitch candidate, defined below */ |
---|
64 | typedef void (*aubio_pitch_detect_t) (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf); |
---|
65 | |
---|
66 | /** callback to convert pitch from one unit to another, defined below */ |
---|
67 | typedef smpl_t(*aubio_pitch_convert_t) (smpl_t value, uint_t samplerate, uint_t bufsize); |
---|
68 | |
---|
69 | /** callback to fetch the confidence of the algorithm */ |
---|
70 | typedef smpl_t (*aubio_pitch_get_conf_t) (void * p); |
---|
71 | |
---|
72 | /** generic pitch detection structure */ |
---|
73 | struct _aubio_pitch_t |
---|
74 | { |
---|
75 | aubio_pitch_type type; /**< pitch detection mode */ |
---|
76 | aubio_pitch_mode mode; /**< pitch detection output mode */ |
---|
77 | uint_t samplerate; /**< samplerate */ |
---|
78 | uint_t bufsize; /**< buffer size */ |
---|
79 | void *p_object; /**< pointer to pitch object */ |
---|
80 | aubio_filter_t *filter; /**< filter */ |
---|
81 | aubio_pvoc_t *pv; /**< phase vocoder for mcomb */ |
---|
82 | cvec_t *fftgrain; /**< spectral frame for mcomb */ |
---|
83 | fvec_t *buf; /**< temporary buffer for yin */ |
---|
84 | aubio_pitch_detect_t detect_cb; /**< callback to get the pitch candidates */ |
---|
85 | aubio_pitch_convert_t conv_cb; /**< callback to convert it to the desired unit */ |
---|
86 | aubio_pitch_get_conf_t conf_cb; /**< pointer to the current confidence callback */ |
---|
87 | smpl_t silence; /**< silence threshold */ |
---|
88 | }; |
---|
89 | |
---|
90 | /* callback functions for pitch detection */ |
---|
91 | static void aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf); |
---|
92 | static void aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf); |
---|
93 | static void aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf); |
---|
94 | static void aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf); |
---|
95 | static void aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf); |
---|
96 | static void aubio_pitch_do_specacf (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf); |
---|
97 | |
---|
98 | /* conversion functions for frequency conversions */ |
---|
99 | smpl_t freqconvbin (smpl_t f, uint_t samplerate, uint_t bufsize); |
---|
100 | smpl_t freqconvmidi (smpl_t f, uint_t samplerate, uint_t bufsize); |
---|
101 | smpl_t freqconvpass (smpl_t f, uint_t samplerate, uint_t bufsize); |
---|
102 | |
---|
103 | /* adapter to stack ibuf new samples at the end of buf, and trim `buf` to `bufsize` */ |
---|
104 | void aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf); |
---|
105 | |
---|
106 | |
---|
107 | aubio_pitch_t * |
---|
108 | new_aubio_pitch (char_t * pitch_mode, |
---|
109 | uint_t bufsize, uint_t hopsize, uint_t samplerate) |
---|
110 | { |
---|
111 | aubio_pitch_t *p = AUBIO_NEW (aubio_pitch_t); |
---|
112 | aubio_pitch_type pitch_type; |
---|
113 | if (strcmp (pitch_mode, "mcomb") == 0) |
---|
114 | pitch_type = aubio_pitcht_mcomb; |
---|
115 | else if (strcmp (pitch_mode, "yinfft") == 0) |
---|
116 | pitch_type = aubio_pitcht_yinfft; |
---|
117 | else if (strcmp (pitch_mode, "yin") == 0) |
---|
118 | pitch_type = aubio_pitcht_yin; |
---|
119 | else if (strcmp (pitch_mode, "schmitt") == 0) |
---|
120 | pitch_type = aubio_pitcht_schmitt; |
---|
121 | else if (strcmp (pitch_mode, "fcomb") == 0) |
---|
122 | pitch_type = aubio_pitcht_fcomb; |
---|
123 | else if (strcmp (pitch_mode, "specacf") == 0) |
---|
124 | pitch_type = aubio_pitcht_specacf; |
---|
125 | else if (strcmp (pitch_mode, "default") == 0) |
---|
126 | pitch_type = aubio_pitcht_default; |
---|
127 | else { |
---|
128 | AUBIO_ERR ("unknown pitch detection method %s, using default.\n", |
---|
129 | pitch_mode); |
---|
130 | pitch_type = aubio_pitcht_default; |
---|
131 | } |
---|
132 | p->samplerate = samplerate; |
---|
133 | p->type = pitch_type; |
---|
134 | aubio_pitch_set_unit (p, "default"); |
---|
135 | p->bufsize = bufsize; |
---|
136 | p->silence = DEFAULT_PITCH_SILENCE; |
---|
137 | p->conf_cb = NULL; |
---|
138 | switch (p->type) { |
---|
139 | case aubio_pitcht_yin: |
---|
140 | p->buf = new_fvec (bufsize); |
---|
141 | p->p_object = new_aubio_pitchyin (bufsize); |
---|
142 | p->detect_cb = aubio_pitch_do_yin; |
---|
143 | p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyin_get_confidence; |
---|
144 | aubio_pitchyin_set_tolerance (p->p_object, 0.15); |
---|
145 | break; |
---|
146 | case aubio_pitcht_mcomb: |
---|
147 | p->pv = new_aubio_pvoc (bufsize, hopsize); |
---|
148 | p->fftgrain = new_cvec (bufsize); |
---|
149 | p->p_object = new_aubio_pitchmcomb (bufsize, hopsize); |
---|
150 | p->filter = new_aubio_filter_c_weighting (samplerate); |
---|
151 | p->detect_cb = aubio_pitch_do_mcomb; |
---|
152 | break; |
---|
153 | case aubio_pitcht_fcomb: |
---|
154 | p->buf = new_fvec (bufsize); |
---|
155 | p->p_object = new_aubio_pitchfcomb (bufsize, hopsize); |
---|
156 | p->detect_cb = aubio_pitch_do_fcomb; |
---|
157 | break; |
---|
158 | case aubio_pitcht_schmitt: |
---|
159 | p->buf = new_fvec (bufsize); |
---|
160 | p->p_object = new_aubio_pitchschmitt (bufsize); |
---|
161 | p->detect_cb = aubio_pitch_do_schmitt; |
---|
162 | break; |
---|
163 | case aubio_pitcht_yinfft: |
---|
164 | p->buf = new_fvec (bufsize); |
---|
165 | p->p_object = new_aubio_pitchyinfft (samplerate, bufsize); |
---|
166 | p->detect_cb = aubio_pitch_do_yinfft; |
---|
167 | p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyinfft_get_confidence; |
---|
168 | aubio_pitchyinfft_set_tolerance (p->p_object, 0.85); |
---|
169 | break; |
---|
170 | case aubio_pitcht_specacf: |
---|
171 | p->buf = new_fvec (bufsize); |
---|
172 | p->p_object = new_aubio_pitchspecacf (bufsize); |
---|
173 | p->detect_cb = aubio_pitch_do_specacf; |
---|
174 | p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchspecacf_get_tolerance; |
---|
175 | aubio_pitchspecacf_set_tolerance (p->p_object, 0.85); |
---|
176 | break; |
---|
177 | default: |
---|
178 | break; |
---|
179 | } |
---|
180 | return p; |
---|
181 | } |
---|
182 | |
---|
183 | void |
---|
184 | del_aubio_pitch (aubio_pitch_t * p) |
---|
185 | { |
---|
186 | switch (p->type) { |
---|
187 | case aubio_pitcht_yin: |
---|
188 | del_fvec (p->buf); |
---|
189 | del_aubio_pitchyin (p->p_object); |
---|
190 | break; |
---|
191 | case aubio_pitcht_mcomb: |
---|
192 | del_aubio_pvoc (p->pv); |
---|
193 | del_cvec (p->fftgrain); |
---|
194 | del_aubio_filter (p->filter); |
---|
195 | del_aubio_pitchmcomb (p->p_object); |
---|
196 | break; |
---|
197 | case aubio_pitcht_schmitt: |
---|
198 | del_fvec (p->buf); |
---|
199 | del_aubio_pitchschmitt (p->p_object); |
---|
200 | break; |
---|
201 | case aubio_pitcht_fcomb: |
---|
202 | del_fvec (p->buf); |
---|
203 | del_aubio_pitchfcomb (p->p_object); |
---|
204 | break; |
---|
205 | case aubio_pitcht_yinfft: |
---|
206 | del_fvec (p->buf); |
---|
207 | del_aubio_pitchyinfft (p->p_object); |
---|
208 | break; |
---|
209 | case aubio_pitcht_specacf: |
---|
210 | del_fvec (p->buf); |
---|
211 | del_aubio_pitchspecacf (p->p_object); |
---|
212 | break; |
---|
213 | default: |
---|
214 | break; |
---|
215 | } |
---|
216 | AUBIO_FREE (p); |
---|
217 | } |
---|
218 | |
---|
219 | void |
---|
220 | aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf) |
---|
221 | { |
---|
222 | uint_t j = 0, overlap_size = 0; |
---|
223 | overlap_size = p->buf->length - ibuf->length; |
---|
224 | for (j = 0; j < overlap_size; j++) { |
---|
225 | p->buf->data[j] = p->buf->data[j + ibuf->length]; |
---|
226 | } |
---|
227 | for (j = 0; j < ibuf->length; j++) { |
---|
228 | p->buf->data[j + overlap_size] = ibuf->data[j]; |
---|
229 | } |
---|
230 | } |
---|
231 | |
---|
232 | uint_t |
---|
233 | aubio_pitch_set_unit (aubio_pitch_t * p, char_t * pitch_unit) |
---|
234 | { |
---|
235 | uint_t err = AUBIO_OK; |
---|
236 | aubio_pitch_mode pitch_mode; |
---|
237 | if (strcmp (pitch_unit, "freq") == 0) |
---|
238 | pitch_mode = aubio_pitchm_freq; |
---|
239 | else if (strcmp (pitch_unit, "midi") == 0) |
---|
240 | pitch_mode = aubio_pitchm_midi; |
---|
241 | else if (strcmp (pitch_unit, "cent") == 0) |
---|
242 | pitch_mode = aubio_pitchm_cent; |
---|
243 | else if (strcmp (pitch_unit, "bin") == 0) |
---|
244 | pitch_mode = aubio_pitchm_bin; |
---|
245 | else if (strcmp (pitch_unit, "default") == 0) |
---|
246 | pitch_mode = aubio_pitchm_default; |
---|
247 | else { |
---|
248 | AUBIO_ERR ("unknown pitch detection unit %s, using default\n", pitch_unit); |
---|
249 | pitch_mode = aubio_pitchm_default; |
---|
250 | err = AUBIO_FAIL; |
---|
251 | } |
---|
252 | p->mode = pitch_mode; |
---|
253 | switch (p->mode) { |
---|
254 | case aubio_pitchm_freq: |
---|
255 | p->conv_cb = freqconvpass; |
---|
256 | break; |
---|
257 | case aubio_pitchm_midi: |
---|
258 | p->conv_cb = freqconvmidi; |
---|
259 | break; |
---|
260 | case aubio_pitchm_cent: |
---|
261 | /* bug: not implemented */ |
---|
262 | p->conv_cb = freqconvmidi; |
---|
263 | break; |
---|
264 | case aubio_pitchm_bin: |
---|
265 | p->conv_cb = freqconvbin; |
---|
266 | break; |
---|
267 | default: |
---|
268 | break; |
---|
269 | } |
---|
270 | return err; |
---|
271 | } |
---|
272 | |
---|
273 | uint_t |
---|
274 | aubio_pitch_set_tolerance (aubio_pitch_t * p, smpl_t tol) |
---|
275 | { |
---|
276 | switch (p->type) { |
---|
277 | case aubio_pitcht_yin: |
---|
278 | aubio_pitchyin_set_tolerance (p->p_object, tol); |
---|
279 | break; |
---|
280 | case aubio_pitcht_yinfft: |
---|
281 | aubio_pitchyinfft_set_tolerance (p->p_object, tol); |
---|
282 | break; |
---|
283 | default: |
---|
284 | break; |
---|
285 | } |
---|
286 | return AUBIO_OK; |
---|
287 | } |
---|
288 | |
---|
289 | uint_t |
---|
290 | aubio_pitch_set_silence (aubio_pitch_t * p, smpl_t silence) |
---|
291 | { |
---|
292 | if (silence < 0 && silence > -200) { |
---|
293 | p->silence = silence; |
---|
294 | return AUBIO_OK; |
---|
295 | } else { |
---|
296 | AUBIO_ERR("pitch: could do set silence to %.2f", silence); |
---|
297 | return AUBIO_FAIL; |
---|
298 | } |
---|
299 | } |
---|
300 | |
---|
301 | smpl_t |
---|
302 | aubio_pitch_get_silence (aubio_pitch_t * p) |
---|
303 | { |
---|
304 | return p->silence; |
---|
305 | } |
---|
306 | |
---|
307 | |
---|
308 | /* do method, calling the detection callback, then the conversion callback */ |
---|
309 | void |
---|
310 | aubio_pitch_do (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf) |
---|
311 | { |
---|
312 | p->detect_cb (p, ibuf, obuf); |
---|
313 | if (aubio_silence_detection(ibuf, p->silence) == 1) { |
---|
314 | obuf->data[0] = 0.; |
---|
315 | } |
---|
316 | obuf->data[0] = p->conv_cb (obuf->data[0], p->samplerate, p->bufsize); |
---|
317 | } |
---|
318 | |
---|
319 | /* do method for each algorithm */ |
---|
320 | void |
---|
321 | aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf) |
---|
322 | { |
---|
323 | aubio_filter_do (p->filter, ibuf); |
---|
324 | aubio_pvoc_do (p->pv, ibuf, p->fftgrain); |
---|
325 | aubio_pitchmcomb_do (p->p_object, p->fftgrain, obuf); |
---|
326 | obuf->data[0] = aubio_bintofreq (obuf->data[0], p->samplerate, p->bufsize); |
---|
327 | } |
---|
328 | |
---|
329 | void |
---|
330 | aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf) |
---|
331 | { |
---|
332 | smpl_t pitch = 0.; |
---|
333 | aubio_pitch_slideblock (p, ibuf); |
---|
334 | aubio_pitchyin_do (p->p_object, p->buf, obuf); |
---|
335 | pitch = obuf->data[0]; |
---|
336 | if (pitch > 0) { |
---|
337 | pitch = p->samplerate / (pitch + 0.); |
---|
338 | } else { |
---|
339 | pitch = 0.; |
---|
340 | } |
---|
341 | obuf->data[0] = pitch; |
---|
342 | } |
---|
343 | |
---|
344 | |
---|
345 | void |
---|
346 | aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf) |
---|
347 | { |
---|
348 | smpl_t pitch = 0.; |
---|
349 | aubio_pitch_slideblock (p, ibuf); |
---|
350 | aubio_pitchyinfft_do (p->p_object, p->buf, obuf); |
---|
351 | pitch = obuf->data[0]; |
---|
352 | if (pitch > 0) { |
---|
353 | pitch = p->samplerate / (pitch + 0.); |
---|
354 | } else { |
---|
355 | pitch = 0.; |
---|
356 | } |
---|
357 | obuf->data[0] = pitch; |
---|
358 | } |
---|
359 | |
---|
360 | void |
---|
361 | aubio_pitch_do_specacf (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out) |
---|
362 | { |
---|
363 | aubio_pitch_slideblock (p, ibuf); |
---|
364 | aubio_pitchspecacf_do (p->p_object, p->buf, out); |
---|
365 | //out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize); |
---|
366 | smpl_t pitch = 0., period = out->data[0]; |
---|
367 | if (period > 0) { |
---|
368 | pitch = p->samplerate / period; |
---|
369 | } else { |
---|
370 | pitch = 0.; |
---|
371 | } |
---|
372 | out->data[0] = pitch; |
---|
373 | } |
---|
374 | |
---|
375 | void |
---|
376 | aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out) |
---|
377 | { |
---|
378 | aubio_pitch_slideblock (p, ibuf); |
---|
379 | aubio_pitchfcomb_do (p->p_object, p->buf, out); |
---|
380 | out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize); |
---|
381 | } |
---|
382 | |
---|
383 | void |
---|
384 | aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out) |
---|
385 | { |
---|
386 | smpl_t period, pitch = 0.; |
---|
387 | aubio_pitch_slideblock (p, ibuf); |
---|
388 | aubio_pitchschmitt_do (p->p_object, p->buf, out); |
---|
389 | period = out->data[0]; |
---|
390 | if (period > 0) { |
---|
391 | pitch = p->samplerate / period; |
---|
392 | } else { |
---|
393 | pitch = 0.; |
---|
394 | } |
---|
395 | out->data[0] = pitch; |
---|
396 | } |
---|
397 | |
---|
398 | /* conversion callbacks */ |
---|
399 | smpl_t |
---|
400 | freqconvbin(smpl_t f, uint_t samplerate, uint_t bufsize) |
---|
401 | { |
---|
402 | return aubio_freqtobin(f, samplerate, bufsize); |
---|
403 | } |
---|
404 | |
---|
405 | smpl_t |
---|
406 | freqconvmidi (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED) |
---|
407 | { |
---|
408 | return aubio_freqtomidi (f); |
---|
409 | } |
---|
410 | |
---|
411 | smpl_t |
---|
412 | freqconvpass (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED) |
---|
413 | { |
---|
414 | return f; |
---|
415 | } |
---|
416 | |
---|
417 | /* confidence callbacks */ |
---|
418 | smpl_t |
---|
419 | aubio_pitch_get_confidence (aubio_pitch_t * p) |
---|
420 | { |
---|
421 | if (p->conf_cb) { |
---|
422 | return p->conf_cb(p->p_object); |
---|
423 | } |
---|
424 | return 0.; |
---|
425 | } |
---|