File: Tracking.ck

package info (click to toggle)
chuck 1.5.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 40,904 kB
  • sloc: cpp: 120,943; ansic: 35,893; javascript: 2,111; yacc: 609; makefile: 456; python: 174; perl: 86
file content (63 lines) | stat: -rw-r--r-- 1,291 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//--
// name: Tracking.ck
// desc: class and process for tracking a peak and amplitutde via FFT
//       the Tracking class can be accessed from external shreds
//
// author: Ge Wang + Rebecca Fiebrink
// date: 2007
//--


// public class
public class Tracking
{
    static float the_freq;
    static float the_gain;
    static Event @ the_event;
}

// initialize separately (due to a bug)
new Event @=> Tracking.the_event;

// analysis
adc => PoleZero dcblock => FFT fft => blackhole;

// set to block DC
.99 => dcblock.blockZero;
// set FFT params
2048 => fft.size;
// window
Windowing.hamming( fft.size() ) => fft.window;

// go for it
while( true )
{
    // take fft
    fft.upchuck() @=> UAnaBlob blob;
    
    // find peak
    0 => float max; float where;
    for( int i; i < blob.fvals().size()/8; i++ )
    {
        // compare
        if( blob.fvals()[i] > max )
        {
            // save
            blob.fvals()[i] => max;
            i => where;
        }
    }
    
    // set freq
    (where / fft.size() * (second / samp)) => Tracking.the_freq;
    // set gain
    (max / .5) => Tracking.the_gain;
    // clamp
    if( Tracking.the_gain > 1 )
        1 => Tracking.the_gain;
    // fire!
    Tracking.the_event.broadcast();

    // hop
    (fft.size()/4)::samp => now;
}