Skip to content

Instantly share code, notes, and snippets.

@catfact
Last active March 6, 2021 08:46
Show Gist options
  • Select an option

  • Save catfact/ac108ff6f08306bad4f81c376572b8b3 to your computer and use it in GitHub Desktop.

Select an option

Save catfact/ac108ff6f08306bad4f81c376572b8b3 to your computer and use it in GitHub Desktop.
sine_adsr_modulated.scd
// run this block first...
(
Routine {
SynthDef(\adsr_sine, {
// named inputs
var gate, out, level, pan, hz,
attack, sustain, decay, release;
// synth variables
var signal, env;
gate = \gate.kr(0);
out = \out.kr(0);
level = \level.kr(0.1);
pan = \pan.kr(0);
hz = \hz.kr;
// these inputs will only be updated at the start of the corresponding segment.
attack = \attack.kr(0.01);
decay = \decay.kr(0.3);
sustain = \sustain.kr(0.5);
release = \release.kr(2.0);
// doneAction:2 makes this synth self-freeing when the envelope is finished.
// depending on the context it may be better to use statically allocated synths that are never freed.
env = EnvGen.ar(Env.adsr(attack, decay, sustain, release), gate, doneAction:2);
signal = SinOsc.ar(hz) * level * env;
Out.ar(out, Pan2.ar(signal, pan));
}).send(s);
~synths= Array.newClear(128);
~note_on = { arg num, vel;
var hz, timescale;
hz = num.midicps;
if(~synths[num].notNil, {
~synths[num].set(\gate, 0);
});
~synths[num] = Synth.new(\adsr_sine, [
\gate, 1, \hz, hz, \attack, vel.linexp(0, 127, 4.0, 0.1)
], target:s);
};
~note_off = {arg num, vel;
if (~synths[num].isNil, {
postln("wuh oh");
}, {
~synths[num].set(\release, vel.linexp(0, 127, 4.0, 0.1));
~synths[num].set(\gate, 0);
~synths[num] = nil;
});
};
}.play;
)
/// ... then these lines
~note_on.value(60, 1);
~note_off.value(60, 127);
~note_on.value(60, 127);
~note_off.value(60, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment