source: src/pitch/pitch.c @ 4b16721

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/timestretchfix/ffmpeg5
Last change on this file since 4b16721 was c268bf2, checked in by Paul Brossier <piem@piem.org>, 8 years ago

src/pitch/pitch.c: add yinfast

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