source: src/pitch/pitch.c @ e6a78ea

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since e6a78ea 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
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/pitch.h"
36
37/** pitch detection algorithm */
38typedef enum {
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;
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 */
54} aubio_pitch_mode;
55
56typedef void (*aubio_pitch_func_t)
57  (aubio_pitch_t *p, fvec_t * ibuf, fvec_t *obuf);
58typedef smpl_t (*aubio_pitch_conv_t)
59  (smpl_t value, uint_t srate, uint_t bufsize);
60
61void aubio_pitch_slideblock(aubio_pitch_t *p, fvec_t *ibuf);
62
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);
68
69/** generic pitch detection structure */
70struct _aubio_pitch_t {
71  aubio_pitch_type type; /**< pitch detection mode */
72  aubio_pitch_mode mode; /**< pitch detection output mode */
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 */
79  aubio_pitchyin_t * yin;   /**< yinfft object */
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 */
84  aubio_pitch_func_t callback; /**< pointer to current pitch detection method */
85  aubio_pitch_conv_t freqconv; /**< pointer to current pitch conversion method */ 
86};
87
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){
92  return aubio_freqtobin(f,srate,bufsize);
93}
94
95smpl_t freqconvmidi(smpl_t f,uint_t srate,uint_t bufsize);
96smpl_t freqconvmidi(smpl_t f,uint_t srate UNUSED,uint_t bufsize UNUSED){
97  return aubio_freqtomidi(f);
98}
99
100smpl_t freqconvpass(smpl_t f,uint_t srate,uint_t bufsize);
101smpl_t freqconvpass(smpl_t f,uint_t srate UNUSED,uint_t bufsize UNUSED){
102  return f;
103}
104
105aubio_pitch_t *
106new_aubio_pitch (char_t * pitch_mode,
107    uint_t bufsize, uint_t hopsize, uint_t channels, uint_t samplerate)
108{
109  aubio_pitch_t *p = AUBIO_NEW(aubio_pitch_t);
110  aubio_pitch_type pitch_type;
111  if (strcmp (pitch_mode, "mcomb") == 0)
112      pitch_type = aubio_pitcht_mcomb;
113  else if (strcmp (pitch_mode, "yinfft") == 0)
114      pitch_type = aubio_pitcht_yin;
115  else if (strcmp (pitch_mode, "yin") == 0)
116      pitch_type = aubio_pitcht_yin;
117  else if (strcmp (pitch_mode, "schmitt") == 0)
118      pitch_type = aubio_pitcht_schmitt;
119  else if (strcmp (pitch_mode, "fcomb") == 0)
120      pitch_type = aubio_pitcht_fcomb;
121  else if (strcmp (pitch_mode, "default") == 0)
122      pitch_type = aubio_pitcht_default;
123  else {
124      AUBIO_ERR ("unknown pitch detection method %s, using default.\n", pitch_mode);
125      pitch_type = aubio_pitcht_default;
126      return NULL;
127  }
128  p->srate = samplerate;
129  p->type = pitch_type;
130  aubio_pitch_set_unit (p, "default");
131  p->bufsize = bufsize;
132  switch(p->type) {
133    case aubio_pitcht_yin:
134      p->buf      = new_fvec(bufsize,channels);
135      p->yin      = new_aubio_pitchyin(bufsize);
136      p->callback = aubio_pitch_do_yin;
137      aubio_pitchyin_set_tolerance (p->yin, 0.15);
138      break;
139    case aubio_pitcht_mcomb:
140      p->pv       = new_aubio_pvoc(bufsize, hopsize, channels);
141      p->fftgrain = new_cvec(bufsize, channels);
142      p->mcomb    = new_aubio_pitchmcomb(bufsize,hopsize,channels);
143      p->filter   = new_aubio_filter_c_weighting (samplerate, channels);
144      p->callback = aubio_pitch_do_mcomb;
145      break;
146    case aubio_pitcht_fcomb:
147      p->buf      = new_fvec(bufsize,channels);
148      p->fcomb    = new_aubio_pitchfcomb(bufsize,hopsize,channels);
149      p->callback = aubio_pitch_do_fcomb;
150      break;
151    case aubio_pitcht_schmitt:
152      p->buf      = new_fvec(bufsize,channels);
153      p->schmitt  = new_aubio_pitchschmitt(bufsize);
154      p->callback = aubio_pitch_do_schmitt;
155      break;
156    case aubio_pitcht_yinfft:
157      p->buf      = new_fvec(bufsize,channels);
158      p->yinfft   = new_aubio_pitchyinfft(bufsize);
159      p->callback = aubio_pitch_do_yinfft;
160      aubio_pitchyinfft_set_tolerance (p->yinfft, 0.85);
161      break;
162    default:
163      break;
164  }
165  return p;
166}
167
168void del_aubio_pitch(aubio_pitch_t * p) {
169  switch(p->type) {
170    case aubio_pitcht_yin:
171      del_fvec(p->buf);
172      del_aubio_pitchyin(p->yin);
173      break;
174    case aubio_pitcht_mcomb:
175      del_aubio_pvoc(p->pv);
176      del_cvec(p->fftgrain);
177      del_aubio_filter(p->filter);
178      del_aubio_pitchmcomb(p->mcomb);
179      break;
180    case aubio_pitcht_schmitt:
181      del_fvec(p->buf);
182      del_aubio_pitchschmitt(p->schmitt);
183      break;
184    case aubio_pitcht_fcomb:
185      del_fvec(p->buf);
186      del_aubio_pitchfcomb(p->fcomb);
187      break;
188    case aubio_pitcht_yinfft:
189      del_fvec(p->buf);
190      del_aubio_pitchyinfft(p->yinfft);
191      break;
192    default:
193      break;
194  }
195  AUBIO_FREE(p);
196}
197
198void aubio_pitch_slideblock(aubio_pitch_t *p, fvec_t *ibuf){
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  }
211}
212
213uint_t aubio_pitch_set_unit (aubio_pitch_t *p, char_t * pitch_unit) {
214  aubio_pitch_mode pitch_mode;
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  }
247  return AUBIO_OK;
248}
249
250uint_t aubio_pitch_set_tolerance(aubio_pitch_t *p, smpl_t tol) {
251  switch(p->type) {
252    case aubio_pitcht_yin:
253      aubio_pitchyin_set_tolerance (p->yin, tol);
254      break;
255    case aubio_pitcht_yinfft:
256      aubio_pitchyinfft_set_tolerance (p->yinfft, tol);
257      break;
258    default:
259      break;
260  }
261  return AUBIO_OK;
262}
263
264void aubio_pitch_do (aubio_pitch_t *p, fvec_t * ibuf, fvec_t *obuf) {
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  }
270}
271
272void aubio_pitch_do_mcomb(aubio_pitch_t *p, fvec_t *ibuf, fvec_t * obuf) {
273  uint_t i;
274  aubio_filter_do(p->filter,ibuf);
275  aubio_pvoc_do(p->pv,ibuf,p->fftgrain);
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);
279  }
280}
281
282void aubio_pitch_do_yin(aubio_pitch_t *p, fvec_t *ibuf, fvec_t * obuf) {
283  smpl_t pitch = 0.;
284  uint_t i;
285  aubio_pitch_slideblock(p,ibuf);
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;
295  }
296}
297
298
299void aubio_pitch_do_yinfft(aubio_pitch_t *p, fvec_t *ibuf, fvec_t * obuf){
300  smpl_t pitch = 0.;
301  uint_t i;
302  aubio_pitch_slideblock(p,ibuf);
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;
312  }
313}
314
315void aubio_pitch_do_fcomb(aubio_pitch_t *p, fvec_t *ibuf, fvec_t * out){
316  uint_t i;
317  aubio_pitch_slideblock(p,ibuf);
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  }
322}
323
324void aubio_pitch_do_schmitt(aubio_pitch_t *p, fvec_t *ibuf, fvec_t *out){
325  smpl_t period, pitch = 0.;
326  uint_t i;
327  aubio_pitch_slideblock(p,ibuf);
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  }
338}
Note: See TracBrowser for help on using the repository browser.