| 1 |
//---------------------------------------------------------------- |
| 2 |
// name: ZeroX (Zero Crossing Detector) |
| 3 |
// desc: Detect the number of zero crosses in a Flip of samples |
| 4 |
// 1. Flip will convert N samples to a UAna Frame |
| 5 |
// 2. ZeroX will then count the number of zero crossings in |
| 6 |
// the UAna Frame |
| 7 |
// |
| 8 |
// Note the ZeroX output in this example is equal to |
| 9 |
// zeroX = 2*freq / (sample_rate / N) |
| 10 |
//---------------------------------------------------------------- |
| 11 |
|
| 12 |
// analysis patch |
| 13 |
SinOsc foo => Flip flip =^ ZeroX zerox => blackhole; |
| 14 |
|
| 15 |
// set flip size (N) |
| 16 |
4096 => flip.size; |
| 17 |
|
| 18 |
// change the frequency of foo randomly every 1-2 seconds |
| 19 |
fun void ctrl() |
| 20 |
{ |
| 21 |
while( true ) |
| 22 |
{ |
| 23 |
// new random freq |
| 24 |
Math.random2f( 100, 2000 ) => foo.freq; |
| 25 |
<<< "setting new freq:", foo.freq() >>>; |
| 26 |
// wait 1-2 seconds |
| 27 |
Math.random2f( 1, 2 )::second => now; |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
// spork frequency changer shred |
| 32 |
spork ~ ctrl(); |
| 33 |
|
| 34 |
// main |
| 35 |
while( true ) |
| 36 |
{ |
| 37 |
// compute ZeroX in the last UAna Frame |
| 38 |
// store the result in a UAnaBlob |
| 39 |
zerox.upchuck() @=> UAnaBlob blob; |
| 40 |
|
| 41 |
// print number of zero crossings |
| 42 |
<<< blob.fvals()[0], "" >>>; |
| 43 |
|
| 44 |
// advance time |
| 45 |
flip.size()::samp => now; |
| 46 |
} |