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/pitchspecacf.h"
36 #include "pitch/pitch.h"
38 #define DEFAULT_PITCH_SILENCE -50.
40 /** pitch detection algorithms */
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 */
50 = aubio_pitcht_yinfft, /**< `default` */
53 /** pitch detection output modes */
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 */
63 /** callback to get pitch candidate, defined below */
64 typedef void (*aubio_pitch_detect_t) (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
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);
69 /** callback to fetch the confidence of the algorithm */
70 typedef smpl_t (*aubio_pitch_get_conf_t) (void * p);
72 /** generic pitch detection structure */
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 fvec_t *filtered; /**< filtered input */
82 aubio_pvoc_t *pv; /**< phase vocoder for mcomb */
83 cvec_t *fftgrain; /**< spectral frame for mcomb */
84 fvec_t *buf; /**< temporary buffer for yin */
85 aubio_pitch_detect_t detect_cb; /**< callback to get the pitch candidates */
86 aubio_pitch_convert_t conv_cb; /**< callback to convert it to the desired unit */
87 aubio_pitch_get_conf_t conf_cb; /**< pointer to the current confidence callback */
88 smpl_t silence; /**< silence threshold */
91 /* callback functions for pitch detection */
92 static void aubio_pitch_do_mcomb (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
93 static void aubio_pitch_do_yin (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
94 static void aubio_pitch_do_schmitt (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
95 static void aubio_pitch_do_fcomb (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
96 static void aubio_pitch_do_yinfft (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
97 static void aubio_pitch_do_specacf (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
99 /* internal functions for frequency conversion */
100 static smpl_t freqconvbin (smpl_t f, uint_t samplerate, uint_t bufsize);
101 static smpl_t freqconvmidi (smpl_t f, uint_t samplerate, uint_t bufsize);
102 static smpl_t freqconvpass (smpl_t f, uint_t samplerate, uint_t bufsize);
104 /* adapter to stack ibuf new samples at the end of buf, and trim `buf` to `bufsize` */
105 void aubio_pitch_slideblock (aubio_pitch_t * p, const fvec_t * ibuf);
109 new_aubio_pitch (const char_t * pitch_mode,
110 uint_t bufsize, uint_t hopsize, uint_t samplerate)
112 aubio_pitch_t *p = AUBIO_NEW (aubio_pitch_t);
113 aubio_pitch_type pitch_type;
114 if (strcmp (pitch_mode, "mcomb") == 0)
115 pitch_type = aubio_pitcht_mcomb;
116 else if (strcmp (pitch_mode, "yinfft") == 0)
117 pitch_type = aubio_pitcht_yinfft;
118 else if (strcmp (pitch_mode, "yin") == 0)
119 pitch_type = aubio_pitcht_yin;
120 else if (strcmp (pitch_mode, "schmitt") == 0)
121 pitch_type = aubio_pitcht_schmitt;
122 else if (strcmp (pitch_mode, "fcomb") == 0)
123 pitch_type = aubio_pitcht_fcomb;
124 else if (strcmp (pitch_mode, "specacf") == 0)
125 pitch_type = aubio_pitcht_specacf;
126 else if (strcmp (pitch_mode, "default") == 0)
127 pitch_type = aubio_pitcht_default;
129 AUBIO_ERR ("unknown pitch detection method %s, using default.\n",
131 pitch_type = aubio_pitcht_default;
134 // check parameters are valid
135 if ((sint_t)hopsize < 1) {
136 AUBIO_ERR("pitch: got hopsize %d, but can not be < 1\n", hopsize);
138 } else if ((sint_t)bufsize < 1) {
139 AUBIO_ERR("pitch: got buffer_size %d, but can not be < 1\n", bufsize);
141 } else if (bufsize < hopsize) {
142 AUBIO_ERR("pitch: hop size (%d) is larger than win size (%d)\n", bufsize, hopsize);
144 } else if ((sint_t)samplerate < 1) {
145 AUBIO_ERR("pitch: samplerate (%d) can not be < 1\n", samplerate);
149 p->samplerate = samplerate;
150 p->type = pitch_type;
151 aubio_pitch_set_unit (p, "default");
152 p->bufsize = bufsize;
153 p->silence = DEFAULT_PITCH_SILENCE;
156 case aubio_pitcht_yin:
157 p->buf = new_fvec (bufsize);
158 p->p_object = new_aubio_pitchyin (bufsize);
159 p->detect_cb = aubio_pitch_do_yin;
160 p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyin_get_confidence;
161 aubio_pitchyin_set_tolerance (p->p_object, 0.15);
163 case aubio_pitcht_mcomb:
164 p->filtered = new_fvec (hopsize);
165 p->pv = new_aubio_pvoc (bufsize, hopsize);
166 p->fftgrain = new_cvec (bufsize);
167 p->p_object = new_aubio_pitchmcomb (bufsize, hopsize);
168 p->filter = new_aubio_filter_c_weighting (samplerate);
169 p->detect_cb = aubio_pitch_do_mcomb;
171 case aubio_pitcht_fcomb:
172 p->buf = new_fvec (bufsize);
173 p->p_object = new_aubio_pitchfcomb (bufsize, hopsize);
174 p->detect_cb = aubio_pitch_do_fcomb;
176 case aubio_pitcht_schmitt:
177 p->buf = new_fvec (bufsize);
178 p->p_object = new_aubio_pitchschmitt (bufsize);
179 p->detect_cb = aubio_pitch_do_schmitt;
181 case aubio_pitcht_yinfft:
182 p->buf = new_fvec (bufsize);
183 p->p_object = new_aubio_pitchyinfft (samplerate, bufsize);
184 p->detect_cb = aubio_pitch_do_yinfft;
185 p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyinfft_get_confidence;
186 aubio_pitchyinfft_set_tolerance (p->p_object, 0.85);
188 case aubio_pitcht_specacf:
189 p->buf = new_fvec (bufsize);
190 p->p_object = new_aubio_pitchspecacf (bufsize);
191 p->detect_cb = aubio_pitch_do_specacf;
192 p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchspecacf_get_tolerance;
193 aubio_pitchspecacf_set_tolerance (p->p_object, 0.85);
206 del_aubio_pitch (aubio_pitch_t * p)
209 case aubio_pitcht_yin:
211 del_aubio_pitchyin (p->p_object);
213 case aubio_pitcht_mcomb:
214 del_fvec (p->filtered);
215 del_aubio_pvoc (p->pv);
216 del_cvec (p->fftgrain);
217 del_aubio_filter (p->filter);
218 del_aubio_pitchmcomb (p->p_object);
220 case aubio_pitcht_schmitt:
222 del_aubio_pitchschmitt (p->p_object);
224 case aubio_pitcht_fcomb:
226 del_aubio_pitchfcomb (p->p_object);
228 case aubio_pitcht_yinfft:
230 del_aubio_pitchyinfft (p->p_object);
232 case aubio_pitcht_specacf:
234 del_aubio_pitchspecacf (p->p_object);
243 aubio_pitch_slideblock (aubio_pitch_t * p, const fvec_t * ibuf)
245 uint_t overlap_size = p->buf->length - ibuf->length;
246 #if 1 //!HAVE_MEMCPY_HACKS
248 for (j = 0; j < overlap_size; j++) {
249 p->buf->data[j] = p->buf->data[j + ibuf->length];
251 for (j = 0; j < ibuf->length; j++) {
252 p->buf->data[j + overlap_size] = ibuf->data[j];
255 smpl_t *data = p->buf->data;
256 smpl_t *newdata = ibuf->data;
257 memmove(data, data + ibuf->length, overlap_size);
258 memcpy(data + overlap_size, newdata, ibuf->length);
263 aubio_pitch_set_unit (aubio_pitch_t * p, const char_t * pitch_unit)
265 uint_t err = AUBIO_OK;
266 aubio_pitch_mode pitch_mode;
267 if (strcmp (pitch_unit, "freq") == 0)
268 pitch_mode = aubio_pitchm_freq;
269 else if (strcmp (pitch_unit, "hertz") == 0)
270 pitch_mode = aubio_pitchm_freq;
271 else if (strcmp (pitch_unit, "Hertz") == 0)
272 pitch_mode = aubio_pitchm_freq;
273 else if (strcmp (pitch_unit, "Hz") == 0)
274 pitch_mode = aubio_pitchm_freq;
275 else if (strcmp (pitch_unit, "f0") == 0)
276 pitch_mode = aubio_pitchm_freq;
277 else if (strcmp (pitch_unit, "midi") == 0)
278 pitch_mode = aubio_pitchm_midi;
279 else if (strcmp (pitch_unit, "cent") == 0)
280 pitch_mode = aubio_pitchm_cent;
281 else if (strcmp (pitch_unit, "bin") == 0)
282 pitch_mode = aubio_pitchm_bin;
283 else if (strcmp (pitch_unit, "default") == 0)
284 pitch_mode = aubio_pitchm_default;
286 AUBIO_ERR ("unknown pitch detection unit %s, using default\n", pitch_unit);
287 pitch_mode = aubio_pitchm_default;
290 p->mode = pitch_mode;
292 case aubio_pitchm_freq:
293 p->conv_cb = freqconvpass;
295 case aubio_pitchm_midi:
296 p->conv_cb = freqconvmidi;
298 case aubio_pitchm_cent:
299 /* bug: not implemented */
300 p->conv_cb = freqconvmidi;
302 case aubio_pitchm_bin:
303 p->conv_cb = freqconvbin;
312 aubio_pitch_set_tolerance (aubio_pitch_t * p, smpl_t tol)
315 case aubio_pitcht_yin:
316 aubio_pitchyin_set_tolerance (p->p_object, tol);
318 case aubio_pitcht_yinfft:
319 aubio_pitchyinfft_set_tolerance (p->p_object, tol);
328 aubio_pitch_get_tolerance (aubio_pitch_t * p)
330 smpl_t tolerance = 1.;
332 case aubio_pitcht_yin:
333 tolerance = aubio_pitchyin_get_tolerance (p->p_object);
335 case aubio_pitcht_yinfft:
336 tolerance = aubio_pitchyinfft_get_tolerance (p->p_object);
345 aubio_pitch_set_silence (aubio_pitch_t * p, smpl_t silence)
347 if (silence <= 0 && silence >= -200) {
348 p->silence = silence;
351 AUBIO_ERR("pitch: could not set silence to %.2f", silence);
357 aubio_pitch_get_silence (aubio_pitch_t * p)
363 /* do method, calling the detection callback, then the conversion callback */
365 aubio_pitch_do (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf)
367 p->detect_cb (p, ibuf, obuf);
368 if (aubio_silence_detection(ibuf, p->silence) == 1) {
371 obuf->data[0] = p->conv_cb (obuf->data[0], p->samplerate, p->bufsize);
374 /* do method for each algorithm */
376 aubio_pitch_do_mcomb (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf)
378 aubio_filter_do_outplace (p->filter, ibuf, p->filtered);
379 aubio_pvoc_do (p->pv, ibuf, p->fftgrain);
380 aubio_pitchmcomb_do (p->p_object, p->fftgrain, obuf);
381 obuf->data[0] = aubio_bintofreq (obuf->data[0], p->samplerate, p->bufsize);
385 aubio_pitch_do_yin (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf)
388 aubio_pitch_slideblock (p, ibuf);
389 aubio_pitchyin_do (p->p_object, p->buf, obuf);
390 pitch = obuf->data[0];
392 pitch = p->samplerate / (pitch + 0.);
396 obuf->data[0] = pitch;
401 aubio_pitch_do_yinfft (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf)
404 aubio_pitch_slideblock (p, ibuf);
405 aubio_pitchyinfft_do (p->p_object, p->buf, obuf);
406 pitch = obuf->data[0];
408 pitch = p->samplerate / (pitch + 0.);
412 obuf->data[0] = pitch;
416 aubio_pitch_do_specacf (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * out)
418 smpl_t pitch = 0., period;
419 aubio_pitch_slideblock (p, ibuf);
420 aubio_pitchspecacf_do (p->p_object, p->buf, out);
421 //out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize);
422 period = out->data[0];
424 pitch = p->samplerate / period;
428 out->data[0] = pitch;
432 aubio_pitch_do_fcomb (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * out)
434 aubio_pitch_slideblock (p, ibuf);
435 aubio_pitchfcomb_do (p->p_object, p->buf, out);
436 out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize);
440 aubio_pitch_do_schmitt (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * out)
442 smpl_t period, pitch = 0.;
443 aubio_pitch_slideblock (p, ibuf);
444 aubio_pitchschmitt_do (p->p_object, p->buf, out);
445 period = out->data[0];
447 pitch = p->samplerate / period;
451 out->data[0] = pitch;
454 /* conversion callbacks */
456 freqconvbin(smpl_t f, uint_t samplerate, uint_t bufsize)
458 return aubio_freqtobin(f, samplerate, bufsize);
462 freqconvmidi (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED)
464 return aubio_freqtomidi (f);
468 freqconvpass (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED)
473 /* confidence callbacks */
475 aubio_pitch_get_confidence (aubio_pitch_t * p)
478 return p->conf_cb(p->p_object);