- Timestamp:
- Mar 12, 2013, 4:44:33 AM (12 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:
- 20b1aed
- Parents:
- 426e6f7
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/pitch/pitch.c
r426e6f7 rb130600 57 57 } aubio_pitch_mode; 58 58 59 typedef void (*aubio_pitch_func_t) 60 (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf); 61 typedef smpl_t (*aubio_pitch_conv_t) 62 (smpl_t value, uint_t srate, uint_t bufsize); 63 64 typedef smpl_t (*aubio_conf_cb_t) (void * p); 65 66 void aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf); 67 59 /** callback to get pitch candidate, defined below */ 60 typedef void (*aubio_pitch_detect_t) (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf); 61 62 /** callback to convert pitch from one unit to another, defined below */ 63 typedef smpl_t(*aubio_pitch_convert_t) (smpl_t value, uint_t samplerate, uint_t bufsize); 64 65 /** callback to fetch the confidence of the algorithm */ 66 typedef smpl_t (*aubio_pitch_get_conf_t) (void * p); 67 68 /** generic pitch detection structure */ 69 struct _aubio_pitch_t 70 { 71 aubio_pitch_type type; /**< pitch detection mode */ 72 aubio_pitch_mode mode; /**< pitch detection output mode */ 73 uint_t samplerate; /**< samplerate */ 74 uint_t bufsize; /**< buffer size */ 75 void *p_object; /**< pointer to pitch object */ 76 aubio_filter_t *filter; /**< filter */ 77 aubio_pvoc_t *pv; /**< phase vocoder for mcomb */ 78 cvec_t *fftgrain; /**< spectral frame for mcomb */ 79 fvec_t *buf; /**< temporary buffer for yin */ 80 aubio_pitch_detect_t detect_cb; /**< callback to get the pitch candidates */ 81 aubio_pitch_convert_t conv_cb; /**< callback to convert it to the desired unit */ 82 aubio_pitch_get_conf_t conf_cb; /**< pointer to the current confidence callback */ 83 }; 84 85 /* callback functions for pitch detection */ 68 86 static void aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf); 69 87 static void aubio_pitch_do_yin (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf); … … 72 90 static void aubio_pitch_do_yinfft (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf); 73 91 74 /** generic pitch detection structure */ 75 struct _aubio_pitch_t 76 { 77 aubio_pitch_type type; /**< pitch detection mode */ 78 aubio_pitch_mode mode; /**< pitch detection output mode */ 79 uint_t srate; /**< samplerate */ 80 uint_t bufsize; /**< buffer size */ 81 aubio_pitchmcomb_t *mcomb; /**< mcomb object */ 82 aubio_pitchfcomb_t *fcomb; /**< fcomb object */ 83 aubio_pitchschmitt_t *schmitt; /**< schmitt object */ 84 aubio_pitchyinfft_t *yinfft; /**< yinfft object */ 85 aubio_pitchyin_t *yin; /**< yinfft object */ 86 void *pitch; 87 aubio_filter_t *filter; /**< filter */ 88 aubio_pvoc_t *pv; /**< phase vocoder for mcomb */ 89 cvec_t *fftgrain; /**< spectral frame for mcomb */ 90 fvec_t *buf; /**< temporary buffer for yin */ 91 aubio_pitch_func_t callback; /**< pointer to current pitch detection method */ 92 aubio_pitch_conv_t freqconv; /**< pointer to current pitch conversion method */ 93 aubio_conf_cb_t confidence_callback; /**< pointer to the current confidence callback */ 94 }; 95 96 /* convenience wrapper function for frequency unit conversions 97 * should probably be rewritten with #defines */ 98 smpl_t freqconvbin (smpl_t f, uint_t srate, uint_t bufsize); 99 smpl_t 100 freqconvbin (smpl_t f, uint_t srate, uint_t bufsize) 101 { 102 return aubio_freqtobin (f, srate, bufsize); 103 } 104 105 smpl_t freqconvmidi (smpl_t f, uint_t srate, uint_t bufsize); 106 smpl_t 107 freqconvmidi (smpl_t f, uint_t srate UNUSED, uint_t bufsize UNUSED) 108 { 109 return aubio_freqtomidi (f); 110 } 111 112 smpl_t freqconvpass (smpl_t f, uint_t srate, uint_t bufsize); 113 smpl_t 114 freqconvpass (smpl_t f, uint_t srate UNUSED, uint_t bufsize UNUSED) 115 { 116 return f; 117 } 92 /* conversion functions for frequency conversions */ 93 smpl_t freqconvbin (smpl_t f, uint_t samplerate, uint_t bufsize); 94 smpl_t freqconvmidi (smpl_t f, uint_t samplerate, uint_t bufsize); 95 smpl_t freqconvpass (smpl_t f, uint_t samplerate, uint_t bufsize); 96 97 /* adapter to stack ibuf new samples at the end of buf, and trim `buf` to `bufsize` */ 98 void aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf); 99 118 100 119 101 aubio_pitch_t * … … 140 122 pitch_type = aubio_pitcht_default; 141 123 } 142 p->s rate = samplerate;124 p->samplerate = samplerate; 143 125 p->type = pitch_type; 144 126 aubio_pitch_set_unit (p, "default"); 145 127 p->bufsize = bufsize; 146 p->conf idence_callback= NULL;128 p->conf_cb = NULL; 147 129 switch (p->type) { 148 130 case aubio_pitcht_yin: 149 131 p->buf = new_fvec (bufsize); 150 p->yin = new_aubio_pitchyin (bufsize); 151 p->callback = aubio_pitch_do_yin; 152 p->confidence_callback = (aubio_conf_cb_t)aubio_pitchyin_get_confidence; 153 p->pitch = (void*)p->yin; 154 aubio_pitchyin_set_tolerance (p->yin, 0.15); 132 p->p_object = new_aubio_pitchyin (bufsize); 133 p->detect_cb = aubio_pitch_do_yin; 134 p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyin_get_confidence; 135 aubio_pitchyin_set_tolerance (p->p_object, 0.15); 155 136 break; 156 137 case aubio_pitcht_mcomb: 157 138 p->pv = new_aubio_pvoc (bufsize, hopsize); 158 139 p->fftgrain = new_cvec (bufsize); 159 p-> mcomb= new_aubio_pitchmcomb (bufsize, hopsize);140 p->p_object = new_aubio_pitchmcomb (bufsize, hopsize); 160 141 p->filter = new_aubio_filter_c_weighting (samplerate); 161 p-> callback= aubio_pitch_do_mcomb;142 p->detect_cb = aubio_pitch_do_mcomb; 162 143 break; 163 144 case aubio_pitcht_fcomb: 164 145 p->buf = new_fvec (bufsize); 165 p-> fcomb= new_aubio_pitchfcomb (bufsize, hopsize);166 p-> callback= aubio_pitch_do_fcomb;146 p->p_object = new_aubio_pitchfcomb (bufsize, hopsize); 147 p->detect_cb = aubio_pitch_do_fcomb; 167 148 break; 168 149 case aubio_pitcht_schmitt: 169 150 p->buf = new_fvec (bufsize); 170 p-> schmitt = new_aubio_pitchschmitt (bufsize);171 p-> callback= aubio_pitch_do_schmitt;151 p->p_object = new_aubio_pitchschmitt (bufsize); 152 p->detect_cb = aubio_pitch_do_schmitt; 172 153 break; 173 154 case aubio_pitcht_yinfft: 174 155 p->buf = new_fvec (bufsize); 175 p->yinfft = new_aubio_pitchyinfft (bufsize); 176 p->callback = aubio_pitch_do_yinfft; 177 p->confidence_callback = (aubio_conf_cb_t)aubio_pitchyinfft_get_confidence; 178 p->pitch = (void*)p->yin; 179 aubio_pitchyinfft_set_tolerance (p->yinfft, 0.85); 156 p->p_object = new_aubio_pitchyinfft (bufsize); 157 p->detect_cb = aubio_pitch_do_yinfft; 158 p->conf_cb = (aubio_pitch_get_conf_t)aubio_pitchyinfft_get_confidence; 159 aubio_pitchyinfft_set_tolerance (p->p_object, 0.85); 180 160 break; 181 161 default: … … 191 171 case aubio_pitcht_yin: 192 172 del_fvec (p->buf); 193 del_aubio_pitchyin (p-> yin);173 del_aubio_pitchyin (p->p_object); 194 174 break; 195 175 case aubio_pitcht_mcomb: … … 197 177 del_cvec (p->fftgrain); 198 178 del_aubio_filter (p->filter); 199 del_aubio_pitchmcomb (p-> mcomb);179 del_aubio_pitchmcomb (p->p_object); 200 180 break; 201 181 case aubio_pitcht_schmitt: 202 182 del_fvec (p->buf); 203 del_aubio_pitchschmitt (p-> schmitt);183 del_aubio_pitchschmitt (p->p_object); 204 184 break; 205 185 case aubio_pitcht_fcomb: 206 186 del_fvec (p->buf); 207 del_aubio_pitchfcomb (p-> fcomb);187 del_aubio_pitchfcomb (p->p_object); 208 188 break; 209 189 case aubio_pitcht_yinfft: 210 190 del_fvec (p->buf); 211 del_aubio_pitchyinfft (p-> yinfft);191 del_aubio_pitchyinfft (p->p_object); 212 192 break; 213 193 default: … … 251 231 switch (p->mode) { 252 232 case aubio_pitchm_freq: 253 p-> freqconv= freqconvpass;233 p->conv_cb = freqconvpass; 254 234 break; 255 235 case aubio_pitchm_midi: 256 p-> freqconv= freqconvmidi;236 p->conv_cb = freqconvmidi; 257 237 break; 258 238 case aubio_pitchm_cent: 259 239 /* bug: not implemented */ 260 p-> freqconv= freqconvmidi;240 p->conv_cb = freqconvmidi; 261 241 break; 262 242 case aubio_pitchm_bin: 263 p-> freqconv= freqconvbin;243 p->conv_cb = freqconvbin; 264 244 break; 265 245 default: … … 274 254 switch (p->type) { 275 255 case aubio_pitcht_yin: 276 aubio_pitchyin_set_tolerance (p-> yin, tol);256 aubio_pitchyin_set_tolerance (p->p_object, tol); 277 257 break; 278 258 case aubio_pitcht_yinfft: 279 aubio_pitchyinfft_set_tolerance (p-> yinfft, tol);259 aubio_pitchyinfft_set_tolerance (p->p_object, tol); 280 260 break; 281 261 default: … … 285 265 } 286 266 267 268 /* do method, calling the detection callback, then the conversion callback */ 287 269 void 288 270 aubio_pitch_do (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf) 289 271 { 290 p->callback (p, ibuf, obuf); 291 obuf->data[0] = p->freqconv (obuf->data[0], p->srate, p->bufsize); 292 } 293 272 p->detect_cb (p, ibuf, obuf); 273 obuf->data[0] = p->conv_cb (obuf->data[0], p->samplerate, p->bufsize); 274 } 275 276 /* do method for each algorithm */ 294 277 void 295 278 aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf) … … 297 280 aubio_filter_do (p->filter, ibuf); 298 281 aubio_pvoc_do (p->pv, ibuf, p->fftgrain); 299 aubio_pitchmcomb_do (p-> mcomb, p->fftgrain, obuf);300 obuf->data[0] = aubio_bintofreq (obuf->data[0], p->s rate, p->bufsize);282 aubio_pitchmcomb_do (p->p_object, p->fftgrain, obuf); 283 obuf->data[0] = aubio_bintofreq (obuf->data[0], p->samplerate, p->bufsize); 301 284 } 302 285 … … 306 289 smpl_t pitch = 0.; 307 290 aubio_pitch_slideblock (p, ibuf); 308 aubio_pitchyin_do (p-> yin, p->buf, obuf);291 aubio_pitchyin_do (p->p_object, p->buf, obuf); 309 292 pitch = obuf->data[0]; 310 293 if (pitch > 0) { 311 pitch = p->s rate / (pitch + 0.);294 pitch = p->samplerate / (pitch + 0.); 312 295 } else { 313 296 pitch = 0.; … … 322 305 smpl_t pitch = 0.; 323 306 aubio_pitch_slideblock (p, ibuf); 324 aubio_pitchyinfft_do (p-> yinfft, p->buf, obuf);307 aubio_pitchyinfft_do (p->p_object, p->buf, obuf); 325 308 pitch = obuf->data[0]; 326 309 if (pitch > 0) { 327 pitch = p->s rate / (pitch + 0.);310 pitch = p->samplerate / (pitch + 0.); 328 311 } else { 329 312 pitch = 0.; … … 336 319 { 337 320 aubio_pitch_slideblock (p, ibuf); 338 aubio_pitchfcomb_do (p-> fcomb, p->buf, out);339 out->data[0] = aubio_bintofreq (out->data[0], p->s rate, p->bufsize);321 aubio_pitchfcomb_do (p->p_object, p->buf, out); 322 out->data[0] = aubio_bintofreq (out->data[0], p->samplerate, p->bufsize); 340 323 } 341 324 … … 345 328 smpl_t period, pitch = 0.; 346 329 aubio_pitch_slideblock (p, ibuf); 347 aubio_pitchschmitt_do (p-> schmitt, p->buf, out);330 aubio_pitchschmitt_do (p->p_object, p->buf, out); 348 331 period = out->data[0]; 349 332 if (period > 0) { 350 pitch = p->s rate / period;333 pitch = p->samplerate / period; 351 334 } else { 352 335 pitch = 0.; 353 336 } 354 337 out->data[0] = pitch; 338 } 339 340 /* conversion callbacks */ 341 smpl_t 342 freqconvbin(smpl_t f, uint_t samplerate, uint_t bufsize) 343 { 344 return aubio_freqtobin(f, samplerate, bufsize); 345 } 346 347 smpl_t 348 freqconvmidi (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED) 349 { 350 return aubio_freqtomidi (f); 351 } 352 353 smpl_t 354 freqconvpass (smpl_t f, uint_t samplerate UNUSED, uint_t bufsize UNUSED) 355 { 356 return f; 355 357 } 356 358 … … 359 361 aubio_pitch_get_confidence (aubio_pitch_t * p) 360 362 { 361 if (p->conf idence_callback) {362 return p->conf idence_callback ((void*)(p->pitch));363 if (p->conf_cb) { 364 return p->conf_cb(p->p_object); 363 365 } 364 366 return 0.;
Note: See TracChangeset
for help on using the changeset viewer.