| 1 |
// Listing 2.8 Two-part random walk music with panning |
| 2 |
|
| 3 |
// 2-part Random Music with Panning |
| 4 |
// by ChucK Team, September 25, 2020 |
| 5 |
// two oscillators, melody and harmony |
| 6 |
SinOsc s => Pan2 mpan => dac; // (1) SinOsc through Pan2 so it canmove around |
| 7 |
TriOsc t => dac; // (2) TriOsc fixed at center location |
| 8 |
|
| 9 |
// we will use these to separate notes later |
| 10 |
0.5 => t.gain; // (3) Float variables to control your note gains |
| 11 |
0.5 => float onGain; |
| 12 |
0.0 => float offGain; |
| 13 |
72 => int melodyNote; // (4) Int variable to control your melody |
| 14 |
|
| 15 |
while (true) |
| 16 |
{ |
| 17 |
// set melody pitch somewhat randomly, with limits |
| 18 |
Math.random2(-3,3) +=> melodyNote; // (5) Randomly changes melody up, down, or not |
| 19 |
|
| 20 |
if (melodyNote < 60) // (6) Lower limit on melody |
| 21 |
{ |
| 22 |
60 => melodyNote; |
| 23 |
} |
| 24 |
if (melodyNote > 84) // (7) Upper limit on melody |
| 25 |
{ |
| 26 |
84 => melodyNote; |
| 27 |
} |
| 28 |
|
| 29 |
Std.mtof(melodyNote) => s.freq; // (8) Sets solo SinOsc pitch |
| 30 |
|
| 31 |
// melody has a random pan for each note |
| 32 |
Math.random2f(-1.0,1.0) => mpan.pan; |
| 33 |
|
| 34 |
// On a "dice roll," change harmony note |
| 35 |
if (Math.random2(1,6) == 1) |
| 36 |
{ // (9) Randomly sticks TriOsc on a pitch |
| 37 |
Std.mtof(melodyNote-12) => t.freq; |
| 38 |
} |
| 39 |
|
| 40 |
// Pick one of three random durations |
| 41 |
Math.random2(1,3)*0.2 => float myDur; |
| 42 |
|
| 43 |
// note on time is 80% of duration |
| 44 |
onGain => s.gain; |
| 45 |
(myDur*0.8)::second => now; |
| 46 |
|
| 47 |
// space between notes is 20% of array duration |
| 48 |
offGain => s.gain; |
| 49 |
(myDur*0.2)::second => now; |
| 50 |
} |
| 51 |
|