Skip to content

Instantly share code, notes, and snippets.

@kpmcc
Created June 7, 2022 01:21
Show Gist options
  • Save kpmcc/851fc780d9a4c6c27b1e77b873cbd3f7 to your computer and use it in GitHub Desktop.
Save kpmcc/851fc780d9a4c6c27b1e77b873cbd3f7 to your computer and use it in GitHub Desktop.
Supercollider Demo files
s.boot;
x = {(PinkNoise.ar * 0.3)!2}.play
x.free;
( // These synths pile up and stay active (persistent lifetimes)
x = {
var sig, env;
env = Line.kr(1, 0, 1);
sig = Pulse.ar(ExpRand(30, 500), 0.5, 0.3, 0.0)!2 * env;
}.play
)
s.freeAll; // This removes all synths
( // Will be cleared when our line envelope terminates, so synths don't pile up.
x = {
var sig, env;
env = Line.kr(1, 0, 1, doneAction:2);
sig = Pulse.ar(ExpRand(30, 500), 0.5, 0.3, 0.0)!2 * env;
}.play
)
( // Using an Xline instead of line, need to set end of range above zero because exponential interpolation
x = {
var sig, freq, env, dur;
dur = 5;
env = XLine.kr(1, 0.01, dur, doneAction:2);
freq = Line.kr(880, 110, dur/2, doneAction:0);
sig = Pulse.ar(freq, 0.8, 0.2)!2 * env;
}.play
)
(
{// Example env gen:
// [0, 1, 0], start, midpoint, end
// [1, 1], durating of segments
// linear interpolation by default
var sig, env;
env = EnvGen.kr(Env.new, doneAction:2);
sig = Pulse.ar(ExpRand(30, 500), 0.5, 0.2, 0)!2 * env;
}.play
)
(
{
var sig, kenv, myenv;
var init_amp, hi, mid, low;
var atk_dur, dec_dur, rel_dur;
var amplitude_array;
var duration_array;
init_amp = 0;
hi = 1;
mid = 0.2;
low = 0;
//var atk_dur, dec_dur, rel_dur;
atk_dur = 0.5;
dec_dur = 1;
rel_dur = 2;
amplitude_array = [init_amp, hi, mid, low];
duration_array = [atk_dur, dec_dur, rel_dur];
myenv = Env.new(amplitude_array, duration_array);
kenv = EnvGen.kr(myenv, doneAction:2);
sig = LFTri.ar(Rand(30, 80).midicps, 0.0, 0.3, 0)!2 * kenv;
}.play
)
// MIDIdef.noteOn(\noteOnTest, {"key down".postln});
// MIDIdef(\noteOnTest).disable;
// MIDIdef(\noteOnTest).enable;
// MIDIdef(\noteOnTest).free; // like other players / funcs
// MIDIdef.freeAll;
MIDIdef.noteOn(\noteOnTestPerm, {"key down".postln}).permanent_(true); // this persists after Cmd + . (usually mididefs are destroyed on cmd + .)
/*(
MIDIdef.noteOn(\noteOnTest, {
arg vel, nn, chan, src;
[vel, nn, chan, src].postln;
{
var sig, env;
sig = LFCub.ar(nn.midicps, 1)!2; // midicps -> midi to cycles per second (hz?) !2 -> stereo output
// not handling midi off yet so default percussive envelope to deal with turning sinosc off
env = EnvGen.kr(Env.perc, doneAction:2);
// linexp -> linear range to exponential range
// taking linear 1 to 127 midi vel range
// and mapping to 0.01 to 0.3 exponential (to prevent clipping etc)
sig = sig * env * vel.linexp(1,127, 0.01, 0.3);
}.play;
});
)*/
MIDIClient.init;
MIDIIn.connectAll;
(
SynthDef.new(\tone, {
arg freq=440, amp=0.3;
var sig, env;
sig = LFTri.ar(freq*2)!2;
env = EnvGen.kr(Env.perc, doneAction:2);
sig = sig * env * amp;
Out.ar(0, sig);
}).add;
)
( \\ midi note ins to our supercollider midi device will go to \tone synth
MIDIdef.noteOn(\noteOnTest, {
arg vel, nn, chan, src;
[vel, nn].postln;
Synth.new(\tone, [\freq, nn.midicps, \amp, vel.linexp(1, 127, 0.01, 0.3)])
});
)
(
{ // saw waves
LFSaw.ar(220, 0, 0.2, 0.0)!2
}.play;
)
(
{ // sine waves
SinOsc.ar(220, 0.0, 0.2, 0)!2
}.play;
)
(
{ // we have triangle waves
LFTri.ar(220, 0.0, 0.2, 0)!2
}.play;
)
(
{ // we have pulse waves
var pw = 0.2;
Pulse.ar(220, pw, 0.2, 0)!2;
}.play;
)
(
{ // doing some pulse width modulation
var pw = SinOsc.kr(0.1, 0, 0.4, 0.5);
Pulse.ar(110, pw, 0.2, 0.0)!2;
}.scope;
)
(
{ // resonant filter demo with envelope
var env = Env.new([1, 0.1], [0.5]);
var myEnv = EnvGen.kr(env, doneAction:2);
var x = LFSaw.ar(100, 0, 0.2);
MoogFF.ar(x, 10000*myEnv, 3.8)!2;
}.play
)
(
SynthDef.new(\sine, {
arg freq=440, atk=0.005, rel=0.3, amp=1, pan=0, pw=0.5;
var sig, env;
sig = Pulse.ar(freq, pw);
env = EnvGen.kr(Env.new([0, 1, 0], [atk, rel], [1, -1]), doneAction:2);
sig = Pan2.ar(sig, pan, amp);
sig = sig * env;
Out.ar(0, sig);
}).add
)
( // Default duration of 1 second per note
p = Pbind(
\type, \note,
\instrument, \sine,
).play;
)
s.plotTree;
(
p = Pbind(
\type, \note,
\instrument, \sine,
\dur, Pseq([0.6, 0.15, 0.15], inf), //.trace shows seq values in post window
\freq, Pseq([330, 247, 370], inf),
).play;
)
(
p = Pbind(
\type, \note,
\instrument, \sine,
\dur, Pseq([0.6, 0.15, 0.15], 4), // repeats duration sequence 4 times (12 notes)
\freq, Pseq([330, 247, 370, 220], inf),
).play;
)
(
p = Pbind( // when using different length repeats for dur and freq, the min seq length is taken
\type, \note,
\instrument, \sine,
\dur, Pseq([0.6, 0.15, 0.15], 4), // repeats freq sequence 2 times (8 notes)
\freq, Pseq([330, 247, 370, 220], 2), // using freq here relies on synthdef args
).play;
)
/// this one is crazy
(
p = Pbind(
\instrument, \sine,
\dur, Pwhite(0.5, 1, inf),
\freq, Pexprand(60, 1000, inf).trace,
\pw, Pwhite(0.1, 0.9, inf),
\atk, Pwhite(2.0, 3.0, inf),
\rel, Pwhite(5.0, 10.0, inf),
\amp, Pexprand(0.01, 0.1, inf),
\pan, Pwhite(-0.8, 0.8, inf),
).play;
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment