Changeset fddfa64 for src/pitch/pitch.c
- Timestamp:
- Nov 3, 2009, 4:14:03 PM (14 years ago)
- Branches:
- feature/autosink, feature/cnn, feature/cnn_org, feature/constantq, feature/crepe, feature/crepe_org, feature/pitchshift, feature/pydocstrings, feature/timestretch, fix/ffmpeg5, master, pitchshift, sampler, timestretch, yinfft+
- Children:
- bafe71d
- Parents:
- 63f3c70
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/pitch/pitch.c
r63f3c70 rfddfa64 36 36 37 37 /** pitch detection algorithm */ 38 typedef enum { 38 typedef enum 39 { 39 40 aubio_pitcht_yin, /**< YIN algorithm */ 40 41 aubio_pitcht_mcomb, /**< Multi-comb filter */ … … 46 47 47 48 /** pitch detection output mode */ 48 typedef enum { 49 typedef enum 50 { 49 51 aubio_pitchm_freq, /**< Frequency (Hz) */ 50 52 aubio_pitchm_midi, /**< MIDI note (0.,127) */ … … 55 57 56 58 typedef void (*aubio_pitch_func_t) 57 (aubio_pitch_t * p, fvec_t * ibuf, fvec_t *obuf);59 (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf); 58 60 typedef smpl_t (*aubio_pitch_conv_t) 59 61 (smpl_t value, uint_t srate, uint_t bufsize); 60 62 61 void aubio_pitch_slideblock (aubio_pitch_t *p, fvec_t *ibuf);62 63 void aubio_pitch_do_mcomb (aubio_pitch_t *p, fvec_t *ibuf, fvec_t *obuf);64 void aubio_pitch_do_yin (aubio_pitch_t *p, fvec_t *ibuf, fvec_t *obuf);65 void aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t *ibuf, fvec_t *obuf);66 void aubio_pitch_do_fcomb (aubio_pitch_t *p, fvec_t *ibuf, fvec_t *obuf);67 void aubio_pitch_do_yinfft (aubio_pitch_t *p, fvec_t *ibuf, fvec_t *obuf);63 void aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf); 64 65 void aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf); 66 void aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf); 67 void aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf); 68 void aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf); 69 void aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf); 68 70 69 71 /** generic pitch detection structure */ 70 struct _aubio_pitch_t { 72 struct _aubio_pitch_t 73 { 71 74 aubio_pitch_type type; /**< pitch detection mode */ 72 75 aubio_pitch_mode mode; /**< pitch detection output mode */ 73 76 uint_t srate; /**< samplerate */ 74 77 uint_t bufsize; /**< buffer size */ 75 aubio_pitchmcomb_t * mcomb;/**< mcomb object */76 aubio_pitchfcomb_t * fcomb;/**< fcomb object */77 aubio_pitchschmitt_t * schmitt;/**< schmitt object */78 aubio_pitchyinfft_t * yinfft;/**< yinfft object */79 aubio_pitchyin_t * yin;/**< yinfft object */80 aubio_filter_t * filter;/**< filter */81 aubio_pvoc_t * pv; /**< phase vocoder for mcomb */82 cvec_t * fftgrain;/**< spectral frame for mcomb */83 fvec_t * buf;/**< temporary buffer for yin */78 aubio_pitchmcomb_t *mcomb; /**< mcomb object */ 79 aubio_pitchfcomb_t *fcomb; /**< fcomb object */ 80 aubio_pitchschmitt_t *schmitt; /**< schmitt object */ 81 aubio_pitchyinfft_t *yinfft; /**< yinfft object */ 82 aubio_pitchyin_t *yin; /**< yinfft object */ 83 aubio_filter_t *filter; /**< filter */ 84 aubio_pvoc_t *pv; /**< phase vocoder for mcomb */ 85 cvec_t *fftgrain; /**< spectral frame for mcomb */ 86 fvec_t *buf; /**< temporary buffer for yin */ 84 87 aubio_pitch_func_t callback; /**< pointer to current pitch detection method */ 85 aubio_pitch_conv_t freqconv; /**< pointer to current pitch conversion method */ 88 aubio_pitch_conv_t freqconv; /**< pointer to current pitch conversion method */ 86 89 }; 87 90 88 91 /* convenience wrapper function for frequency unit conversions 89 92 * should probably be rewritten with #defines */ 90 smpl_t freqconvbin(smpl_t f,uint_t srate,uint_t bufsize); 91 smpl_t freqconvbin(smpl_t f,uint_t srate,uint_t bufsize){ 92 return aubio_freqtobin(f,srate,bufsize); 93 } 94 95 smpl_t freqconvmidi(smpl_t f,uint_t srate,uint_t bufsize); 96 smpl_t freqconvmidi(smpl_t f,uint_t srate UNUSED,uint_t bufsize UNUSED){ 97 return aubio_freqtomidi(f); 98 } 99 100 smpl_t freqconvpass(smpl_t f,uint_t srate,uint_t bufsize); 101 smpl_t freqconvpass(smpl_t f,uint_t srate UNUSED,uint_t bufsize UNUSED){ 93 smpl_t freqconvbin (smpl_t f, uint_t srate, uint_t bufsize); 94 smpl_t 95 freqconvbin (smpl_t f, uint_t srate, uint_t bufsize) 96 { 97 return aubio_freqtobin (f, srate, bufsize); 98 } 99 100 smpl_t freqconvmidi (smpl_t f, uint_t srate, uint_t bufsize); 101 smpl_t 102 freqconvmidi (smpl_t f, uint_t srate UNUSED, uint_t bufsize UNUSED) 103 { 104 return aubio_freqtomidi (f); 105 } 106 107 smpl_t freqconvpass (smpl_t f, uint_t srate, uint_t bufsize); 108 smpl_t 109 freqconvpass (smpl_t f, uint_t srate UNUSED, uint_t bufsize UNUSED) 110 { 102 111 return f; 103 112 } … … 107 116 uint_t bufsize, uint_t hopsize, uint_t channels, uint_t samplerate) 108 117 { 109 aubio_pitch_t *p = AUBIO_NEW (aubio_pitch_t);118 aubio_pitch_t *p = AUBIO_NEW (aubio_pitch_t); 110 119 aubio_pitch_type pitch_type; 111 120 if (strcmp (pitch_mode, "mcomb") == 0) 112 121 pitch_type = aubio_pitcht_mcomb; 113 122 else if (strcmp (pitch_mode, "yinfft") == 0) 114 123 pitch_type = aubio_pitcht_yin; 115 124 else if (strcmp (pitch_mode, "yin") == 0) 116 125 pitch_type = aubio_pitcht_yin; 117 126 else if (strcmp (pitch_mode, "schmitt") == 0) 118 127 pitch_type = aubio_pitcht_schmitt; 119 128 else if (strcmp (pitch_mode, "fcomb") == 0) 120 129 pitch_type = aubio_pitcht_fcomb; 121 130 else if (strcmp (pitch_mode, "default") == 0) 122 131 pitch_type = aubio_pitcht_default; 123 132 else { 124 AUBIO_ERR ("unknown pitch detection method %s, using default.\n", pitch_mode); 125 pitch_type = aubio_pitcht_default; 126 return NULL; 133 AUBIO_ERR ("unknown pitch detection method %s, using default.\n", 134 pitch_mode); 135 pitch_type = aubio_pitcht_default; 136 return NULL; 127 137 } 128 138 p->srate = samplerate; … … 130 140 aubio_pitch_set_unit (p, "default"); 131 141 p->bufsize = bufsize; 132 switch (p->type) {142 switch (p->type) { 133 143 case aubio_pitcht_yin: 134 p->buf = new_fvec(bufsize,channels);135 p->yin = new_aubio_pitchyin(bufsize);144 p->buf = new_fvec (bufsize, channels); 145 p->yin = new_aubio_pitchyin (bufsize); 136 146 p->callback = aubio_pitch_do_yin; 137 147 aubio_pitchyin_set_tolerance (p->yin, 0.15); 138 148 break; 139 149 case aubio_pitcht_mcomb: 140 p->pv = new_aubio_pvoc(bufsize, hopsize, channels);141 p->fftgrain = new_cvec (bufsize, channels);142 p->mcomb = new_aubio_pitchmcomb(bufsize,hopsize,channels);143 p->filter 150 p->pv = new_aubio_pvoc (bufsize, hopsize, channels); 151 p->fftgrain = new_cvec (bufsize, channels); 152 p->mcomb = new_aubio_pitchmcomb (bufsize, hopsize, channels); 153 p->filter = new_aubio_filter_c_weighting (samplerate, channels); 144 154 p->callback = aubio_pitch_do_mcomb; 145 155 break; 146 156 case aubio_pitcht_fcomb: 147 p->buf = new_fvec(bufsize,channels);148 p->fcomb = new_aubio_pitchfcomb(bufsize,hopsize,channels);157 p->buf = new_fvec (bufsize, channels); 158 p->fcomb = new_aubio_pitchfcomb (bufsize, hopsize, channels); 149 159 p->callback = aubio_pitch_do_fcomb; 150 160 break; 151 161 case aubio_pitcht_schmitt: 152 p->buf = new_fvec(bufsize,channels);153 p->schmitt = new_aubio_pitchschmitt(bufsize);162 p->buf = new_fvec (bufsize, channels); 163 p->schmitt = new_aubio_pitchschmitt (bufsize); 154 164 p->callback = aubio_pitch_do_schmitt; 155 165 break; 156 166 case aubio_pitcht_yinfft: 157 p->buf = new_fvec(bufsize,channels);158 p->yinfft = new_aubio_pitchyinfft(bufsize);167 p->buf = new_fvec (bufsize, channels); 168 p->yinfft = new_aubio_pitchyinfft (bufsize); 159 169 p->callback = aubio_pitch_do_yinfft; 160 170 aubio_pitchyinfft_set_tolerance (p->yinfft, 0.85); … … 166 176 } 167 177 168 void del_aubio_pitch(aubio_pitch_t * p) { 169 switch(p->type) { 178 void 179 del_aubio_pitch (aubio_pitch_t * p) 180 { 181 switch (p->type) { 170 182 case aubio_pitcht_yin: 171 del_fvec (p->buf);172 del_aubio_pitchyin (p->yin);183 del_fvec (p->buf); 184 del_aubio_pitchyin (p->yin); 173 185 break; 174 186 case aubio_pitcht_mcomb: 175 del_aubio_pvoc (p->pv);176 del_cvec (p->fftgrain);177 del_aubio_filter (p->filter);178 del_aubio_pitchmcomb (p->mcomb);187 del_aubio_pvoc (p->pv); 188 del_cvec (p->fftgrain); 189 del_aubio_filter (p->filter); 190 del_aubio_pitchmcomb (p->mcomb); 179 191 break; 180 192 case aubio_pitcht_schmitt: 181 del_fvec (p->buf);182 del_aubio_pitchschmitt (p->schmitt);193 del_fvec (p->buf); 194 del_aubio_pitchschmitt (p->schmitt); 183 195 break; 184 196 case aubio_pitcht_fcomb: 185 del_fvec (p->buf);186 del_aubio_pitchfcomb (p->fcomb);197 del_fvec (p->buf); 198 del_aubio_pitchfcomb (p->fcomb); 187 199 break; 188 200 case aubio_pitcht_yinfft: 189 del_fvec (p->buf);190 del_aubio_pitchyinfft (p->yinfft);201 del_fvec (p->buf); 202 del_aubio_pitchyinfft (p->yinfft); 191 203 break; 192 204 default: 193 205 break; 194 206 } 195 AUBIO_FREE(p); 196 } 197 198 void aubio_pitch_slideblock(aubio_pitch_t *p, fvec_t *ibuf){ 199 uint_t i,j = 0, overlap_size = 0; 200 overlap_size = p->buf->length-ibuf->length; 201 for (i=0;i<p->buf->channels;i++){ 202 for (j=0;j<overlap_size;j++){ 203 p->buf->data[i][j] = p->buf->data[i][j+ibuf->length]; 204 } 205 } 206 for (i=0;i<ibuf->channels;i++){ 207 for (j=0;j<ibuf->length;j++){ 208 p->buf->data[i][j+overlap_size] = ibuf->data[i][j]; 209 } 210 } 211 } 212 213 uint_t aubio_pitch_set_unit (aubio_pitch_t *p, char_t * pitch_unit) { 207 AUBIO_FREE (p); 208 } 209 210 void 211 aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf) 212 { 213 uint_t i, j = 0, overlap_size = 0; 214 overlap_size = p->buf->length - ibuf->length; 215 for (i = 0; i < p->buf->channels; i++) { 216 for (j = 0; j < overlap_size; j++) { 217 p->buf->data[i][j] = p->buf->data[i][j + ibuf->length]; 218 } 219 } 220 for (i = 0; i < ibuf->channels; i++) { 221 for (j = 0; j < ibuf->length; j++) { 222 p->buf->data[i][j + overlap_size] = ibuf->data[i][j]; 223 } 224 } 225 } 226 227 uint_t 228 aubio_pitch_set_unit (aubio_pitch_t * p, char_t * pitch_unit) 229 { 214 230 aubio_pitch_mode pitch_mode; 215 231 if (strcmp (pitch_unit, "freq") == 0) 216 232 pitch_mode = aubio_pitchm_freq; 217 233 else if (strcmp (pitch_unit, "midi") == 0) 218 234 pitch_mode = aubio_pitchm_midi; 219 235 else if (strcmp (pitch_unit, "cent") == 0) 220 236 pitch_mode = aubio_pitchm_cent; 221 237 else if (strcmp (pitch_unit, "bin") == 0) 222 238 pitch_mode = aubio_pitchm_bin; 223 239 else if (strcmp (pitch_unit, "default") == 0) 224 240 pitch_mode = aubio_pitchm_default; 225 241 else { 226 227 242 AUBIO_ERR ("unknown pitch detection unit %s, using default\n", pitch_unit); 243 pitch_mode = aubio_pitchm_default; 228 244 } 229 245 p->mode = pitch_mode; 230 switch (p->mode) {246 switch (p->mode) { 231 247 case aubio_pitchm_freq: 232 248 p->freqconv = freqconvpass; … … 248 264 } 249 265 250 uint_t aubio_pitch_set_tolerance(aubio_pitch_t *p, smpl_t tol) { 251 switch(p->type) { 266 uint_t 267 aubio_pitch_set_tolerance (aubio_pitch_t * p, smpl_t tol) 268 { 269 switch (p->type) { 252 270 case aubio_pitcht_yin: 253 271 aubio_pitchyin_set_tolerance (p->yin, tol); … … 262 280 } 263 281 264 void aubio_pitch_do (aubio_pitch_t *p, fvec_t * ibuf, fvec_t *obuf) { 265 uint_t i; 266 p->callback(p, ibuf, obuf); 282 void 283 aubio_pitch_do (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf) 284 { 285 uint_t i; 286 p->callback (p, ibuf, obuf); 267 287 for (i = 0; i < obuf->channels; i++) { 268 p->freqconv(obuf->data[i][0],p->srate,p->bufsize); 269 } 270 } 271 272 void aubio_pitch_do_mcomb(aubio_pitch_t *p, fvec_t *ibuf, fvec_t * obuf) { 273 uint_t i; 274 aubio_filter_do(p->filter,ibuf); 275 aubio_pvoc_do(p->pv,ibuf,p->fftgrain); 276 aubio_pitchmcomb_do(p->mcomb,p->fftgrain, obuf); 288 p->freqconv (obuf->data[i][0], p->srate, p->bufsize); 289 } 290 } 291 292 void 293 aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf) 294 { 295 uint_t i; 296 aubio_filter_do (p->filter, ibuf); 297 aubio_pvoc_do (p->pv, ibuf, p->fftgrain); 298 aubio_pitchmcomb_do (p->mcomb, p->fftgrain, obuf); 277 299 for (i = 0; i < obuf->channels; i++) { 278 300 obuf->data[i][0] = aubio_bintofreq (obuf->data[i][0], p->srate, p->bufsize); … … 280 302 } 281 303 282 void aubio_pitch_do_yin(aubio_pitch_t *p, fvec_t *ibuf, fvec_t * obuf) { 304 void 305 aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf) 306 { 283 307 smpl_t pitch = 0.; 284 308 uint_t i; 285 aubio_pitch_slideblock (p,ibuf);286 aubio_pitchyin_do (p->yin,p->buf, obuf);309 aubio_pitch_slideblock (p, ibuf); 310 aubio_pitchyin_do (p->yin, p->buf, obuf); 287 311 for (i = 0; i < obuf->channels; i++) { 288 312 pitch = obuf->data[i][0]; 289 if (pitch >0) {290 pitch = p->srate /(pitch+0.);313 if (pitch > 0) { 314 pitch = p->srate / (pitch + 0.); 291 315 } else { 292 316 pitch = 0.; … … 297 321 298 322 299 void aubio_pitch_do_yinfft(aubio_pitch_t *p, fvec_t *ibuf, fvec_t * obuf){ 323 void 324 aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf) 325 { 300 326 smpl_t pitch = 0.; 301 327 uint_t i; 302 aubio_pitch_slideblock (p,ibuf);303 aubio_pitchyinfft_do (p->yinfft,p->buf,obuf);328 aubio_pitch_slideblock (p, ibuf); 329 aubio_pitchyinfft_do (p->yinfft, p->buf, obuf); 304 330 for (i = 0; i < obuf->channels; i++) { 305 331 pitch = obuf->data[i][0]; 306 if (pitch >0) {307 pitch = p->srate /(pitch+0.);332 if (pitch > 0) { 333 pitch = p->srate / (pitch + 0.); 308 334 } else { 309 335 pitch = 0.; … … 313 339 } 314 340 315 void aubio_pitch_do_fcomb(aubio_pitch_t *p, fvec_t *ibuf, fvec_t * out){ 316 uint_t i; 317 aubio_pitch_slideblock(p,ibuf); 318 aubio_pitchfcomb_do(p->fcomb,p->buf, out); 341 void 342 aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out) 343 { 344 uint_t i; 345 aubio_pitch_slideblock (p, ibuf); 346 aubio_pitchfcomb_do (p->fcomb, p->buf, out); 319 347 for (i = 0; i < out->channels; i++) { 320 348 out->data[i][0] = aubio_bintofreq (out->data[i][0], p->srate, p->bufsize); … … 322 350 } 323 351 324 void aubio_pitch_do_schmitt(aubio_pitch_t *p, fvec_t *ibuf, fvec_t *out){ 352 void 353 aubio_pitch_do_schmitt (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out) 354 { 325 355 smpl_t period, pitch = 0.; 326 356 uint_t i; 327 aubio_pitch_slideblock (p,ibuf);328 aubio_pitchschmitt_do (p->schmitt,p->buf, out);357 aubio_pitch_slideblock (p, ibuf); 358 aubio_pitchschmitt_do (p->schmitt, p->buf, out); 329 359 for (i = 0; i < out->channels; i++) { 330 360 period = out->data[i][0]; 331 if (period >0) {332 pitch = p->srate /period;361 if (period > 0) { 362 pitch = p->srate / period; 333 363 } else { 334 364 pitch = 0.;
Note: See TracChangeset
for help on using the changeset viewer.