source: src/pitch/pitch.c @ 5284e0d

feature/autosinkfeature/cnnfeature/cnn_orgfeature/constantqfeature/crepefeature/crepe_orgfeature/pitchshiftfeature/pydocstringsfeature/timestretchfix/ffmpeg5pitchshiftsamplertimestretchyinfft+
Last change on this file since 5284e0d 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
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 algorithms */
38typedef enum
39{
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` */
47} aubio_pitch_type;
48
49/** pitch detection output modes */
50typedef enum
51{
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 */
57} aubio_pitch_mode;
58
59typedef void (*aubio_pitch_func_t)
60  (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf);
61typedef smpl_t (*aubio_pitch_conv_t)
62  (smpl_t value, uint_t srate, uint_t bufsize);
63
64typedef smpl_t (*aubio_conf_cb_t) (void * p);
65
66void aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf);
67
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);
73
74/** generic pitch detection structure */
75struct _aubio_pitch_t
76{
77  aubio_pitch_type type; /**< pitch detection mode */
78  aubio_pitch_mode mode; /**< pitch detection output mode */
79  uint_t srate;                   /**< samplerate */
80  uint_t bufsize;                 /**< buffer size */
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 */
86  void *pitch;
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 */
91  aubio_pitch_func_t callback; /**< pointer to current pitch detection method */
92  aubio_pitch_conv_t freqconv; /**< pointer to current pitch conversion method */
93  aubio_conf_cb_t confidence_callback; /**< pointer to the current confidence callback */
94};
95
96/* convenience wrapper function for frequency unit conversions
97 * should probably be rewritten with #defines */
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);
103}
104
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);
110}
111
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{
116  return f;
117}
118
119aubio_pitch_t *
120new_aubio_pitch (char_t * pitch_mode,
121    uint_t bufsize, uint_t hopsize, uint_t samplerate)
122{
123  aubio_pitch_t *p = AUBIO_NEW (aubio_pitch_t);
124  aubio_pitch_type pitch_type;
125  if (strcmp (pitch_mode, "mcomb") == 0)
126    pitch_type = aubio_pitcht_mcomb;
127  else if (strcmp (pitch_mode, "yinfft") == 0)
128    pitch_type = aubio_pitcht_yinfft;
129  else if (strcmp (pitch_mode, "yin") == 0)
130    pitch_type = aubio_pitcht_yin;
131  else if (strcmp (pitch_mode, "schmitt") == 0)
132    pitch_type = aubio_pitcht_schmitt;
133  else if (strcmp (pitch_mode, "fcomb") == 0)
134    pitch_type = aubio_pitcht_fcomb;
135  else if (strcmp (pitch_mode, "default") == 0)
136    pitch_type = aubio_pitcht_default;
137  else {
138    AUBIO_ERR ("unknown pitch detection method %s, using default.\n",
139        pitch_mode);
140    pitch_type = aubio_pitcht_default;
141  }
142  p->srate = samplerate;
143  p->type = pitch_type;
144  aubio_pitch_set_unit (p, "default");
145  p->bufsize = bufsize;
146  p->confidence_callback = NULL;
147  switch (p->type) {
148    case aubio_pitcht_yin:
149      p->buf = new_fvec (bufsize);
150      p->yin = new_aubio_pitchyin (bufsize);
151      p->callback = aubio_pitch_do_yin;
152      p->confidence_callback = (aubio_conf_cb_t)aubio_pitchyin_get_confidence;
153      p->pitch = (void*)p->yin;
154      aubio_pitchyin_set_tolerance (p->yin, 0.15);
155      break;
156    case aubio_pitcht_mcomb:
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);
161      p->callback = aubio_pitch_do_mcomb;
162      break;
163    case aubio_pitcht_fcomb:
164      p->buf = new_fvec (bufsize);
165      p->fcomb = new_aubio_pitchfcomb (bufsize, hopsize);
166      p->callback = aubio_pitch_do_fcomb;
167      break;
168    case aubio_pitcht_schmitt:
169      p->buf = new_fvec (bufsize);
170      p->schmitt = new_aubio_pitchschmitt (bufsize);
171      p->callback = aubio_pitch_do_schmitt;
172      break;
173    case aubio_pitcht_yinfft:
174      p->buf = new_fvec (bufsize);
175      p->yinfft = new_aubio_pitchyinfft (bufsize);
176      p->callback = aubio_pitch_do_yinfft;
177      p->confidence_callback = (aubio_conf_cb_t)aubio_pitchyinfft_get_confidence;
178      p->pitch = (void*)p->yin;
179      aubio_pitchyinfft_set_tolerance (p->yinfft, 0.85);
180      break;
181    default:
182      break;
183  }
184  return p;
185}
186
187void
188del_aubio_pitch (aubio_pitch_t * p)
189{
190  switch (p->type) {
191    case aubio_pitcht_yin:
192      del_fvec (p->buf);
193      del_aubio_pitchyin (p->yin);
194      break;
195    case aubio_pitcht_mcomb:
196      del_aubio_pvoc (p->pv);
197      del_cvec (p->fftgrain);
198      del_aubio_filter (p->filter);
199      del_aubio_pitchmcomb (p->mcomb);
200      break;
201    case aubio_pitcht_schmitt:
202      del_fvec (p->buf);
203      del_aubio_pitchschmitt (p->schmitt);
204      break;
205    case aubio_pitcht_fcomb:
206      del_fvec (p->buf);
207      del_aubio_pitchfcomb (p->fcomb);
208      break;
209    case aubio_pitcht_yinfft:
210      del_fvec (p->buf);
211      del_aubio_pitchyinfft (p->yinfft);
212      break;
213    default:
214      break;
215  }
216  AUBIO_FREE (p);
217}
218
219void
220aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf)
221{
222  uint_t j = 0, overlap_size = 0;
223  overlap_size = p->buf->length - ibuf->length;
224  for (j = 0; j < overlap_size; j++) {
225    p->buf->data[j] = p->buf->data[j + ibuf->length];
226  }
227  for (j = 0; j < ibuf->length; j++) {
228    p->buf->data[j + overlap_size] = ibuf->data[j];
229  }
230}
231
232uint_t
233aubio_pitch_set_unit (aubio_pitch_t * p, char_t * pitch_unit)
234{
235  aubio_pitch_mode pitch_mode;
236  if (strcmp (pitch_unit, "freq") == 0)
237    pitch_mode = aubio_pitchm_freq;
238  else if (strcmp (pitch_unit, "midi") == 0)
239    pitch_mode = aubio_pitchm_midi;
240  else if (strcmp (pitch_unit, "cent") == 0)
241    pitch_mode = aubio_pitchm_cent;
242  else if (strcmp (pitch_unit, "bin") == 0)
243    pitch_mode = aubio_pitchm_bin;
244  else if (strcmp (pitch_unit, "default") == 0)
245    pitch_mode = aubio_pitchm_default;
246  else {
247    AUBIO_ERR ("unknown pitch detection unit %s, using default\n", pitch_unit);
248    pitch_mode = aubio_pitchm_default;
249  }
250  p->mode = pitch_mode;
251  switch (p->mode) {
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  }
268  return AUBIO_OK;
269}
270
271uint_t
272aubio_pitch_set_tolerance (aubio_pitch_t * p, smpl_t tol)
273{
274  switch (p->type) {
275    case aubio_pitcht_yin:
276      aubio_pitchyin_set_tolerance (p->yin, tol);
277      break;
278    case aubio_pitcht_yinfft:
279      aubio_pitchyinfft_set_tolerance (p->yinfft, tol);
280      break;
281    default:
282      break;
283  }
284  return AUBIO_OK;
285}
286
287void
288aubio_pitch_do (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
289{
290  p->callback (p, ibuf, obuf);
291  obuf->data[0] = p->freqconv (obuf->data[0], p->srate, p->bufsize);
292}
293
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);
300  obuf->data[0] = aubio_bintofreq (obuf->data[0], p->srate, p->bufsize);
301}
302
303void
304aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
305{
306  smpl_t pitch = 0.;
307  aubio_pitch_slideblock (p, ibuf);
308  aubio_pitchyin_do (p->yin, p->buf, obuf);
309  pitch = obuf->data[0];
310  if (pitch > 0) {
311    pitch = p->srate / (pitch + 0.);
312  } else {
313    pitch = 0.;
314  }
315  obuf->data[0] = pitch;
316}
317
318
319void
320aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf)
321{
322  smpl_t pitch = 0.;
323  aubio_pitch_slideblock (p, ibuf);
324  aubio_pitchyinfft_do (p->yinfft, p->buf, obuf);
325  pitch = obuf->data[0];
326  if (pitch > 0) {
327    pitch = p->srate / (pitch + 0.);
328  } else {
329    pitch = 0.;
330  }
331  obuf->data[0] = pitch;
332}
333
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);
339  out->data[0] = aubio_bintofreq (out->data[0], p->srate, p->bufsize);
340}
341
342void
343aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out)
344{
345  smpl_t period, pitch = 0.;
346  aubio_pitch_slideblock (p, ibuf);
347  aubio_pitchschmitt_do (p->schmitt, p->buf, out);
348  period = out->data[0];
349  if (period > 0) {
350    pitch = p->srate / period;
351  } else {
352    pitch = 0.;
353  }
354  out->data[0] = pitch;
355}
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.