source: src/pitch/pitch.c @ 8f68dfb

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

src/pitch/pitch.c: improve documentation

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