| 1 |
//-- |
| 2 |
// name: Tracking.ck |
| 3 |
// desc: class and process for tracking a peak and amplitutde via FFT |
| 4 |
// the Tracking class can be accessed from external shreds |
| 5 |
// |
| 6 |
// author: Ge Wang + Rebecca Fiebrink |
| 7 |
// date: 2007 |
| 8 |
//-- |
| 9 |
|
| 10 |
|
| 11 |
// public class |
| 12 |
public class Tracking |
| 13 |
{ |
| 14 |
static float the_freq; |
| 15 |
static float the_gain; |
| 16 |
static Event @ the_event; |
| 17 |
} |
| 18 |
|
| 19 |
// initialize separately (due to a bug) |
| 20 |
new Event @=> Tracking.the_event; |
| 21 |
|
| 22 |
// analysis |
| 23 |
adc => PoleZero dcblock => FFT fft => blackhole; |
| 24 |
|
| 25 |
// set to block DC |
| 26 |
.99 => dcblock.blockZero; |
| 27 |
// set FFT params |
| 28 |
2048 => fft.size; |
| 29 |
// window |
| 30 |
Windowing.hamming( fft.size() ) => fft.window; |
| 31 |
|
| 32 |
// go for it |
| 33 |
while( true ) |
| 34 |
{ |
| 35 |
// take fft |
| 36 |
fft.upchuck() @=> UAnaBlob blob; |
| 37 |
|
| 38 |
// find peak |
| 39 |
0 => float max; float where; |
| 40 |
for( int i; i < blob.fvals().size()/8; i++ ) |
| 41 |
{ |
| 42 |
// compare |
| 43 |
if( blob.fvals()[i] > max ) |
| 44 |
{ |
| 45 |
// save |
| 46 |
blob.fvals()[i] => max; |
| 47 |
i => where; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
// set freq |
| 52 |
(where / fft.size() * (second / samp)) => Tracking.the_freq; |
| 53 |
// set gain |
| 54 |
(max / .5) => Tracking.the_gain; |
| 55 |
// clamp |
| 56 |
if( Tracking.the_gain > 1 ) |
| 57 |
1 => Tracking.the_gain; |
| 58 |
// fire! |
| 59 |
Tracking.the_event.broadcast(); |
| 60 |
|
| 61 |
// hop |
| 62 |
(fft.size()/4)::samp => now; |
| 63 |
} |