| 1 |
//--------------------------------------------------------------------- |
| 2 |
// name: event-x-bpm-2.ck |
| 3 |
// desc: this is the same as event-x-bpm-1.ck, but one() uses array |
| 4 |
// lookup (instead of if/else) to figure out what to play on tick |
| 5 |
//--------------------------------------------------------------------- |
| 6 |
|
| 7 |
// sometimes it's helpful to define a custom kind of Event |
| 8 |
// so we can associate our own data with it |
| 9 |
class MyEvent extends Event |
| 10 |
{ |
| 11 |
// measure number |
| 12 |
int measure; |
| 13 |
// smallest beat subdivision within a measure (e.g., 16th notes) |
| 14 |
// we will assume the event will be broadcast every tick |
| 15 |
int tick; |
| 16 |
} |
| 17 |
|
| 18 |
// make a shared event that any shred can wait on |
| 19 |
MyEvent PULSE; |
| 20 |
// bpm variable |
| 21 |
100 => float BPM; |
| 22 |
// how many ticks per measure? |
| 23 |
16 => float ticksPerMeasure; |
| 24 |
|
| 25 |
// play volume / probablity per tick (can think of this as a sequence) |
| 26 |
[1.0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0] @=> float playProbs[]; |
| 27 |
// play rate per tick |
| 28 |
[.5, 0, 0, 0, 1, 0, 0, 0, .5, 0, 0, 0, 1, 0, 0, 0] @=> float playRates[]; |
| 29 |
// NOTE: the sizes of these arrays should equal ticksPerMeasure |
| 30 |
|
| 31 |
// spork timekeeper |
| 32 |
spork ~ metronome(); |
| 33 |
// different functions |
| 34 |
spork ~ one() @=> Shred @ shOne; |
| 35 |
spork ~ two() @=> Shred @ shTwo; |
| 36 |
// a function to change the BPM |
| 37 |
spork ~ tempoController(); |
| 38 |
|
| 39 |
// keep things running |
| 40 |
while( true ) |
| 41 |
{ |
| 42 |
1::second => now; |
| 43 |
} |
| 44 |
|
| 45 |
// a thing to change BPM when desired |
| 46 |
fun void tempoController() |
| 47 |
{ |
| 48 |
// a loop to control the BPM |
| 49 |
while( true ) |
| 50 |
{ |
| 51 |
// every two measures, change the BPM for fun |
| 52 |
if( PULSE.tick == 0 && (PULSE.measure%2 == 0) ) |
| 53 |
{ |
| 54 |
// randomize BPM lol |
| 55 |
Math.random2f( 60, 120 ) => BPM; |
| 56 |
// print BPM |
| 57 |
<<< "new BPM:", BPM >>>; |
| 58 |
} |
| 59 |
|
| 60 |
// wait on the pulse |
| 61 |
PULSE => now; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
// metronome (our "conductor") |
| 66 |
fun void metronome() |
| 67 |
{ |
| 68 |
while( true ) |
| 69 |
{ |
| 70 |
// compute every time in case BPM changes |
| 71 |
1/BPM * minute => dur BEAT; |
| 72 |
// assume 4/4, compute durationm of a tick |
| 73 |
BEAT*4/ticksPerMeasure => dur TICK; |
| 74 |
|
| 75 |
// broadcast to all shreds waiting on PULSE |
| 76 |
PULSE.broadcast(); |
| 77 |
|
| 78 |
// increment tick count |
| 79 |
PULSE.tick++; |
| 80 |
// print |
| 81 |
cherr <= "."; cherr.flush(); |
| 82 |
// if we reached the end of a measure |
| 83 |
if( PULSE.tick >= ticksPerMeasure ) |
| 84 |
{ |
| 85 |
// increment measure number |
| 86 |
PULSE.measure++; |
| 87 |
// reset tick count to 0 |
| 88 |
0 => PULSE.tick; |
| 89 |
// next line |
| 90 |
cherr <= IO.newline(); |
| 91 |
} |
| 92 |
|
| 93 |
// advance time by a tick duration |
| 94 |
TICK => now; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
// define function |
| 99 |
fun void one() |
| 100 |
{ |
| 101 |
SndBuf buffy => dac; |
| 102 |
"special:dope" => buffy.read; |
| 103 |
0 => buffy.gain; |
| 104 |
|
| 105 |
while( true ) |
| 106 |
{ |
| 107 |
// wait on the shared event |
| 108 |
PULSE => now; |
| 109 |
// look up |
| 110 |
if( playProbs[PULSE.tick] > 0 ) // instead of 0, it could be a threshold controlled by a knob |
| 111 |
{ |
| 112 |
// play |
| 113 |
0 => buffy.pos; |
| 114 |
// use the prob value also as volume |
| 115 |
playProbs[PULSE.tick] => buffy.gain; |
| 116 |
// look up the play rate |
| 117 |
playRates[PULSE.tick] => buffy.rate; |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
// define function |
| 123 |
fun void two() |
| 124 |
{ |
| 125 |
SndBuf buffy => dac; |
| 126 |
"special:dope" => buffy.read; |
| 127 |
0 => buffy.gain; |
| 128 |
|
| 129 |
while( true ) |
| 130 |
{ |
| 131 |
// wait on the shared event |
| 132 |
PULSE => now; |
| 133 |
// in this case play every tick |
| 134 |
0 => buffy.pos; |
| 135 |
// HIGH PITCHED DOH |
| 136 |
2 => buffy.rate; |
| 137 |
.1 => buffy.gain; |
| 138 |
} |
| 139 |
} |