With this synth, the ADSR envelope doesn’t work-- the sound starts at full amplitude, then cuts off immediately when ‘gate’ is set to 0. If I simply change the oscillator to Saw.ar
, the envelope works as expected…what’s going on here?
SynthDef("mysynth", {
arg freq = 110, out = 0, amp = 0.6, gate = 1, atk = 3.0, decay = 2.0, sustain = 0.0, rel = 2.0;
var env = EnvGen.kr(Env.adsr(atk, decay, sustain, rel), gate, doneAction:2);
Out.ar(out, SinOsc.ar([freq, freq+1], amp * env));
}).add;
There are 2 mistakes :
- sustain is not duration, but a level. Therefore it should not be 0.
- the second parameter of SinOsc is phase, not amplitude.
The following works:
arg freq = 510, out = 0, amp = 0.6, gate = 1, atk = 3.0, decay = 2.0, sustain = 0.5, rel = 2.0;
var env = EnvGen.kr(Env.adsr(atk, decay, sustain, rel), gate, doneAction:2);
Out.ar(out, SinOsc.ar([freq, freq+1], 0, amp * env));
}).add;
Ah HA! I had forgotten about the ‘phase’ parameter for SinOsc.
Thanks!
Bryan
Elsewhere, Nathan has proposed that we should avoid using the mul
and add
arguments, and just write *
and +
instead.
I don’t think it’s necessary to be too strict about it, but this case supports his idea:
-
If you use mul
, then you have to remember which argument is mul
= one more opportunity for bugs.
-
If you write SinOsc.ar(...) * env
, then there is absolutely no room for doubt.
hjh