source: src/pitch/pitch.c @ f5699b9

feature/crepe
Last change on this file since f5699b9 was f5699b9, checked in by Paul Brossier <piem@piem.org>, 2 years ago

[pitch] add crepe

  • Property mode set to 100644
File size: 17.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/pitchyinfast.h"
36#include "pitch/pitchspecacf.h"
37#include "pitch/pitch.h"
38
39#define DEFAULT_PITCH_SILENCE -50.
40
41/** pitch detection algorithms */
42typedef enum
43{
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 */
51  aubio_pitcht_crepe,      /**< `crepe`, convolutional neural network */
52  aubio_pitcht_default
53    = aubio_pitcht_yinfft, /**< `default` */
54} aubio_pitch_type;
55
56/** pitch detection output modes */
57typedef enum
58{
59  aubio_pitchm_freq,   /**< Frequency (Hz) */
60  aubio_pitchm_midi,   /**< MIDI note (0.,127) */
61  aubio_pitchm_cent,   /**< Cent */
62  aubio_pitchm_bin,    /**< Frequency bin (0,bufsize) */
63  aubio_pitchm_default = aubio_pitchm_freq, /**< the one used when "default" is asked */
64} aubio_pitch_mode;
65
66/** callback to get pitch candidate, defined below */
67typedef void (*aubio_pitch_detect_t) (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
68
69/** callback to convert pitch from one unit to another, defined below */
70typedef smpl_t(*aubio_pitch_convert_t) (smpl_t value, uint_t samplerate, uint_t bufsize);
71
72/** callback to fetch the confidence of the algorithm */
73typedef smpl_t (*aubio_pitch_get_conf_t) (void * p);
74
75/** generic pitch detection structure */
76struct _aubio_pitch_t
77{
78  aubio_pitch_type type;          /**< pitch detection mode */
79  aubio_pitch_mode mode;          /**< pitch detection output mode */
80  uint_t samplerate;              /**< samplerate */
81  uint_t bufsize;                 /**< buffer size */
82  void *p_object;                 /**< pointer to pitch object */
83  aubio_filter_t *filter;         /**< filter */
84  fvec_t *filtered;               /**< filtered input */
85  aubio_pvoc_t *pv;               /**< phase vocoder for mcomb */
86  cvec_t *fftgrain;               /**< spectral frame for mcomb */
87  fvec_t *buf;                    /**< temporary buffer for yin */
88  aubio_pitch_detect_t detect_cb; /**< callback to get the pitch candidates */
89  aubio_pitch_convert_t conv_cb;  /**< callback to convert it to the desired unit */
90  aubio_pitch_get_conf_t conf_cb; /**< pointer to the current confidence callback */
91  smpl_t silence;                 /**< silence threshold */
92};
93
94/* callback functions for pitch detection */
95static void aubio_pitch_do_mcomb (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
96static void aubio_pitch_do_yin (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
97static void aubio_pitch_do_schmitt (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
98static void aubio_pitch_do_fcomb (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
99static void aubio_pitch_do_yinfft (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
100static void aubio_pitch_do_yinfast (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
101static void aubio_pitch_do_specacf (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
102static void aubio_pitch_do_crepe (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf);
103
104/* internal functions for frequency conversion */
105static smpl_t freqconvbin (smpl_t f, uint_t samplerate, uint_t bufsize);
106static smpl_t freqconvmidi (smpl_t f, uint_t samplerate, uint_t bufsize);
107static smpl_t freqconvpass (smpl_t f, uint_t samplerate, uint_t bufsize);
108
109typedef struct _aubio_pitch_crepe_t aubio_pitch_crepe_t;
110extern aubio_pitch_crepe_t *new_aubio_pitch_crepe(void);
111extern void aubio_pitch_crepe_do(aubio_pitch_crepe_t *t, fvec_t *input, fvec_t *out);
112extern void del_aubio_pitch_crepe(aubio_pitch_crepe_t *t);
113extern smpl_t aubio_pitch_crepe_get_confidence (aubio_pitch_crepe_t * o);
114uint_t aubio_pitch_crepe_set_tolerance(aubio_pitch_crepe_t * o, smpl_t
115    tolerance);
116smpl_t aubio_pitch_crepe_get_tolerance (aubio_pitch_crepe_t * o);
117
118/* adapter to stack ibuf new samples at the end of buf, and trim `buf` to `bufsize` */
119void aubio_pitch_slideblock (aubio_pitch_t * p, const fvec_t * ibuf);
120
121
122aubio_pitch_t *
123new_aubio_pitch (const char_t * pitch_mode,
124    uint_t bufsize, uint_t hopsize, uint_t samplerate)
125{
126  aubio_pitch_t *p = AUBIO_NEW (aubio_pitch_t);
127  aubio_pitch_type pitch_type;
128  if (pitch_mode == NULL) {
129    AUBIO_ERR ("pitch: can not use ‘NULL‘ for pitch detection method\n");
130    goto beach;
131  }
132  if (strcmp (pitch_mode, "mcomb") == 0)
133    pitch_type = aubio_pitcht_mcomb;
134  else if (strcmp (pitch_mode, "yinfast") == 0)
135    pitch_type = aubio_pitcht_yinfast;
136  else if (strcmp (pitch_mode, "yinfft") == 0)
137    pitch_type = aubio_pitcht_yinfft;
138  else if (strcmp (pitch_mode, "yin") == 0)
139    pitch_type = aubio_pitcht_yin;
140  else if (strcmp (pitch_mode, "schmitt") == 0)
141    pitch_type = aubio_pitcht_schmitt;
142  else if (strcmp (pitch_mode, "fcomb") == 0)
143    pitch_type = aubio_pitcht_fcomb;
144  else if (strcmp (pitch_mode, "specacf") == 0)
145    pitch_type = aubio_pitcht_specacf;
146  else if (strcmp (pitch_mode, "crepe") == 0)
147    pitch_type = aubio_pitcht_crepe;
148  else if (strcmp (pitch_mode, "default") == 0)
149    pitch_type = aubio_pitcht_default;
150  else {
151    AUBIO_ERR ("pitch: unknown pitch detection method ‘%s’\n", pitch_mode);
152    goto beach;
153  }
154
155  // check parameters are valid
156  if ((sint_t)hopsize < 1) {
157    AUBIO_ERR("pitch: got hopsize %d, but can not be < 1\n", hopsize);
158    goto beach;
159  } else if ((sint_t)bufsize < 1) {
160    AUBIO_ERR("pitch: got buffer_size %d, but can not be < 1\n", bufsize);
161    goto beach;
162  } else if (bufsize < hopsize) {
163    AUBIO_ERR("pitch: hop size (%d) is larger than win size (%d)\n", hopsize, bufsize);
164    goto beach;
165  } else if ((sint_t)samplerate < 1) {
166    AUBIO_ERR("pitch: samplerate (%d) can not be < 1\n", samplerate);
167    goto beach;
168  }
169
170  p->samplerate = samplerate;
171  p->type = pitch_type;
172  aubio_pitch_set_unit (p, "default");
173  p->bufsize = bufsize;
174  p->silence = DEFAULT_PITCH_SILENCE;
175  p->conf_cb = NULL;
176  switch (p->type) {
177    case aubio_pitcht_yin:
178      p->buf = new_fvec (bufsize);
179      p->p_object = new_aubio_pitchyin (bufsize);
180      if (!p->p_object) goto beach;
181      p->detect_cb = aubio_pitch_do_yin;
182      p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyin_get_confidence;
183      aubio_pitchyin_set_tolerance (p->p_object, 0.15);
184      break;
185    case aubio_pitcht_mcomb:
186      p->filtered = new_fvec (hopsize);
187      p->pv = new_aubio_pvoc (bufsize, hopsize);
188      if (!p->pv) goto beach;
189      p->fftgrain = new_cvec (bufsize);
190      p->p_object = new_aubio_pitchmcomb (bufsize, hopsize);
191      p->filter = new_aubio_filter_c_weighting (samplerate);
192      p->detect_cb = aubio_pitch_do_mcomb;
193      break;
194    case aubio_pitcht_fcomb:
195      p->buf = new_fvec (bufsize);
196      p->p_object = new_aubio_pitchfcomb (bufsize, hopsize);
197      if (!p->p_object) goto beach;
198      p->detect_cb = aubio_pitch_do_fcomb;
199      break;
200    case aubio_pitcht_schmitt:
201      p->buf = new_fvec (bufsize);
202      p->p_object = new_aubio_pitchschmitt (bufsize);
203      p->detect_cb = aubio_pitch_do_schmitt;
204      break;
205    case aubio_pitcht_yinfft:
206      p->buf = new_fvec (bufsize);
207      p->p_object = new_aubio_pitchyinfft (samplerate, bufsize);
208      if (!p->p_object) goto beach;
209      p->detect_cb = aubio_pitch_do_yinfft;
210      p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyinfft_get_confidence;
211      aubio_pitchyinfft_set_tolerance (p->p_object, 0.85);
212      break;
213    case aubio_pitcht_yinfast:
214      p->buf = new_fvec (bufsize);
215      p->p_object = new_aubio_pitchyinfast (bufsize);
216      if (!p->p_object) goto beach;
217      p->detect_cb = aubio_pitch_do_yinfast;
218      p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyinfast_get_confidence;
219      aubio_pitchyinfast_set_tolerance (p->p_object, 0.15);
220      break;
221    case aubio_pitcht_specacf:
222      p->buf = new_fvec (bufsize);
223      p->p_object = new_aubio_pitchspecacf (bufsize);
224      if (!p->p_object) goto beach;
225      p->detect_cb = aubio_pitch_do_specacf;
226      p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchspecacf_get_tolerance;
227      aubio_pitchspecacf_set_tolerance (p->p_object, 0.85);
228      break;
229    case aubio_pitcht_crepe:
230      p->buf = new_fvec (bufsize);
231      p->p_object = new_aubio_pitch_crepe();
232      if (!p->p_object) goto beach;
233      p->detect_cb = aubio_pitch_do_crepe;
234      p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitch_crepe_get_confidence;
235      //aubio_pitch_crepe_set_tolerance (p->p_object, 0.85);
236      break;
237    default:
238      break;
239  }
240  return p;
241
242beach:
243  if (p->filtered) del_fvec(p->filtered);
244  if (p->buf) del_fvec(p->buf);
245  AUBIO_FREE(p);
246  return NULL;
247}
248
249void
250del_aubio_pitch (aubio_pitch_t * p)
251{
252  switch (p->type) {
253    case aubio_pitcht_yin:
254      del_fvec (p->buf);
255      del_aubio_pitchyin (p->p_object);
256      break;
257    case aubio_pitcht_mcomb:
258      del_fvec (p->filtered);
259      del_aubio_pvoc (p->pv);
260      del_cvec (p->fftgrain);
261      del_aubio_filter (p->filter);
262      del_aubio_pitchmcomb (p->p_object);
263      break;
264    case aubio_pitcht_schmitt:
265      del_fvec (p->buf);
266      del_aubio_pitchschmitt (p->p_object);
267      break;
268    case aubio_pitcht_fcomb:
269      del_fvec (p->buf);
270      del_aubio_pitchfcomb (p->p_object);
271      break;
272    case aubio_pitcht_yinfft:
273      del_fvec (p->buf);
274      del_aubio_pitchyinfft (p->p_object);
275      break;
276    case aubio_pitcht_yinfast:
277      del_fvec (p->buf);
278      del_aubio_pitchyinfast (p->p_object);
279      break;
280    case aubio_pitcht_specacf:
281      del_fvec (p->buf);
282      del_aubio_pitchspecacf (p->p_object);
283      break;
284    case aubio_pitcht_crepe:
285      del_fvec (p->buf);
286      del_aubio_pitch_crepe (p->p_object);
287      break;
288    default:
289      break;
290  }
291  AUBIO_FREE (p);
292}
293
294void
295aubio_pitch_slideblock (aubio_pitch_t * p, const fvec_t * ibuf)
296{
297  uint_t overlap_size = p->buf->length - ibuf->length;
298#if 1 //!HAVE_MEMCPY_HACKS
299  uint_t j;
300  for (j = 0; j < overlap_size; j++) {
301    p->buf->data[j] = p->buf->data[j + ibuf->length];
302  }
303  for (j = 0; j < ibuf->length; j++) {
304    p->buf->data[j + overlap_size] = ibuf->data[j];
305  }
306#else
307  smpl_t *data = p->buf->data;
308  smpl_t *newdata = ibuf->data;
309  memmove(data, data + ibuf->length, overlap_size);
310  memcpy(data + overlap_size, newdata, ibuf->length);
311#endif
312}
313
314uint_t
315aubio_pitch_set_unit (aubio_pitch_t * p, const char_t * pitch_unit)
316{
317  uint_t err = AUBIO_OK;
318  aubio_pitch_mode pitch_mode;
319  if (strcmp (pitch_unit, "freq") == 0)
320    pitch_mode = aubio_pitchm_freq;
321  else if (strcmp (pitch_unit, "hertz") == 0)
322    pitch_mode = aubio_pitchm_freq;
323  else if (strcmp (pitch_unit, "Hertz") == 0)
324    pitch_mode = aubio_pitchm_freq;
325  else if (strcmp (pitch_unit, "Hz") == 0)
326    pitch_mode = aubio_pitchm_freq;
327  else if (strcmp (pitch_unit, "f0") == 0)
328    pitch_mode = aubio_pitchm_freq;
329  else if (strcmp (pitch_unit, "midi") == 0)
330    pitch_mode = aubio_pitchm_midi;
331  else if (strcmp (pitch_unit, "cent") == 0)
332    pitch_mode = aubio_pitchm_cent;
333  else if (strcmp (pitch_unit, "bin") == 0)
334    pitch_mode = aubio_pitchm_bin;
335  else if (strcmp (pitch_unit, "default") == 0)
336    pitch_mode = aubio_pitchm_default;
337  else {
338    AUBIO_WRN("pitch: unknown pitch detection unit ‘%s’, using default\n",
339        pitch_unit);
340    pitch_mode = aubio_pitchm_default;
341    err = AUBIO_FAIL;
342  }
343  p->mode = pitch_mode;
344  switch (p->mode) {
345    case aubio_pitchm_freq:
346      p->conv_cb = freqconvpass;
347      break;
348    case aubio_pitchm_midi:
349      p->conv_cb = freqconvmidi;
350      break;
351    case aubio_pitchm_cent:
352      /* bug: not implemented */
353      p->conv_cb = freqconvmidi;
354      break;
355    case aubio_pitchm_bin:
356      p->conv_cb = freqconvbin;
357      break;
358    default:
359      break;
360  }
361  return err;
362}
363
364uint_t
365aubio_pitch_set_tolerance (aubio_pitch_t * p, smpl_t tol)
366{
367  switch (p->type) {
368    case aubio_pitcht_yin:
369      aubio_pitchyin_set_tolerance (p->p_object, tol);
370      break;
371    case aubio_pitcht_yinfft:
372      aubio_pitchyinfft_set_tolerance (p->p_object, tol);
373      break;
374    case aubio_pitcht_yinfast:
375      aubio_pitchyinfast_set_tolerance (p->p_object, tol);
376      break;
377    case aubio_pitcht_crepe:
378      aubio_pitch_crepe_set_tolerance (p->p_object, tol);
379      break;
380    default:
381      break;
382  }
383  return AUBIO_OK;
384}
385
386smpl_t
387aubio_pitch_get_tolerance (aubio_pitch_t * p)
388{
389  smpl_t tolerance = 1.;
390  switch (p->type) {
391    case aubio_pitcht_yin:
392      tolerance = aubio_pitchyin_get_tolerance (p->p_object);
393      break;
394    case aubio_pitcht_yinfft:
395      tolerance = aubio_pitchyinfft_get_tolerance (p->p_object);
396      break;
397    case aubio_pitcht_yinfast:
398      tolerance = aubio_pitchyinfast_get_tolerance (p->p_object);
399      break;
400    case aubio_pitcht_crepe:
401      tolerance = aubio_pitch_crepe_get_tolerance (p->p_object);
402      break;
403    default:
404      break;
405  }
406  return tolerance;
407}
408
409uint_t
410aubio_pitch_set_silence (aubio_pitch_t * p, smpl_t silence)
411{
412  if (silence <= 0 && silence >= -200) {
413    p->silence = silence;
414    return AUBIO_OK;
415  } else {
416    AUBIO_WRN("pitch: could not set silence to %.2f\n", silence);
417    return AUBIO_FAIL;
418  }
419}
420
421smpl_t
422aubio_pitch_get_silence (aubio_pitch_t * p)
423{
424  return p->silence;
425}
426
427
428/* do method, calling the detection callback, then the conversion callback */
429void
430aubio_pitch_do (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf)
431{
432  p->detect_cb (p, ibuf, obuf);
433  if (aubio_silence_detection(ibuf, p->silence) == 1) {
434    obuf->data[0] = 0.;
435  }
436  obuf->data[0] = p->conv_cb (obuf->data[0], p->samplerate, p->bufsize);
437}
438
439/* do method for each algorithm */
440void
441aubio_pitch_do_mcomb (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf)
442{
443  aubio_filter_do_outplace (p->filter, ibuf, p->filtered);
444  aubio_pvoc_do (p->pv, ibuf, p->fftgrain);
445  aubio_pitchmcomb_do (p->p_object, p->fftgrain, obuf);
446  obuf->data[0] = aubio_bintofreq (obuf->data[0], p->samplerate, p->bufsize);
447}
448
449void
450aubio_pitch_do_yin (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf)
451{
452  smpl_t pitch = 0.;
453  aubio_pitch_slideblock (p, ibuf);
454  aubio_pitchyin_do (p->p_object, p->buf, obuf);
455  pitch = obuf->data[0];
456  if (pitch > 0) {
457    pitch = p->samplerate / (pitch + 0.);
458  } else {
459    pitch = 0.;
460  }
461  obuf->data[0] = pitch;
462}
463
464
465void
466aubio_pitch_do_yinfft (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf)
467{
468  smpl_t pitch = 0.;
469  aubio_pitch_slideblock (p, ibuf);
470  aubio_pitchyinfft_do (p->p_object, p->buf, obuf);
471  pitch = obuf->data[0];
472  if (pitch > 0) {
473    pitch = p->samplerate / (pitch + 0.);
474  } else {
475    pitch = 0.;
476  }
477  obuf->data[0] = pitch;
478}
479
480void
481aubio_pitch_do_yinfast (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * obuf)
482{
483  smpl_t pitch = 0.;
484  aubio_pitch_slideblock (p, ibuf);
485  aubio_pitchyinfast_do (p->p_object, p->buf, obuf);
486  pitch = obuf->data[0];
487  if (pitch > 0) {
488    pitch = p->samplerate / (pitch + 0.);
489  } else {
490    pitch = 0.;
491  }
492  obuf->data[0] = pitch;
493}
494
495void
496aubio_pitch_do_specacf (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * out)
497{
498  smpl_t pitch = 0., period;
499  aubio_pitch_slideblock (p, ibuf);
500  aubio_pitchspecacf_do (p->p_object, p->buf, out);
501  //out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize);
502  period = out->data[0];
503  if (period > 0) {
504    pitch = p->samplerate / period;
505  } else {
506    pitch = 0.;
507  }
508  out->data[0] = pitch;
509}
510
511void
512aubio_pitch_do_crepe (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * out)
513{
514  //smpl_t pitch = 0., period;
515  aubio_pitch_slideblock (p, ibuf);
516  aubio_pitch_crepe_do(p->p_object, p->buf, out);
517  out->data[0] = aubio_miditofreq(out->data[0]);
518}
519
520void
521aubio_pitch_do_fcomb (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * out)
522{
523  aubio_pitch_slideblock (p, ibuf);
524  aubio_pitchfcomb_do (p->p_object, p->buf, out);
525  out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize);
526}
527
528void
529aubio_pitch_do_schmitt (aubio_pitch_t * p, const fvec_t * ibuf, fvec_t * out)
530{
531  smpl_t period, pitch = 0.;
532  aubio_pitch_slideblock (p, ibuf);
533  aubio_pitchschmitt_do (p->p_object, p->buf, out);
534  period = out->data[0];
535  if (period > 0) {
536    pitch = p->samplerate / period;
537  } else {
538    pitch = 0.;
539  }
540  out->data[0] = pitch;
541}
542
543/* conversion callbacks */
544smpl_t
545freqconvbin(smpl_t f, uint_t samplerate, uint_t bufsize)
546{
547  return aubio_freqtobin(f, samplerate, bufsize);
548}
549
550smpl_t
551freqconvmidi (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED)
552{
553  return aubio_freqtomidi (f);
554}
555
556smpl_t
557freqconvpass (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED)
558{
559  return f;
560}
561
562/* confidence callbacks */
563smpl_t
564aubio_pitch_get_confidence (aubio_pitch_t * p)
565{
566  if (p->conf_cb) {
567    return p->conf_cb(p->p_object);
568  }
569  return 0.;
570}
Note: See TracBrowser for help on using the repository browser.