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/pitch.h"
37 /** pitch detection algorithms */
40 aubio_pitcht_yin, /**< `yin`, YIN algorithm */
41 aubio_pitcht_mcomb, /**< `mcomb`, Multi-comb filter */
42 aubio_pitcht_schmitt, /**< `schmitt`, Schmitt trigger */
43 aubio_pitcht_fcomb, /**< `fcomb`, Fast comb filter */
44 aubio_pitcht_yinfft, /**< `yinfft`, Spectral YIN */
46 = aubio_pitcht_yinfft, /**< `default` */
49 /** pitch detection output modes */
52 aubio_pitchm_freq, /**< Frequency (Hz) */
53 aubio_pitchm_midi, /**< MIDI note (0.,127) */
54 aubio_pitchm_cent, /**< Cent */
55 aubio_pitchm_bin, /**< Frequency bin (0,bufsize) */
56 aubio_pitchm_default = aubio_pitchm_freq, /**< the one used when "default" is asked */
59 /** callback to get pitch candidate, defined below */
60 typedef void (*aubio_pitch_detect_t) (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
62 /** callback to convert pitch from one unit to another, defined below */
63 typedef smpl_t(*aubio_pitch_convert_t) (smpl_t value, uint_t samplerate, uint_t bufsize);
65 /** callback to fetch the confidence of the algorithm */
66 typedef smpl_t (*aubio_pitch_get_conf_t) (void * p);
68 /** generic pitch detection structure */
71 aubio_pitch_type type; /**< pitch detection mode */
72 aubio_pitch_mode mode; /**< pitch detection output mode */
73 uint_t samplerate; /**< samplerate */
74 uint_t bufsize; /**< buffer size */
75 void *p_object; /**< pointer to pitch object */
76 aubio_filter_t *filter; /**< filter */
77 aubio_pvoc_t *pv; /**< phase vocoder for mcomb */
78 cvec_t *fftgrain; /**< spectral frame for mcomb */
79 fvec_t *buf; /**< temporary buffer for yin */
80 aubio_pitch_detect_t detect_cb; /**< callback to get the pitch candidates */
81 aubio_pitch_convert_t conv_cb; /**< callback to convert it to the desired unit */
82 aubio_pitch_get_conf_t conf_cb; /**< pointer to the current confidence callback */
85 /* callback functions for pitch detection */
86 static void aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
87 static void aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
88 static void aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
89 static void aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
90 static void aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
92 /* conversion functions for frequency conversions */
93 smpl_t freqconvbin (smpl_t f, uint_t samplerate, uint_t bufsize);
94 smpl_t freqconvmidi (smpl_t f, uint_t samplerate, uint_t bufsize);
95 smpl_t freqconvpass (smpl_t f, uint_t samplerate, uint_t bufsize);
97 /* adapter to stack ibuf new samples at the end of buf, and trim `buf` to `bufsize` */
98 void aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf);
102 new_aubio_pitch (char_t * pitch_mode,
103 uint_t bufsize, uint_t hopsize, uint_t samplerate)
105 aubio_pitch_t *p = AUBIO_NEW (aubio_pitch_t);
106 aubio_pitch_type pitch_type;
107 if (strcmp (pitch_mode, "mcomb") == 0)
108 pitch_type = aubio_pitcht_mcomb;
109 else if (strcmp (pitch_mode, "yinfft") == 0)
110 pitch_type = aubio_pitcht_yinfft;
111 else if (strcmp (pitch_mode, "yin") == 0)
112 pitch_type = aubio_pitcht_yin;
113 else if (strcmp (pitch_mode, "schmitt") == 0)
114 pitch_type = aubio_pitcht_schmitt;
115 else if (strcmp (pitch_mode, "fcomb") == 0)
116 pitch_type = aubio_pitcht_fcomb;
117 else if (strcmp (pitch_mode, "default") == 0)
118 pitch_type = aubio_pitcht_default;
120 AUBIO_ERR ("unknown pitch detection method %s, using default.\n",
122 pitch_type = aubio_pitcht_default;
124 p->samplerate = samplerate;
125 p->type = pitch_type;
126 aubio_pitch_set_unit (p, "default");
127 p->bufsize = bufsize;
130 case aubio_pitcht_yin:
131 p->buf = new_fvec (bufsize);
132 p->p_object = new_aubio_pitchyin (bufsize);
133 p->detect_cb = aubio_pitch_do_yin;
134 p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyin_get_confidence;
135 aubio_pitchyin_set_tolerance (p->p_object, 0.15);
137 case aubio_pitcht_mcomb:
138 p->pv = new_aubio_pvoc (bufsize, hopsize);
139 p->fftgrain = new_cvec (bufsize);
140 p->p_object = new_aubio_pitchmcomb (bufsize, hopsize);
141 p->filter = new_aubio_filter_c_weighting (samplerate);
142 p->detect_cb = aubio_pitch_do_mcomb;
144 case aubio_pitcht_fcomb:
145 p->buf = new_fvec (bufsize);
146 p->p_object = new_aubio_pitchfcomb (bufsize, hopsize);
147 p->detect_cb = aubio_pitch_do_fcomb;
149 case aubio_pitcht_schmitt:
150 p->buf = new_fvec (bufsize);
151 p->p_object = new_aubio_pitchschmitt (bufsize);
152 p->detect_cb = aubio_pitch_do_schmitt;
154 case aubio_pitcht_yinfft:
155 p->buf = new_fvec (bufsize);
156 p->p_object = new_aubio_pitchyinfft (bufsize);
157 p->detect_cb = aubio_pitch_do_yinfft;
158 p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyinfft_get_confidence;
159 aubio_pitchyinfft_set_tolerance (p->p_object, 0.85);
168 del_aubio_pitch (aubio_pitch_t * p)
171 case aubio_pitcht_yin:
173 del_aubio_pitchyin (p->p_object);
175 case aubio_pitcht_mcomb:
176 del_aubio_pvoc (p->pv);
177 del_cvec (p->fftgrain);
178 del_aubio_filter (p->filter);
179 del_aubio_pitchmcomb (p->p_object);
181 case aubio_pitcht_schmitt:
183 del_aubio_pitchschmitt (p->p_object);
185 case aubio_pitcht_fcomb:
187 del_aubio_pitchfcomb (p->p_object);
189 case aubio_pitcht_yinfft:
191 del_aubio_pitchyinfft (p->p_object);
200 aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf)
202 uint_t j = 0, overlap_size = 0;
203 overlap_size = p->buf->length - ibuf->length;
204 for (j = 0; j < overlap_size; j++) {
205 p->buf->data[j] = p->buf->data[j + ibuf->length];
207 for (j = 0; j < ibuf->length; j++) {
208 p->buf->data[j + overlap_size] = ibuf->data[j];
213 aubio_pitch_set_unit (aubio_pitch_t * p, char_t * pitch_unit)
215 aubio_pitch_mode pitch_mode;
216 if (strcmp (pitch_unit, "freq") == 0)
217 pitch_mode = aubio_pitchm_freq;
218 else if (strcmp (pitch_unit, "midi") == 0)
219 pitch_mode = aubio_pitchm_midi;
220 else if (strcmp (pitch_unit, "cent") == 0)
221 pitch_mode = aubio_pitchm_cent;
222 else if (strcmp (pitch_unit, "bin") == 0)
223 pitch_mode = aubio_pitchm_bin;
224 else if (strcmp (pitch_unit, "default") == 0)
225 pitch_mode = aubio_pitchm_default;
227 AUBIO_ERR ("unknown pitch detection unit %s, using default\n", pitch_unit);
228 pitch_mode = aubio_pitchm_default;
230 p->mode = pitch_mode;
232 case aubio_pitchm_freq:
233 p->conv_cb = freqconvpass;
235 case aubio_pitchm_midi:
236 p->conv_cb = freqconvmidi;
238 case aubio_pitchm_cent:
239 /* bug: not implemented */
240 p->conv_cb = freqconvmidi;
242 case aubio_pitchm_bin:
243 p->conv_cb = freqconvbin;
252 aubio_pitch_set_tolerance (aubio_pitch_t * p, smpl_t tol)
255 case aubio_pitcht_yin:
256 aubio_pitchyin_set_tolerance (p->p_object, tol);
258 case aubio_pitcht_yinfft:
259 aubio_pitchyinfft_set_tolerance (p->p_object, tol);
268 /* do method, calling the detection callback, then the conversion callback */
270 aubio_pitch_do (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
272 p->detect_cb (p, ibuf, obuf);
273 obuf->data[0] = p->conv_cb (obuf->data[0], p->samplerate, p->bufsize);
276 /* do method for each algorithm */
278 aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
280 aubio_filter_do (p->filter, ibuf);
281 aubio_pvoc_do (p->pv, ibuf, p->fftgrain);
282 aubio_pitchmcomb_do (p->p_object, p->fftgrain, obuf);
283 obuf->data[0] = aubio_bintofreq (obuf->data[0], p->samplerate, p->bufsize);
287 aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
290 aubio_pitch_slideblock (p, ibuf);
291 aubio_pitchyin_do (p->p_object, p->buf, obuf);
292 pitch = obuf->data[0];
294 pitch = p->samplerate / (pitch + 0.);
298 obuf->data[0] = pitch;
303 aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
306 aubio_pitch_slideblock (p, ibuf);
307 aubio_pitchyinfft_do (p->p_object, p->buf, obuf);
308 pitch = obuf->data[0];
310 pitch = p->samplerate / (pitch + 0.);
314 obuf->data[0] = pitch;
318 aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out)
320 aubio_pitch_slideblock (p, ibuf);
321 aubio_pitchfcomb_do (p->p_object, p->buf, out);
322 out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize);
326 aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out)
328 smpl_t period, pitch = 0.;
329 aubio_pitch_slideblock (p, ibuf);
330 aubio_pitchschmitt_do (p->p_object, p->buf, out);
331 period = out->data[0];
333 pitch = p->samplerate / period;
337 out->data[0] = pitch;
340 /* conversion callbacks */
342 freqconvbin(smpl_t f, uint_t samplerate, uint_t bufsize)
344 return aubio_freqtobin(f, samplerate, bufsize);
348 freqconvmidi (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED)
350 return aubio_freqtomidi (f);
354 freqconvpass (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED)
359 /* confidence callbacks */
361 aubio_pitch_get_confidence (aubio_pitch_t * p)
364 return p->conf_cb(p->p_object);