Debian Codegraph @

examples/event/event-x-bpm-1.ck

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