source: src/pitch/pitch.c @ 426e6f7

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

src/pitch: start adding confidence

  • Property mode set to 100644
File size: 10.7 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
[ca1abdd]59typedef void (*aubio_pitch_func_t)
[fddfa64]60  (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
[ca1abdd]61typedef smpl_t (*aubio_pitch_conv_t)
[6d4ec49]62  (smpl_t value, uint_t srate, uint_t bufsize);
63
[5284e0d]64typedef smpl_t (*aubio_conf_cb_t) (void * p);
65
[fddfa64]66void aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf);
[c078336]67
[5284e0d]68static void aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
69static void aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
70static void aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
71static void aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
72static void aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
[f44b111]73
[475da2f]74/** generic pitch detection structure */
[fddfa64]75struct _aubio_pitch_t
76{
[ca1abdd]77  aubio_pitch_type type; /**< pitch detection mode */
78  aubio_pitch_mode mode; /**< pitch detection output mode */
[475da2f]79  uint_t srate;                   /**< samplerate */
80  uint_t bufsize;                 /**< buffer size */
[fddfa64]81  aubio_pitchmcomb_t *mcomb;      /**< mcomb object */
82  aubio_pitchfcomb_t *fcomb;      /**< fcomb object */
83  aubio_pitchschmitt_t *schmitt;  /**< schmitt object */
84  aubio_pitchyinfft_t *yinfft;    /**< yinfft object */
85  aubio_pitchyin_t *yin;    /**< yinfft object */
[5284e0d]86  void *pitch;
[fddfa64]87  aubio_filter_t *filter;         /**< filter */
88  aubio_pvoc_t *pv;               /**< phase vocoder for mcomb */
89  cvec_t *fftgrain;               /**< spectral frame for mcomb */
90  fvec_t *buf;                    /**< temporary buffer for yin */
[ca1abdd]91  aubio_pitch_func_t callback; /**< pointer to current pitch detection method */
[fddfa64]92  aubio_pitch_conv_t freqconv; /**< pointer to current pitch conversion method */
[5284e0d]93  aubio_conf_cb_t confidence_callback; /**< pointer to the current confidence callback */
[96fb8ad]94};
95
[3ec9d9c]96/* convenience wrapper function for frequency unit conversions
97 * should probably be rewritten with #defines */
[fddfa64]98smpl_t freqconvbin (smpl_t f, uint_t srate, uint_t bufsize);
99smpl_t
100freqconvbin (smpl_t f, uint_t srate, uint_t bufsize)
101{
102  return aubio_freqtobin (f, srate, bufsize);
[3ec9d9c]103}
104
[fddfa64]105smpl_t freqconvmidi (smpl_t f, uint_t srate, uint_t bufsize);
106smpl_t
107freqconvmidi (smpl_t f, uint_t srate UNUSED, uint_t bufsize UNUSED)
108{
109  return aubio_freqtomidi (f);
[3ec9d9c]110}
111
[fddfa64]112smpl_t freqconvpass (smpl_t f, uint_t srate, uint_t bufsize);
113smpl_t
114freqconvpass (smpl_t f, uint_t srate UNUSED, uint_t bufsize UNUSED)
115{
[6d4ec49]116  return f;
[3ec9d9c]117}
118
[ca1abdd]119aubio_pitch_t *
120new_aubio_pitch (char_t * pitch_mode,
[168337e]121    uint_t bufsize, uint_t hopsize, uint_t samplerate)
[96fb8ad]122{
[fddfa64]123  aubio_pitch_t *p = AUBIO_NEW (aubio_pitch_t);
[ca1abdd]124  aubio_pitch_type pitch_type;
[fe163ad]125  if (strcmp (pitch_mode, "mcomb") == 0)
[fddfa64]126    pitch_type = aubio_pitcht_mcomb;
[fe163ad]127  else if (strcmp (pitch_mode, "yinfft") == 0)
[97a5878b]128    pitch_type = aubio_pitcht_yinfft;
[fe163ad]129  else if (strcmp (pitch_mode, "yin") == 0)
[fddfa64]130    pitch_type = aubio_pitcht_yin;
[fe163ad]131  else if (strcmp (pitch_mode, "schmitt") == 0)
[fddfa64]132    pitch_type = aubio_pitcht_schmitt;
[fe163ad]133  else if (strcmp (pitch_mode, "fcomb") == 0)
[fddfa64]134    pitch_type = aubio_pitcht_fcomb;
[fe163ad]135  else if (strcmp (pitch_mode, "default") == 0)
[fddfa64]136    pitch_type = aubio_pitcht_default;
[fe163ad]137  else {
[fddfa64]138    AUBIO_ERR ("unknown pitch detection method %s, using default.\n",
139        pitch_mode);
140    pitch_type = aubio_pitcht_default;
[fe163ad]141  }
[6d4ec49]142  p->srate = samplerate;
[fe163ad]143  p->type = pitch_type;
[ca1abdd]144  aubio_pitch_set_unit (p, "default");
[6d4ec49]145  p->bufsize = bufsize;
[5284e0d]146  p->confidence_callback = NULL;
[fddfa64]147  switch (p->type) {
[ca1abdd]148    case aubio_pitcht_yin:
[168337e]149      p->buf = new_fvec (bufsize);
[fddfa64]150      p->yin = new_aubio_pitchyin (bufsize);
[ca1abdd]151      p->callback = aubio_pitch_do_yin;
[5284e0d]152      p->confidence_callback = (aubio_conf_cb_t)aubio_pitchyin_get_confidence;
153      p->pitch = (void*)p->yin;
[7a6cbbe]154      aubio_pitchyin_set_tolerance (p->yin, 0.15);
[6d4ec49]155      break;
[ca1abdd]156    case aubio_pitcht_mcomb:
[168337e]157      p->pv = new_aubio_pvoc (bufsize, hopsize);
158      p->fftgrain = new_cvec (bufsize);
159      p->mcomb = new_aubio_pitchmcomb (bufsize, hopsize);
160      p->filter = new_aubio_filter_c_weighting (samplerate);
[ca1abdd]161      p->callback = aubio_pitch_do_mcomb;
[6d4ec49]162      break;
[ca1abdd]163    case aubio_pitcht_fcomb:
[168337e]164      p->buf = new_fvec (bufsize);
165      p->fcomb = new_aubio_pitchfcomb (bufsize, hopsize);
[ca1abdd]166      p->callback = aubio_pitch_do_fcomb;
[6d4ec49]167      break;
[ca1abdd]168    case aubio_pitcht_schmitt:
[168337e]169      p->buf = new_fvec (bufsize);
[fddfa64]170      p->schmitt = new_aubio_pitchschmitt (bufsize);
[ca1abdd]171      p->callback = aubio_pitch_do_schmitt;
[6d4ec49]172      break;
[ca1abdd]173    case aubio_pitcht_yinfft:
[168337e]174      p->buf = new_fvec (bufsize);
[fddfa64]175      p->yinfft = new_aubio_pitchyinfft (bufsize);
[ca1abdd]176      p->callback = aubio_pitch_do_yinfft;
[5284e0d]177      p->confidence_callback = (aubio_conf_cb_t)aubio_pitchyinfft_get_confidence;
178      p->pitch = (void*)p->yin;
[7a6cbbe]179      aubio_pitchyinfft_set_tolerance (p->yinfft, 0.85);
[6d4ec49]180      break;
181    default:
182      break;
183  }
184  return p;
[96fb8ad]185}
186
[fddfa64]187void
188del_aubio_pitch (aubio_pitch_t * p)
189{
190  switch (p->type) {
[ca1abdd]191    case aubio_pitcht_yin:
[fddfa64]192      del_fvec (p->buf);
193      del_aubio_pitchyin (p->yin);
[6d4ec49]194      break;
[ca1abdd]195    case aubio_pitcht_mcomb:
[fddfa64]196      del_aubio_pvoc (p->pv);
197      del_cvec (p->fftgrain);
198      del_aubio_filter (p->filter);
199      del_aubio_pitchmcomb (p->mcomb);
[6d4ec49]200      break;
[ca1abdd]201    case aubio_pitcht_schmitt:
[fddfa64]202      del_fvec (p->buf);
203      del_aubio_pitchschmitt (p->schmitt);
[6d4ec49]204      break;
[ca1abdd]205    case aubio_pitcht_fcomb:
[fddfa64]206      del_fvec (p->buf);
207      del_aubio_pitchfcomb (p->fcomb);
[6d4ec49]208      break;
[ca1abdd]209    case aubio_pitcht_yinfft:
[fddfa64]210      del_fvec (p->buf);
211      del_aubio_pitchyinfft (p->yinfft);
[6d4ec49]212      break;
213    default:
214      break;
215  }
[fddfa64]216  AUBIO_FREE (p);
[96fb8ad]217}
218
[fddfa64]219void
220aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf)
221{
[168337e]222  uint_t j = 0, overlap_size = 0;
[fddfa64]223  overlap_size = p->buf->length - ibuf->length;
[168337e]224  for (j = 0; j < overlap_size; j++) {
225    p->buf->data[j] = p->buf->data[j + ibuf->length];
[6d4ec49]226  }
[168337e]227  for (j = 0; j < ibuf->length; j++) {
228    p->buf->data[j + overlap_size] = ibuf->data[j];
[6d4ec49]229  }
[651b97e]230}
231
[fddfa64]232uint_t
233aubio_pitch_set_unit (aubio_pitch_t * p, char_t * pitch_unit)
234{
[ca1abdd]235  aubio_pitch_mode pitch_mode;
[fe163ad]236  if (strcmp (pitch_unit, "freq") == 0)
[fddfa64]237    pitch_mode = aubio_pitchm_freq;
[fe163ad]238  else if (strcmp (pitch_unit, "midi") == 0)
[fddfa64]239    pitch_mode = aubio_pitchm_midi;
[fe163ad]240  else if (strcmp (pitch_unit, "cent") == 0)
[fddfa64]241    pitch_mode = aubio_pitchm_cent;
[fe163ad]242  else if (strcmp (pitch_unit, "bin") == 0)
[fddfa64]243    pitch_mode = aubio_pitchm_bin;
[fe163ad]244  else if (strcmp (pitch_unit, "default") == 0)
[fddfa64]245    pitch_mode = aubio_pitchm_default;
[fe163ad]246  else {
[fddfa64]247    AUBIO_ERR ("unknown pitch detection unit %s, using default\n", pitch_unit);
248    pitch_mode = aubio_pitchm_default;
[fe163ad]249  }
250  p->mode = pitch_mode;
[fddfa64]251  switch (p->mode) {
[fe163ad]252    case aubio_pitchm_freq:
253      p->freqconv = freqconvpass;
254      break;
255    case aubio_pitchm_midi:
256      p->freqconv = freqconvmidi;
257      break;
258    case aubio_pitchm_cent:
259      /* bug: not implemented */
260      p->freqconv = freqconvmidi;
261      break;
262    case aubio_pitchm_bin:
263      p->freqconv = freqconvbin;
264      break;
265    default:
266      break;
267  }
[93177fa]268  return AUBIO_OK;
[fe163ad]269}
270
[fddfa64]271uint_t
272aubio_pitch_set_tolerance (aubio_pitch_t * p, smpl_t tol)
273{
274  switch (p->type) {
[ca1abdd]275    case aubio_pitcht_yin:
[7a6cbbe]276      aubio_pitchyin_set_tolerance (p->yin, tol);
277      break;
[ca1abdd]278    case aubio_pitcht_yinfft:
[7a6cbbe]279      aubio_pitchyinfft_set_tolerance (p->yinfft, tol);
280      break;
281    default:
282      break;
283  }
[93177fa]284  return AUBIO_OK;
[f8a38c5]285}
286
[fddfa64]287void
288aubio_pitch_do (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
289{
290  p->callback (p, ibuf, obuf);
[168337e]291  obuf->data[0] = p->freqconv (obuf->data[0], p->srate, p->bufsize);
[c078336]292}
293
[fddfa64]294void
295aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
296{
297  aubio_filter_do (p->filter, ibuf);
298  aubio_pvoc_do (p->pv, ibuf, p->fftgrain);
299  aubio_pitchmcomb_do (p->mcomb, p->fftgrain, obuf);
[168337e]300  obuf->data[0] = aubio_bintofreq (obuf->data[0], p->srate, p->bufsize);
[c078336]301}
302
[fddfa64]303void
304aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
305{
[6d4ec49]306  smpl_t pitch = 0.;
[fddfa64]307  aubio_pitch_slideblock (p, ibuf);
308  aubio_pitchyin_do (p->yin, p->buf, obuf);
[168337e]309  pitch = obuf->data[0];
310  if (pitch > 0) {
311    pitch = p->srate / (pitch + 0.);
312  } else {
313    pitch = 0.;
[6d4ec49]314  }
[168337e]315  obuf->data[0] = pitch;
[c078336]316}
317
318
[fddfa64]319void
320aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
321{
[6d4ec49]322  smpl_t pitch = 0.;
[fddfa64]323  aubio_pitch_slideblock (p, ibuf);
324  aubio_pitchyinfft_do (p->yinfft, p->buf, obuf);
[168337e]325  pitch = obuf->data[0];
326  if (pitch > 0) {
327    pitch = p->srate / (pitch + 0.);
328  } else {
329    pitch = 0.;
[6d4ec49]330  }
[168337e]331  obuf->data[0] = pitch;
[650e39b]332}
333
[fddfa64]334void
335aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out)
336{
337  aubio_pitch_slideblock (p, ibuf);
338  aubio_pitchfcomb_do (p->fcomb, p->buf, out);
[168337e]339  out->data[0] = aubio_bintofreq (out->data[0], p->srate, p->bufsize);
[c078336]340}
341
[fddfa64]342void
343aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out)
344{
[7a6cbbe]345  smpl_t period, pitch = 0.;
[fddfa64]346  aubio_pitch_slideblock (p, ibuf);
347  aubio_pitchschmitt_do (p->schmitt, p->buf, out);
[168337e]348  period = out->data[0];
349  if (period > 0) {
350    pitch = p->srate / period;
351  } else {
352    pitch = 0.;
[7a6cbbe]353  }
[168337e]354  out->data[0] = pitch;
[96fb8ad]355}
[5284e0d]356
357/* confidence callbacks */
358smpl_t
359aubio_pitch_get_confidence (aubio_pitch_t * p)
360{
361  if (p->confidence_callback) {
362    return p->confidence_callback ((void*)(p->pitch));
363  }
364  return 0.;
365}
Note: See TracBrowser for help on using the repository browser.