source: src/pitch/pitchdetection.c @ a695854

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

src/pitch/pitchdetection.c: include filter.h

  • Property mode set to 100644
File size: 8.0 KB
Line 
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.
17   */
18
19#include "aubio_priv.h"
20#include "fvec.h"
21#include "cvec.h"
22#include "spectral/phasevoc.h"
23#include "mathutils.h"
24#include "temporal/filter.h"
25#include "temporal/cdesign.h"
26#include "pitch/pitchmcomb.h"
27#include "pitch/pitchyin.h"
28#include "pitch/pitchfcomb.h"
29#include "pitch/pitchschmitt.h"
30#include "pitch/pitchyinfft.h"
31#include "pitch/pitchdetection.h"
32
33typedef smpl_t (*aubio_pitchdetection_func_t)
34  (aubio_pitchdetection_t *p, fvec_t * ibuf);
35typedef smpl_t (*aubio_pitchdetection_conv_t)
36  (smpl_t value, uint_t srate, uint_t bufsize);
37
38void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf);
39
40smpl_t aubio_pitchdetection_mcomb   (aubio_pitchdetection_t *p, fvec_t *ibuf);
41smpl_t aubio_pitchdetection_yin     (aubio_pitchdetection_t *p, fvec_t *ibuf);
42smpl_t aubio_pitchdetection_schmitt (aubio_pitchdetection_t *p, fvec_t *ibuf);
43smpl_t aubio_pitchdetection_fcomb   (aubio_pitchdetection_t *p, fvec_t *ibuf);
44smpl_t aubio_pitchdetection_yinfft  (aubio_pitchdetection_t *p, fvec_t *ibuf);
45
46/** generic pitch detection structure */
47struct _aubio_pitchdetection_t {
48  aubio_pitchdetection_type type; /**< pitch detection mode */
49  aubio_pitchdetection_mode mode; /**< pitch detection output mode */
50  uint_t srate;                   /**< samplerate */
51  uint_t bufsize;                 /**< buffer size */
52  aubio_pitchmcomb_t * mcomb;     /**< mcomb object */
53  aubio_pitchfcomb_t * fcomb;     /**< fcomb object */
54  aubio_pitchschmitt_t * schmitt; /**< schmitt object */
55  aubio_pitchyinfft_t * yinfft;   /**< yinfft object */
56  aubio_filter_t * filter;        /**< filter */
57  aubio_pvoc_t * pv;              /**< phase vocoder for mcomb */ 
58  cvec_t * fftgrain;              /**< spectral frame for mcomb */
59  fvec_t * buf;                   /**< temporary buffer for yin */
60  fvec_t * yin;                   /**< yin function */
61  smpl_t yinthres;                /**< yin peak picking threshold parameter */
62  aubio_pitchdetection_func_t callback; /**< pointer to current pitch detection method */
63  aubio_pitchdetection_conv_t freqconv; /**< pointer to current pitch conversion method */ 
64};
65
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){
70  return aubio_freqtobin(f,srate,bufsize);
71}
72
73smpl_t freqconvmidi(smpl_t f,uint_t srate,uint_t bufsize);
74smpl_t freqconvmidi(smpl_t f,uint_t srate UNUSED,uint_t bufsize UNUSED){
75  return aubio_freqtomidi(f);
76}
77
78smpl_t freqconvpass(smpl_t f,uint_t srate,uint_t bufsize);
79smpl_t freqconvpass(smpl_t f,uint_t srate UNUSED,uint_t bufsize UNUSED){
80  return f;
81}
82
83aubio_pitchdetection_t * new_aubio_pitchdetection(uint_t bufsize, 
84    uint_t hopsize, 
85    uint_t channels,
86    uint_t samplerate,
87    aubio_pitchdetection_type type,
88    aubio_pitchdetection_mode mode)
89{
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);
98      p->yin      = new_fvec(bufsize/2,channels);
99      p->callback = aubio_pitchdetection_yin;
100      p->yinthres = 0.15;
101      break;
102    case aubio_pitch_mcomb:
103      p->pv       = new_aubio_pvoc(bufsize, hopsize, channels);
104      p->fftgrain = new_cvec(bufsize, channels);
105      p->mcomb    = new_aubio_pitchmcomb(bufsize,hopsize,channels,samplerate);
106      p->filter   = new_aubio_cdsgn_filter(samplerate, channels);
107      p->callback = aubio_pitchdetection_mcomb;
108      break;
109    case aubio_pitch_fcomb:
110      p->buf      = new_fvec(bufsize,channels);
111      p->fcomb    = new_aubio_pitchfcomb(bufsize,hopsize,samplerate);
112      p->callback = aubio_pitchdetection_fcomb;
113      break;
114    case aubio_pitch_schmitt:
115      p->buf      = new_fvec(bufsize,channels);
116      p->schmitt  = new_aubio_pitchschmitt(bufsize,samplerate);
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;
123      p->yinthres = 0.85;
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;
146}
147
148void del_aubio_pitchdetection(aubio_pitchdetection_t * p) {
149  switch(p->type) {
150    case aubio_pitch_yin:
151      del_fvec(p->yin);
152      del_fvec(p->buf);
153      break;
154    case aubio_pitch_mcomb:
155      del_aubio_pvoc(p->pv);
156      del_cvec(p->fftgrain);
157      del_aubio_filter(p->filter);
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);
176}
177
178void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf){
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  }
191}
192
193void aubio_pitchdetection_set_yinthresh(aubio_pitchdetection_t *p, smpl_t thres) {
194  p->yinthres = thres;
195}
196
197smpl_t aubio_pitchdetection(aubio_pitchdetection_t *p, fvec_t * ibuf) {
198  return p->freqconv(p->callback(p,ibuf),p->srate,p->bufsize);
199}
200
201smpl_t aubio_pitchdetection_mcomb(aubio_pitchdetection_t *p, fvec_t *ibuf) {
202  smpl_t pitch = 0.;
203  aubio_filter_do(p->filter,ibuf);
204  aubio_pvoc_do(p->pv,ibuf,p->fftgrain);
205  pitch = aubio_pitchmcomb_detect(p->mcomb,p->fftgrain);
206  /** \bug should move the >0 check within aubio_bintofreq */
207  if (pitch>0.) {
208    pitch = aubio_bintofreq(pitch,p->srate,p->bufsize);
209  } else {
210    pitch = 0.;
211  }
212  return pitch;
213}
214
215smpl_t aubio_pitchdetection_yin(aubio_pitchdetection_t *p, fvec_t *ibuf) {
216  smpl_t pitch = 0.;
217  aubio_pitchdetection_slideblock(p,ibuf);
218  pitch = aubio_pitchyin_getpitchfast(p->buf,p->yin, p->yinthres);
219  if (pitch>0) {
220    pitch = p->srate/(pitch+0.);
221  } else {
222    pitch = 0.;
223  }
224  return pitch;
225}
226
227
228smpl_t aubio_pitchdetection_yinfft(aubio_pitchdetection_t *p, fvec_t *ibuf){
229  smpl_t pitch = 0.;
230  aubio_pitchdetection_slideblock(p,ibuf);
231  pitch = aubio_pitchyinfft_detect(p->yinfft,p->buf,p->yinthres);
232  if (pitch>0) {
233    pitch = p->srate/(pitch+0.);
234  } else {
235    pitch = 0.;
236  }
237  return pitch; 
238}
239
240smpl_t aubio_pitchdetection_fcomb(aubio_pitchdetection_t *p, fvec_t *ibuf){
241  aubio_pitchdetection_slideblock(p,ibuf);
242  return aubio_pitchfcomb_detect(p->fcomb,p->buf);
243}
244
245smpl_t aubio_pitchdetection_schmitt(aubio_pitchdetection_t *p, fvec_t *ibuf){
246  aubio_pitchdetection_slideblock(p,ibuf);
247  return aubio_pitchschmitt_detect(p->schmitt,p->buf);
248}
Note: See TracBrowser for help on using the repository browser.