Debian Codegraph @

examples/book/digital-artists/chapter6/Listing6.5.ck

raw
1 // Listing 6.5 Simple violin using SawOsc, Envelope, and SinOsc for vibrato
2
3 // Simple SawOsc-based violin with ADSR envelope and vibrato
4 SinOsc vibrato => SawOsc viol => ADSR env => dac;
5
6 // (1) Tells the oscillator to interpret input as frequency modulation
7 2 => viol.sync;
8
9 6.0 => vibrato.freq; // (2) set vibrato frequency to 6 Hz
10
11 // (3) set all A D S R parameters at once
12 env.set(0.1 :: second, 0.1 :: second, 0.5, 0.1 :: second);
13
14 // define a D Major Scale (in MIDI note numbers)
15 [62, 64, 66, 67, 69, 71, 73, 74] @=> int scale[]; // (4) Makes a scale note array
16
17 // run through our scale one note at a time
18 for (0 => int i; i < scale.cap(); i++) // (5) Plays through whole scale using for loop
19 {
20 // set frequency according to note number
21 Std.mtof(scale[i]) => viol.freq; // (6) Sets frequency for each note
22
23 // trigger note and wait a bit
24 1 => env.keyOn;
25 0.3 :: second => now;
26
27 // turn off note and wait a bit
28 1 => env.keyOff;
29 0.1 :: second => now;
30 }
31
32 // repeat last note with lots of vibrato
33 1 => env.keyOn;
34 10.0 => vibrato.gain;
35 1.0 :: second => now;
36
37 0 => env.keyOff;
38 0.2 :: second => now;