SuperCollider Forum
May 19, 2013, 09:15:46 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: The SuperCollider forum is currently experiencing a rash of spambot registrations. New user requests may not be approved quickly as a result -- and if your email address looks like spam, it might not be approved at all. We are working to improve the security of the registration process, and to provide alternate means to contact the administrators to get a new account. Thanks for your patience.
 
   Home   Help Search Calendar Login Register  
Pages: [1]
  Print  
Author Topic: Envelopes in SynthDefs  (Read 3033 times)
0 Members and 1 Guest are viewing this topic.
halalkebabhut
Newbie
*

Karma: 0
Posts: 9


View Profile
« on: August 19, 2008, 03:41:26 PM »

I've been using Envelopes to control various parameters but have a couple of questions which I think should be easy to answer but useful for any other Newbies on a steep learning curve like myself.

1)is it possible to map an envelope onto a synth, using synth.new, .map or .set ? eg. I tried the following but the envelope didn't happen

(
   SynthDef.new(\PlainSin, {arg freq = 440, amp = 0.2, sig, pan;
   sig = SinOsc.ar(freq ,0, amp);
   sig = Pan2.ar(sig, pan);
   Out.ar(0,sig )
   }).send(s);
);

//then...
(
var env;
env = EnvGen.kr(Env.new([0, 1.5, 0.5], [1.5, 4], ['sine',-4]),1,1,0,1,2);
x = Synth.new(\PlainSin, [\pan, 0.3,\amp, env]);
)

in the end I used lots of different synthdefs + control busses to get round this but it feels very inefficient. Why can't I use envelopes like Line.kr

2) when I use multiple envelopes in the same synthdef eg. one for freq and one for amps the shorter one seems to force the release. Do they always have to be the same length or is there a way round this as well ?

Hope these aren't too rudimentary questions...

S
Logged
joshp
AdminGroup
Full Member
*

Karma: 5
Posts: 121



View Profile WWW
« Reply #1 on: August 21, 2008, 12:39:49 PM »

For your second question (it is simpler to answer), it sounds like your various envelope generators are all using a doneAction of 2, which releases the synth when they are finished executing. If they use doneAction: 0, they will not release the synth, and you can use envs of varying lengths.

For your second question, if you want to map values from outside the synth, it is control busses that you need to use. So it sounds like you are on the right track. You can also use Control.names inside a SynthDef to pass in envelopes (or more specifically, arrays that describe your envelope) when the Synth is created.

If you are interested in looking at other options for doing this, the Ctk quark offers another Object oriented approach (like Synth) that allows for some of this kind of patching to be done a little easier. Envs can be passed in as arguments for parameters that are set up with Control.names, and CtkControl.env can create envs that map onto an argument to a SynthDef.
Code:
s.boot;

a = CtkSynthDef(\test2, {arg gate = 1, freq, amp;
var env, envgen, src;
env = Control.names([\env]).kr(Env.newClear(8));
envgen = EnvGen.kr(env, gate, doneAction: 2);
src = BPF.ar(WhiteNoise.ar(amp), freq.poll, 0.01, amp * envgen);
Out.ar(0, Pan2.ar(src, Rand(-1.0, 1.0)));
});

// create a new note instance based on the SynthDef in 'a'
b = a.new.freq_(440).amp_(1).env_(Env([0, 1, 0], [2, 2], \sin, 1)).play;
b.release; // free it

// create a new note with the same amplitude envelope, but also create and map an
// envelope for freq

b = a.new.freq_(CtkControl.env(Env([440, 880], [4], \exp))).amp_(1).env_(Env([0, 1, 0], [2, 2], \sin, 1)).play;

// after the env for freq has finished, you can still control it
b.freq_(440);

// CtkControl also offers a few LFO choices, and you can nest CtkControls:

b.freq_(CtkControl.lfo(LFNoise2, 1, 440, CtkControl.env(Env([440, 880], [10], \exp))));

b.release; // all are freed!

Hope that helps.

josh


Logged
dewdrop_world
AdminGroup
Full Member
*

Karma: 9
Posts: 193



View Profile WWW
« Reply #2 on: August 21, 2008, 12:56:30 PM »

There are a couple of ways to do this. Which one is best depends on what you're trying to do.

If you have a large number of distinct envelopes (say, every synth should have a radically different envelope shape), see the EnvGen help file for an example of using an array of synth control to define the shape. (This technique got a little easier in the current SVN development build, since you should not have to do .setn explicitly for the envelope control points -- that code still new and may change, though.)

If the envelopes have the same basic contours, but just different levels and times, you can create the envelope using synth arguments:

Code:
SynthDef(\xyz, { |attack, decay, sustain, release, gate = 1  /* other args */|
var env = Env.adsr(attack, decay, sustain, release),
envg = EnvGen.kr(env, gate, doneAction: 2);
...
});

But my favorite is to use Instr.

Code:
Instr("plainsin", { |freq, gate, env|
SinOsc.ar(freq) * EnvGen.kr(env, gate, doneAction: 2)
}, [\freq, \gate, EnvSpec(Env.adsr(0.1, 0.5, 0.4, 0.2))]);

mydef = Patch("plainsin").asSynthDef; // use default envelope

// replace with a different envelope
myOtherDef = Patch("plainsin", [nil, nil, Env.asr(0.8, 1, 1.8)]).asSynthDef;

Now you have two synthdefs from the same template, with different envelopes, and without a lot of extra Controls that you might not be using.

If Instr is too confusing, you can write functions to generate synthdefs.

Code:
makeADef = { |name, env|
SynthDef(name, { |freq, gate = 1  /* other args */|
var envg = EnvGen.kr(env, gate, doneAction: 2);
...
});
};

makeADef.("s1", Env(....)).send(s);

Quote
Why can't I use envelopes like Line.kr?

Because a SynthDef is a fixed, predefined arrangement of UGens. There is no way to add new UGens to a synth while it's playing. It's not only envelopes -- that's true of any UGen.

James
Logged

halalkebabhut
Newbie
*

Karma: 0
Posts: 9


View Profile
« Reply #3 on: August 28, 2008, 04:41:38 AM »

Thanks for such comprehensive answers.

There's so many ways of doing the same thing in SC it gets quite difficult to work out a strategy for what you want to do...

though the instrument one looks especially good for my purposes.


S
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.4 | SMF © 2006-2007, Simple Machines LLC Valid XHTML 1.0! Valid CSS!