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/pitch.h" |
---|
36 | |
---|
37 | /** pitch detection algorithms */ |
---|
38 | typedef enum |
---|
39 | { |
---|
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 */ |
---|
45 | aubio_pitcht_default |
---|
46 | = aubio_pitcht_yinfft, /**< `default` */ |
---|
47 | } aubio_pitch_type; |
---|
48 | |
---|
49 | /** pitch detection output modes */ |
---|
50 | typedef enum |
---|
51 | { |
---|
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 */ |
---|
57 | } aubio_pitch_mode; |
---|
58 | |
---|
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); |
---|
61 | |
---|
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); |
---|
64 | |
---|
65 | /** callback to fetch the confidence of the algorithm */ |
---|
66 | typedef smpl_t (*aubio_pitch_get_conf_t) (void * p); |
---|
67 | |
---|
68 | /** generic pitch detection structure */ |
---|
69 | struct _aubio_pitch_t |
---|
70 | { |
---|
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 */ |
---|
83 | }; |
---|
84 | |
---|
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); |
---|
91 | |
---|
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); |
---|
96 | |
---|
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); |
---|
99 | |
---|
100 | |
---|
101 | aubio_pitch_t * |
---|
102 | new_aubio_pitch (char_t * pitch_mode, |
---|
103 | uint_t bufsize, uint_t hopsize, uint_t samplerate) |
---|
104 | { |
---|
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; |
---|
119 | else { |
---|
120 | AUBIO_ERR ("unknown pitch detection method %s, using default.\n", |
---|
121 | pitch_mode); |
---|
122 | pitch_type = aubio_pitcht_default; |
---|
123 | } |
---|
124 | p->samplerate = samplerate; |
---|
125 | p->type = pitch_type; |
---|
126 | aubio_pitch_set_unit (p, "default"); |
---|
127 | p->bufsize = bufsize; |
---|
128 | p->conf_cb = NULL; |
---|
129 | switch (p->type) { |
---|
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); |
---|
136 | break; |
---|
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; |
---|
143 | break; |
---|
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; |
---|
148 | break; |
---|
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; |
---|
153 | break; |
---|
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); |
---|
160 | break; |
---|
161 | default: |
---|
162 | break; |
---|
163 | } |
---|
164 | return p; |
---|
165 | } |
---|
166 | |
---|
167 | void |
---|
168 | del_aubio_pitch (aubio_pitch_t * p) |
---|
169 | { |
---|
170 | switch (p->type) { |
---|
171 | case aubio_pitcht_yin: |
---|
172 | del_fvec (p->buf); |
---|
173 | del_aubio_pitchyin (p->p_object); |
---|
174 | break; |
---|
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); |
---|
180 | break; |
---|
181 | case aubio_pitcht_schmitt: |
---|
182 | del_fvec (p->buf); |
---|
183 | del_aubio_pitchschmitt (p->p_object); |
---|
184 | break; |
---|
185 | case aubio_pitcht_fcomb: |
---|
186 | del_fvec (p->buf); |
---|
187 | del_aubio_pitchfcomb (p->p_object); |
---|
188 | break; |
---|
189 | case aubio_pitcht_yinfft: |
---|
190 | del_fvec (p->buf); |
---|
191 | del_aubio_pitchyinfft (p->p_object); |
---|
192 | break; |
---|
193 | default: |
---|
194 | break; |
---|
195 | } |
---|
196 | AUBIO_FREE (p); |
---|
197 | } |
---|
198 | |
---|
199 | void |
---|
200 | aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf) |
---|
201 | { |
---|
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]; |
---|
206 | } |
---|
207 | for (j = 0; j < ibuf->length; j++) { |
---|
208 | p->buf->data[j + overlap_size] = ibuf->data[j]; |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | uint_t |
---|
213 | aubio_pitch_set_unit (aubio_pitch_t * p, char_t * pitch_unit) |
---|
214 | { |
---|
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; |
---|
226 | else { |
---|
227 | AUBIO_ERR ("unknown pitch detection unit %s, using default\n", pitch_unit); |
---|
228 | pitch_mode = aubio_pitchm_default; |
---|
229 | } |
---|
230 | p->mode = pitch_mode; |
---|
231 | switch (p->mode) { |
---|
232 | case aubio_pitchm_freq: |
---|
233 | p->conv_cb = freqconvpass; |
---|
234 | break; |
---|
235 | case aubio_pitchm_midi: |
---|
236 | p->conv_cb = freqconvmidi; |
---|
237 | break; |
---|
238 | case aubio_pitchm_cent: |
---|
239 | /* bug: not implemented */ |
---|
240 | p->conv_cb = freqconvmidi; |
---|
241 | break; |
---|
242 | case aubio_pitchm_bin: |
---|
243 | p->conv_cb = freqconvbin; |
---|
244 | break; |
---|
245 | default: |
---|
246 | break; |
---|
247 | } |
---|
248 | return AUBIO_OK; |
---|
249 | } |
---|
250 | |
---|
251 | uint_t |
---|
252 | aubio_pitch_set_tolerance (aubio_pitch_t * p, smpl_t tol) |
---|
253 | { |
---|
254 | switch (p->type) { |
---|
255 | case aubio_pitcht_yin: |
---|
256 | aubio_pitchyin_set_tolerance (p->p_object, tol); |
---|
257 | break; |
---|
258 | case aubio_pitcht_yinfft: |
---|
259 | aubio_pitchyinfft_set_tolerance (p->p_object, tol); |
---|
260 | break; |
---|
261 | default: |
---|
262 | break; |
---|
263 | } |
---|
264 | return AUBIO_OK; |
---|
265 | } |
---|
266 | |
---|
267 | |
---|
268 | /* do method, calling the detection callback, then the conversion callback */ |
---|
269 | void |
---|
270 | aubio_pitch_do (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf) |
---|
271 | { |
---|
272 | p->detect_cb (p, ibuf, obuf); |
---|
273 | obuf->data[0] = p->conv_cb (obuf->data[0], p->samplerate, p->bufsize); |
---|
274 | } |
---|
275 | |
---|
276 | /* do method for each algorithm */ |
---|
277 | void |
---|
278 | aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf) |
---|
279 | { |
---|
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); |
---|
284 | } |
---|
285 | |
---|
286 | void |
---|
287 | aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf) |
---|
288 | { |
---|
289 | smpl_t pitch = 0.; |
---|
290 | aubio_pitch_slideblock (p, ibuf); |
---|
291 | aubio_pitchyin_do (p->p_object, p->buf, obuf); |
---|
292 | pitch = obuf->data[0]; |
---|
293 | if (pitch > 0) { |
---|
294 | pitch = p->samplerate / (pitch + 0.); |
---|
295 | } else { |
---|
296 | pitch = 0.; |
---|
297 | } |
---|
298 | obuf->data[0] = pitch; |
---|
299 | } |
---|
300 | |
---|
301 | |
---|
302 | void |
---|
303 | aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf) |
---|
304 | { |
---|
305 | smpl_t pitch = 0.; |
---|
306 | aubio_pitch_slideblock (p, ibuf); |
---|
307 | aubio_pitchyinfft_do (p->p_object, p->buf, obuf); |
---|
308 | pitch = obuf->data[0]; |
---|
309 | if (pitch > 0) { |
---|
310 | pitch = p->samplerate / (pitch + 0.); |
---|
311 | } else { |
---|
312 | pitch = 0.; |
---|
313 | } |
---|
314 | obuf->data[0] = pitch; |
---|
315 | } |
---|
316 | |
---|
317 | void |
---|
318 | aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out) |
---|
319 | { |
---|
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); |
---|
323 | } |
---|
324 | |
---|
325 | void |
---|
326 | aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out) |
---|
327 | { |
---|
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]; |
---|
332 | if (period > 0) { |
---|
333 | pitch = p->samplerate / period; |
---|
334 | } else { |
---|
335 | pitch = 0.; |
---|
336 | } |
---|
337 | out->data[0] = pitch; |
---|
338 | } |
---|
339 | |
---|
340 | /* conversion callbacks */ |
---|
341 | smpl_t |
---|
342 | freqconvbin(smpl_t f, uint_t samplerate, uint_t bufsize) |
---|
343 | { |
---|
344 | return aubio_freqtobin(f, samplerate, bufsize); |
---|
345 | } |
---|
346 | |
---|
347 | smpl_t |
---|
348 | freqconvmidi (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED) |
---|
349 | { |
---|
350 | return aubio_freqtomidi (f); |
---|
351 | } |
---|
352 | |
---|
353 | smpl_t |
---|
354 | freqconvpass (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED) |
---|
355 | { |
---|
356 | return f; |
---|
357 | } |
---|
358 | |
---|
359 | /* confidence callbacks */ |
---|
360 | smpl_t |
---|
361 | aubio_pitch_get_confidence (aubio_pitch_t * p) |
---|
362 | { |
---|
363 | if (p->conf_cb) { |
---|
364 | return p->conf_cb(p->p_object); |
---|
365 | } |
---|
366 | return 0.; |
---|
367 | } |
---|