source: src/pitch/pitch.c @ 130b651

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 130b651 was 973eb75, checked in by Paul Brossier <piem@piem.org>, 9 years ago

src/pitch/pitch.c: allow for silence == 0, improve error message

  • Property mode set to 100644
File size: 13.5 KB
Line 
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 */
41typedef 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 */
54typedef 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 */
64typedef 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 */
67typedef 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 */
70typedef smpl_t (*aubio_pitch_get_conf_t) (void * p);
71
72/** generic pitch detection structure */
73struct _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 */
91static void aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
92static void aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
93static void aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
94static void aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
95static void aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
96static void aubio_pitch_do_specacf (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
97
98/* conversion functions for frequency conversions */
99smpl_t freqconvbin (smpl_t f, uint_t samplerate, uint_t bufsize);
100smpl_t freqconvmidi (smpl_t f, uint_t samplerate, uint_t bufsize);
101smpl_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` */
104void aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf);
105
106
107aubio_pitch_t *
108new_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
133  // check parameters are valid
134  if ((sint_t)hopsize < 1) {
135    AUBIO_ERR("onset: got hopsize %d, but can not be < 1\n", hopsize);
136    goto beach;
137  } else if ((sint_t)bufsize < 1) {
138    AUBIO_ERR("onset: got buffer_size %d, but can not be < 1\n", bufsize);
139    goto beach;
140  } else if (bufsize < hopsize) {
141    AUBIO_ERR("onset: hop size (%d) is larger than win size (%d)\n", bufsize, hopsize);
142    goto beach;
143  } else if ((sint_t)samplerate < 1) {
144    AUBIO_ERR("onset: samplerate (%d) can not be < 1\n", samplerate);
145    goto beach;
146  }
147
148  p->samplerate = samplerate;
149  p->type = pitch_type;
150  aubio_pitch_set_unit (p, "default");
151  p->bufsize = bufsize;
152  p->silence = DEFAULT_PITCH_SILENCE;
153  p->conf_cb = NULL;
154  switch (p->type) {
155    case aubio_pitcht_yin:
156      p->buf = new_fvec (bufsize);
157      p->p_object = new_aubio_pitchyin (bufsize);
158      p->detect_cb = aubio_pitch_do_yin;
159      p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyin_get_confidence;
160      aubio_pitchyin_set_tolerance (p->p_object, 0.15);
161      break;
162    case aubio_pitcht_mcomb:
163      p->pv = new_aubio_pvoc (bufsize, hopsize);
164      p->fftgrain = new_cvec (bufsize);
165      p->p_object = new_aubio_pitchmcomb (bufsize, hopsize);
166      p->filter = new_aubio_filter_c_weighting (samplerate);
167      p->detect_cb = aubio_pitch_do_mcomb;
168      break;
169    case aubio_pitcht_fcomb:
170      p->buf = new_fvec (bufsize);
171      p->p_object = new_aubio_pitchfcomb (bufsize, hopsize);
172      p->detect_cb = aubio_pitch_do_fcomb;
173      break;
174    case aubio_pitcht_schmitt:
175      p->buf = new_fvec (bufsize);
176      p->p_object = new_aubio_pitchschmitt (bufsize);
177      p->detect_cb = aubio_pitch_do_schmitt;
178      break;
179    case aubio_pitcht_yinfft:
180      p->buf = new_fvec (bufsize);
181      p->p_object = new_aubio_pitchyinfft (samplerate, bufsize);
182      p->detect_cb = aubio_pitch_do_yinfft;
183      p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyinfft_get_confidence;
184      aubio_pitchyinfft_set_tolerance (p->p_object, 0.85);
185      break;
186    case aubio_pitcht_specacf:
187      p->buf = new_fvec (bufsize);
188      p->p_object = new_aubio_pitchspecacf (bufsize);
189      p->detect_cb = aubio_pitch_do_specacf;
190      p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchspecacf_get_tolerance;
191      aubio_pitchspecacf_set_tolerance (p->p_object, 0.85);
192      break;
193    default:
194      break;
195  }
196  return p;
197
198beach:
199  AUBIO_FREE(p);
200  return NULL;
201}
202
203void
204del_aubio_pitch (aubio_pitch_t * p)
205{
206  switch (p->type) {
207    case aubio_pitcht_yin:
208      del_fvec (p->buf);
209      del_aubio_pitchyin (p->p_object);
210      break;
211    case aubio_pitcht_mcomb:
212      del_aubio_pvoc (p->pv);
213      del_cvec (p->fftgrain);
214      del_aubio_filter (p->filter);
215      del_aubio_pitchmcomb (p->p_object);
216      break;
217    case aubio_pitcht_schmitt:
218      del_fvec (p->buf);
219      del_aubio_pitchschmitt (p->p_object);
220      break;
221    case aubio_pitcht_fcomb:
222      del_fvec (p->buf);
223      del_aubio_pitchfcomb (p->p_object);
224      break;
225    case aubio_pitcht_yinfft:
226      del_fvec (p->buf);
227      del_aubio_pitchyinfft (p->p_object);
228      break;
229    case aubio_pitcht_specacf:
230      del_fvec (p->buf);
231      del_aubio_pitchspecacf (p->p_object);
232      break;
233    default:
234      break;
235  }
236  AUBIO_FREE (p);
237}
238
239void
240aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf)
241{
242  uint_t j = 0, overlap_size = 0;
243  overlap_size = p->buf->length - ibuf->length;
244  for (j = 0; j < overlap_size; j++) {
245    p->buf->data[j] = p->buf->data[j + ibuf->length];
246  }
247  for (j = 0; j < ibuf->length; j++) {
248    p->buf->data[j + overlap_size] = ibuf->data[j];
249  }
250}
251
252uint_t
253aubio_pitch_set_unit (aubio_pitch_t * p, char_t * pitch_unit)
254{
255  uint_t err = AUBIO_OK;
256  aubio_pitch_mode pitch_mode;
257  if (strcmp (pitch_unit, "freq") == 0)
258    pitch_mode = aubio_pitchm_freq;
259  else if (strcmp (pitch_unit, "hertz") == 0)
260    pitch_mode = aubio_pitchm_freq;
261  else if (strcmp (pitch_unit, "Hertz") == 0)
262    pitch_mode = aubio_pitchm_freq;
263  else if (strcmp (pitch_unit, "Hz") == 0)
264    pitch_mode = aubio_pitchm_freq;
265  else if (strcmp (pitch_unit, "f0") == 0)
266    pitch_mode = aubio_pitchm_freq;
267  else if (strcmp (pitch_unit, "midi") == 0)
268    pitch_mode = aubio_pitchm_midi;
269  else if (strcmp (pitch_unit, "cent") == 0)
270    pitch_mode = aubio_pitchm_cent;
271  else if (strcmp (pitch_unit, "bin") == 0)
272    pitch_mode = aubio_pitchm_bin;
273  else if (strcmp (pitch_unit, "default") == 0)
274    pitch_mode = aubio_pitchm_default;
275  else {
276    AUBIO_ERR ("unknown pitch detection unit %s, using default\n", pitch_unit);
277    pitch_mode = aubio_pitchm_default;
278    err = AUBIO_FAIL;
279  }
280  p->mode = pitch_mode;
281  switch (p->mode) {
282    case aubio_pitchm_freq:
283      p->conv_cb = freqconvpass;
284      break;
285    case aubio_pitchm_midi:
286      p->conv_cb = freqconvmidi;
287      break;
288    case aubio_pitchm_cent:
289      /* bug: not implemented */
290      p->conv_cb = freqconvmidi;
291      break;
292    case aubio_pitchm_bin:
293      p->conv_cb = freqconvbin;
294      break;
295    default:
296      break;
297  }
298  return err;
299}
300
301uint_t
302aubio_pitch_set_tolerance (aubio_pitch_t * p, smpl_t tol)
303{
304  switch (p->type) {
305    case aubio_pitcht_yin:
306      aubio_pitchyin_set_tolerance (p->p_object, tol);
307      break;
308    case aubio_pitcht_yinfft:
309      aubio_pitchyinfft_set_tolerance (p->p_object, tol);
310      break;
311    default:
312      break;
313  }
314  return AUBIO_OK;
315}
316
317uint_t
318aubio_pitch_set_silence (aubio_pitch_t * p, smpl_t silence)
319{
320  if (silence <= 0 && silence >= -200) {
321    p->silence = silence;
322    return AUBIO_OK;
323  } else {
324    AUBIO_ERR("pitch: could not set silence to %.2f", silence);
325    return AUBIO_FAIL;
326  }
327}
328
329smpl_t
330aubio_pitch_get_silence (aubio_pitch_t * p)
331{
332  return p->silence;
333}
334
335
336/* do method, calling the detection callback, then the conversion callback */
337void
338aubio_pitch_do (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
339{
340  p->detect_cb (p, ibuf, obuf);
341  if (aubio_silence_detection(ibuf, p->silence) == 1) {
342    obuf->data[0] = 0.;
343  }
344  obuf->data[0] = p->conv_cb (obuf->data[0], p->samplerate, p->bufsize);
345}
346
347/* do method for each algorithm */
348void
349aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
350{
351  aubio_filter_do (p->filter, ibuf);
352  aubio_pvoc_do (p->pv, ibuf, p->fftgrain);
353  aubio_pitchmcomb_do (p->p_object, p->fftgrain, obuf);
354  obuf->data[0] = aubio_bintofreq (obuf->data[0], p->samplerate, p->bufsize);
355}
356
357void
358aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
359{
360  smpl_t pitch = 0.;
361  aubio_pitch_slideblock (p, ibuf);
362  aubio_pitchyin_do (p->p_object, p->buf, obuf);
363  pitch = obuf->data[0];
364  if (pitch > 0) {
365    pitch = p->samplerate / (pitch + 0.);
366  } else {
367    pitch = 0.;
368  }
369  obuf->data[0] = pitch;
370}
371
372
373void
374aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
375{
376  smpl_t pitch = 0.;
377  aubio_pitch_slideblock (p, ibuf);
378  aubio_pitchyinfft_do (p->p_object, p->buf, obuf);
379  pitch = obuf->data[0];
380  if (pitch > 0) {
381    pitch = p->samplerate / (pitch + 0.);
382  } else {
383    pitch = 0.;
384  }
385  obuf->data[0] = pitch;
386}
387
388void
389aubio_pitch_do_specacf (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out)
390{
391  smpl_t pitch = 0., period;
392  aubio_pitch_slideblock (p, ibuf);
393  aubio_pitchspecacf_do (p->p_object, p->buf, out);
394  //out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize);
395  period = out->data[0];
396  if (period > 0) {
397    pitch = p->samplerate / period;
398  } else {
399    pitch = 0.;
400  }
401  out->data[0] = pitch;
402}
403
404void
405aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out)
406{
407  aubio_pitch_slideblock (p, ibuf);
408  aubio_pitchfcomb_do (p->p_object, p->buf, out);
409  out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize);
410}
411
412void
413aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out)
414{
415  smpl_t period, pitch = 0.;
416  aubio_pitch_slideblock (p, ibuf);
417  aubio_pitchschmitt_do (p->p_object, p->buf, out);
418  period = out->data[0];
419  if (period > 0) {
420    pitch = p->samplerate / period;
421  } else {
422    pitch = 0.;
423  }
424  out->data[0] = pitch;
425}
426
427/* conversion callbacks */
428smpl_t
429freqconvbin(smpl_t f, uint_t samplerate, uint_t bufsize)
430{
431  return aubio_freqtobin(f, samplerate, bufsize);
432}
433
434smpl_t
435freqconvmidi (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED)
436{
437  return aubio_freqtomidi (f);
438}
439
440smpl_t
441freqconvpass (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED)
442{
443  return f;
444}
445
446/* confidence callbacks */
447smpl_t
448aubio_pitch_get_confidence (aubio_pitch_t * p)
449{
450  if (p->conf_cb) {
451    return p->conf_cb(p->p_object);
452  }
453  return 0.;
454}
Note: See TracBrowser for help on using the repository browser.