Debian Codegraph @

examples/book/digital-artists/chapter9/Listing9.4.ck

raw
1 // Listing 9.4 Public class BPM acting like a global conductor
2
3 public class BPM
4 {
5 // (1) Variables declared this way are accessible globally
6 dur quarterNote, eighthNote, sixteenthNote, thirtysecondNote;
7 fun void tempo(float beat) { // (2) Function to set tempo (all variables)
8 // beat argument is BPM, example 120 beats per minute
9 // (3) Does a little math to go from BPM to seconds per beat
10 60.0/(beat) => float SPB; // seconds per beat
11 SPB :: second => quarterNote; // (4) Uses that to set all durations
12 quarterNote*0.5 => eighthNote;
13 eighthNote*0.5 => sixteenthNote;
14 sixteenthNote*0.5 => thirtysecondNote;
15 }
16 }
17
18 SinOsc s => dac;
19 BPM t; // Define t Object of Class BPM // (5) Tests your BPM object by making one
20 t.tempo(300); // set tempo in BPM // (6) Sets tempo of new BPM object
21
22 400 => int freq;
23 while (freq < 800)
24 {
25 freq => s.freq;
26 t.quarterNote => now; // (7) Uses .quarterNote method to advance time
27 50 +=> freq;
28 }