source: src/pitch/pitch.c @ 218f670

sampler
Last change on this file since 218f670 was 3f512b8, checked in by Paul Brossier <piem@piem.org>, 8 years ago

src/pitch/pitch.c: return NULL if pitch_mode is NULL

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