| 1 |
//--------------------------------------------------------------------- |
| 2 |
// name: centroid.ck |
| 3 |
// desc: fantastic audio features and where to extract them: |
| 4 |
// extracting spectral centroid; also demonstrates using |
| 5 |
// unit analyzers (UAna), connecting them using the |
| 6 |
// upchuck operator (=^), and getting results |
| 7 |
// |
| 8 |
// author: Ge Wang (https://ccrma.stanford.edu/~ge/) |
| 9 |
// date: Spring 2023 |
| 10 |
//--------------------------------------------------------------------- |
| 11 |
|
| 12 |
// synthesis / analysis network |
| 13 |
SinOsc foo => FFT fft =^ Centroid centroid => blackhole; |
| 14 |
// set sine frequency |
| 15 |
440 => foo.freq; |
| 16 |
|
| 17 |
// set FFT size |
| 18 |
1024 => 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 |
// compute srate |
| 24 |
second / samp => float srate; |
| 25 |
|
| 26 |
// let one FFT-size of time pass (to buffer) |
| 27 |
fft.size()::samp => now; |
| 28 |
// control loop |
| 29 |
while( true ) |
| 30 |
{ |
| 31 |
//---------------------------------------------------------------- |
| 32 |
// upchuck() computes our centroid, automatically computing upstream |
| 33 |
// dependencies connected to it using =^ (e.g., FFT) |
| 34 |
//---------------------------------------------------------------- |
| 35 |
centroid.upchuck(); |
| 36 |
|
| 37 |
// get and print the output |
| 38 |
<<< "centroid (hz):", centroid.fval(0) * srate / 2 >>>; |
| 39 |
|
| 40 |
// advance time |
| 41 |
HOP => now; |
| 42 |
} |