concept note in category chuck

This is an excerpt from Manning's book Programming for Musicians and Digital Artists: Creating music with ChucK.
Let’s use your newfound knowledge about integer variables to play a couple of notes, as shown in the next listing. Here you store your pitch in an integer variable, and you create two integers called onGain and offGain to store the values of 1 and 0 so you can turn the sound of your oscillator on and off.
The program in listing 2.3 demonstrates how random numbers can be used in the composition process. As you can see, you set up a for loop to play a total of 16 notes
. In each step of the loop, a random integer between 48 and 72 is created and stored in integer variable myNote
, and a random floating-point number between 0.05 and 0.9 is generated and stored in the float variable myGain
. After printing both out to the console, myNote is used to set the frequency of oscillator s via your old friend Std.mtof(), and myGain is used to set the volume.
Thus, a new MIDI note (frequency)
and volume are generated every fifth of a second
, making a composition that randomly generates notes. Although this generates no known melody, we think you’ll agree that it sounds somewhat musical, because of the note-by-note changes in volume (called accents by musicians and composers) and the ever-changing frequencies. Run it a few times, and you’ll hear that the notes and volumes generated are different each time. The only thing that’s guaranteed is that there will be exactly 16 notes.
Random number generators allow you to write musical programs that never sound exactly the same, but you can do so in a fairly controllable way, which can be an exciting technique as a modern composer. There’s a way to make sure that a sequence of random numbers is always the same but different from all other such sequences. Say you don’t want to write out what the notes are specifically, and you want them to be randomly generated, but you want the composition to be the same every time you play it. How do you accomplish this?
The key to controlling random number generation, along with specifying the range, is generating a seed, which the random number generator uses to start the sequence of random numbers it will generate from then on. The seed usually is created using something that’s constantly changing, like your computer’s system clock. Thus, every time you run the program of listing 2.3, a different seed is generated and the program generates a completely new set of notes and volumes. But starting with the same seed would ensure that the sequence of random numbers would be exactly the same across different executions of the same program. So how can you set the seed yourself? Type in the following new line of code and insert it into listing 2.3, right before the for loop at
:
Listing 7.14. ChucK’s shout-out to the Minimoog, the Moog UGen
// Homage to a classic analog synth sound Moog mog => dac; // table of pitches to use [77,72,69,75,70,67,74,70,65,72] @=> int cars[]; // play first note; play(cars[0],0.25); // then play most of the rest for (1 => int i; i < cars.cap()-1; i++) { if (i % 3 == 0) play(cars[i],0.5); else play(cars[i],0.25); } // push up "mod wheel" for last note 0.25 => mog.vibratoGain; play(cars[cars.cap()-1], 1.0); // function to play notes on/off fun void play(int note, float howLong) { Std.mtof(note) => mog.freq; 1 => mog.noteOn; (howLong - 0.05) :: second => now; 1 => mog.noteOff; 0.05 :: second => now; }
. In each step of the loop, a random integer between
, and a random floating-point number between
. After printing both out to the console, myNote is used to set the frequency of oscillator s via your old friend
Thus, a new MIDI note (frequency)
and volume are generated every fifth of a second
, making a composition that randomly generates notes. Although this generates no known melody, we think you’ll agree that it sounds somewhat musical, because of the note-by-note changes in volume (called accents by musicians and composers) and the ever-changing frequencies. Run it a few times, and you’ll hear that the notes and volumes generated are different each time. The only thing that’s guaranteed is that there will be exactly 16 notes.