| 1 |
// name: autocorr.ck |
| 2 |
// desc: finding the pitch of a note |
| 3 |
// author: Curtis Ullerich |
| 4 |
|
| 5 |
// UGen/UAna patch |
| 6 |
SinOsc s => Flip flip =^ AutoCorr corr => blackhole; |
| 7 |
// analysis zie |
| 8 |
1024 => flip.size; |
| 9 |
// output in [-1,1] |
| 10 |
true => corr.normalize; |
| 11 |
// calculate sample rate |
| 12 |
second/samp => float srate; |
| 13 |
|
| 14 |
// pitch estimate |
| 15 |
fun float estimatePitch() |
| 16 |
{ |
| 17 |
// perform analysis at corr (propagating backwards) |
| 18 |
corr.upchuck(); |
| 19 |
// for simplicity, ignore bins for notes that are "too high" |
| 20 |
// to care about; stop at the mid-point because it's symmetrical. |
| 21 |
(srate/Std.mtof(90)) $ int => int maxBin; |
| 22 |
// iterate over result of analysis |
| 23 |
for( maxBin => int bin; bin < corr.fvals().size()/2; bin++ ) |
| 24 |
{ |
| 25 |
// look for max |
| 26 |
if( corr.fval(bin) >= corr.fval(maxBin) ) { |
| 27 |
bin => maxBin; |
| 28 |
} |
| 29 |
} |
| 30 |
// return frequency |
| 31 |
return srate/maxBin; |
| 32 |
} |
| 33 |
|
| 34 |
// loop |
| 35 |
for (int i; i < 10; i++) |
| 36 |
{ |
| 37 |
// randomize freq |
| 38 |
Math.random2f(Std.mtof(40), Std.mtof(80)) => s.freq; |
| 39 |
// move forward in time (by analysis frame size) |
| 40 |
flip.size()::samp => now; |
| 41 |
// print |
| 42 |
<<< "pitch", s.freq(), "estimated to be", estimatePitch(), "hz" >>>; |
| 43 |
} |