source: src/pitch/pitchdetection.c @ cfa0f12

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

src/pitch/: change all _detect prototypes to _do

  • 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 "lvec.h"
23#include "spectral/phasevoc.h"
24#include "mathutils.h"
25#include "temporal/filter.h"
26#include "temporal/c_weighting.h"
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"
32#include "pitch/pitchdetection.h"
33
34typedef smpl_t (*aubio_pitchdetection_func_t)
35  (aubio_pitchdetection_t *p, fvec_t * ibuf);
36typedef smpl_t (*aubio_pitchdetection_conv_t)
37  (smpl_t value, uint_t srate, uint_t bufsize);
38
39void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf);
40
41smpl_t aubio_pitchdetection_mcomb   (aubio_pitchdetection_t *p, fvec_t *ibuf);
42smpl_t aubio_pitchdetection_yin     (aubio_pitchdetection_t *p, fvec_t *ibuf);
43smpl_t aubio_pitchdetection_schmitt (aubio_pitchdetection_t *p, fvec_t *ibuf);
44smpl_t aubio_pitchdetection_fcomb   (aubio_pitchdetection_t *p, fvec_t *ibuf);
45smpl_t aubio_pitchdetection_yinfft  (aubio_pitchdetection_t *p, fvec_t *ibuf);
46
47/** generic pitch detection structure */
48struct _aubio_pitchdetection_t {
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 */
57  aubio_filter_t * filter;        /**< filter */
58  aubio_pvoc_t * pv;              /**< phase vocoder for mcomb */ 
59  cvec_t * fftgrain;              /**< spectral frame for mcomb */
60  fvec_t * buf;                   /**< temporary buffer for yin */
61  fvec_t * yin;                   /**< yin function */
62  smpl_t yinthres;                /**< yin peak picking threshold parameter */
63  aubio_pitchdetection_func_t callback; /**< pointer to current pitch detection method */
64  aubio_pitchdetection_conv_t freqconv; /**< pointer to current pitch conversion method */ 
65};
66
67/* convenience wrapper function for frequency unit conversions
68 * should probably be rewritten with #defines */
69smpl_t freqconvbin(smpl_t f,uint_t srate,uint_t bufsize);
70smpl_t freqconvbin(smpl_t f,uint_t srate,uint_t bufsize){
71  return aubio_freqtobin(f,srate,bufsize);
72}
73
74smpl_t freqconvmidi(smpl_t f,uint_t srate,uint_t bufsize);
75smpl_t freqconvmidi(smpl_t f,uint_t srate UNUSED,uint_t bufsize UNUSED){
76  return aubio_freqtomidi(f);
77}
78
79smpl_t freqconvpass(smpl_t f,uint_t srate,uint_t bufsize);
80smpl_t freqconvpass(smpl_t f,uint_t srate UNUSED,uint_t bufsize UNUSED){
81  return f;
82}
83
84aubio_pitchdetection_t * new_aubio_pitchdetection(uint_t bufsize, 
85    uint_t hopsize, 
86    uint_t channels,
87    uint_t samplerate,
88    aubio_pitchdetection_type type,
89    aubio_pitchdetection_mode mode)
90{
91  aubio_pitchdetection_t *p = AUBIO_NEW(aubio_pitchdetection_t);
92  p->srate = samplerate;
93  p->type = type;
94  p->mode = mode;
95  p->bufsize = bufsize;
96  switch(p->type) {
97    case aubio_pitch_yin:
98      p->buf      = new_fvec(bufsize,channels);
99      p->yin      = new_fvec(bufsize/2,channels);
100      p->callback = aubio_pitchdetection_yin;
101      p->yinthres = 0.15;
102      break;
103    case aubio_pitch_mcomb:
104      p->pv       = new_aubio_pvoc(bufsize, hopsize, channels);
105      p->fftgrain = new_cvec(bufsize, channels);
106      p->mcomb    = new_aubio_pitchmcomb(bufsize,hopsize,channels,samplerate);
107      p->filter   = new_aubio_filter_c_weighting (samplerate, channels);
108      p->callback = aubio_pitchdetection_mcomb;
109      break;
110    case aubio_pitch_fcomb:
111      p->buf      = new_fvec(bufsize,channels);
112      p->fcomb    = new_aubio_pitchfcomb(bufsize,hopsize,samplerate);
113      p->callback = aubio_pitchdetection_fcomb;
114      break;
115    case aubio_pitch_schmitt:
116      p->buf      = new_fvec(bufsize,channels);
117      p->schmitt  = new_aubio_pitchschmitt(bufsize,samplerate);
118      p->callback = aubio_pitchdetection_schmitt;
119      break;
120    case aubio_pitch_yinfft:
121      p->buf      = new_fvec(bufsize,channels);
122      p->yinfft   = new_aubio_pitchyinfft(bufsize);
123      p->callback = aubio_pitchdetection_yinfft;
124      p->yinthres = 0.85;
125      break;
126    default:
127      break;
128  }
129  switch(p->mode) {
130    case aubio_pitchm_freq:
131      p->freqconv = freqconvpass;
132      break;
133    case aubio_pitchm_midi:
134      p->freqconv = freqconvmidi;
135      break;
136    case aubio_pitchm_cent:
137      /* bug: not implemented */
138      p->freqconv = freqconvmidi;
139      break;
140    case aubio_pitchm_bin:
141      p->freqconv = freqconvbin;
142      break;
143    default:
144      break;
145  }
146  return p;
147}
148
149void del_aubio_pitchdetection(aubio_pitchdetection_t * p) {
150  switch(p->type) {
151    case aubio_pitch_yin:
152      del_fvec(p->yin);
153      del_fvec(p->buf);
154      break;
155    case aubio_pitch_mcomb:
156      del_aubio_pvoc(p->pv);
157      del_cvec(p->fftgrain);
158      del_aubio_filter(p->filter);
159      del_aubio_pitchmcomb(p->mcomb);
160      break;
161    case aubio_pitch_schmitt:
162      del_fvec(p->buf);
163      del_aubio_pitchschmitt(p->schmitt);
164      break;
165    case aubio_pitch_fcomb:
166      del_fvec(p->buf);
167      del_aubio_pitchfcomb(p->fcomb);
168      break;
169    case aubio_pitch_yinfft:
170      del_fvec(p->buf);
171      del_aubio_pitchyinfft(p->yinfft);
172      break;
173    default:
174      break;
175  }
176  AUBIO_FREE(p);
177}
178
179void aubio_pitchdetection_slideblock(aubio_pitchdetection_t *p, fvec_t *ibuf){
180  uint_t i,j = 0, overlap_size = 0;
181  overlap_size = p->buf->length-ibuf->length;
182  for (i=0;i<p->buf->channels;i++){
183    for (j=0;j<overlap_size;j++){
184      p->buf->data[i][j] = p->buf->data[i][j+ibuf->length];
185    }
186  }
187  for (i=0;i<ibuf->channels;i++){
188    for (j=0;j<ibuf->length;j++){
189      p->buf->data[i][j+overlap_size] = ibuf->data[i][j];
190    }
191  }
192}
193
194void aubio_pitchdetection_set_yinthresh(aubio_pitchdetection_t *p, smpl_t thres) {
195  p->yinthres = thres;
196}
197
198smpl_t aubio_pitchdetection_do (aubio_pitchdetection_t *p, fvec_t * ibuf) {
199  return p->freqconv(p->callback(p,ibuf),p->srate,p->bufsize);
200}
201
202smpl_t aubio_pitchdetection_mcomb(aubio_pitchdetection_t *p, fvec_t *ibuf) {
203  smpl_t pitch = 0.;
204  aubio_filter_do(p->filter,ibuf);
205  aubio_pvoc_do(p->pv,ibuf,p->fftgrain);
206  pitch = aubio_pitchmcomb_do(p->mcomb,p->fftgrain);
207  /** \bug should move the >0 check within aubio_bintofreq */
208  if (pitch>0.) {
209    pitch = aubio_bintofreq(pitch,p->srate,p->bufsize);
210  } else {
211    pitch = 0.;
212  }
213  return pitch;
214}
215
216smpl_t aubio_pitchdetection_yin(aubio_pitchdetection_t *p, fvec_t *ibuf) {
217  smpl_t pitch = 0.;
218  aubio_pitchdetection_slideblock(p,ibuf);
219  pitch = aubio_pitchyin_getpitchfast(p->buf,p->yin, p->yinthres);
220  if (pitch>0) {
221    pitch = p->srate/(pitch+0.);
222  } else {
223    pitch = 0.;
224  }
225  return pitch;
226}
227
228
229smpl_t aubio_pitchdetection_yinfft(aubio_pitchdetection_t *p, fvec_t *ibuf){
230  smpl_t pitch = 0.;
231  aubio_pitchdetection_slideblock(p,ibuf);
232  pitch = aubio_pitchyinfft_do(p->yinfft,p->buf,p->yinthres);
233  if (pitch>0) {
234    pitch = p->srate/(pitch+0.);
235  } else {
236    pitch = 0.;
237  }
238  return pitch; 
239}
240
241smpl_t aubio_pitchdetection_fcomb(aubio_pitchdetection_t *p, fvec_t *ibuf){
242  aubio_pitchdetection_slideblock(p,ibuf);
243  return aubio_pitchfcomb_do(p->fcomb,p->buf);
244}
245
246smpl_t aubio_pitchdetection_schmitt(aubio_pitchdetection_t *p, fvec_t *ibuf){
247  aubio_pitchdetection_slideblock(p,ibuf);
248  return aubio_pitchschmitt_do(p->schmitt,p->buf);
249}
Note: See TracBrowser for help on using the repository browser.