source: src/pitch/pitch.c @ 95dc7f2

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

src/pitch/: add first draft for specacf

  • Property mode set to 100644
File size: 12.0 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/pitchspecacf.h"
36#include "pitch/pitch.h"
37
38/** pitch detection algorithms */
39typedef enum
40{
41  aubio_pitcht_yin,        /**< `yin`, YIN algorithm */
42  aubio_pitcht_mcomb,      /**< `mcomb`, Multi-comb filter */
43  aubio_pitcht_schmitt,    /**< `schmitt`, Schmitt trigger */
44  aubio_pitcht_fcomb,      /**< `fcomb`, Fast comb filter */
45  aubio_pitcht_yinfft,     /**< `yinfft`, Spectral YIN */
46  aubio_pitcht_specacf,    /**< `specacf`, Spectral autocorrelation */
47  aubio_pitcht_default
48    = aubio_pitcht_yinfft, /**< `default` */
49} aubio_pitch_type;
50
51/** pitch detection output modes */
52typedef enum
53{
54  aubio_pitchm_freq,   /**< Frequency (Hz) */
55  aubio_pitchm_midi,   /**< MIDI note (0.,127) */
56  aubio_pitchm_cent,   /**< Cent */
57  aubio_pitchm_bin,    /**< Frequency bin (0,bufsize) */
58  aubio_pitchm_default = aubio_pitchm_freq, /**< the one used when "default" is asked */
59} aubio_pitch_mode;
60
61/** callback to get pitch candidate, defined below */
62typedef void (*aubio_pitch_detect_t) (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
63
64/** callback to convert pitch from one unit to another, defined below */
65typedef smpl_t(*aubio_pitch_convert_t) (smpl_t value, uint_t samplerate, uint_t bufsize);
66
67/** callback to fetch the confidence of the algorithm */
68typedef smpl_t (*aubio_pitch_get_conf_t) (void * p);
69
70/** generic pitch detection structure */
71struct _aubio_pitch_t
72{
73  aubio_pitch_type type;          /**< pitch detection mode */
74  aubio_pitch_mode mode;          /**< pitch detection output mode */
75  uint_t samplerate;              /**< samplerate */
76  uint_t bufsize;                 /**< buffer size */
77  void *p_object;                 /**< pointer to pitch object */
78  aubio_filter_t *filter;         /**< filter */
79  aubio_pvoc_t *pv;               /**< phase vocoder for mcomb */
80  cvec_t *fftgrain;               /**< spectral frame for mcomb */
81  fvec_t *buf;                    /**< temporary buffer for yin */
82  aubio_pitch_detect_t detect_cb; /**< callback to get the pitch candidates */
83  aubio_pitch_convert_t conv_cb;  /**< callback to convert it to the desired unit */
84  aubio_pitch_get_conf_t conf_cb; /**< pointer to the current confidence callback */
85};
86
87/* callback functions for pitch detection */
88static void aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
89static void aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
90static void aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
91static void aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
92static void aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
93static void aubio_pitch_do_specacf (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
94
95/* conversion functions for frequency conversions */
96smpl_t freqconvbin (smpl_t f, uint_t samplerate, uint_t bufsize);
97smpl_t freqconvmidi (smpl_t f, uint_t samplerate, uint_t bufsize);
98smpl_t freqconvpass (smpl_t f, uint_t samplerate, uint_t bufsize);
99
100/* adapter to stack ibuf new samples at the end of buf, and trim `buf` to `bufsize` */
101void aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf);
102
103
104aubio_pitch_t *
105new_aubio_pitch (char_t * pitch_mode,
106    uint_t bufsize, uint_t hopsize, uint_t samplerate)
107{
108  aubio_pitch_t *p = AUBIO_NEW (aubio_pitch_t);
109  aubio_pitch_type pitch_type;
110  if (strcmp (pitch_mode, "mcomb") == 0)
111    pitch_type = aubio_pitcht_mcomb;
112  else if (strcmp (pitch_mode, "yinfft") == 0)
113    pitch_type = aubio_pitcht_yinfft;
114  else if (strcmp (pitch_mode, "yin") == 0)
115    pitch_type = aubio_pitcht_yin;
116  else if (strcmp (pitch_mode, "schmitt") == 0)
117    pitch_type = aubio_pitcht_schmitt;
118  else if (strcmp (pitch_mode, "fcomb") == 0)
119    pitch_type = aubio_pitcht_fcomb;
120  else if (strcmp (pitch_mode, "specacf") == 0)
121    pitch_type = aubio_pitcht_specacf;
122  else if (strcmp (pitch_mode, "default") == 0)
123    pitch_type = aubio_pitcht_default;
124  else {
125    AUBIO_ERR ("unknown pitch detection method %s, using default.\n",
126        pitch_mode);
127    pitch_type = aubio_pitcht_default;
128  }
129  p->samplerate = samplerate;
130  p->type = pitch_type;
131  aubio_pitch_set_unit (p, "default");
132  p->bufsize = bufsize;
133  p->conf_cb = NULL;
134  switch (p->type) {
135    case aubio_pitcht_yin:
136      p->buf = new_fvec (bufsize);
137      p->p_object = new_aubio_pitchyin (bufsize);
138      p->detect_cb = aubio_pitch_do_yin;
139      p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyin_get_confidence;
140      aubio_pitchyin_set_tolerance (p->p_object, 0.15);
141      break;
142    case aubio_pitcht_mcomb:
143      p->pv = new_aubio_pvoc (bufsize, hopsize);
144      p->fftgrain = new_cvec (bufsize);
145      p->p_object = new_aubio_pitchmcomb (bufsize, hopsize);
146      p->filter = new_aubio_filter_c_weighting (samplerate);
147      p->detect_cb = aubio_pitch_do_mcomb;
148      break;
149    case aubio_pitcht_fcomb:
150      p->buf = new_fvec (bufsize);
151      p->p_object = new_aubio_pitchfcomb (bufsize, hopsize);
152      p->detect_cb = aubio_pitch_do_fcomb;
153      break;
154    case aubio_pitcht_schmitt:
155      p->buf = new_fvec (bufsize);
156      p->p_object = new_aubio_pitchschmitt (bufsize);
157      p->detect_cb = aubio_pitch_do_schmitt;
158      break;
159    case aubio_pitcht_yinfft:
160      p->buf = new_fvec (bufsize);
161      p->p_object = new_aubio_pitchyinfft (bufsize);
162      p->detect_cb = aubio_pitch_do_yinfft;
163      p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyinfft_get_confidence;
164      aubio_pitchyinfft_set_tolerance (p->p_object, 0.85);
165      break;
166    case aubio_pitcht_specacf:
167      p->buf = new_fvec (bufsize);
168      p->p_object = new_aubio_pitchspecacf (bufsize);
169      p->detect_cb = aubio_pitch_do_specacf;
170      p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchspecacf_get_tolerance;
171      aubio_pitchspecacf_set_tolerance (p->p_object, 0.85);
172      break;
173    default:
174      break;
175  }
176  return p;
177}
178
179void
180del_aubio_pitch (aubio_pitch_t * p)
181{
182  switch (p->type) {
183    case aubio_pitcht_yin:
184      del_fvec (p->buf);
185      del_aubio_pitchyin (p->p_object);
186      break;
187    case aubio_pitcht_mcomb:
188      del_aubio_pvoc (p->pv);
189      del_cvec (p->fftgrain);
190      del_aubio_filter (p->filter);
191      del_aubio_pitchmcomb (p->p_object);
192      break;
193    case aubio_pitcht_schmitt:
194      del_fvec (p->buf);
195      del_aubio_pitchschmitt (p->p_object);
196      break;
197    case aubio_pitcht_fcomb:
198      del_fvec (p->buf);
199      del_aubio_pitchfcomb (p->p_object);
200      break;
201    case aubio_pitcht_yinfft:
202      del_fvec (p->buf);
203      del_aubio_pitchyinfft (p->p_object);
204      break;
205    case aubio_pitcht_specacf:
206      del_fvec (p->buf);
207      del_aubio_pitchspecacf (p->p_object);
208      break;
209    default:
210      break;
211  }
212  AUBIO_FREE (p);
213}
214
215void
216aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf)
217{
218  uint_t j = 0, overlap_size = 0;
219  overlap_size = p->buf->length - ibuf->length;
220  for (j = 0; j < overlap_size; j++) {
221    p->buf->data[j] = p->buf->data[j + ibuf->length];
222  }
223  for (j = 0; j < ibuf->length; j++) {
224    p->buf->data[j + overlap_size] = ibuf->data[j];
225  }
226}
227
228uint_t
229aubio_pitch_set_unit (aubio_pitch_t * p, char_t * pitch_unit)
230{
231  aubio_pitch_mode pitch_mode;
232  if (strcmp (pitch_unit, "freq") == 0)
233    pitch_mode = aubio_pitchm_freq;
234  else if (strcmp (pitch_unit, "midi") == 0)
235    pitch_mode = aubio_pitchm_midi;
236  else if (strcmp (pitch_unit, "cent") == 0)
237    pitch_mode = aubio_pitchm_cent;
238  else if (strcmp (pitch_unit, "bin") == 0)
239    pitch_mode = aubio_pitchm_bin;
240  else if (strcmp (pitch_unit, "default") == 0)
241    pitch_mode = aubio_pitchm_default;
242  else {
243    AUBIO_ERR ("unknown pitch detection unit %s, using default\n", pitch_unit);
244    pitch_mode = aubio_pitchm_default;
245  }
246  p->mode = pitch_mode;
247  switch (p->mode) {
248    case aubio_pitchm_freq:
249      p->conv_cb = freqconvpass;
250      break;
251    case aubio_pitchm_midi:
252      p->conv_cb = freqconvmidi;
253      break;
254    case aubio_pitchm_cent:
255      /* bug: not implemented */
256      p->conv_cb = freqconvmidi;
257      break;
258    case aubio_pitchm_bin:
259      p->conv_cb = freqconvbin;
260      break;
261    default:
262      break;
263  }
264  return AUBIO_OK;
265}
266
267uint_t
268aubio_pitch_set_tolerance (aubio_pitch_t * p, smpl_t tol)
269{
270  switch (p->type) {
271    case aubio_pitcht_yin:
272      aubio_pitchyin_set_tolerance (p->p_object, tol);
273      break;
274    case aubio_pitcht_yinfft:
275      aubio_pitchyinfft_set_tolerance (p->p_object, tol);
276      break;
277    default:
278      break;
279  }
280  return AUBIO_OK;
281}
282
283
284/* do method, calling the detection callback, then the conversion callback */
285void
286aubio_pitch_do (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
287{
288  p->detect_cb (p, ibuf, obuf);
289  obuf->data[0] = p->conv_cb (obuf->data[0], p->samplerate, p->bufsize);
290}
291
292/* do method for each algorithm */
293void
294aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
295{
296  aubio_filter_do (p->filter, ibuf);
297  aubio_pvoc_do (p->pv, ibuf, p->fftgrain);
298  aubio_pitchmcomb_do (p->p_object, p->fftgrain, obuf);
299  obuf->data[0] = aubio_bintofreq (obuf->data[0], p->samplerate, p->bufsize);
300}
301
302void
303aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
304{
305  smpl_t pitch = 0.;
306  aubio_pitch_slideblock (p, ibuf);
307  aubio_pitchyin_do (p->p_object, p->buf, obuf);
308  pitch = obuf->data[0];
309  if (pitch > 0) {
310    pitch = p->samplerate / (pitch + 0.);
311  } else {
312    pitch = 0.;
313  }
314  obuf->data[0] = pitch;
315}
316
317
318void
319aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
320{
321  smpl_t pitch = 0.;
322  aubio_pitch_slideblock (p, ibuf);
323  aubio_pitchyinfft_do (p->p_object, p->buf, obuf);
324  pitch = obuf->data[0];
325  if (pitch > 0) {
326    pitch = p->samplerate / (pitch + 0.);
327  } else {
328    pitch = 0.;
329  }
330  obuf->data[0] = pitch;
331}
332
333void
334aubio_pitch_do_specacf (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out)
335{
336  aubio_pitch_slideblock (p, ibuf);
337  aubio_pitchspecacf_do (p->p_object, p->buf, out);
338  //out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize);
339  smpl_t pitch = 0., period = out->data[0];
340  if (period > 0) {
341    pitch = p->samplerate / period;
342  } else {
343    pitch = 0.;
344  }
345  out->data[0] = pitch;
346}
347
348void
349aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out)
350{
351  aubio_pitch_slideblock (p, ibuf);
352  aubio_pitchfcomb_do (p->p_object, p->buf, out);
353  out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize);
354}
355
356void
357aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out)
358{
359  smpl_t period, pitch = 0.;
360  aubio_pitch_slideblock (p, ibuf);
361  aubio_pitchschmitt_do (p->p_object, p->buf, out);
362  period = out->data[0];
363  if (period > 0) {
364    pitch = p->samplerate / period;
365  } else {
366    pitch = 0.;
367  }
368  out->data[0] = pitch;
369}
370
371/* conversion callbacks */
372smpl_t
373freqconvbin(smpl_t f, uint_t samplerate, uint_t bufsize)
374{
375  return aubio_freqtobin(f, samplerate, bufsize);
376}
377
378smpl_t
379freqconvmidi (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED)
380{
381  return aubio_freqtomidi (f);
382}
383
384smpl_t
385freqconvpass (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED)
386{
387  return f;
388}
389
390/* confidence callbacks */
391smpl_t
392aubio_pitch_get_confidence (aubio_pitch_t * p)
393{
394  if (p->conf_cb) {
395    return p->conf_cb(p->p_object);
396  }
397  return 0.;
398}
Note: See TracBrowser for help on using the repository browser.