Debian Codegraph @

examples/ai/features/chroma.ck

raw
1 //---------------------------------------------------------------------
2 // name: chroma.ck
3 // desc: chroma unit anlyzer, used for feature extraction of pitch data
4 // e.g., a set of frequencies
5 //
6 // uncomment for Chroma API:
7 // Chroma.help();
8 //
9 // author: Yikai Li
10 // Ge Wang (https://ccrma.stanford.edu/~ge/)
11 // date: Winter 2023
12 //---------------------------------------------------------------------
13
14 // synthesis / analysis network
15 SinOsc s => FFT fft =^ Chroma chroma => blackhole;
16
17 // set FFT size
18 2048 => fft.size;
19 // set window type and size
20 Windowing.hann(fft.size()) => fft.window;
21 // our hop size (how often to perform analysis)
22 fft.size()::samp => dur HOP;
23
24 // pitch names
25 ["C4", "C#", "D4", "D#", "E4", "F4", "F#", "G4", "G#", "A4", "A#", "B4"] @=> string note_names[];
26 // test frequencies
27 [262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494] @=> int freqs[];
28
29 // print header
30 cherr <= "freq ";
31 for (0 => int i; i < 12; i++) {
32 cherr <= note_names[i] + " ";
33 }
34 cherr <= IO.newline();
35
36 // let one FFT-size of time pass (to buffer)
37 fft.size()::samp => now;
38
39 // extract chroma features
40 for( 0 => int i; i < freqs.size(); i++ )
41 {
42 // set frequency for test
43 freqs[i] => s.freq;
44
45 //----------------------------------------------------------------
46 // upchuck() computes our Chroma, automatically computing upstream
47 // dependencies connected to it using =^ (e.g., FFT)
48 //----------------------------------------------------------------
49 chroma.upchuck();
50
51 // print features
52 cherr <= freqs[i] <= ": ";
53 for( 0 => int j; j < chroma.fvals().size(); j++ )
54 {
55 Math.round(chroma.fval(j) * 100) => float tmp;
56 cherr <= (tmp < 10 ? " ":"") <= tmp <= " ";
57 }
58 cherr <= IO.newline();
59
60 // advance time
61 HOP => now;
62 }