- Timestamp:
- Nov 3, 2009, 4:14:03 PM (15 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
- Location:
- src/pitch
- Files:
-
- 10 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.; -
src/pitch/pitch.h
r63f3c70 rfddfa64 45 45 46 46 */ 47 void aubio_pitch_do (aubio_pitch_t * o, fvec_t * in, 48 fvec_t * out); 47 void aubio_pitch_do (aubio_pitch_t * o, fvec_t * in, fvec_t * out); 49 48 50 49 /** change yin or yinfft tolerance threshold … … 54 53 55 54 */ 56 uint_t aubio_pitch_set_tolerance (aubio_pitch_t * o, 57 smpl_t tol); 55 uint_t aubio_pitch_set_tolerance (aubio_pitch_t * o, smpl_t tol); 58 56 59 57 /** deletion of the pitch detection object … … 73 71 74 72 */ 75 aubio_pitch_t * 73 aubio_pitch_t *new_aubio_pitch (char_t * mode, 76 74 uint_t bufsize, uint_t hopsize, uint_t channels, uint_t samplerate); 77 75 … … 82 80 83 81 */ 84 uint_t aubio_pitch_set_unit (aubio_pitch_t * o, 85 char_t * mode); 82 uint_t aubio_pitch_set_unit (aubio_pitch_t * o, char_t * mode); 86 83 87 84 #ifdef __cplusplus -
src/pitch/pitchfcomb.c
r63f3c70 rfddfa64 30 30 #define MAX_PEAKS 8 31 31 32 typedef struct { 32 typedef struct 33 { 33 34 smpl_t bin; 34 35 smpl_t db; 35 36 } aubio_fpeak_t; 36 37 37 struct _aubio_pitchfcomb_t { 38 struct _aubio_pitchfcomb_t 39 { 38 40 uint_t fftSize; 39 41 uint_t stepSize; 40 42 uint_t rate; 41 fvec_t * 42 fvec_t * 43 cvec_t * 44 fvec_t * 45 aubio_fft_t * 43 fvec_t *winput; 44 fvec_t *win; 45 cvec_t *fftOut; 46 fvec_t *fftLastPhase; 47 aubio_fft_t *fft; 46 48 }; 47 49 48 aubio_pitchfcomb_t * new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize, uint_t channels) 50 aubio_pitchfcomb_t * 51 new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize, uint_t channels) 49 52 { 50 aubio_pitchfcomb_t * p = AUBIO_NEW(aubio_pitchfcomb_t);51 p->fftSize 52 p->stepSize 53 p->winput = new_fvec(bufsize,1);54 p->fftOut = new_cvec(bufsize,1);55 p->fftLastPhase = new_fvec (bufsize, channels);56 p->fft = new_aubio_fft (bufsize, 1);57 p->win = new_aubio_window ("hanning", bufsize);53 aubio_pitchfcomb_t *p = AUBIO_NEW (aubio_pitchfcomb_t); 54 p->fftSize = bufsize; 55 p->stepSize = hopsize; 56 p->winput = new_fvec (bufsize, 1); 57 p->fftOut = new_cvec (bufsize, 1); 58 p->fftLastPhase = new_fvec (bufsize, channels); 59 p->fft = new_aubio_fft (bufsize, 1); 60 p->win = new_aubio_window ("hanning", bufsize); 58 61 return p; 59 62 } 60 63 61 64 /* input must be stepsize long */ 62 void aubio_pitchfcomb_do (aubio_pitchfcomb_t * p, fvec_t * input, fvec_t * output) 65 void 66 aubio_pitchfcomb_do (aubio_pitchfcomb_t * p, fvec_t * input, fvec_t * output) 63 67 { 64 68 uint_t i, k, l, maxharm = 0; 65 smpl_t phaseDifference = TWO_PI *(smpl_t)p->stepSize/(smpl_t)p->fftSize;69 smpl_t phaseDifference = TWO_PI * (smpl_t) p->stepSize / (smpl_t) p->fftSize; 66 70 aubio_fpeak_t peaks[MAX_PEAKS]; 67 71 68 72 for (i = 0; i < input->channels; i++) { 69 73 70 for (k=0; k<MAX_PEAKS; k++) {71 peaks[k].db = -200.;72 peaks[k].bin = 0.;73 }74 for (k = 0; k < MAX_PEAKS; k++) { 75 peaks[k].db = -200.; 76 peaks[k].bin = 0.; 77 } 74 78 75 for (k=0; k < input->length; k++){76 p->winput->data[0][k] = p->win->data[0][k] * input->data[i][k];77 }78 aubio_fft_do(p->fft,p->winput,p->fftOut);79 for (k = 0; k < input->length; k++) { 80 p->winput->data[0][k] = p->win->data[0][k] * input->data[i][k]; 81 } 82 aubio_fft_do (p->fft, p->winput, p->fftOut); 79 83 80 for (k=0; k<=p->fftSize/2; k++) {81 smpl_t82 magnitude = 20.*LOG10(2.*p->fftOut->norm[0][k]/(smpl_t)p->fftSize),83 phase = p->fftOut->phas[0][k],84 tmp, bin;84 for (k = 0; k <= p->fftSize / 2; k++) { 85 smpl_t 86 magnitude = 87 20. * LOG10 (2. * p->fftOut->norm[0][k] / (smpl_t) p->fftSize), 88 phase = p->fftOut->phas[0][k], tmp, bin; 85 89 86 /* compute phase difference */87 tmp = phase - p->fftLastPhase->data[i][k];88 p->fftLastPhase->data[i][k] = phase;90 /* compute phase difference */ 91 tmp = phase - p->fftLastPhase->data[i][k]; 92 p->fftLastPhase->data[i][k] = phase; 89 93 90 /* subtract expected phase difference */91 tmp -= (smpl_t)k*phaseDifference;94 /* subtract expected phase difference */ 95 tmp -= (smpl_t) k *phaseDifference; 92 96 93 /* map delta phase into +/- Pi interval */94 tmp = aubio_unwrap2pi(tmp);97 /* map delta phase into +/- Pi interval */ 98 tmp = aubio_unwrap2pi (tmp); 95 99 96 /* get deviation from bin frequency from the +/- Pi interval */97 tmp = p->fftSize/(smpl_t)p->stepSize*tmp/(TWO_PI);100 /* get deviation from bin frequency from the +/- Pi interval */ 101 tmp = p->fftSize / (smpl_t) p->stepSize * tmp / (TWO_PI); 98 102 99 /* compute the k-th partials' true bin */100 bin = (smpl_t)k + tmp;103 /* compute the k-th partials' true bin */ 104 bin = (smpl_t) k + tmp; 101 105 102 if (bin > 0.0 && magnitude > peaks[0].db) { // && magnitude < 0) { 103 memmove(peaks+1, peaks, sizeof(aubio_fpeak_t)*(MAX_PEAKS-1)); 104 peaks[0].bin = bin; 105 peaks[0].db = magnitude; 106 if (bin > 0.0 && magnitude > peaks[0].db) { // && magnitude < 0) { 107 memmove (peaks + 1, peaks, sizeof (aubio_fpeak_t) * (MAX_PEAKS - 1)); 108 peaks[0].bin = bin; 109 peaks[0].db = magnitude; 110 } 106 111 } 107 }108 112 109 k = 0;110 for (l=1; l<MAX_PEAKS && peaks[l].bin > 0.0; l++) {111 sint_t harmonic;112 for (harmonic=5; harmonic>1; harmonic--) {113 if (peaks[0].bin / peaks[l].bin < harmonic+.02 &&114 peaks[0].bin / peaks[l].bin > harmonic-.02) {115 if (harmonic > (sint_t)maxharm &&116 peaks[0].db < peaks[l].db/2) {117 maxharm = harmonic;118 k = l;113 k = 0; 114 for (l = 1; l < MAX_PEAKS && peaks[l].bin > 0.0; l++) { 115 sint_t harmonic; 116 for (harmonic = 5; harmonic > 1; harmonic--) { 117 if (peaks[0].bin / peaks[l].bin < harmonic + .02 && 118 peaks[0].bin / peaks[l].bin > harmonic - .02) { 119 if (harmonic > (sint_t) maxharm && peaks[0].db < peaks[l].db / 2) { 120 maxharm = harmonic; 121 k = l; 122 } 119 123 } 120 124 } 121 125 } 122 }123 output->data[i][0] = peaks[k].bin;124 /* quick hack to clean output a bit */125 if (peaks[k].bin > 5000.)output->data[i][0] = 0.;126 output->data[i][0] = peaks[k].bin; 127 /* quick hack to clean output a bit */ 128 if (peaks[k].bin > 5000.) 129 output->data[i][0] = 0.; 126 130 } 127 131 } 128 132 129 void del_aubio_pitchfcomb (aubio_pitchfcomb_t * p) 133 void 134 del_aubio_pitchfcomb (aubio_pitchfcomb_t * p) 130 135 { 131 del_cvec (p->fftOut);132 del_fvec (p->fftLastPhase);133 del_fvec (p->win);134 del_fvec (p->winput);135 del_aubio_fft (p->fft);136 AUBIO_FREE (p);136 del_cvec (p->fftOut); 137 del_fvec (p->fftLastPhase); 138 del_fvec (p->win); 139 del_fvec (p->winput); 140 del_aubio_fft (p->fft); 141 AUBIO_FREE (p); 137 142 } 138 -
src/pitch/pitchfcomb.h
r63f3c70 rfddfa64 50 50 51 51 */ 52 void aubio_pitchfcomb_do (aubio_pitchfcomb_t *p, fvec_t * input, fvec_t * output); 52 void aubio_pitchfcomb_do (aubio_pitchfcomb_t * p, fvec_t * input, 53 fvec_t * output); 54 53 55 /** creation of the pitch detection object 54 56 … … 58 60 59 61 */ 60 aubio_pitchfcomb_t * new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize, uint_t channels); 62 aubio_pitchfcomb_t *new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize, 63 uint_t channels); 64 61 65 /** deletion of the pitch detection object 62 66 … … 64 68 65 69 */ 66 void del_aubio_pitchfcomb (aubio_pitchfcomb_t *p); 67 70 void del_aubio_pitchfcomb (aubio_pitchfcomb_t * p); 68 71 69 72 #ifdef __cplusplus -
src/pitch/pitchmcomb.c
r63f3c70 rfddfa64 29 29 typedef struct _aubio_spectralpeak_t aubio_spectralpeak_t; 30 30 typedef struct _aubio_spectralcandidate_t aubio_spectralcandidate_t; 31 uint_t aubio_pitchmcomb_get_root_peak(aubio_spectralpeak_t * peaks, uint_t length); 32 uint_t aubio_pitchmcomb_quadpick(aubio_spectralpeak_t * spectral_peaks, fvec_t * X); 33 void aubio_pitchmcomb_spectral_pp(aubio_pitchmcomb_t * p, fvec_t * oldmag); 34 void aubio_pitchmcomb_combdet(aubio_pitchmcomb_t * p, fvec_t * newmag); 31 uint_t aubio_pitchmcomb_get_root_peak (aubio_spectralpeak_t * peaks, 32 uint_t length); 33 uint_t aubio_pitchmcomb_quadpick (aubio_spectralpeak_t * spectral_peaks, 34 fvec_t * X); 35 void aubio_pitchmcomb_spectral_pp (aubio_pitchmcomb_t * p, fvec_t * oldmag); 36 void aubio_pitchmcomb_combdet (aubio_pitchmcomb_t * p, fvec_t * newmag); 35 37 /* not used but useful : sort by amplitudes (or anything else) 36 38 * sort_pitchpeak(peaks, length); 37 39 */ 38 40 /** spectral_peak comparison function (must return signed int) */ 39 static sint_t aubio_pitchmcomb_sort_peak_comp (const void *x, const void *y);41 static sint_t aubio_pitchmcomb_sort_peak_comp (const void *x, const void *y); 40 42 /** sort spectral_peak against their mag */ 41 void aubio_pitchmcomb_sort_peak (aubio_spectralpeak_t * peaks, uint_t nbins);43 void aubio_pitchmcomb_sort_peak (aubio_spectralpeak_t * peaks, uint_t nbins); 42 44 /** select the best candidates */ 43 uint_t aubio_pitch_cands(aubio_pitchmcomb_t * p, cvec_t * fftgrain, smpl_t * cands); 45 uint_t aubio_pitch_cands (aubio_pitchmcomb_t * p, cvec_t * fftgrain, 46 smpl_t * cands); 44 47 45 48 /** sort spectral_candidate against their comb ene */ 46 void aubio_pitchmcomb_sort_cand_ene(aubio_spectralcandidate_t ** candidates, uint_t nbins); 49 void aubio_pitchmcomb_sort_cand_ene (aubio_spectralcandidate_t ** candidates, 50 uint_t nbins); 47 51 /** sort spectral_candidate against their frequency */ 48 void aubio_pitchmcomb_sort_cand_freq(aubio_spectralcandidate_t ** candidates, uint_t nbins); 49 50 struct _aubio_pitchmcomb_t { 52 void aubio_pitchmcomb_sort_cand_freq (aubio_spectralcandidate_t ** candidates, 53 uint_t nbins); 54 55 struct _aubio_pitchmcomb_t 56 { 51 57 smpl_t threshold; /**< offset threshold [0.033 or 0.01] */ 52 58 smpl_t alpha; /**< normalisation exponent [9] */ … … 61 67 uint_t goodcandidate; /**< best candidate */ 62 68 uint_t spec_partition; /**< spectrum partition to consider */ 63 aubio_spectralpeak_t * peaks;/**< up to length win/spec_partition */64 aubio_spectralcandidate_t ** candidates;/** up to five candidates */69 aubio_spectralpeak_t *peaks; /**< up to length win/spec_partition */ 70 aubio_spectralcandidate_t **candidates; /** up to five candidates */ 65 71 /* some scratch pads */ 66 72 /** \bug (unnecessary copied from fftgrain?) */ 67 fvec_t * newmag;/**< vec to store mag */68 fvec_t * scratch;/**< vec to store modified mag */69 fvec_t * scratch2;/**< vec to compute moving median */70 fvec_t * theta;/**< vec to store phase */73 fvec_t *newmag; /**< vec to store mag */ 74 fvec_t *scratch; /**< vec to store modified mag */ 75 fvec_t *scratch2; /**< vec to compute moving median */ 76 fvec_t *theta; /**< vec to store phase */ 71 77 smpl_t phasediff; 72 78 smpl_t phasefreq; … … 78 84 79 85 /** spectral peak object */ 80 struct _aubio_spectralpeak_t { 86 struct _aubio_spectralpeak_t 87 { 81 88 uint_t bin; /**< bin [0-(length-1)] */ 82 89 smpl_t ebin; /**< estimated bin */ … … 85 92 86 93 /** spectral candidates array object */ 87 struct _aubio_spectralcandidate_t { 94 struct _aubio_spectralcandidate_t 95 { 88 96 smpl_t ebin; /**< interpolated bin */ 89 smpl_t * ecomb;/**< comb */97 smpl_t *ecomb; /**< comb */ 90 98 smpl_t ene; /**< candidate energy */ 91 99 smpl_t len; /**< length */ … … 93 101 94 102 95 void aubio_pitchmcomb_do (aubio_pitchmcomb_t * p, cvec_t * fftgrain, fvec_t * output) { 96 uint_t i,j; 103 void 104 aubio_pitchmcomb_do (aubio_pitchmcomb_t * p, cvec_t * fftgrain, fvec_t * output) 105 { 106 uint_t i, j; 97 107 smpl_t instfreq; 98 fvec_t * newmag = (fvec_t *)p->newmag;108 fvec_t *newmag = (fvec_t *) p->newmag; 99 109 //smpl_t hfc; //fe=instfreq(theta1,theta,ops); //theta1=theta; 100 110 /* copy incoming grain to newmag */ 101 for (i =0; i< fftgrain->channels; i++) {102 for (j=0; j< newmag->length; j++)103 newmag->data[0][j]=fftgrain->norm[i][j];104 /* detect only if local energy > 10. */105 //if (fvec_local_energy(newmag)>10.) {111 for (i = 0; i < fftgrain->channels; i++) { 112 for (j = 0; j < newmag->length; j++) 113 newmag->data[0][j] = fftgrain->norm[i][j]; 114 /* detect only if local energy > 10. */ 115 //if (fvec_local_energy(newmag)>10.) { 106 116 //hfc = fvec_local_hfc(newmag); //not used 107 aubio_pitchmcomb_spectral_pp (p, newmag);108 aubio_pitchmcomb_combdet (p,newmag);117 aubio_pitchmcomb_spectral_pp (p, newmag); 118 aubio_pitchmcomb_combdet (p, newmag); 109 119 //aubio_pitchmcomb_sort_cand_freq(p->candidates,p->ncand); 110 120 //return p->candidates[p->goodcandidate]->ebin; 111 j = (uint_t)FLOOR(p->candidates[p->goodcandidate]->ebin+.5); 112 instfreq = aubio_unwrap2pi(fftgrain->phas[i][j] 113 - p->theta->data[i][j] - j*p->phasediff); 114 instfreq *= p->phasefreq; 115 /* store phase for next run */ 116 for (j=0; j< p->theta->length; j++) { 117 p->theta->data[i][j]=fftgrain->phas[i][j]; 118 } 119 //return p->candidates[p->goodcandidate]->ebin; 120 output->data[i][0] = FLOOR(p->candidates[p->goodcandidate]->ebin+.5) + instfreq; 121 /*} else { 122 return -1.; 123 }*/ 124 } 125 } 126 127 uint_t aubio_pitch_cands(aubio_pitchmcomb_t * p, cvec_t * fftgrain, 128 smpl_t * cands) { 129 uint_t i=0,j; 121 j = (uint_t) FLOOR (p->candidates[p->goodcandidate]->ebin + .5); 122 instfreq = aubio_unwrap2pi (fftgrain->phas[i][j] 123 - p->theta->data[i][j] - j * p->phasediff); 124 instfreq *= p->phasefreq; 125 /* store phase for next run */ 126 for (j = 0; j < p->theta->length; j++) { 127 p->theta->data[i][j] = fftgrain->phas[i][j]; 128 } 129 //return p->candidates[p->goodcandidate]->ebin; 130 output->data[i][0] = 131 FLOOR (p->candidates[p->goodcandidate]->ebin + .5) + instfreq; 132 /*} else { 133 return -1.; 134 } */ 135 } 136 } 137 138 uint_t 139 aubio_pitch_cands (aubio_pitchmcomb_t * p, cvec_t * fftgrain, smpl_t * cands) 140 { 141 uint_t i = 0, j; 130 142 uint_t k; 131 fvec_t * newmag = (fvec_t *)p->newmag;132 aubio_spectralcandidate_t ** 133 (aubio_spectralcandidate_t **)(p->candidates);143 fvec_t *newmag = (fvec_t *) p->newmag; 144 aubio_spectralcandidate_t **scands = 145 (aubio_spectralcandidate_t **) (p->candidates); 134 146 //smpl_t hfc; //fe=instfreq(theta1,theta,ops); //theta1=theta; 135 147 /* copy incoming grain to newmag */ 136 for (j =0; j< newmag->length; j++)137 newmag->data[i][j] =fftgrain->norm[i][j];148 for (j = 0; j < newmag->length; j++) 149 newmag->data[i][j] = fftgrain->norm[i][j]; 138 150 /* detect only if local energy > 10. */ 139 if (fvec_local_energy (newmag)>10.) {151 if (fvec_local_energy (newmag) > 10.) { 140 152 /* hfc = fvec_local_hfc(newmag); do not use */ 141 aubio_pitchmcomb_spectral_pp (p, newmag);142 aubio_pitchmcomb_combdet (p,newmag);143 aubio_pitchmcomb_sort_cand_freq (scands,p->ncand);153 aubio_pitchmcomb_spectral_pp (p, newmag); 154 aubio_pitchmcomb_combdet (p, newmag); 155 aubio_pitchmcomb_sort_cand_freq (scands, p->ncand); 144 156 /* store ncand comb energies in cands[1:ncand] */ 145 for (k = 0; k <p->ncand; k++)157 for (k = 0; k < p->ncand; k++) 146 158 cands[k] = p->candidates[k]->ene; 147 159 /* store ncand[end] freq in cands[end] */ 148 cands[p->ncand] = p->candidates[p->ncand -1]->ebin;160 cands[p->ncand] = p->candidates[p->ncand - 1]->ebin; 149 161 return 1; 150 162 } else { 151 for (k = 0; k <p->ncand; k++)163 for (k = 0; k < p->ncand; k++) 152 164 cands[k] = 0; 153 165 return 0; … … 155 167 } 156 168 157 void aubio_pitchmcomb_spectral_pp(aubio_pitchmcomb_t * p, fvec_t * newmag) { 158 fvec_t * mag = (fvec_t *)p->scratch; 159 fvec_t * tmp = (fvec_t *)p->scratch2; 160 uint_t i=0,j; 169 void 170 aubio_pitchmcomb_spectral_pp (aubio_pitchmcomb_t * p, fvec_t * newmag) 171 { 172 fvec_t *mag = (fvec_t *) p->scratch; 173 fvec_t *tmp = (fvec_t *) p->scratch2; 174 uint_t i = 0, j; 161 175 uint_t length = mag->length; 162 176 /* copy newmag to mag (scracth) */ 163 for (j =0;j<length;j++) {177 for (j = 0; j < length; j++) { 164 178 mag->data[i][j] = newmag->data[i][j]; 165 179 } 166 fvec_min_removal (mag);/* min removal */167 fvec_alpha_normalise (mag,p->alpha); /* alpha normalisation */168 /* skipped */ 180 fvec_min_removal (mag); /* min removal */ 181 fvec_alpha_normalise (mag, p->alpha); /* alpha normalisation */ 182 /* skipped *//* low pass filtering */ 169 183 /** \bug fvec_moving_thres may write out of bounds */ 170 fvec_adapt_thres (mag,tmp,p->win_post,p->win_pre,i);/* adaptative threshold */171 fvec_add (mag,-p->threshold); /* fixed threshold */184 fvec_adapt_thres (mag, tmp, p->win_post, p->win_pre, i); /* adaptative threshold */ 185 fvec_add (mag, -p->threshold); /* fixed threshold */ 172 186 { 173 aubio_spectralpeak_t * peaks = (aubio_spectralpeak_t *)p->peaks;187 aubio_spectralpeak_t *peaks = (aubio_spectralpeak_t *) p->peaks; 174 188 uint_t count; 175 189 /* return bin and ebin */ 176 count = aubio_pitchmcomb_quadpick (peaks,mag);177 for (j =0;j<count;j++)190 count = aubio_pitchmcomb_quadpick (peaks, mag); 191 for (j = 0; j < count; j++) 178 192 peaks[j].mag = newmag->data[i][peaks[j].bin]; 179 193 /* reset non peaks */ 180 for (j =count;j<length;j++)194 for (j = count; j < length; j++) 181 195 peaks[j].mag = 0.; 182 196 p->peaks = peaks; … … 185 199 } 186 200 187 void aubio_pitchmcomb_combdet(aubio_pitchmcomb_t * p, fvec_t * newmag) { 188 aubio_spectralpeak_t * peaks = (aubio_spectralpeak_t *)p->peaks; 189 aubio_spectralcandidate_t ** candidate = 190 (aubio_spectralcandidate_t **)p->candidates; 201 void 202 aubio_pitchmcomb_combdet (aubio_pitchmcomb_t * p, fvec_t * newmag) 203 { 204 aubio_spectralpeak_t *peaks = (aubio_spectralpeak_t *) p->peaks; 205 aubio_spectralcandidate_t **candidate = 206 (aubio_spectralcandidate_t **) p->candidates; 191 207 192 208 /* parms */ 193 uint_t N = p->npartials; /* maximum number of partials to be considered 10 */194 uint_t M = p->ncand; /* maximum number of combs to be considered 5 */209 uint_t N = p->npartials; /* maximum number of partials to be considered 10 */ 210 uint_t M = p->ncand; /* maximum number of combs to be considered 5 */ 195 211 uint_t length = newmag->length; 196 212 uint_t count = p->count; … … 209 225 210 226 /* get the biggest peak in the spectrum */ 211 root_peak = aubio_pitchmcomb_get_root_peak (peaks,count);227 root_peak = aubio_pitchmcomb_get_root_peak (peaks, count); 212 228 /* not enough partials in highest notes, could be forced */ 213 229 //if (peaks[root_peak].ebin >= aubio_miditofreq(85.)/p->tau) N=2; 214 230 //if (peaks[root_peak].ebin >= aubio_miditofreq(90.)/p->tau) N=1; 215 231 /* now calculate the energy of each of the 5 combs */ 216 for (l =0;l<M;l++) {217 smpl_t scaler = (1. /(l+1.));218 candidate[l]->ene = 0.; /* reset ene and len sums */232 for (l = 0; l < M; l++) { 233 smpl_t scaler = (1. / (l + 1.)); 234 candidate[l]->ene = 0.; /* reset ene and len sums */ 219 235 candidate[l]->len = 0.; 220 candidate[l]->ebin =scaler*peaks[root_peak].ebin;236 candidate[l]->ebin = scaler * peaks[root_peak].ebin; 221 237 /* if less than N peaks available, curlen < N */ 222 238 if (candidate[l]->ebin != 0.) 223 curlen = (uint_t) FLOOR(length/(candidate[l]->ebin));224 curlen = (N < curlen )? N : curlen;239 curlen = (uint_t) FLOOR (length / (candidate[l]->ebin)); 240 curlen = (N < curlen) ? N : curlen; 225 241 /* fill candidate[l]->ecomb[k] with (k+1)*candidate[l]->ebin */ 226 for (k =0;k<curlen;k++)227 candidate[l]->ecomb[k] =(candidate[l]->ebin)*(k+1.);228 for (k =curlen;k<length;k++)229 candidate[l]->ecomb[k] =0.;242 for (k = 0; k < curlen; k++) 243 candidate[l]->ecomb[k] = (candidate[l]->ebin) * (k + 1.); 244 for (k = curlen; k < length; k++) 245 candidate[l]->ecomb[k] = 0.; 230 246 /* for each in candidate[l]->ecomb[k] */ 231 for (k =0;k<curlen;k++) {247 for (k = 0; k < curlen; k++) { 232 248 xx = 100000.; 233 249 /** get the candidate->ecomb the closer to peaks.ebin 234 250 * (to cope with the inharmonicity)*/ 235 for (d =0;d<count;d++) {236 delta2 = ABS (candidate[l]->ecomb[k]-peaks[d].ebin);251 for (d = 0; d < count; d++) { 252 delta2 = ABS (candidate[l]->ecomb[k] - peaks[d].ebin); 237 253 if (delta2 <= xx) { 238 254 position = d; … … 242 258 /* for a Q factor of 17, maintaining "constant Q filtering", 243 259 * and sum energy and length over non null combs */ 244 if ( 17. * xx < candidate[l]->ecomb[k] ) { 245 candidate[l]->ecomb[k]=peaks[position].ebin; 246 candidate[l]->ene += /* ecomb rounded to nearest int */ 247 POW(newmag->data[0][(uint_t)FLOOR(candidate[l]->ecomb[k]+.5)],0.25); 248 candidate[l]->len += 1./curlen; 260 if (17. * xx < candidate[l]->ecomb[k]) { 261 candidate[l]->ecomb[k] = peaks[position].ebin; 262 candidate[l]->ene += /* ecomb rounded to nearest int */ 263 POW (newmag->data[0][(uint_t) FLOOR (candidate[l]->ecomb[k] + .5)], 264 0.25); 265 candidate[l]->len += 1. / curlen; 249 266 } else 250 candidate[l]->ecomb[k] =0.;267 candidate[l]->ecomb[k] = 0.; 251 268 } 252 269 /* punishment */ 253 270 /*if (candidate[l]->len<0.6) 254 candidate[l]->ene=0.; */271 candidate[l]->ene=0.; */ 255 272 /* remember best candidate energy (in polyphonic, could check for 256 273 * tmpene*1.1 < candidate->ene to reduce jumps towards low frequencies) */ … … 270 287 * \bug peak-picking too picky, sometimes counts too many peaks ? 271 288 */ 272 uint_t aubio_pitchmcomb_quadpick(aubio_spectralpeak_t * spectral_peaks, fvec_t * X){ 289 uint_t 290 aubio_pitchmcomb_quadpick (aubio_spectralpeak_t * spectral_peaks, fvec_t * X) 291 { 273 292 uint_t i, j, ispeak, count = 0; 274 for (i =0;i<X->channels;i++)275 for (j =1;j<X->length-1;j++) {276 ispeak = fvec_peakpick (X,j);293 for (i = 0; i < X->channels; i++) 294 for (j = 1; j < X->length - 1; j++) { 295 ispeak = fvec_peakpick (X, j); 277 296 if (ispeak) { 278 297 count += ispeak; 279 spectral_peaks[count -1].bin = j;280 spectral_peaks[count -1].ebin = fvec_quadint(X, j, i) - 1.;298 spectral_peaks[count - 1].bin = j; 299 spectral_peaks[count - 1].ebin = fvec_quadint (X, j, i) - 1.; 281 300 } 282 301 } … … 285 304 286 305 /* get predominant partial */ 287 uint_t aubio_pitchmcomb_get_root_peak(aubio_spectralpeak_t * peaks, uint_t length) { 288 uint_t i,pos=0; 306 uint_t 307 aubio_pitchmcomb_get_root_peak (aubio_spectralpeak_t * peaks, uint_t length) 308 { 309 uint_t i, pos = 0; 289 310 smpl_t tmp = 0.; 290 for (i =0;i<length;i++)311 for (i = 0; i < length; i++) 291 312 if (tmp <= peaks[i].mag) { 292 313 pos = i; … … 296 317 } 297 318 298 void aubio_pitchmcomb_sort_peak(aubio_spectralpeak_t * peaks, uint_t nbins) { 299 qsort(peaks, nbins, sizeof(aubio_spectralpeak_t), 319 void 320 aubio_pitchmcomb_sort_peak (aubio_spectralpeak_t * peaks, uint_t nbins) 321 { 322 qsort (peaks, nbins, sizeof (aubio_spectralpeak_t), 300 323 aubio_pitchmcomb_sort_peak_comp); 301 324 } 302 static sint_t aubio_pitchmcomb_sort_peak_comp(const void *x, const void *y) { 303 return (((aubio_spectralpeak_t *)y)->mag - ((aubio_spectralpeak_t *)x)->mag); 304 } 305 306 307 void aubio_pitchmcomb_sort_cand_ene(aubio_spectralcandidate_t ** candidates, uint_t nbins) { 325 326 static sint_t 327 aubio_pitchmcomb_sort_peak_comp (const void *x, const void *y) 328 { 329 return (((aubio_spectralpeak_t *) y)->mag - 330 ((aubio_spectralpeak_t *) x)->mag); 331 } 332 333 334 void 335 aubio_pitchmcomb_sort_cand_ene (aubio_spectralcandidate_t ** candidates, 336 uint_t nbins) 337 { 308 338 uint_t cur = 0; 309 339 uint_t run = 0; 310 for (cur =0;cur<nbins;cur++) {340 for (cur = 0; cur < nbins; cur++) { 311 341 run = cur + 1; 312 for (run=cur;run<nbins;run++) { 313 if(candidates[run]->ene > candidates[cur]->ene) 314 CAND_SWAP(candidates[run], candidates[cur]); 315 } 316 } 317 } 318 319 320 void aubio_pitchmcomb_sort_cand_freq(aubio_spectralcandidate_t ** candidates, uint_t nbins) { 342 for (run = cur; run < nbins; run++) { 343 if (candidates[run]->ene > candidates[cur]->ene) 344 CAND_SWAP (candidates[run], candidates[cur]); 345 } 346 } 347 } 348 349 350 void 351 aubio_pitchmcomb_sort_cand_freq (aubio_spectralcandidate_t ** candidates, 352 uint_t nbins) 353 { 321 354 uint_t cur = 0; 322 355 uint_t run = 0; 323 for (cur =0;cur<nbins;cur++) {356 for (cur = 0; cur < nbins; cur++) { 324 357 run = cur + 1; 325 for (run=cur;run<nbins;run++) { 326 if(candidates[run]->ebin < candidates[cur]->ebin) 327 CAND_SWAP(candidates[run], candidates[cur]); 328 } 329 } 330 } 331 332 aubio_pitchmcomb_t * new_aubio_pitchmcomb(uint_t bufsize, uint_t hopsize, uint_t channels) { 333 aubio_pitchmcomb_t * p = AUBIO_NEW(aubio_pitchmcomb_t); 358 for (run = cur; run < nbins; run++) { 359 if (candidates[run]->ebin < candidates[cur]->ebin) 360 CAND_SWAP (candidates[run], candidates[cur]); 361 } 362 } 363 } 364 365 aubio_pitchmcomb_t * 366 new_aubio_pitchmcomb (uint_t bufsize, uint_t hopsize, uint_t channels) 367 { 368 aubio_pitchmcomb_t *p = AUBIO_NEW (aubio_pitchmcomb_t); 334 369 /* bug: should check if size / 8 > post+pre+1 */ 335 370 uint_t i, j; 336 371 uint_t spec_size; 337 p->spec_partition 338 p->ncand 339 p->npartials 340 p->cutoff 341 p->threshold 342 p->win_post 343 p->win_pre 372 p->spec_partition = 4; 373 p->ncand = 5; 374 p->npartials = 5; 375 p->cutoff = 1.; 376 p->threshold = 0.01; 377 p->win_post = 8; 378 p->win_pre = 7; 344 379 // p->tau = samplerate/bufsize; 345 p->alpha 346 p->goodcandidate 347 p->phasefreq = bufsize/hopsize/TWO_PI;348 p->phasediff = TWO_PI*hopsize/bufsize;349 spec_size = bufsize /p->spec_partition;380 p->alpha = 9.; 381 p->goodcandidate = 0; 382 p->phasefreq = bufsize / hopsize / TWO_PI; 383 p->phasediff = TWO_PI * hopsize / bufsize; 384 spec_size = bufsize / p->spec_partition; 350 385 //p->pickerfn = quadpick; 351 386 //p->biquad = new_biquad(0.1600,0.3200,0.1600, -0.5949, 0.2348); 352 387 /* allocate temp memory */ 353 p->newmag = new_fvec(spec_size,1);388 p->newmag = new_fvec (spec_size, 1); 354 389 /* array for median */ 355 p->scratch = new_fvec(spec_size,1);390 p->scratch = new_fvec (spec_size, 1); 356 391 /* array for phase */ 357 p->theta = new_fvec(spec_size,channels);392 p->theta = new_fvec (spec_size, channels); 358 393 /* array for adaptative threshold */ 359 p->scratch2 = new_fvec(p->win_post+p->win_pre+1,1);394 p->scratch2 = new_fvec (p->win_post + p->win_pre + 1, 1); 360 395 /* array of spectral peaks */ 361 p->peaks = AUBIO_ARRAY(aubio_spectralpeak_t,spec_size);396 p->peaks = AUBIO_ARRAY (aubio_spectralpeak_t, spec_size); 362 397 for (i = 0; i < spec_size; i++) { 363 398 p->peaks[i].bin = 0.; … … 366 401 } 367 402 /* array of pointers to spectral candidates */ 368 p->candidates = AUBIO_ARRAY (aubio_spectralcandidate_t *,p->ncand);369 for (i =0;i<p->ncand;i++) {370 p->candidates[i] = AUBIO_NEW (aubio_spectralcandidate_t);371 p->candidates[i]->ecomb = AUBIO_ARRAY (smpl_t, spec_size);372 for (j =0; j < spec_size; j++) {403 p->candidates = AUBIO_ARRAY (aubio_spectralcandidate_t *, p->ncand); 404 for (i = 0; i < p->ncand; i++) { 405 p->candidates[i] = AUBIO_NEW (aubio_spectralcandidate_t); 406 p->candidates[i]->ecomb = AUBIO_ARRAY (smpl_t, spec_size); 407 for (j = 0; j < spec_size; j++) { 373 408 p->candidates[i]->ecomb[j] = 0.; 374 409 } … … 381 416 382 417 383 void del_aubio_pitchmcomb (aubio_pitchmcomb_t *p) { 418 void 419 del_aubio_pitchmcomb (aubio_pitchmcomb_t * p) 420 { 384 421 uint_t i; 385 del_fvec (p->newmag);386 del_fvec (p->scratch);387 del_fvec (p->theta);388 del_fvec (p->scratch2);389 AUBIO_FREE (p->peaks);390 for (i =0;i<p->ncand;i++) {391 AUBIO_FREE (p->candidates[i]->ecomb);392 AUBIO_FREE (p->candidates[i]);393 } 394 AUBIO_FREE (p->candidates);395 AUBIO_FREE (p);396 } 422 del_fvec (p->newmag); 423 del_fvec (p->scratch); 424 del_fvec (p->theta); 425 del_fvec (p->scratch2); 426 AUBIO_FREE (p->peaks); 427 for (i = 0; i < p->ncand; i++) { 428 AUBIO_FREE (p->candidates[i]->ecomb); 429 AUBIO_FREE (p->candidates[i]); 430 } 431 AUBIO_FREE (p->candidates); 432 AUBIO_FREE (p); 433 } -
src/pitch/pitchmcomb.h
r63f3c70 rfddfa64 50 50 51 51 */ 52 void aubio_pitchmcomb_do (aubio_pitchmcomb_t * p, cvec_t * fftgrain, fvec_t * output); 52 void aubio_pitchmcomb_do (aubio_pitchmcomb_t * p, cvec_t * fftgrain, 53 fvec_t * output); 54 53 55 /** creation of the pitch detection object 54 56 … … 59 61 60 62 */ 61 aubio_pitchmcomb_t * new_aubio_pitchmcomb(uint_t bufsize, uint_t hopsize, uint_t channels); 63 aubio_pitchmcomb_t *new_aubio_pitchmcomb (uint_t bufsize, uint_t hopsize, 64 uint_t channels); 65 62 66 /** deletion of the pitch detection object 63 67 … … 65 69 66 70 */ 67 void del_aubio_pitchmcomb (aubio_pitchmcomb_t *p);71 void del_aubio_pitchmcomb (aubio_pitchmcomb_t * p); 68 72 69 73 #ifdef __cplusplus -
src/pitch/pitchyin.c
r63f3c70 rfddfa64 33 33 #include "pitch/pitchyin.h" 34 34 35 struct _aubio_pitchyin_t { 36 fvec_t * yin; 35 struct _aubio_pitchyin_t 36 { 37 fvec_t *yin; 37 38 smpl_t tol; 38 39 }; … … 44 45 45 46 */ 46 void aubio_pitchyin_diff (fvec_t * input, fvec_t * yinbuf);47 void aubio_pitchyin_diff (fvec_t * input, fvec_t * yinbuf); 47 48 48 49 /** in place computation of the YIN cumulative normalised function … … 51 52 52 53 */ 53 void aubio_pitchyin_getcum (fvec_t * yinbuf);54 void aubio_pitchyin_getcum (fvec_t * yinbuf); 54 55 55 56 /** detect pitch in a YIN function … … 58 59 59 60 */ 60 uint_t aubio_pitchyin_getpitch (fvec_t *yinbuf);61 uint_t aubio_pitchyin_getpitch (fvec_t * yinbuf); 61 62 62 aubio_pitchyin_t * new_aubio_pitchyin (uint_t bufsize) { 63 aubio_pitchyin_t * o = AUBIO_NEW(aubio_pitchyin_t); 64 o->yin = new_fvec (bufsize/2, 1); 63 aubio_pitchyin_t * 64 new_aubio_pitchyin (uint_t bufsize) 65 { 66 aubio_pitchyin_t *o = AUBIO_NEW (aubio_pitchyin_t); 67 o->yin = new_fvec (bufsize / 2, 1); 65 68 o->tol = 0.15; 66 69 return o; 67 70 } 68 71 69 void del_aubio_pitchyin (aubio_pitchyin_t *o) { 70 del_fvec(o->yin); 71 AUBIO_FREE(o); 72 void 73 del_aubio_pitchyin (aubio_pitchyin_t * o) 74 { 75 del_fvec (o->yin); 76 AUBIO_FREE (o); 72 77 } 73 78 74 79 /* outputs the difference function */ 75 void aubio_pitchyin_diff(fvec_t * input, fvec_t * yin){ 76 uint_t c,j,tau; 80 void 81 aubio_pitchyin_diff (fvec_t * input, fvec_t * yin) 82 { 83 uint_t c, j, tau; 77 84 smpl_t tmp; 78 for (c=0;c<input->channels;c++) 79 { 80 for (tau=0;tau<yin->length;tau++) 81 { 85 for (c = 0; c < input->channels; c++) { 86 for (tau = 0; tau < yin->length; tau++) { 82 87 yin->data[c][tau] = 0.; 83 88 } 84 for (tau=1;tau<yin->length;tau++) 85 { 86 for (j=0;j<yin->length;j++) 87 { 88 tmp = input->data[c][j] - input->data[c][j+tau]; 89 yin->data[c][tau] += SQR(tmp); 89 for (tau = 1; tau < yin->length; tau++) { 90 for (j = 0; j < yin->length; j++) { 91 tmp = input->data[c][j] - input->data[c][j + tau]; 92 yin->data[c][tau] += SQR (tmp); 90 93 } 91 94 } … … 94 97 95 98 /* cumulative mean normalized difference function */ 96 void aubio_pitchyin_getcum(fvec_t * yin) { 97 uint_t c,tau; 99 void 100 aubio_pitchyin_getcum (fvec_t * yin) 101 { 102 uint_t c, tau; 98 103 smpl_t tmp; 99 for (c=0;c<yin->channels;c++) 100 { 104 for (c = 0; c < yin->channels; c++) { 101 105 tmp = 0.; 102 106 yin->data[c][0] = 1.; 103 107 //AUBIO_DBG("%f\t",yin->data[c][0]); 104 for (tau=1;tau<yin->length;tau++) 105 { 108 for (tau = 1; tau < yin->length; tau++) { 106 109 tmp += yin->data[c][tau]; 107 yin->data[c][tau] *= tau /tmp;110 yin->data[c][tau] *= tau / tmp; 108 111 //AUBIO_DBG("%f\t",yin->data[c][tau]); 109 112 } … … 112 115 } 113 116 114 uint_t aubio_pitchyin_getpitch(fvec_t * yin) { 115 uint_t c=0,tau=1; 116 do 117 { 118 if(yin->data[c][tau] < 0.1) { 119 while (yin->data[c][tau+1] < yin->data[c][tau]) { 117 uint_t 118 aubio_pitchyin_getpitch (fvec_t * yin) 119 { 120 uint_t c = 0, tau = 1; 121 do { 122 if (yin->data[c][tau] < 0.1) { 123 while (yin->data[c][tau + 1] < yin->data[c][tau]) { 120 124 tau++; 121 125 } … … 123 127 } 124 128 tau++; 125 } while (tau <yin->length);129 } while (tau < yin->length); 126 130 //AUBIO_DBG("No pitch found"); 127 131 return 0; … … 130 134 131 135 /* all the above in one */ 132 void aubio_pitchyin_do(aubio_pitchyin_t *o, fvec_t * input, fvec_t * out){ 136 void 137 aubio_pitchyin_do (aubio_pitchyin_t * o, fvec_t * input, fvec_t * out) 138 { 133 139 smpl_t tol = o->tol; 134 fvec_t * 135 uint_t c , j,tau = 0;140 fvec_t *yin = o->yin; 141 uint_t c, j, tau = 0; 136 142 sint_t period; 137 143 smpl_t tmp = 0., tmp2 = 0.; 138 144 for (c = 0; c < input->channels; c++) { 139 145 yin->data[c][0] = 1.; 140 for (tau=1;tau<yin->length;tau++) 141 { 146 for (tau = 1; tau < yin->length; tau++) { 142 147 yin->data[c][tau] = 0.; 143 for (j=0;j<yin->length;j++) 144 { 145 tmp = input->data[c][j] - input->data[c][j+tau]; 146 yin->data[c][tau] += SQR(tmp); 148 for (j = 0; j < yin->length; j++) { 149 tmp = input->data[c][j] - input->data[c][j + tau]; 150 yin->data[c][tau] += SQR (tmp); 147 151 } 148 152 tmp2 += yin->data[c][tau]; 149 yin->data[c][tau] *= tau /tmp2;150 period = tau -3;151 if (tau > 4 && (yin->data[c][period] < tol) &&152 (yin->data[c][period] < yin->data[c][period +1])) {153 out->data[c][0] = fvec_quadint (yin,period,c);153 yin->data[c][tau] *= tau / tmp2; 154 period = tau - 3; 155 if (tau > 4 && (yin->data[c][period] < tol) && 156 (yin->data[c][period] < yin->data[c][period + 1])) { 157 out->data[c][0] = fvec_quadint (yin, period, c); 154 158 goto beach; 155 159 } 156 160 } 157 out->data[c][0] = fvec_quadint (yin,fvec_min_elem(yin),c);158 beach:161 out->data[c][0] = fvec_quadint (yin, fvec_min_elem (yin), c); 162 beach: 159 163 continue; 160 164 } … … 162 166 } 163 167 164 uint_t aubio_pitchyin_set_tolerance (aubio_pitchyin_t *o, smpl_t tol) { 168 uint_t 169 aubio_pitchyin_set_tolerance (aubio_pitchyin_t * o, smpl_t tol) 170 { 165 171 o->tol = tol; 166 172 return 0; 167 173 } 168 174 169 smpl_t aubio_pitchyin_get_tolerance (aubio_pitchyin_t *o) { 175 smpl_t 176 aubio_pitchyin_get_tolerance (aubio_pitchyin_t * o) 177 { 170 178 return o->tol; 171 179 } -
src/pitch/pitchyin.h
r63f3c70 rfddfa64 48 48 49 49 */ 50 aubio_pitchyin_t * 50 aubio_pitchyin_t *new_aubio_pitchyin (uint_t bufsize); 51 51 52 52 /** deletion of the pitch detection object … … 73 73 74 74 */ 75 uint_t aubio_pitchyin_set_tolerance (aubio_pitchyin_t * o, smpl_t tol);75 uint_t aubio_pitchyin_set_tolerance (aubio_pitchyin_t * o, smpl_t tol); 76 76 77 77 /** get tolerance parameter for YIN algorithm -
src/pitch/pitchyinfft.c
r63f3c70 rfddfa64 27 27 28 28 /** pitch yinfft structure */ 29 struct _aubio_pitchyinfft_t { 30 fvec_t * win; /**< temporal weighting window */ 31 fvec_t * winput; /**< windowed spectrum */ 32 cvec_t * res; /**< complex vector to compute square difference function */ 33 fvec_t * sqrmag; /**< square difference function */ 34 fvec_t * weight; /**< spectral weighting window (psychoacoustic model) */ 35 cvec_t * fftout; /**< Fourier transform output */ 36 aubio_fft_t * fft; /**< fft object to compute square difference function */ 37 fvec_t * yinfft; /**< Yin function */ 29 struct _aubio_pitchyinfft_t 30 { 31 fvec_t *win; /**< temporal weighting window */ 32 fvec_t *winput; /**< windowed spectrum */ 33 cvec_t *res; /**< complex vector to compute square difference function */ 34 fvec_t *sqrmag; /**< square difference function */ 35 fvec_t *weight; /**< spectral weighting window (psychoacoustic model) */ 36 cvec_t *fftout; /**< Fourier transform output */ 37 aubio_fft_t *fft; /**< fft object to compute square difference function */ 38 fvec_t *yinfft; /**< Yin function */ 38 39 smpl_t tol; /**< Yin tolerance */ 39 40 }; 40 41 41 static const smpl_t freqs[] = { 0., 20., 25., 31.5, 40., 50., 63., 80., 100.,42 static const smpl_t freqs[] = { 0., 20., 25., 31.5, 40., 50., 63., 80., 100., 42 43 125., 160., 200., 250., 315., 400., 500., 630., 800., 1000., 1250., 43 44 1600., 2000., 2500., 3150., 4000., 5000., 6300., 8000., 9000., 10000., 44 12500., 15000., 20000., 25100}; 45 12500., 15000., 20000., 25100 46 }; 45 47 46 static const smpl_t weight[] = { -75.8, -70.1, -60.8, -52.1, -44.2, -37.5,48 static const smpl_t weight[] = { -75.8, -70.1, -60.8, -52.1, -44.2, -37.5, 47 49 -31.3, -25.6, -20.9, -16.5, -12.6, -9.6, -7.0, -4.7, -3.0, -1.8, -0.8, 48 50 -0.2, -0.0, 0.5, 1.6, 3.2, 5.4, 7.8, 8.1, 5.3, -2.4, -11.1, -12.8, 49 -12.2, -7.4, -17.8, -17.8, -17.8}; 51 -12.2, -7.4, -17.8, -17.8, -17.8 52 }; 50 53 51 aubio_pitchyinfft_t * new_aubio_pitchyinfft (uint_t bufsize) 54 aubio_pitchyinfft_t * 55 new_aubio_pitchyinfft (uint_t bufsize) 52 56 { 53 aubio_pitchyinfft_t * p = AUBIO_NEW(aubio_pitchyinfft_t);54 p->winput = new_fvec (bufsize,1);55 p->fft = new_aubio_fft(bufsize, 1);56 p->fftout = new_cvec (bufsize,1);57 p->sqrmag = new_fvec (bufsize,1);58 p->res = new_cvec(bufsize,1);59 p->yinfft = new_fvec (bufsize/2+1,1);60 p->tol 61 p->win = new_aubio_window("hanningz", bufsize);62 p->weight = new_fvec(bufsize/2+1,1);57 aubio_pitchyinfft_t *p = AUBIO_NEW (aubio_pitchyinfft_t); 58 p->winput = new_fvec (bufsize, 1); 59 p->fft = new_aubio_fft (bufsize, 1); 60 p->fftout = new_cvec (bufsize, 1); 61 p->sqrmag = new_fvec (bufsize, 1); 62 p->res = new_cvec (bufsize, 1); 63 p->yinfft = new_fvec (bufsize / 2 + 1, 1); 64 p->tol = 0.85; 65 p->win = new_aubio_window ("hanningz", bufsize); 66 p->weight = new_fvec (bufsize / 2 + 1, 1); 63 67 { 64 68 uint_t i = 0, j = 1; 65 69 smpl_t freq = 0, a0 = 0, a1 = 0, f0 = 0, f1 = 0; 66 for (i =0; i<p->weight->length; i++) {67 freq = (smpl_t) i/(smpl_t)bufsize*(smpl_t)44100.;70 for (i = 0; i < p->weight->length; i++) { 71 freq = (smpl_t) i / (smpl_t) bufsize *(smpl_t) 44100.; 68 72 while (freq > freqs[j]) { 69 j += 1;70 71 a0 = weight[j -1];72 f0 = freqs[j -1];73 73 j += 1; 74 } 75 a0 = weight[j - 1]; 76 f0 = freqs[j - 1]; 77 a1 = weight[j]; 74 78 f1 = freqs[j]; 75 if (f0 == f1) { // just in case79 if (f0 == f1) { // just in case 76 80 p->weight->data[0][i] = a0; 77 } else if (f0 == 0) { // y = ax+b78 p->weight->data[0][i] = (a1 -a0)/f1*freq + a0;81 } else if (f0 == 0) { // y = ax+b 82 p->weight->data[0][i] = (a1 - a0) / f1 * freq + a0; 79 83 } else { 80 p->weight->data[0][i] = (a1 -a0)/(f1-f0)*freq +81 (a0 - (a1 - a0)/(f1/f0 - 1.));84 p->weight->data[0][i] = (a1 - a0) / (f1 - f0) * freq + 85 (a0 - (a1 - a0) / (f1 / f0 - 1.)); 82 86 } 83 87 while (freq > freqs[j]) { 84 j += 1;88 j += 1; 85 89 } 86 90 //AUBIO_DBG("%f\n",p->weight->data[0][i]); 87 p->weight->data[0][i] = DB2LIN (p->weight->data[0][i]);91 p->weight->data[0][i] = DB2LIN (p->weight->data[0][i]); 88 92 //p->weight->data[0][i] = SQRT(DB2LIN(p->weight->data[0][i])); 89 93 } … … 92 96 } 93 97 94 void aubio_pitchyinfft_do (aubio_pitchyinfft_t * p, fvec_t * input, fvec_t * output) { 98 void 99 aubio_pitchyinfft_do (aubio_pitchyinfft_t * p, fvec_t * input, fvec_t * output) 100 { 95 101 uint_t i, tau, l; 96 102 uint_t halfperiod; 97 103 smpl_t tmp, sum; 98 cvec_t * res = (cvec_t *)p->res; 99 fvec_t * yin = (fvec_t *)p->yinfft; 100 for (i=0; i < input->channels; i++){ 101 l = 0; tmp = 0.; sum = 0.; 102 for (l=0; l < input->length; l++){ 103 p->winput->data[0][l] = p->win->data[0][l] * input->data[i][l]; 104 } 105 aubio_fft_do(p->fft,p->winput,p->fftout); 106 for (l=0; l < p->fftout->length; l++){ 107 p->sqrmag->data[0][l] = SQR(p->fftout->norm[0][l]); 108 p->sqrmag->data[0][l] *= p->weight->data[0][l]; 109 } 110 for (l=1; l < p->fftout->length; l++){ 111 p->sqrmag->data[0][(p->fftout->length-1)*2-l] = 112 SQR(p->fftout->norm[0][l]); 113 p->sqrmag->data[0][(p->fftout->length-1)*2-l] *= 114 p->weight->data[0][l]; 115 } 116 for (l=0; l < p->sqrmag->length/2+1; l++) { 117 sum += p->sqrmag->data[0][l]; 118 } 119 sum *= 2.; 120 aubio_fft_do(p->fft,p->sqrmag,res); 121 yin->data[0][0] = 1.; 122 for (tau=1; tau < yin->length; tau++) { 123 yin->data[0][tau] = sum - 124 res->norm[0][tau]*COS(res->phas[0][tau]); 125 tmp += yin->data[0][tau]; 126 yin->data[0][tau] *= tau/tmp; 127 } 128 tau = fvec_min_elem(yin); 129 if (yin->data[0][tau] < p->tol) { 130 /* no interpolation */ 131 //return tau; 132 /* 3 point quadratic interpolation */ 133 //return fvec_quadint_min(yin,tau,1); 134 /* additional check for (unlikely) octave doubling in higher frequencies */ 135 if (tau>35) { 136 output->data[i][0] = fvec_quadint(yin,tau,i); 104 cvec_t *res = (cvec_t *) p->res; 105 fvec_t *yin = (fvec_t *) p->yinfft; 106 for (i = 0; i < input->channels; i++) { 107 l = 0; 108 tmp = 0.; 109 sum = 0.; 110 for (l = 0; l < input->length; l++) { 111 p->winput->data[0][l] = p->win->data[0][l] * input->data[i][l]; 112 } 113 aubio_fft_do (p->fft, p->winput, p->fftout); 114 for (l = 0; l < p->fftout->length; l++) { 115 p->sqrmag->data[0][l] = SQR (p->fftout->norm[0][l]); 116 p->sqrmag->data[0][l] *= p->weight->data[0][l]; 117 } 118 for (l = 1; l < p->fftout->length; l++) { 119 p->sqrmag->data[0][(p->fftout->length - 1) * 2 - l] = 120 SQR (p->fftout->norm[0][l]); 121 p->sqrmag->data[0][(p->fftout->length - 1) * 2 - l] *= 122 p->weight->data[0][l]; 123 } 124 for (l = 0; l < p->sqrmag->length / 2 + 1; l++) { 125 sum += p->sqrmag->data[0][l]; 126 } 127 sum *= 2.; 128 aubio_fft_do (p->fft, p->sqrmag, res); 129 yin->data[0][0] = 1.; 130 for (tau = 1; tau < yin->length; tau++) { 131 yin->data[0][tau] = sum - res->norm[0][tau] * COS (res->phas[0][tau]); 132 tmp += yin->data[0][tau]; 133 yin->data[0][tau] *= tau / tmp; 134 } 135 tau = fvec_min_elem (yin); 136 if (yin->data[0][tau] < p->tol) { 137 /* no interpolation */ 138 //return tau; 139 /* 3 point quadratic interpolation */ 140 //return fvec_quadint_min(yin,tau,1); 141 /* additional check for (unlikely) octave doubling in higher frequencies */ 142 if (tau > 35) { 143 output->data[i][0] = fvec_quadint (yin, tau, i); 144 } else { 145 /* should compare the minimum value of each interpolated peaks */ 146 halfperiod = FLOOR (tau / 2 + .5); 147 if (yin->data[0][halfperiod] < p->tol) 148 output->data[i][0] = fvec_quadint (yin, halfperiod, i); 149 else 150 output->data[i][0] = fvec_quadint (yin, tau, i); 151 } 137 152 } else { 138 /* should compare the minimum value of each interpolated peaks */ 139 halfperiod = FLOOR(tau/2+.5); 140 if (yin->data[0][halfperiod] < p->tol) 141 output->data[i][0] = fvec_quadint(yin,halfperiod,i); 142 else 143 output->data[i][0] = fvec_quadint(yin,tau,i); 153 output->data[i][0] = 0.; 144 154 } 145 } else {146 output->data[i][0] = 0.;147 }148 155 } 149 156 } 150 157 151 void del_aubio_pitchyinfft(aubio_pitchyinfft_t *p){ 152 del_fvec(p->win); 153 del_aubio_fft(p->fft); 154 del_fvec(p->yinfft); 155 del_fvec(p->sqrmag); 156 del_cvec(p->res); 157 del_cvec(p->fftout); 158 del_fvec(p->winput); 159 del_fvec(p->weight); 160 AUBIO_FREE(p); 158 void 159 del_aubio_pitchyinfft (aubio_pitchyinfft_t * p) 160 { 161 del_fvec (p->win); 162 del_aubio_fft (p->fft); 163 del_fvec (p->yinfft); 164 del_fvec (p->sqrmag); 165 del_cvec (p->res); 166 del_cvec (p->fftout); 167 del_fvec (p->winput); 168 del_fvec (p->weight); 169 AUBIO_FREE (p); 161 170 } 162 171 163 uint_t aubio_pitchyinfft_set_tolerance (aubio_pitchyinfft_t * p, smpl_t tol) { 172 uint_t 173 aubio_pitchyinfft_set_tolerance (aubio_pitchyinfft_t * p, smpl_t tol) 174 { 164 175 p->tol = tol; 165 176 return 0; 166 177 } 167 178 168 smpl_t aubio_pitchyinfft_get_tolerance (aubio_pitchyinfft_t * p) { 179 smpl_t 180 aubio_pitchyinfft_get_tolerance (aubio_pitchyinfft_t * p) 181 { 169 182 return p->tol; 170 183 } -
src/pitch/pitchyinfft.h
r63f3c70 rfddfa64 51 51 52 52 */ 53 void aubio_pitchyinfft_do (aubio_pitchyinfft_t * p, fvec_t * in, fvec_t * out);53 void aubio_pitchyinfft_do (aubio_pitchyinfft_t * p, fvec_t * in, fvec_t * out); 54 54 /** creation of the pitch detection object 55 55 … … 57 57 58 58 */ 59 aubio_pitchyinfft_t * 59 aubio_pitchyinfft_t *new_aubio_pitchyinfft (uint_t bufsize); 60 60 /** deletion of the pitch detection object 61 61 … … 63 63 64 64 */ 65 void del_aubio_pitchyinfft (aubio_pitchyinfft_t * p);65 void del_aubio_pitchyinfft (aubio_pitchyinfft_t * p); 66 66 67 67 /** get tolerance parameter for YIN algorithm
Note: See TracChangeset
for help on using the changeset viewer.