source: src/pitch/pitchdetection.c @ ed631e9

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

src/: more moving and splitting

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