Skip to content

Instantly share code, notes, and snippets.

@bion
Last active September 7, 2021 06:08
Show Gist options
  • Save bion/e905f36308ee3ac6b99729fda128c84f to your computer and use it in GitHub Desktop.
Save bion/e905f36308ee3ac6b99729fda128c84f to your computer and use it in GitHub Desktop.
supercollider demo
// start scsynth, the server
// s is bound to the default server when the sclang starts
s.boot;
// define a simple synthesizer definition and send it to the server
SynthDef('simpleSine', {
Out.ar(0, SinOsc.ar(440, 0, -16.dbamp));
}).send(s);
// create a synth using that definition
a = Synth('simpleSine');
// destroy the synth
a.free;
SynthDef('fm', {
arg freq, index;
var car, mod;
mod = SinOsc.kr(freq, 0, freq * index);
car = SinOsc.ar(freq + mod);
Out.ar(0, car * -16.dbamp);
}).send(s);
f = Synth('fm', ['freq', 440, 'index', 0.1]);
f.free;
f = Synth('fm', ['freq', 440, 'index', 0.5]);
f.free;
f = Synth('fm', ['freq', 440, 'index', 1]);
f.free;
f = Synth('fm', ['freq', 440, 'index', 5]);
f.free;
SynthDef('fmWithMouse', {
arg freq;
var car, mod, index;
// use the mouse to control index
index = MouseX.kr(0.01, 5);
mod = SinOsc.kr(freq, 0, freq * index);
car = SinOsc.ar(freq + mod);
Out.ar(0, car * -16.dbamp);
}).send(s);
f = Synth('fmWithMouse', ['freq', 440]);
f.free;
SynthDef('fmWithEnvelope', {
arg freq, index;
var car, mod, env;
mod = SinOsc.kr(freq, 0, freq * index);
car = SinOsc.ar(freq + mod);
// Env.perc defines the shape of a percussive envelope
// EnvGen "plays" that envelope
env = EnvGen.kr(Env.perc, doneAction: Done.freeSelf);
Out.ar(0, car * env * -16.dbamp);
}).send(s);
// no need to call free because it frees itself
Synth('fmWithEnvelope', ['freq', 440, 'index', 1]);
SynthDef('filterExample', {
var sig;
// all numbers implement the #midicps method which converts from
// a midi note number to a cycles per second frequency value
sig = Saw.ar(48.midicps);
sig = LPF.ar(sig, MouseX.kr(48.midicps, 8000));
Out.ar(0, sig * -16.dbamp);
}).send(s);
f = Synth('filterExample');
f.free;
SynthDef('multichannelExpansion', {
var sig;
// using an array as an argument to a unit generator creates an
// instance of the ugen for each array element.
// the * operator with a number and an array will return a new array
// containing each element of the original array multiplied by the number
sig = Saw.ar(48.midicps * [0.99, 1, 1.01]).sum;
sig = LPF.ar(sig, MouseX.kr(48.midicps, 8000));
sig = sig * -22.dbamp;
// multichannel expansion here will send the same signal to outputs 0 and 1,
// giving us stereo output
Out.ar([0, 1], sig);
// alternatively, the Out UGen can take an array of signals for its second argument
// and will route them to consecutive output channels, so this does the same thing
// as the above example:
// Out.ar(0, [sig, sig]);
}).send(s);
f = Synth('multichannelExpansion');
f.free;
// realtime audio processing
// wear headphones to avoid feedback or uncomment the delay
SynthDef('voiceShift', {
var in;
in = SoundIn.ar(0);
// delay by two seconds to avoid feedback at least for a little while
// in = DelayC.ar(in, 2, 2);
in = PitchShift.ar(in,
windowSize: 0.2,
pitchRatio: MouseX.kr(0.25, 4).poll,
pitchDispersion: 0,
timeDispersion: 0.01);
Out.ar(0, in);
}).send(s);
v = Synth('voiceShift');
v.free;
// there are many, many great learning resources for supercollider online
// a good place to start is the official intro tutorial:
// https://doc.sccode.org/Tutorials/Getting-Started/00-Getting-Started-With-SC.html
// the supercollider book is also good, and very large:
// https://mitpress.mit.edu/books/supercollider-book
// the community is generally helpful and responsive to beginners.
// slack:
// https://join.slack.com/t/scsynth/shared_invite/zt-ezoyz15j-SVM7JVul94pxtDiUDRnd0w
// mailing list (i strongly recommend using gmail filters to tag and archive these or your inbox will be inundated):
// http://www.birmingham.ac.uk/facilities/BEAST/research/supercollider/mailinglist.aspx
// forums:
// https://scsynth.org/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment