source: src/pitch/pitchdetection.c @ ae4d5de

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

filter.c: add denormal, make multichannel using lvecs, update adsgn, cdsgn, pitchdetection

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