Author |
Message |
nelsen
Joined: Jun 25, 2009 Posts: 2 Location: London, UK
|
Posted: Thu Jun 25, 2009 12:14 am Post subject:
Using a loop inside a SynthDef |
 |
|
I'm trying to create an echo Synthdef which produces a specifiable number of echos of the input. I've tried it like this:
Code: |
(
SynthDef(\echo, {
arg in=0, out=0, period=0.25, feedback=0.5, repeats=4;
var source, output;
source = SoundIn.ar(in, 1);
output = source;
repeats.do({
arg i;
var j=i+1;
output = output + DelayN.ar(source, j*period, j*period, feedback**j);
});
Out.ar(out, output);
}).send(s);
)
|
However, I only get one echo of the input signal. If I add, before it works, although then the number of repeats is fixed.
I guess the problem is that the loop just runs once on creation of the synthdef, but I can't think of a better way of doing this!
Thanks,
John |
|
Back to top
|
|
 |
dewdrop_world

Joined: Aug 28, 2006 Posts: 858 Location: Guangzhou, China
Audio files: 4
|
Posted: Thu Jun 25, 2009 6:14 am Post subject:
|
 |
|
You can't use a synthdef argument to control the number of repeats.
A synthdef is a fixed structure, but you're asking it to change its structure (increase the number of UGens) when you create a new Synth. That's not possible.
The loop runs just once because the server (which runs the synth) does not know ANYTHING about language flow-of-control constructs.
Easiest way is to create a function that generates a synthdef.
Code: | (
f = { |repeats = 4|
SynthDef(\echo ++ repeats, {
arg in=0, out=0, period=0.25, feedback=0.5;
var source, output, delayed;
source = SoundIn.ar(in, 1);
output = source;
repeats.do({
arg i;
var j=i+1;
output = output + DelayN.ar(source, j*period, j*period, feedback**j);
});
Out.ar(out, output);
}).send(s);
};
)
f.value(4); // creates "echo4"
f.value(3); // creates "echo3" |
Also, this small change will save some memory:
Code: | (
f = { |repeats = 4|
SynthDef(\echo ++ repeats, {
arg in=0, out=0, period=0.25, feedback=0.5;
var source, output;
source = SoundIn.ar(in, 1);
output = source;
delayed = source;
repeats.do({
arg i;
var j=i+1;
delayed = DelayN.ar(delayed, period, period);
output = output + (delayed * (feedback**j));
});
Out.ar(out, output);
}).send(s);
};
) |
hjh _________________ ddw online: http://www.dewdrop-world.net
sc3 online: http://supercollider.sourceforge.net |
|
Back to top
|
|
 |
nelsen
Joined: Jun 25, 2009 Posts: 2 Location: London, UK
|
Posted: Thu Jun 25, 2009 3:48 pm Post subject:
|
 |
|
Awesome, thank you! |
|
Back to top
|
|
 |
|