source: src/pitch/pitchdetection.c @ 7a6cbbe

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

src/pitch/pitchdetection.{c,h}: clean and update prototypes, sync with latest pitch objects

  • Property mode set to 100644
File size: 8.9 KB
RevLine 
[96fb8ad]1/*
2   Copyright (C) 2003 Paul Brossier
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
[6d4ec49]17   */
[96fb8ad]18
19#include "aubio_priv.h"
[6c7d49b]20#include "fvec.h"
21#include "cvec.h"
[a4364b8]22#include "lvec.h"
[32d6958]23#include "spectral/phasevoc.h"
[96fb8ad]24#include "mathutils.h"
[a695854]25#include "temporal/filter.h"
[c159aeb]26#include "temporal/c_weighting.h"
[2d8cffa]27#include "pitch/pitchmcomb.h"
28#include "pitch/pitchyin.h"
29#include "pitch/pitchfcomb.h"
30#include "pitch/pitchschmitt.h"
31#include "pitch/pitchyinfft.h"
[32d6958]32#include "pitch/pitchdetection.h"
[96fb8ad]33
[7a6cbbe]34typedef void (*aubio_pitchdetection_func_t)
35  (aubio_pitchdetection_t *p, fvec_t * ibuf, fvec_t *obuf);
[6d4ec49]36typedef smpl_t (*aubio_pitchdetection_conv_t)
37  (smpl_t value, uint_t srate, uint_t bufsize);
38
[651b97e]39void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf);
[c078336]40
[7a6cbbe]41void aubio_pitchdetection_mcomb   (aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t *obuf);
42void aubio_pitchdetection_yin     (aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t *obuf);
43void aubio_pitchdetection_schmitt (aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t *obuf);
44void aubio_pitchdetection_fcomb   (aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t *obuf);
45void aubio_pitchdetection_yinfft  (aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t *obuf);
[f44b111]46
[475da2f]47/** generic pitch detection structure */
[96fb8ad]48struct _aubio_pitchdetection_t {
[475da2f]49  aubio_pitchdetection_type type; /**< pitch detection mode */
50  aubio_pitchdetection_mode mode; /**< pitch detection output mode */
51  uint_t srate;                   /**< samplerate */
52  uint_t bufsize;                 /**< buffer size */
53  aubio_pitchmcomb_t * mcomb;     /**< mcomb object */
54  aubio_pitchfcomb_t * fcomb;     /**< fcomb object */
55  aubio_pitchschmitt_t * schmitt; /**< schmitt object */
56  aubio_pitchyinfft_t * yinfft;   /**< yinfft object */
[7a6cbbe]57  aubio_pitchyin_t * yin;   /**< yinfft object */
[475da2f]58  aubio_filter_t * filter;        /**< filter */
59  aubio_pvoc_t * pv;              /**< phase vocoder for mcomb */ 
60  cvec_t * fftgrain;              /**< spectral frame for mcomb */
61  fvec_t * buf;                   /**< temporary buffer for yin */
62  aubio_pitchdetection_func_t callback; /**< pointer to current pitch detection method */
63  aubio_pitchdetection_conv_t freqconv; /**< pointer to current pitch conversion method */ 
[96fb8ad]64};
65
[3ec9d9c]66/* convenience wrapper function for frequency unit conversions
67 * should probably be rewritten with #defines */
68smpl_t freqconvbin(smpl_t f,uint_t srate,uint_t bufsize);
69smpl_t freqconvbin(smpl_t f,uint_t srate,uint_t bufsize){
[6d4ec49]70  return aubio_freqtobin(f,srate,bufsize);
[3ec9d9c]71}
72
73smpl_t freqconvmidi(smpl_t f,uint_t srate,uint_t bufsize);
[b377d04]74smpl_t freqconvmidi(smpl_t f,uint_t srate UNUSED,uint_t bufsize UNUSED){
[6d4ec49]75  return aubio_freqtomidi(f);
[3ec9d9c]76}
77
78smpl_t freqconvpass(smpl_t f,uint_t srate,uint_t bufsize);
[b377d04]79smpl_t freqconvpass(smpl_t f,uint_t srate UNUSED,uint_t bufsize UNUSED){
[6d4ec49]80  return f;
[3ec9d9c]81}
82
[96fb8ad]83aubio_pitchdetection_t * new_aubio_pitchdetection(uint_t bufsize, 
[6d4ec49]84    uint_t hopsize, 
85    uint_t channels,
86    uint_t samplerate,
87    aubio_pitchdetection_type type,
88    aubio_pitchdetection_mode mode)
[96fb8ad]89{
[6d4ec49]90  aubio_pitchdetection_t *p = AUBIO_NEW(aubio_pitchdetection_t);
91  p->srate = samplerate;
92  p->type = type;
93  p->mode = mode;
94  p->bufsize = bufsize;
95  switch(p->type) {
96    case aubio_pitch_yin:
97      p->buf      = new_fvec(bufsize,channels);
[7a6cbbe]98      p->yin      = new_aubio_pitchyin(bufsize);
[6d4ec49]99      p->callback = aubio_pitchdetection_yin;
[7a6cbbe]100      aubio_pitchyin_set_tolerance (p->yin, 0.15);
[6d4ec49]101      break;
102    case aubio_pitch_mcomb:
103      p->pv       = new_aubio_pvoc(bufsize, hopsize, channels);
104      p->fftgrain = new_cvec(bufsize, channels);
[7a6cbbe]105      p->mcomb    = new_aubio_pitchmcomb(bufsize,hopsize,channels);
[c159aeb]106      p->filter   = new_aubio_filter_c_weighting (samplerate, channels);
[6d4ec49]107      p->callback = aubio_pitchdetection_mcomb;
108      break;
109    case aubio_pitch_fcomb:
110      p->buf      = new_fvec(bufsize,channels);
[7a6cbbe]111      p->fcomb    = new_aubio_pitchfcomb(bufsize,hopsize,channels);
[6d4ec49]112      p->callback = aubio_pitchdetection_fcomb;
113      break;
114    case aubio_pitch_schmitt:
115      p->buf      = new_fvec(bufsize,channels);
[7a6cbbe]116      p->schmitt  = new_aubio_pitchschmitt(bufsize);
[6d4ec49]117      p->callback = aubio_pitchdetection_schmitt;
118      break;
119    case aubio_pitch_yinfft:
120      p->buf      = new_fvec(bufsize,channels);
121      p->yinfft   = new_aubio_pitchyinfft(bufsize);
122      p->callback = aubio_pitchdetection_yinfft;
[7a6cbbe]123      aubio_pitchyinfft_set_tolerance (p->yinfft, 0.85);
[6d4ec49]124      break;
125    default:
126      break;
127  }
128  switch(p->mode) {
129    case aubio_pitchm_freq:
130      p->freqconv = freqconvpass;
131      break;
132    case aubio_pitchm_midi:
133      p->freqconv = freqconvmidi;
134      break;
135    case aubio_pitchm_cent:
136      /* bug: not implemented */
137      p->freqconv = freqconvmidi;
138      break;
139    case aubio_pitchm_bin:
140      p->freqconv = freqconvbin;
141      break;
142    default:
143      break;
144  }
145  return p;
[96fb8ad]146}
147
148void del_aubio_pitchdetection(aubio_pitchdetection_t * p) {
[6d4ec49]149  switch(p->type) {
150    case aubio_pitch_yin:
151      del_fvec(p->buf);
[7a6cbbe]152      del_aubio_pitchyin(p->yin);
[6d4ec49]153      break;
154    case aubio_pitch_mcomb:
155      del_aubio_pvoc(p->pv);
156      del_cvec(p->fftgrain);
[44d7cb7]157      del_aubio_filter(p->filter);
[6d4ec49]158      del_aubio_pitchmcomb(p->mcomb);
159      break;
160    case aubio_pitch_schmitt:
161      del_fvec(p->buf);
162      del_aubio_pitchschmitt(p->schmitt);
163      break;
164    case aubio_pitch_fcomb:
165      del_fvec(p->buf);
166      del_aubio_pitchfcomb(p->fcomb);
167      break;
168    case aubio_pitch_yinfft:
169      del_fvec(p->buf);
170      del_aubio_pitchyinfft(p->yinfft);
171      break;
172    default:
173      break;
174  }
175  AUBIO_FREE(p);
[96fb8ad]176}
177
[651b97e]178void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf){
[6d4ec49]179  uint_t i,j = 0, overlap_size = 0;
180  overlap_size = p->buf->length-ibuf->length;
181  for (i=0;i<p->buf->channels;i++){
182    for (j=0;j<overlap_size;j++){
183      p->buf->data[i][j] = p->buf->data[i][j+ibuf->length];
184    }
185  }
186  for (i=0;i<ibuf->channels;i++){
187    for (j=0;j<ibuf->length;j++){
188      p->buf->data[i][j+overlap_size] = ibuf->data[i][j];
189    }
190  }
[651b97e]191}
192
[7a6cbbe]193void aubio_pitchdetection_set_tolerance(aubio_pitchdetection_t *p, smpl_t tol) {
194  switch(p->type) {
195    case aubio_pitch_yin:
196      aubio_pitchyin_set_tolerance (p->yin, tol);
197      break;
198    case aubio_pitch_yinfft:
199      aubio_pitchyinfft_set_tolerance (p->yinfft, tol);
200      break;
201    default:
202      break;
203  }
[f8a38c5]204}
205
[7a6cbbe]206void aubio_pitchdetection_do (aubio_pitchdetection_t *p, fvec_t * ibuf, fvec_t *obuf) {
207  uint_t i;
208  p->callback(p, ibuf, obuf);
209  for (i = 0; i < obuf->channels; i++) {
210    p->freqconv(obuf->data[i][0],p->srate,p->bufsize);
211  }
[c078336]212}
213
[7a6cbbe]214void aubio_pitchdetection_mcomb(aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t * obuf) {
215  uint_t i;
[6d4ec49]216  aubio_filter_do(p->filter,ibuf);
217  aubio_pvoc_do(p->pv,ibuf,p->fftgrain);
[7a6cbbe]218  aubio_pitchmcomb_do(p->mcomb,p->fftgrain, obuf);
219  for (i = 0; i < obuf->channels; i++) {
220    obuf->data[i][0] = aubio_bintofreq (obuf->data[i][0], p->srate, p->bufsize);
[6d4ec49]221  }
[c078336]222}
223
[7a6cbbe]224void aubio_pitchdetection_yin(aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t * obuf) {
[6d4ec49]225  smpl_t pitch = 0.;
[7a6cbbe]226  uint_t i;
[6d4ec49]227  aubio_pitchdetection_slideblock(p,ibuf);
[7a6cbbe]228  aubio_pitchyin_do(p->yin,p->buf, obuf);
229  for (i = 0; i < obuf->channels; i++) {
230    pitch = obuf->data[i][0];
231    if (pitch>0) {
232      pitch = p->srate/(pitch+0.);
233    } else {
234      pitch = 0.;
235    }
236    obuf->data[i][0] = pitch;
[6d4ec49]237  }
[c078336]238}
239
240
[7a6cbbe]241void aubio_pitchdetection_yinfft(aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t * obuf){
[6d4ec49]242  smpl_t pitch = 0.;
[7a6cbbe]243  uint_t i;
[6d4ec49]244  aubio_pitchdetection_slideblock(p,ibuf);
[7a6cbbe]245  aubio_pitchyinfft_do(p->yinfft,p->buf,obuf);
246  for (i = 0; i < obuf->channels; i++) {
247    pitch = obuf->data[i][0];
248    if (pitch>0) {
249      pitch = p->srate/(pitch+0.);
250    } else {
251      pitch = 0.;
252    }
253    obuf->data[i][0] = pitch;
[6d4ec49]254  }
[650e39b]255}
256
[7a6cbbe]257void aubio_pitchdetection_fcomb(aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t * out){
258  uint_t i;
[6d4ec49]259  aubio_pitchdetection_slideblock(p,ibuf);
[7a6cbbe]260  aubio_pitchfcomb_do(p->fcomb,p->buf, out);
261  for (i = 0; i < out->channels; i++) {
262    out->data[i][0] = aubio_bintofreq (out->data[i][0], p->srate, p->bufsize);
263  }
[c078336]264}
265
[7a6cbbe]266void aubio_pitchdetection_schmitt(aubio_pitchdetection_t *p, fvec_t *ibuf, fvec_t *out){
267  smpl_t period, pitch = 0.;
268  uint_t i;
[6d4ec49]269  aubio_pitchdetection_slideblock(p,ibuf);
[7a6cbbe]270  aubio_pitchschmitt_do(p->schmitt,p->buf, out);
271  for (i = 0; i < out->channels; i++) {
272    period = out->data[i][0];
273    if (period>0) {
274      pitch = p->srate/period;
275    } else {
276      pitch = 0.;
277    }
278    out->data[i][0] = pitch;
279  }
[96fb8ad]280}
Note: See TracBrowser for help on using the repository browser.