- Timestamp:
- Dec 4, 2009, 1:44:41 AM (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:
- 02a01dd
- Parents:
- fc61225
- Location:
- src/pitch
- Files:
-
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
src/pitch/pitch.c
rfc61225 r168337e 114 114 aubio_pitch_t * 115 115 new_aubio_pitch (char_t * pitch_mode, 116 uint_t bufsize, uint_t hopsize, uint_t channels, uint_tsamplerate)116 uint_t bufsize, uint_t hopsize, uint_t samplerate) 117 117 { 118 118 aubio_pitch_t *p = AUBIO_NEW (aubio_pitch_t); … … 142 142 switch (p->type) { 143 143 case aubio_pitcht_yin: 144 p->buf = new_fvec (bufsize , channels);144 p->buf = new_fvec (bufsize); 145 145 p->yin = new_aubio_pitchyin (bufsize); 146 146 p->callback = aubio_pitch_do_yin; … … 148 148 break; 149 149 case aubio_pitcht_mcomb: 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);150 p->pv = new_aubio_pvoc (bufsize, hopsize); 151 p->fftgrain = new_cvec (bufsize); 152 p->mcomb = new_aubio_pitchmcomb (bufsize, hopsize); 153 p->filter = new_aubio_filter_c_weighting (samplerate); 154 154 p->callback = aubio_pitch_do_mcomb; 155 155 break; 156 156 case aubio_pitcht_fcomb: 157 p->buf = new_fvec (bufsize , channels);158 p->fcomb = new_aubio_pitchfcomb (bufsize, hopsize , channels);157 p->buf = new_fvec (bufsize); 158 p->fcomb = new_aubio_pitchfcomb (bufsize, hopsize); 159 159 p->callback = aubio_pitch_do_fcomb; 160 160 break; 161 161 case aubio_pitcht_schmitt: 162 p->buf = new_fvec (bufsize , channels);162 p->buf = new_fvec (bufsize); 163 163 p->schmitt = new_aubio_pitchschmitt (bufsize); 164 164 p->callback = aubio_pitch_do_schmitt; 165 165 break; 166 166 case aubio_pitcht_yinfft: 167 p->buf = new_fvec (bufsize , channels);167 p->buf = new_fvec (bufsize); 168 168 p->yinfft = new_aubio_pitchyinfft (bufsize); 169 169 p->callback = aubio_pitch_do_yinfft; … … 211 211 aubio_pitch_slideblock (aubio_pitch_t * p, fvec_t * ibuf) 212 212 { 213 uint_t i,j = 0, overlap_size = 0;213 uint_t j = 0, overlap_size = 0; 214 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 } 215 for (j = 0; j < overlap_size; j++) { 216 p->buf->data[j] = p->buf->data[j + ibuf->length]; 217 } 218 for (j = 0; j < ibuf->length; j++) { 219 p->buf->data[j + overlap_size] = ibuf->data[j]; 224 220 } 225 221 } … … 283 279 aubio_pitch_do (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf) 284 280 { 285 uint_t i;286 281 p->callback (p, ibuf, obuf); 287 for (i = 0; i < obuf->channels; i++) { 288 p->freqconv (obuf->data[i][0], p->srate, p->bufsize); 289 } 282 obuf->data[0] = p->freqconv (obuf->data[0], p->srate, p->bufsize); 290 283 } 291 284 … … 293 286 aubio_pitch_do_mcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * obuf) 294 287 { 295 uint_t i;296 288 aubio_filter_do (p->filter, ibuf); 297 289 aubio_pvoc_do (p->pv, ibuf, p->fftgrain); 298 290 aubio_pitchmcomb_do (p->mcomb, p->fftgrain, obuf); 299 for (i = 0; i < obuf->channels; i++) { 300 obuf->data[i][0] = aubio_bintofreq (obuf->data[i][0], p->srate, p->bufsize); 301 } 291 obuf->data[0] = aubio_bintofreq (obuf->data[0], p->srate, p->bufsize); 302 292 } 303 293 … … 306 296 { 307 297 smpl_t pitch = 0.; 308 uint_t i;309 298 aubio_pitch_slideblock (p, ibuf); 310 299 aubio_pitchyin_do (p->yin, p->buf, obuf); 311 for (i = 0; i < obuf->channels; i++) { 312 pitch = obuf->data[i][0]; 313 if (pitch > 0) { 314 pitch = p->srate / (pitch + 0.); 315 } else { 316 pitch = 0.; 317 } 318 obuf->data[i][0] = pitch; 319 } 300 pitch = obuf->data[0]; 301 if (pitch > 0) { 302 pitch = p->srate / (pitch + 0.); 303 } else { 304 pitch = 0.; 305 } 306 obuf->data[0] = pitch; 320 307 } 321 308 … … 325 312 { 326 313 smpl_t pitch = 0.; 327 uint_t i;328 314 aubio_pitch_slideblock (p, ibuf); 329 315 aubio_pitchyinfft_do (p->yinfft, p->buf, obuf); 330 for (i = 0; i < obuf->channels; i++) { 331 pitch = obuf->data[i][0]; 332 if (pitch > 0) { 333 pitch = p->srate / (pitch + 0.); 334 } else { 335 pitch = 0.; 336 } 337 obuf->data[i][0] = pitch; 338 } 316 pitch = obuf->data[0]; 317 if (pitch > 0) { 318 pitch = p->srate / (pitch + 0.); 319 } else { 320 pitch = 0.; 321 } 322 obuf->data[0] = pitch; 339 323 } 340 324 … … 342 326 aubio_pitch_do_fcomb (aubio_pitch_t * p, fvec_t * ibuf, fvec_t * out) 343 327 { 344 uint_t i;345 328 aubio_pitch_slideblock (p, ibuf); 346 329 aubio_pitchfcomb_do (p->fcomb, p->buf, out); 347 for (i = 0; i < out->channels; i++) { 348 out->data[i][0] = aubio_bintofreq (out->data[i][0], p->srate, p->bufsize); 349 } 330 out->data[0] = aubio_bintofreq (out->data[0], p->srate, p->bufsize); 350 331 } 351 332 … … 354 335 { 355 336 smpl_t period, pitch = 0.; 356 uint_t i;357 337 aubio_pitch_slideblock (p, ibuf); 358 338 aubio_pitchschmitt_do (p->schmitt, p->buf, out); 359 for (i = 0; i < out->channels; i++) { 360 period = out->data[i][0]; 361 if (period > 0) { 362 pitch = p->srate / period; 363 } else { 364 pitch = 0.; 365 } 366 out->data[i][0] = pitch; 367 } 368 } 339 period = out->data[0]; 340 if (period > 0) { 341 pitch = p->srate / period; 342 } else { 343 pitch = 0.; 344 } 345 out->data[0] = pitch; 346 } -
src/pitch/pitch.h
rfc61225 r168337e 41 41 42 42 \param o pitch detection object as returned by new_aubio_pitch() 43 \param in input signal of size [hop_size x channels]44 \param out output pitch candidates of size [1 x channels]43 \param in input signal of size [hop_size] 44 \param out output pitch candidates of size [1] 45 45 46 46 */ … … 67 67 \param buf_size size of the input buffer to analyse 68 68 \param hop_size step size between two consecutive analysis instant 69 \param channels number of channels to analyse70 69 \param samplerate sampling rate of the signal 71 70 72 71 */ 73 72 aubio_pitch_t *new_aubio_pitch (char_t * method, 74 uint_t buf_size, uint_t hop_size, uint_t channels, uint_tsamplerate);73 uint_t buf_size, uint_t hop_size, uint_t samplerate); 75 74 76 75 /** set the output unit of the pitch detection object -
src/pitch/pitchfcomb.c
rfc61225 r168337e 49 49 50 50 aubio_pitchfcomb_t * 51 new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize , uint_t channels)51 new_aubio_pitchfcomb (uint_t bufsize, uint_t hopsize) 52 52 { 53 53 aubio_pitchfcomb_t *p = AUBIO_NEW (aubio_pitchfcomb_t); 54 54 p->fftSize = bufsize; 55 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);56 p->winput = new_fvec (bufsize); 57 p->fftOut = new_cvec (bufsize); 58 p->fftLastPhase = new_fvec (bufsize); 59 p->fft = new_aubio_fft (bufsize); 60 60 p->win = new_aubio_window ("hanning", bufsize); 61 61 return p; … … 66 66 aubio_pitchfcomb_do (aubio_pitchfcomb_t * p, fvec_t * input, fvec_t * output) 67 67 { 68 uint_t i,k, l, maxharm = 0;68 uint_t k, l, maxharm = 0; 69 69 smpl_t phaseDifference = TWO_PI * (smpl_t) p->stepSize / (smpl_t) p->fftSize; 70 70 aubio_fpeak_t peaks[MAX_PEAKS]; 71 71 72 for (i = 0; i < input->channels; i++) { 72 for (k = 0; k < MAX_PEAKS; k++) { 73 peaks[k].db = -200.; 74 peaks[k].bin = 0.; 75 } 73 76 74 for (k = 0; k < MAX_PEAKS; k++) { 75 peaks[k].db = -200.; 76 peaks[k].bin = 0.; 77 for (k = 0; k < input->length; k++) { 78 p->winput->data[k] = p->win->data[k] * input->data[k]; 79 } 80 aubio_fft_do (p->fft, p->winput, p->fftOut); 81 82 for (k = 0; k <= p->fftSize / 2; k++) { 83 smpl_t 84 magnitude = 85 20. * LOG10 (2. * p->fftOut->norm[k] / (smpl_t) p->fftSize), 86 phase = p->fftOut->phas[k], tmp, bin; 87 88 /* compute phase difference */ 89 tmp = phase - p->fftLastPhase->data[k]; 90 p->fftLastPhase->data[k] = phase; 91 92 /* subtract expected phase difference */ 93 tmp -= (smpl_t) k *phaseDifference; 94 95 /* map delta phase into +/- Pi interval */ 96 tmp = aubio_unwrap2pi (tmp); 97 98 /* get deviation from bin frequency from the +/- Pi interval */ 99 tmp = p->fftSize / (smpl_t) p->stepSize * tmp / (TWO_PI); 100 101 /* compute the k-th partials' true bin */ 102 bin = (smpl_t) k + tmp; 103 104 if (bin > 0.0 && magnitude > peaks[0].db) { // && magnitude < 0) { 105 memmove (peaks + 1, peaks, sizeof (aubio_fpeak_t) * (MAX_PEAKS - 1)); 106 peaks[0].bin = bin; 107 peaks[0].db = magnitude; 77 108 } 109 } 78 110 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); 83 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; 89 90 /* compute phase difference */ 91 tmp = phase - p->fftLastPhase->data[i][k]; 92 p->fftLastPhase->data[i][k] = phase; 93 94 /* subtract expected phase difference */ 95 tmp -= (smpl_t) k *phaseDifference; 96 97 /* map delta phase into +/- Pi interval */ 98 tmp = aubio_unwrap2pi (tmp); 99 100 /* get deviation from bin frequency from the +/- Pi interval */ 101 tmp = p->fftSize / (smpl_t) p->stepSize * tmp / (TWO_PI); 102 103 /* compute the k-th partials' true bin */ 104 bin = (smpl_t) k + tmp; 105 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 } 111 } 112 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 } 111 k = 0; 112 for (l = 1; l < MAX_PEAKS && peaks[l].bin > 0.0; l++) { 113 sint_t harmonic; 114 for (harmonic = 5; harmonic > 1; harmonic--) { 115 if (peaks[0].bin / peaks[l].bin < harmonic + .02 && 116 peaks[0].bin / peaks[l].bin > harmonic - .02) { 117 if (harmonic > (sint_t) maxharm && peaks[0].db < peaks[l].db / 2) { 118 maxharm = harmonic; 119 k = l; 123 120 } 124 121 } 125 122 } 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.;130 123 } 124 output->data[0] = peaks[k].bin; 125 /* quick hack to clean output a bit */ 126 if (peaks[k].bin > 5000.) 127 output->data[0] = 0.; 131 128 } 132 129 -
src/pitch/pitchfcomb.h
rfc61225 r168337e 57 57 \param buf_size size of the input buffer to analyse 58 58 \param hop_size step size between two consecutive analysis instant 59 \param channels number of channels to detect pitch on60 59 61 60 */ 62 aubio_pitchfcomb_t *new_aubio_pitchfcomb (uint_t buf_size, uint_t hop_size, 63 uint_t channels); 61 aubio_pitchfcomb_t *new_aubio_pitchfcomb (uint_t buf_size, uint_t hop_size); 64 62 65 63 /** deletion of the pitch detection object -
src/pitch/pitchmcomb.c
rfc61225 r168337e 104 104 aubio_pitchmcomb_do (aubio_pitchmcomb_t * p, cvec_t * fftgrain, fvec_t * output) 105 105 { 106 uint_t i,j;106 uint_t j; 107 107 smpl_t instfreq; 108 108 fvec_t *newmag = (fvec_t *) p->newmag; 109 109 //smpl_t hfc; //fe=instfreq(theta1,theta,ops); //theta1=theta; 110 110 /* copy incoming grain to newmag */ 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.) { 116 //hfc = fvec_local_hfc(newmag); //not used 117 aubio_pitchmcomb_spectral_pp (p, newmag); 118 aubio_pitchmcomb_combdet (p, newmag); 119 //aubio_pitchmcomb_sort_cand_freq(p->candidates,p->ncand); 120 //return p->candidates[p->goodcandidate]->ebin; 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 } 111 for (j = 0; j < newmag->length; j++) 112 newmag->data[j] = fftgrain->norm[j]; 113 /* detect only if local energy > 10. */ 114 //if (fvec_local_energy(newmag)>10.) { 115 //hfc = fvec_local_hfc(newmag); //not used 116 aubio_pitchmcomb_spectral_pp (p, newmag); 117 aubio_pitchmcomb_combdet (p, newmag); 118 //aubio_pitchmcomb_sort_cand_freq(p->candidates,p->ncand); 119 //return p->candidates[p->goodcandidate]->ebin; 120 j = (uint_t) FLOOR (p->candidates[p->goodcandidate]->ebin + .5); 121 instfreq = aubio_unwrap2pi (fftgrain->phas[j] 122 - p->theta->data[j] - j * p->phasediff); 123 instfreq *= p->phasefreq; 124 /* store phase for next run */ 125 for (j = 0; j < p->theta->length; j++) { 126 p->theta->data[j] = fftgrain->phas[j]; 127 } 128 //return p->candidates[p->goodcandidate]->ebin; 129 output->data[0] = 130 FLOOR (p->candidates[p->goodcandidate]->ebin + .5) + instfreq; 131 /*} else { 132 return -1.; 133 } */ 136 134 } 137 135 … … 139 137 aubio_pitch_cands (aubio_pitchmcomb_t * p, cvec_t * fftgrain, smpl_t * cands) 140 138 { 141 uint_t i = 0,j;139 uint_t j; 142 140 uint_t k; 143 141 fvec_t *newmag = (fvec_t *) p->newmag; … … 147 145 /* copy incoming grain to newmag */ 148 146 for (j = 0; j < newmag->length; j++) 149 newmag->data[ i][j] = fftgrain->norm[i][j];147 newmag->data[j] = fftgrain->norm[j]; 150 148 /* detect only if local energy > 10. */ 151 149 if (fvec_local_energy (newmag) > 10.) { … … 172 170 fvec_t *mag = (fvec_t *) p->scratch; 173 171 fvec_t *tmp = (fvec_t *) p->scratch2; 174 uint_t i = 0,j;172 uint_t j; 175 173 uint_t length = mag->length; 176 174 /* copy newmag to mag (scracth) */ 177 175 for (j = 0; j < length; j++) { 178 mag->data[ i][j] = newmag->data[i][j];176 mag->data[j] = newmag->data[j]; 179 177 } 180 178 fvec_min_removal (mag); /* min removal */ … … 182 180 /* skipped *//* low pass filtering */ 183 181 /** \bug fvec_moving_thres may write out of bounds */ 184 fvec_adapt_thres (mag, tmp, p->win_post, p->win_pre , i); /* adaptative threshold */182 fvec_adapt_thres (mag, tmp, p->win_post, p->win_pre); /* adaptative threshold */ 185 183 fvec_add (mag, -p->threshold); /* fixed threshold */ 186 184 { … … 190 188 count = aubio_pitchmcomb_quadpick (peaks, mag); 191 189 for (j = 0; j < count; j++) 192 peaks[j].mag = newmag->data[ i][peaks[j].bin];190 peaks[j].mag = newmag->data[peaks[j].bin]; 193 191 /* reset non peaks */ 194 192 for (j = count; j < length; j++) … … 261 259 candidate[l]->ecomb[k] = peaks[position].ebin; 262 260 candidate[l]->ene += /* ecomb rounded to nearest int */ 263 POW (newmag->data[ 0][(uint_t) FLOOR (candidate[l]->ecomb[k] + .5)],261 POW (newmag->data[(uint_t) FLOOR (candidate[l]->ecomb[k] + .5)], 264 262 0.25); 265 263 candidate[l]->len += 1. / curlen; … … 290 288 aubio_pitchmcomb_quadpick (aubio_spectralpeak_t * spectral_peaks, fvec_t * X) 291 289 { 292 uint_t i, j, ispeak, count = 0; 293 for (i = 0; i < X->channels; i++) 294 for (j = 1; j < X->length - 1; j++) { 295 ispeak = fvec_peakpick (X, j); 296 if (ispeak) { 297 count += ispeak; 298 spectral_peaks[count - 1].bin = j; 299 spectral_peaks[count - 1].ebin = fvec_quadint (X, j, i) - 1.; 300 } 301 } 290 uint_t j, ispeak, count = 0; 291 for (j = 1; j < X->length - 1; j++) { 292 ispeak = fvec_peakpick (X, j); 293 if (ispeak) { 294 count += ispeak; 295 spectral_peaks[count - 1].bin = j; 296 spectral_peaks[count - 1].ebin = fvec_quadint (X, j) - 1.; 297 } 298 } 302 299 return count; 303 300 } … … 364 361 365 362 aubio_pitchmcomb_t * 366 new_aubio_pitchmcomb (uint_t bufsize, uint_t hopsize , uint_t channels)363 new_aubio_pitchmcomb (uint_t bufsize, uint_t hopsize) 367 364 { 368 365 aubio_pitchmcomb_t *p = AUBIO_NEW (aubio_pitchmcomb_t); … … 386 383 //p->biquad = new_biquad(0.1600,0.3200,0.1600, -0.5949, 0.2348); 387 384 /* allocate temp memory */ 388 p->newmag = new_fvec (spec_size , 1);385 p->newmag = new_fvec (spec_size); 389 386 /* array for median */ 390 p->scratch = new_fvec (spec_size , 1);387 p->scratch = new_fvec (spec_size); 391 388 /* array for phase */ 392 p->theta = new_fvec (spec_size , channels);389 p->theta = new_fvec (spec_size); 393 390 /* array for adaptative threshold */ 394 p->scratch2 = new_fvec (p->win_post + p->win_pre + 1 , 1);391 p->scratch2 = new_fvec (p->win_post + p->win_pre + 1); 395 392 /* array of spectral peaks */ 396 393 p->peaks = AUBIO_ARRAY (aubio_spectralpeak_t, spec_size); -
src/pitch/pitchmcomb.h
rfc61225 r168337e 57 57 \param buf_size size of the input buffer to analyse 58 58 \param hop_size step size between two consecutive analysis instant 59 \param channels number of channels to analyse60 59 \param samplerate sampling rate of the signal 61 60 62 61 */ 63 aubio_pitchmcomb_t *new_aubio_pitchmcomb (uint_t buf_size, uint_t hop_size, 64 uint_t channels); 62 aubio_pitchmcomb_t *new_aubio_pitchmcomb (uint_t buf_size, uint_t hop_size); 65 63 66 64 /** deletion of the pitch detection object -
src/pitch/pitchschmitt.c
rfc61225 r168337e 51 51 fvec_t * output) 52 52 { 53 uint_t i, j; 54 for (i = 0; i < input->channels; i++) { 55 for (j = 0; j < input->length; j++) { 56 p->buf[j] = input->data[i][j] * 32768.; 57 } 58 output->data[i][0] = aubio_schmittS16LE (p, input->length, p->buf); 53 uint_t j; 54 for (j = 0; j < input->length; j++) { 55 p->buf[j] = input->data[j] * 32768.; 59 56 } 57 output->data[0] = aubio_schmittS16LE (p, input->length, p->buf); 60 58 } 61 59 -
src/pitch/pitchyin.c
rfc61225 r168337e 65 65 { 66 66 aubio_pitchyin_t *o = AUBIO_NEW (aubio_pitchyin_t); 67 o->yin = new_fvec (bufsize / 2 , 1);67 o->yin = new_fvec (bufsize / 2); 68 68 o->tol = 0.15; 69 69 return o; … … 81 81 aubio_pitchyin_diff (fvec_t * input, fvec_t * yin) 82 82 { 83 uint_t c,j, tau;83 uint_t j, tau; 84 84 smpl_t tmp; 85 for (c = 0; c < input->channels; c++) { 86 for (tau = 0; tau < yin->length; tau++) { 87 yin->data[c][tau] = 0.; 88 } 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); 93 } 85 for (tau = 0; tau < yin->length; tau++) { 86 yin->data[tau] = 0.; 87 } 88 for (tau = 1; tau < yin->length; tau++) { 89 for (j = 0; j < yin->length; j++) { 90 tmp = input->data[j] - input->data[j + tau]; 91 yin->data[tau] += SQR (tmp); 94 92 } 95 93 } … … 100 98 aubio_pitchyin_getcum (fvec_t * yin) 101 99 { 102 uint_t c,tau;100 uint_t tau; 103 101 smpl_t tmp; 104 for (c = 0; c < yin->channels; c++) { 105 tmp = 0.; 106 yin->data[c][0] = 1.; 107 //AUBIO_DBG("%f\t",yin->data[c][0]); 108 for (tau = 1; tau < yin->length; tau++) { 109 tmp += yin->data[c][tau]; 110 yin->data[c][tau] *= tau / tmp; 111 //AUBIO_DBG("%f\t",yin->data[c][tau]); 112 } 113 //AUBIO_DBG("\n"); 102 tmp = 0.; 103 yin->data[0] = 1.; 104 //AUBIO_DBG("%f\t",yin->data[0]); 105 for (tau = 1; tau < yin->length; tau++) { 106 tmp += yin->data[tau]; 107 yin->data[tau] *= tau / tmp; 108 //AUBIO_DBG("%f\t",yin->data[tau]); 114 109 } 110 //AUBIO_DBG("\n"); 115 111 } 116 112 … … 118 114 aubio_pitchyin_getpitch (fvec_t * yin) 119 115 { 120 uint_t c = 0,tau = 1;116 uint_t tau = 1; 121 117 do { 122 if (yin->data[ c][tau] < 0.1) {123 while (yin->data[ c][tau + 1] < yin->data[c][tau]) {118 if (yin->data[tau] < 0.1) { 119 while (yin->data[tau + 1] < yin->data[tau]) { 124 120 tau++; 125 121 } … … 139 135 smpl_t tol = o->tol; 140 136 fvec_t *yin = o->yin; 141 uint_t c,j, tau = 0;137 uint_t j, tau = 0; 142 138 sint_t period; 143 139 smpl_t tmp = 0., tmp2 = 0.; 144 for (c = 0; c < input->channels; c++) { 145 yin->data[c][0] = 1.; 146 for (tau = 1; tau < yin->length; tau++) { 147 yin->data[c][tau] = 0.; 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); 151 } 152 tmp2 += yin->data[c][tau]; 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); 158 goto beach; 159 } 140 yin->data[0] = 1.; 141 for (tau = 1; tau < yin->length; tau++) { 142 yin->data[tau] = 0.; 143 for (j = 0; j < yin->length; j++) { 144 tmp = input->data[j] - input->data[j + tau]; 145 yin->data[tau] += SQR (tmp); 160 146 } 161 out->data[c][0] = fvec_quadint (yin, fvec_min_elem (yin), c); 162 beach: 163 continue; 147 tmp2 += yin->data[tau]; 148 yin->data[tau] *= tau / tmp2; 149 period = tau - 3; 150 if (tau > 4 && (yin->data[period] < tol) && 151 (yin->data[period] < yin->data[period + 1])) { 152 out->data[0] = fvec_quadint (yin, period); 153 goto beach; 154 } 164 155 } 165 //return 0; 156 out->data[0] = fvec_quadint (yin, fvec_min_elem (yin)); 157 beach: 158 return; 166 159 } 167 160 -
src/pitch/pitchyinfft.c
rfc61225 r168337e 56 56 { 57 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);58 p->winput = new_fvec (bufsize); 59 p->fft = new_aubio_fft (bufsize); 60 p->fftout = new_cvec (bufsize); 61 p->sqrmag = new_fvec (bufsize); 62 p->res = new_cvec (bufsize); 63 p->yinfft = new_fvec (bufsize / 2 + 1); 64 64 p->tol = 0.85; 65 65 p->win = new_aubio_window ("hanningz", bufsize); 66 p->weight = new_fvec (bufsize / 2 + 1, 1); 67 { 68 uint_t i = 0, j = 1; 69 smpl_t freq = 0, a0 = 0, a1 = 0, f0 = 0, f1 = 0; 70 for (i = 0; i < p->weight->length; i++) { 71 freq = (smpl_t) i / (smpl_t) bufsize *(smpl_t) 44100.; 72 while (freq > freqs[j]) { 73 j += 1; 74 } 75 a0 = weight[j - 1]; 76 f0 = freqs[j - 1]; 77 a1 = weight[j]; 78 f1 = freqs[j]; 79 if (f0 == f1) { // just in case 80 p->weight->data[0][i] = a0; 81 } else if (f0 == 0) { // y = ax+b 82 p->weight->data[0][i] = (a1 - a0) / f1 * freq + a0; 83 } else { 84 p->weight->data[0][i] = (a1 - a0) / (f1 - f0) * freq + 85 (a0 - (a1 - a0) / (f1 / f0 - 1.)); 86 } 87 while (freq > freqs[j]) { 88 j += 1; 89 } 90 //AUBIO_DBG("%f\n",p->weight->data[0][i]); 91 p->weight->data[0][i] = DB2LIN (p->weight->data[0][i]); 92 //p->weight->data[0][i] = SQRT(DB2LIN(p->weight->data[0][i])); 66 p->weight = new_fvec (bufsize / 2 + 1); 67 uint_t i = 0, j = 1; 68 smpl_t freq = 0, a0 = 0, a1 = 0, f0 = 0, f1 = 0; 69 for (i = 0; i < p->weight->length; i++) { 70 freq = (smpl_t) i / (smpl_t) bufsize *(smpl_t) 44100.; 71 while (freq > freqs[j]) { 72 j += 1; 93 73 } 74 a0 = weight[j - 1]; 75 f0 = freqs[j - 1]; 76 a1 = weight[j]; 77 f1 = freqs[j]; 78 if (f0 == f1) { // just in case 79 p->weight->data[i] = a0; 80 } else if (f0 == 0) { // y = ax+b 81 p->weight->data[i] = (a1 - a0) / f1 * freq + a0; 82 } else { 83 p->weight->data[i] = (a1 - a0) / (f1 - f0) * freq + 84 (a0 - (a1 - a0) / (f1 / f0 - 1.)); 85 } 86 while (freq > freqs[j]) { 87 j += 1; 88 } 89 //AUBIO_DBG("%f\n",p->weight->data[i]); 90 p->weight->data[i] = DB2LIN (p->weight->data[i]); 91 //p->weight->data[i] = SQRT(DB2LIN(p->weight->data[i])); 94 92 } 95 93 return p; … … 99 97 aubio_pitchyinfft_do (aubio_pitchyinfft_t * p, fvec_t * input, fvec_t * output) 100 98 { 101 uint_t i,tau, l;99 uint_t tau, l; 102 100 uint_t halfperiod; 103 101 smpl_t tmp, sum; 104 102 cvec_t *res = (cvec_t *) p->res; 105 103 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]; 104 l = 0; 105 tmp = 0.; 106 sum = 0.; 107 for (l = 0; l < input->length; l++) { 108 p->winput->data[l] = p->win->data[l] * input->data[l]; 109 } 110 aubio_fft_do (p->fft, p->winput, p->fftout); 111 for (l = 0; l < p->fftout->length; l++) { 112 p->sqrmag->data[l] = SQR (p->fftout->norm[l]); 113 p->sqrmag->data[l] *= p->weight->data[l]; 114 } 115 for (l = 1; l < p->fftout->length; l++) { 116 p->sqrmag->data[(p->fftout->length - 1) * 2 - l] = 117 SQR (p->fftout->norm[l]); 118 p->sqrmag->data[(p->fftout->length - 1) * 2 - l] *= 119 p->weight->data[l]; 120 } 121 for (l = 0; l < p->sqrmag->length / 2 + 1; l++) { 122 sum += p->sqrmag->data[l]; 123 } 124 sum *= 2.; 125 aubio_fft_do (p->fft, p->sqrmag, res); 126 yin->data[0] = 1.; 127 for (tau = 1; tau < yin->length; tau++) { 128 yin->data[tau] = sum - res->norm[tau] * COS (res->phas[tau]); 129 tmp += yin->data[tau]; 130 yin->data[tau] *= tau / tmp; 131 } 132 tau = fvec_min_elem (yin); 133 if (yin->data[tau] < p->tol) { 134 /* no interpolation */ 135 //return tau; 136 /* 3 point quadratic interpolation */ 137 //return fvec_quadint_min(yin,tau,1); 138 /* additional check for (unlikely) octave doubling in higher frequencies */ 139 if (tau > 35) { 140 output->data[0] = fvec_quadint (yin, tau); 141 } else { 142 /* should compare the minimum value of each interpolated peaks */ 143 halfperiod = FLOOR (tau / 2 + .5); 144 if (yin->data[halfperiod] < p->tol) 145 output->data[0] = fvec_quadint (yin, halfperiod); 146 else 147 output->data[0] = fvec_quadint (yin, tau); 112 148 } 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 } 152 } else { 153 output->data[i][0] = 0.; 154 } 149 } else { 150 output->data[0] = 0.; 155 151 } 156 152 }
Note: See TracChangeset
for help on using the changeset viewer.