source: src/pitch/pitch.c @ 5d16185

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 5d16185 was b130600, checked in by Paul Brossier <piem@piem.org>, 12 years ago

src/pitch/pitch.c: clarify, use one void pointer for all algorithms

  • Property mode set to 100644
File size: 10.9 KB
RevLine 
[96fb8ad]1/*
[e6a78ea]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*/
[96fb8ad]20
21#include "aubio_priv.h"
[6c7d49b]22#include "fvec.h"
23#include "cvec.h"
[a4364b8]24#include "lvec.h"
[96fb8ad]25#include "mathutils.h"
[83963b3]26#include "musicutils.h"
27#include "spectral/phasevoc.h"
[a695854]28#include "temporal/filter.h"
[c159aeb]29#include "temporal/c_weighting.h"
[2d8cffa]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"
[ca1abdd]35#include "pitch/pitch.h"
[96fb8ad]36
[a64ef1d]37/** pitch detection algorithms */
[fddfa64]38typedef enum
39{
[a64ef1d]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` */
[ca1abdd]47} aubio_pitch_type;
[fe163ad]48
[a64ef1d]49/** pitch detection output modes */
[fddfa64]50typedef enum
51{
[fe163ad]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 */
[ca1abdd]57} aubio_pitch_mode;
[fe163ad]58
[b130600]59/** callback to get pitch candidate, defined below */
60typedef void (*aubio_pitch_detect_t) (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
[6d4ec49]61
[b130600]62/** callback to convert pitch from one unit to another, defined below */
63typedef smpl_t(*aubio_pitch_convert_t) (smpl_t value, uint_t samplerate, uint_t bufsize);
[5284e0d]64
[b130600]65/** callback to fetch the confidence of the algorithm */
66typedef smpl_t (*aubio_pitch_get_conf_t) (void * p);
[f44b111]67
[475da2f]68/** generic pitch detection structure */
[fddfa64]69struct _aubio_pitch_t
70{
[b130600]71  aubio_pitch_type type;          /**< pitch detection mode */
72  aubio_pitch_mode mode;          /**< pitch detection output mode */
73  uint_t samplerate;              /**< samplerate */
[475da2f]74  uint_t bufsize;                 /**< buffer size */
[b130600]75  void *p_object;                 /**< pointer to pitch object */
[fddfa64]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 */
[b130600]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 */
[96fb8ad]83};
84
[b130600]85/* callback functions for pitch detection */
86static void aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
87static void aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
88static void aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
89static void aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
90static void aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
[3ec9d9c]91
[b130600]92/* conversion functions for frequency conversions */
93smpl_t freqconvbin (smpl_t f, uint_t samplerate, uint_t bufsize);
94smpl_t freqconvmidi (smpl_t f, uint_t samplerate, uint_t bufsize);
95smpl_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` */
98void aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf);
[3ec9d9c]99
100
[ca1abdd]101aubio_pitch_t *
102new_aubio_pitch (char_t * pitch_mode,
[168337e]103    uint_t bufsize, uint_t hopsize, uint_t samplerate)
[96fb8ad]104{
[fddfa64]105  aubio_pitch_t *p = AUBIO_NEW (aubio_pitch_t);
[ca1abdd]106  aubio_pitch_type pitch_type;
[fe163ad]107  if (strcmp (pitch_mode, "mcomb") == 0)
[fddfa64]108    pitch_type = aubio_pitcht_mcomb;
[fe163ad]109  else if (strcmp (pitch_mode, "yinfft") == 0)
[97a5878b]110    pitch_type = aubio_pitcht_yinfft;
[fe163ad]111  else if (strcmp (pitch_mode, "yin") == 0)
[fddfa64]112    pitch_type = aubio_pitcht_yin;
[fe163ad]113  else if (strcmp (pitch_mode, "schmitt") == 0)
[fddfa64]114    pitch_type = aubio_pitcht_schmitt;
[fe163ad]115  else if (strcmp (pitch_mode, "fcomb") == 0)
[fddfa64]116    pitch_type = aubio_pitcht_fcomb;
[fe163ad]117  else if (strcmp (pitch_mode, "default") == 0)
[fddfa64]118    pitch_type = aubio_pitcht_default;
[fe163ad]119  else {
[fddfa64]120    AUBIO_ERR ("unknown pitch detection method %s, using default.\n",
121        pitch_mode);
122    pitch_type = aubio_pitcht_default;
[fe163ad]123  }
[b130600]124  p->samplerate = samplerate;
[fe163ad]125  p->type = pitch_type;
[ca1abdd]126  aubio_pitch_set_unit (p, "default");
[6d4ec49]127  p->bufsize = bufsize;
[b130600]128  p->conf_cb = NULL;
[fddfa64]129  switch (p->type) {
[ca1abdd]130    case aubio_pitcht_yin:
[168337e]131      p->buf = new_fvec (bufsize);
[b130600]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);
[6d4ec49]136      break;
[ca1abdd]137    case aubio_pitcht_mcomb:
[168337e]138      p->pv = new_aubio_pvoc (bufsize, hopsize);
139      p->fftgrain = new_cvec (bufsize);
[b130600]140      p->p_object = new_aubio_pitchmcomb (bufsize, hopsize);
[168337e]141      p->filter = new_aubio_filter_c_weighting (samplerate);
[b130600]142      p->detect_cb = aubio_pitch_do_mcomb;
[6d4ec49]143      break;
[ca1abdd]144    case aubio_pitcht_fcomb:
[168337e]145      p->buf = new_fvec (bufsize);
[b130600]146      p->p_object = new_aubio_pitchfcomb (bufsize, hopsize);
147      p->detect_cb = aubio_pitch_do_fcomb;
[6d4ec49]148      break;
[ca1abdd]149    case aubio_pitcht_schmitt:
[168337e]150      p->buf = new_fvec (bufsize);
[b130600]151      p->p_object = new_aubio_pitchschmitt (bufsize);
152      p->detect_cb = aubio_pitch_do_schmitt;
[6d4ec49]153      break;
[ca1abdd]154    case aubio_pitcht_yinfft:
[168337e]155      p->buf = new_fvec (bufsize);
[b130600]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);
[6d4ec49]160      break;
161    default:
162      break;
163  }
164  return p;
[96fb8ad]165}
166
[fddfa64]167void
168del_aubio_pitch (aubio_pitch_t * p)
169{
170  switch (p->type) {
[ca1abdd]171    case aubio_pitcht_yin:
[fddfa64]172      del_fvec (p->buf);
[b130600]173      del_aubio_pitchyin (p->p_object);
[6d4ec49]174      break;
[ca1abdd]175    case aubio_pitcht_mcomb:
[fddfa64]176      del_aubio_pvoc (p->pv);
177      del_cvec (p->fftgrain);
178      del_aubio_filter (p->filter);
[b130600]179      del_aubio_pitchmcomb (p->p_object);
[6d4ec49]180      break;
[ca1abdd]181    case aubio_pitcht_schmitt:
[fddfa64]182      del_fvec (p->buf);
[b130600]183      del_aubio_pitchschmitt (p->p_object);
[6d4ec49]184      break;
[ca1abdd]185    case aubio_pitcht_fcomb:
[fddfa64]186      del_fvec (p->buf);
[b130600]187      del_aubio_pitchfcomb (p->p_object);
[6d4ec49]188      break;
[ca1abdd]189    case aubio_pitcht_yinfft:
[fddfa64]190      del_fvec (p->buf);
[b130600]191      del_aubio_pitchyinfft (p->p_object);
[6d4ec49]192      break;
193    default:
194      break;
195  }
[fddfa64]196  AUBIO_FREE (p);
[96fb8ad]197}
198
[fddfa64]199void
200aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf)
201{
[168337e]202  uint_t j = 0, overlap_size = 0;
[fddfa64]203  overlap_size = p->buf->length - ibuf->length;
[168337e]204  for (j = 0; j < overlap_size; j++) {
205    p->buf->data[j] = p->buf->data[j + ibuf->length];
[6d4ec49]206  }
[168337e]207  for (j = 0; j < ibuf->length; j++) {
208    p->buf->data[j + overlap_size] = ibuf->data[j];
[6d4ec49]209  }
[651b97e]210}
211
[fddfa64]212uint_t
213aubio_pitch_set_unit (aubio_pitch_t * p, char_t * pitch_unit)
214{
[ca1abdd]215  aubio_pitch_mode pitch_mode;
[fe163ad]216  if (strcmp (pitch_unit, "freq") == 0)
[fddfa64]217    pitch_mode = aubio_pitchm_freq;
[fe163ad]218  else if (strcmp (pitch_unit, "midi") == 0)
[fddfa64]219    pitch_mode = aubio_pitchm_midi;
[fe163ad]220  else if (strcmp (pitch_unit, "cent") == 0)
[fddfa64]221    pitch_mode = aubio_pitchm_cent;
[fe163ad]222  else if (strcmp (pitch_unit, "bin") == 0)
[fddfa64]223    pitch_mode = aubio_pitchm_bin;
[fe163ad]224  else if (strcmp (pitch_unit, "default") == 0)
[fddfa64]225    pitch_mode = aubio_pitchm_default;
[fe163ad]226  else {
[fddfa64]227    AUBIO_ERR ("unknown pitch detection unit %s, using default\n", pitch_unit);
228    pitch_mode = aubio_pitchm_default;
[fe163ad]229  }
230  p->mode = pitch_mode;
[fddfa64]231  switch (p->mode) {
[fe163ad]232    case aubio_pitchm_freq:
[b130600]233      p->conv_cb = freqconvpass;
[fe163ad]234      break;
235    case aubio_pitchm_midi:
[b130600]236      p->conv_cb = freqconvmidi;
[fe163ad]237      break;
238    case aubio_pitchm_cent:
239      /* bug: not implemented */
[b130600]240      p->conv_cb = freqconvmidi;
[fe163ad]241      break;
242    case aubio_pitchm_bin:
[b130600]243      p->conv_cb = freqconvbin;
[fe163ad]244      break;
245    default:
246      break;
247  }
[93177fa]248  return AUBIO_OK;
[fe163ad]249}
250
[fddfa64]251uint_t
252aubio_pitch_set_tolerance (aubio_pitch_t * p, smpl_t tol)
253{
254  switch (p->type) {
[ca1abdd]255    case aubio_pitcht_yin:
[b130600]256      aubio_pitchyin_set_tolerance (p->p_object, tol);
[7a6cbbe]257      break;
[ca1abdd]258    case aubio_pitcht_yinfft:
[b130600]259      aubio_pitchyinfft_set_tolerance (p->p_object, tol);
[7a6cbbe]260      break;
261    default:
262      break;
263  }
[93177fa]264  return AUBIO_OK;
[f8a38c5]265}
266
[b130600]267
268/* do method, calling the detection callback, then the conversion callback */
[fddfa64]269void
270aubio_pitch_do (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
271{
[b130600]272  p->detect_cb (p, ibuf, obuf);
273  obuf->data[0] = p->conv_cb (obuf->data[0], p->samplerate, p->bufsize);
[c078336]274}
275
[b130600]276/* do method for each algorithm */
[fddfa64]277void
278aubio_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);
[b130600]282  aubio_pitchmcomb_do (p->p_object, p->fftgrain, obuf);
283  obuf->data[0] = aubio_bintofreq (obuf->data[0], p->samplerate, p->bufsize);
[c078336]284}
285
[fddfa64]286void
287aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
288{
[6d4ec49]289  smpl_t pitch = 0.;
[fddfa64]290  aubio_pitch_slideblock (p, ibuf);
[b130600]291  aubio_pitchyin_do (p->p_object, p->buf, obuf);
[168337e]292  pitch = obuf->data[0];
293  if (pitch > 0) {
[b130600]294    pitch = p->samplerate / (pitch + 0.);
[168337e]295  } else {
296    pitch = 0.;
[6d4ec49]297  }
[168337e]298  obuf->data[0] = pitch;
[c078336]299}
300
301
[fddfa64]302void
303aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
304{
[6d4ec49]305  smpl_t pitch = 0.;
[fddfa64]306  aubio_pitch_slideblock (p, ibuf);
[b130600]307  aubio_pitchyinfft_do (p->p_object, p->buf, obuf);
[168337e]308  pitch = obuf->data[0];
309  if (pitch > 0) {
[b130600]310    pitch = p->samplerate / (pitch + 0.);
[168337e]311  } else {
312    pitch = 0.;
[6d4ec49]313  }
[168337e]314  obuf->data[0] = pitch;
[650e39b]315}
316
[fddfa64]317void
318aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out)
319{
320  aubio_pitch_slideblock (p, ibuf);
[b130600]321  aubio_pitchfcomb_do (p->p_object, p->buf, out);
322  out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize);
[c078336]323}
324
[fddfa64]325void
326aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out)
327{
[7a6cbbe]328  smpl_t period, pitch = 0.;
[fddfa64]329  aubio_pitch_slideblock (p, ibuf);
[b130600]330  aubio_pitchschmitt_do (p->p_object, p->buf, out);
[168337e]331  period = out->data[0];
332  if (period > 0) {
[b130600]333    pitch = p->samplerate / period;
[168337e]334  } else {
335    pitch = 0.;
[7a6cbbe]336  }
[168337e]337  out->data[0] = pitch;
[96fb8ad]338}
[5284e0d]339
[b130600]340/* conversion callbacks */
341smpl_t
342freqconvbin(smpl_t f, uint_t samplerate, uint_t bufsize)
343{
344  return aubio_freqtobin(f, samplerate, bufsize);
345}
346
347smpl_t
348freqconvmidi (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED)
349{
350  return aubio_freqtomidi (f);
351}
352
353smpl_t
354freqconvpass (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED)
355{
356  return f;
357}
358
[5284e0d]359/* confidence callbacks */
360smpl_t
361aubio_pitch_get_confidence (aubio_pitch_t * p)
362{
[b130600]363  if (p->conf_cb) {
364    return p->conf_cb(p->p_object);
[5284e0d]365  }
366  return 0.;
367}
Note: See TracBrowser for help on using the repository browser.