In Learning Modular Synthesis: Part 1, I presented a block diagram illustrating the signal flow of several elements. We looked at representations of that block diagram in 3 different applications simulating a Eurorack modular synthesizer. While each application has a particular workflow, knowledge of modular signal flow is applicable to all 3. This same knowledge is useful if we are to write Patch 1 as text-based code.
Below (view content in browser to see code example) is an example utilizing the elements in the block diagram, programmed in ChucK (https://chuck.cs.princeton.edu/).
If you're not familiar with the ChucK music programming language, no worries! I'll walk you through this program, line by line. But before I do, you might ask, "how is this code related to the Patch 1 block diagram?" The block diagram from Patch 1 illustrates a higher level of abstraction, in which the blocks and the interconnectivity between them show a general overview of the system. The block diagram doesn't contain details about each step of the sequence. The code above, however, illustrates each stage of the MIDI sequence, representing a lower level of abstraction.
This program requires setup, which I start on line 4. On this line I create an object called sine, from the unit generator (UGen) class in ChucK called SinOsc, and connect it directly (via the ChucK operator =>) to the dac. Next, I declare and initialize an array called cMajorScale on line 6, which contains the MIDI notes for the C Major scale.
Lines 8-13 is a loop and will repeat the number of times there are notes in the array, so 8 times. Lines 9-12 run with each iteration of the loop. On line 9, I'm printing to the console each iteration number and the corresponding MIDI note number. You can see the console output below:
On line 10, I call the mtof() function from the Std class to convert the MIDI notes to frequency. I chuck this to the sine oscillator (sine.freq). On line 11, I control the amplitude of each note played by chucking an amount of 1.0 (full amplitude) to the sine oscillator (sine.gain). Finally, I chuck a note duration of 1.0 second to now, meaning that upon run, each note will play for a duration of 1 second. This also means that each iteration of the loop lasts for 1 second. This result is a sequenced performance of the C Major Scale.
The ChucK operator (=>) and the idea of "chucking" are similar to patching. However here, instead of chucking to the next element in the signal chain, we're chucking control values to an object, the sine oscillator, to modify its connection to the dac.