Skip to content

Instantly share code, notes, and snippets.

@jrsa
Created May 15, 2017 22:19
Show Gist options
  • Save jrsa/7fb3003b8520c369b5d47af44130c173 to your computer and use it in GitHub Desktop.
Save jrsa/7fb3003b8520c369b5d47af44130c173 to your computer and use it in GitHub Desktop.
simple chuck patch providing a bank of oscillators, and iteratively controlling them over time. divided into compositional sections
// how many oscillators will there be?
30 => int bankWidth;
TriOsc sines[bankWidth];
// adjust gain of bank to avoid clipping
0.1 / bankWidth => float oscGain;
// rest of dsp chain
LPF f => Pan2 p => dac;
10000.0 => f.freq;
// song parameters
67 => int bottomNote; 14 => int offset; 11./8. => float tune;
// add oscs to dsp chain and set gain
for(0 => int i; i < bankWidth; i++) {
Std.mtof(bottomNote) => sines[i].freq;
oscGain => sines[i].gain;
// add to dsp chain
sines[i] => f;
}
// use fewer oscillators as time goes on
bankWidth => int activeOscs;
bankWidth / 2 => int sectionCount;
9 => int sectionLength;
350::ms => dur noteLength;
// main loop sets the oscillators to some random pitches
for(0 => int i; i < sectionCount; i++) {
<<<"section " + i + ": root " + bottomNote + " - " + activeOscs + " oscs active">>>;
sectionLength => int j;
// random pitch value for each note
while(j--) {
j % activeOscs => int oscIndex;
Std.mtof(Math.random2f( bottomNote, bottomNote + offset )) * tune => sines[oscIndex].freq;
// pan value must be between -1 and 1
j $ float/sectionLength - 0.5 => p.pan;
<<< p.pan() >>>;
noteLength => now;
}
// paramater adjustments for next section
bottomNote + 3 => bottomNote;
noteLength + 50::ms => noteLength;
activeOscs - 1 => activeOscs;
}
// fade out oscs
oscGain => float gain;
for(0 => int i; i < bankWidth; i++) {
<<<"fading " + i>>>;
// keep decrementing gain until it is zero
while(gain > 0.0) {
gain => sines[i].gain;
gain - 0.0001 => gain;
1::ms => now;
}
oscGain => gain;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment