source: src/pitch/pitch.c @ 197f2ec

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

src: update all headers to GPLv3

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