2 Copyright (C) 2003-2009 Paul Brossier <piem@aubio.org>
4 This file is part of aubio.
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.
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.
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/>.
21 #include "aubio_priv.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/pitchyinfast.h"
36 #include "pitch/pitchspecacf.h"
37 #include "pitch/pitch.h"
39 #define DEFAULT_PITCH_SILENCE -50.
41 /** pitch detection algorithms */
44 aubio_pitcht_yin, /**< `yin`, YIN algorithm */
45 aubio_pitcht_mcomb, /**< `mcomb`, Multi-comb filter */
46 aubio_pitcht_schmitt, /**< `schmitt`, Schmitt trigger */
47 aubio_pitcht_fcomb, /**< `fcomb`, Fast comb filter */
48 aubio_pitcht_yinfft, /**< `yinfft`, Spectral YIN */
49 aubio_pitcht_yinfast, /**< `yinfast`, YIN fast */
50 aubio_pitcht_specacf, /**< `specacf`, Spectral autocorrelation */
52 = aubio_pitcht_yinfft, /**< `default` */
55 /** pitch detection output modes */
58 aubio_pitchm_freq, /**< Frequency (Hz) */
59 aubio_pitchm_midi, /**< MIDI note (0.,127) */
60 aubio_pitchm_cent, /**< Cent */
61 aubio_pitchm_bin, /**< Frequency bin (0,bufsize) */
62 aubio_pitchm_default = aubio_pitchm_freq, /**< the one used when "default" is asked */
65 /** callback to get pitch candidate, defined below */
66 typedef void (*aubio_pitch_detect_t) (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
68 /** callback to convert pitch from one unit to another, defined below */
69 typedef smpl_t(*aubio_pitch_convert_t) (smpl_t value, uint_t samplerate, uint_t bufsize);
71 /** callback to fetch the confidence of the algorithm */
72 typedef smpl_t (*aubio_pitch_get_conf_t) (void * p);
74 /** generic pitch detection structure */
77 aubio_pitch_type type; /**< pitch detection mode */
78 aubio_pitch_mode mode; /**< pitch detection output mode */
79 uint_t samplerate; /**< samplerate */
80 uint_t bufsize; /**< buffer size */
81 void *p_object; /**< pointer to pitch object */
82 aubio_filter_t *filter; /**< filter */
83 fvec_t *filtered; /**< filtered input */
84 aubio_pvoc_t *pv; /**< phase vocoder for mcomb */
85 cvec_t *fftgrain; /**< spectral frame for mcomb */
86 fvec_t *buf; /**< temporary buffer for yin */
87 aubio_pitch_detect_t detect_cb; /**< callback to get the pitch candidates */
88 aubio_pitch_convert_t conv_cb; /**< callback to convert it to the desired unit */
89 aubio_pitch_get_conf_t conf_cb; /**< pointer to the current confidence callback */
90 smpl_t silence; /**< silence threshold */
93 /* callback functions for pitch detection */
94 static void aubio_pitch_do_mcomb (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
95 static void aubio_pitch_do_yin (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
96 static void aubio_pitch_do_schmitt (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
97 static void aubio_pitch_do_fcomb (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
98 static void aubio_pitch_do_yinfft (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
99 static void aubio_pitch_do_yinfast (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
100 static void aubio_pitch_do_specacf (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
102 /* internal functions for frequency conversion */
103 static smpl_t freqconvbin (smpl_t f, uint_t samplerate, uint_t bufsize);
104 static smpl_t freqconvmidi (smpl_t f, uint_t samplerate, uint_t bufsize);
105 static smpl_t freqconvpass (smpl_t f, uint_t samplerate, uint_t bufsize);
107 /* adapter to stack ibuf new samples at the end of buf, and trim `buf` to `bufsize` */
108 void aubio_pitch_slideblock (aubio_pitch_t * p, const fvec_t * ibuf);
112 new_aubio_pitch (const char_t * pitch_mode,
113 uint_t bufsize, uint_t hopsize, uint_t samplerate)
115 aubio_pitch_t *p = AUBIO_NEW (aubio_pitch_t);
116 aubio_pitch_type pitch_type;
117 if (pitch_mode == NULL) {
118 AUBIO_ERR ("pitch: can not use ‘NULL‘ for pitch detection method\n");
121 if (strcmp (pitch_mode, "mcomb") == 0)
122 pitch_type = aubio_pitcht_mcomb;
123 else if (strcmp (pitch_mode, "yinfast") == 0)
124 pitch_type = aubio_pitcht_yinfast;
125 else if (strcmp (pitch_mode, "yinfft") == 0)
126 pitch_type = aubio_pitcht_yinfft;
127 else if (strcmp (pitch_mode, "yin") == 0)
128 pitch_type = aubio_pitcht_yin;
129 else if (strcmp (pitch_mode, "schmitt") == 0)
130 pitch_type = aubio_pitcht_schmitt;
131 else if (strcmp (pitch_mode, "fcomb") == 0)
132 pitch_type = aubio_pitcht_fcomb;
133 else if (strcmp (pitch_mode, "specacf") == 0)
134 pitch_type = aubio_pitcht_specacf;
135 else if (strcmp (pitch_mode, "default") == 0)
136 pitch_type = aubio_pitcht_default;
138 AUBIO_ERR ("pitch: unknown pitch detection method ‘%s’\n", pitch_mode);
142 // check parameters are valid
143 if ((sint_t)hopsize < 1) {
144 AUBIO_ERR("pitch: got hopsize %d, but can not be < 1\n", hopsize);
146 } else if ((sint_t)bufsize < 1) {
147 AUBIO_ERR("pitch: got buffer_size %d, but can not be < 1\n", bufsize);
149 } else if (bufsize < hopsize) {
150 AUBIO_ERR("pitch: hop size (%d) is larger than win size (%d)\n", hopsize, bufsize);
152 } else if ((sint_t)samplerate < 1) {
153 AUBIO_ERR("pitch: samplerate (%d) can not be < 1\n", samplerate);
157 p->samplerate = samplerate;
158 p->type = pitch_type;
159 aubio_pitch_set_unit (p, "default");
160 p->bufsize = bufsize;
161 p->silence = DEFAULT_PITCH_SILENCE;
164 case aubio_pitcht_yin:
165 p->buf = new_fvec (bufsize);
166 p->p_object = new_aubio_pitchyin (bufsize);
167 if (!p->p_object) goto beach;
168 p->detect_cb = aubio_pitch_do_yin;
169 p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyin_get_confidence;
170 aubio_pitchyin_set_tolerance (p->p_object, 0.15);
172 case aubio_pitcht_mcomb:
173 p->filtered = new_fvec (hopsize);
174 p->pv = new_aubio_pvoc (bufsize, hopsize);
175 if (!p->pv) goto beach;
176 p->fftgrain = new_cvec (bufsize);
177 p->p_object = new_aubio_pitchmcomb (bufsize, hopsize);
178 p->filter = new_aubio_filter_c_weighting (samplerate);
179 p->detect_cb = aubio_pitch_do_mcomb;
181 case aubio_pitcht_fcomb:
182 p->buf = new_fvec (bufsize);
183 p->p_object = new_aubio_pitchfcomb (bufsize, hopsize);
184 if (!p->p_object) goto beach;
185 p->detect_cb = aubio_pitch_do_fcomb;
187 case aubio_pitcht_schmitt:
188 p->buf = new_fvec (bufsize);
189 p->p_object = new_aubio_pitchschmitt (bufsize);
190 p->detect_cb = aubio_pitch_do_schmitt;
192 case aubio_pitcht_yinfft:
193 p->buf = new_fvec (bufsize);
194 p->p_object = new_aubio_pitchyinfft (samplerate, bufsize);
195 if (!p->p_object) goto beach;
196 p->detect_cb = aubio_pitch_do_yinfft;
197 p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyinfft_get_confidence;
198 aubio_pitchyinfft_set_tolerance (p->p_object, 0.85);
200 case aubio_pitcht_yinfast:
201 p->buf = new_fvec (bufsize);
202 p->p_object = new_aubio_pitchyinfast (bufsize);
203 if (!p->p_object) goto beach;
204 p->detect_cb = aubio_pitch_do_yinfast;
205 p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyinfast_get_confidence;
206 aubio_pitchyinfast_set_tolerance (p->p_object, 0.15);
208 case aubio_pitcht_specacf:
209 p->buf = new_fvec (bufsize);
210 p->p_object = new_aubio_pitchspecacf (bufsize);
211 if (!p->p_object) goto beach;
212 p->detect_cb = aubio_pitch_do_specacf;
213 p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchspecacf_get_tolerance;
214 aubio_pitchspecacf_set_tolerance (p->p_object, 0.85);
222 if (p->filtered) del_fvec(p->filtered);
223 if (p->buf) del_fvec(p->buf);
229 del_aubio_pitch (aubio_pitch_t * p)
232 case aubio_pitcht_yin:
234 del_aubio_pitchyin (p->p_object);
236 case aubio_pitcht_mcomb:
237 del_fvec (p->filtered);
238 del_aubio_pvoc (p->pv);
239 del_cvec (p->fftgrain);
240 del_aubio_filter (p->filter);
241 del_aubio_pitchmcomb (p->p_object);
243 case aubio_pitcht_schmitt:
245 del_aubio_pitchschmitt (p->p_object);
247 case aubio_pitcht_fcomb:
249 del_aubio_pitchfcomb (p->p_object);
251 case aubio_pitcht_yinfft:
253 del_aubio_pitchyinfft (p->p_object);
255 case aubio_pitcht_yinfast:
257 del_aubio_pitchyinfast (p->p_object);
259 case aubio_pitcht_specacf:
261 del_aubio_pitchspecacf (p->p_object);
270 aubio_pitch_slideblock (aubio_pitch_t * p, const fvec_t * ibuf)
272 uint_t overlap_size = p->buf->length - ibuf->length;
273 #if 1 //!HAVE_MEMCPY_HACKS
275 for (j = 0; j < overlap_size; j++) {
276 p->buf->data[j] = p->buf->data[j + ibuf->length];
278 for (j = 0; j < ibuf->length; j++) {
279 p->buf->data[j + overlap_size] = ibuf->data[j];
282 smpl_t *data = p->buf->data;
283 smpl_t *newdata = ibuf->data;
284 memmove(data, data + ibuf->length, overlap_size);
285 memcpy(data + overlap_size, newdata, ibuf->length);
290 aubio_pitch_set_unit (aubio_pitch_t * p, const char_t * pitch_unit)
292 uint_t err = AUBIO_OK;
293 aubio_pitch_mode pitch_mode;
294 if (strcmp (pitch_unit, "freq") == 0)
295 pitch_mode = aubio_pitchm_freq;
296 else if (strcmp (pitch_unit, "hertz") == 0)
297 pitch_mode = aubio_pitchm_freq;
298 else if (strcmp (pitch_unit, "Hertz") == 0)
299 pitch_mode = aubio_pitchm_freq;
300 else if (strcmp (pitch_unit, "Hz") == 0)
301 pitch_mode = aubio_pitchm_freq;
302 else if (strcmp (pitch_unit, "f0") == 0)
303 pitch_mode = aubio_pitchm_freq;
304 else if (strcmp (pitch_unit, "midi") == 0)
305 pitch_mode = aubio_pitchm_midi;
306 else if (strcmp (pitch_unit, "cent") == 0)
307 pitch_mode = aubio_pitchm_cent;
308 else if (strcmp (pitch_unit, "bin") == 0)
309 pitch_mode = aubio_pitchm_bin;
310 else if (strcmp (pitch_unit, "default") == 0)
311 pitch_mode = aubio_pitchm_default;
313 AUBIO_WRN("pitch: unknown pitch detection unit ‘%s’, using default\n",
315 pitch_mode = aubio_pitchm_default;
318 p->mode = pitch_mode;
320 case aubio_pitchm_freq:
321 p->conv_cb = freqconvpass;
323 case aubio_pitchm_midi:
324 p->conv_cb = freqconvmidi;
326 case aubio_pitchm_cent:
327 /* bug: not implemented */
328 p->conv_cb = freqconvmidi;
330 case aubio_pitchm_bin:
331 p->conv_cb = freqconvbin;
340 aubio_pitch_set_tolerance (aubio_pitch_t * p, smpl_t tol)
343 case aubio_pitcht_yin:
344 aubio_pitchyin_set_tolerance (p->p_object, tol);
346 case aubio_pitcht_yinfft:
347 aubio_pitchyinfft_set_tolerance (p->p_object, tol);
349 case aubio_pitcht_yinfast:
350 aubio_pitchyinfast_set_tolerance (p->p_object, tol);
359 aubio_pitch_get_tolerance (aubio_pitch_t * p)
361 smpl_t tolerance = 1.;
363 case aubio_pitcht_yin:
364 tolerance = aubio_pitchyin_get_tolerance (p->p_object);
366 case aubio_pitcht_yinfft:
367 tolerance = aubio_pitchyinfft_get_tolerance (p->p_object);
369 case aubio_pitcht_yinfast:
370 tolerance = aubio_pitchyinfast_get_tolerance (p->p_object);
379 aubio_pitch_set_silence (aubio_pitch_t * p, smpl_t silence)
381 if (silence <= 0 && silence >= -200) {
382 p->silence = silence;
385 AUBIO_WRN("pitch: could not set silence to %.2f\n", silence);
391 aubio_pitch_get_silence (aubio_pitch_t * p)
397 /* do method, calling the detection callback, then the conversion callback */
399 aubio_pitch_do (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf)
401 p->detect_cb (p, ibuf, obuf);
402 if (aubio_silence_detection(ibuf, p->silence) == 1) {
405 obuf->data[0] = p->conv_cb (obuf->data[0], p->samplerate, p->bufsize);
408 /* do method for each algorithm */
410 aubio_pitch_do_mcomb (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf)
412 aubio_filter_do_outplace (p->filter, ibuf, p->filtered);
413 aubio_pvoc_do (p->pv, ibuf, p->fftgrain);
414 aubio_pitchmcomb_do (p->p_object, p->fftgrain, obuf);
415 obuf->data[0] = aubio_bintofreq (obuf->data[0], p->samplerate, p->bufsize);
419 aubio_pitch_do_yin (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf)
422 aubio_pitch_slideblock (p, ibuf);
423 aubio_pitchyin_do (p->p_object, p->buf, obuf);
424 pitch = obuf->data[0];
426 pitch = p->samplerate / (pitch + 0.);
430 obuf->data[0] = pitch;
435 aubio_pitch_do_yinfft (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf)
438 aubio_pitch_slideblock (p, ibuf);
439 aubio_pitchyinfft_do (p->p_object, p->buf, obuf);
440 pitch = obuf->data[0];
442 pitch = p->samplerate / (pitch + 0.);
446 obuf->data[0] = pitch;
450 aubio_pitch_do_yinfast (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf)
453 aubio_pitch_slideblock (p, ibuf);
454 aubio_pitchyinfast_do (p->p_object, p->buf, obuf);
455 pitch = obuf->data[0];
457 pitch = p->samplerate / (pitch + 0.);
461 obuf->data[0] = pitch;
465 aubio_pitch_do_specacf (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * out)
467 smpl_t pitch = 0., period;
468 aubio_pitch_slideblock (p, ibuf);
469 aubio_pitchspecacf_do (p->p_object, p->buf, out);
470 //out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize);
471 period = out->data[0];
473 pitch = p->samplerate / period;
477 out->data[0] = pitch;
481 aubio_pitch_do_fcomb (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * out)
483 aubio_pitch_slideblock (p, ibuf);
484 aubio_pitchfcomb_do (p->p_object, p->buf, out);
485 out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize);
489 aubio_pitch_do_schmitt (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * out)
491 smpl_t period, pitch = 0.;
492 aubio_pitch_slideblock (p, ibuf);
493 aubio_pitchschmitt_do (p->p_object, p->buf, out);
494 period = out->data[0];
496 pitch = p->samplerate / period;
500 out->data[0] = pitch;
503 /* conversion callbacks */
505 freqconvbin(smpl_t f, uint_t samplerate, uint_t bufsize)
507 return aubio_freqtobin(f, samplerate, bufsize);
511 freqconvmidi (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED)
513 return aubio_freqtomidi (f);
517 freqconvpass (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED)
522 /* confidence callbacks */
524 aubio_pitch_get_confidence (aubio_pitch_t * p)
527 return p->conf_cb(p->p_object);