SynthDef from indexed Flucoma Buffer

Hey there,
I want to make a SynthDef from a function that was suggested in the Flucoma Tutorial
The function plays slices of a Buffer.

~play_slice = {
	|buf, slice, index, vol|
	{
		var startsamp = Index.kr(slice, index),
		stopsamp = Index.kr(slice, index +1),
		phs = Phasor.ar(0, BufRateScale.ir(buf), startsamp, stopsamp),
		sig = BufRd.ar(1, buf, phs),
		dursec =( stopsamp - startsamp)/BufSampleRate.ir(buf),
		env = EnvGen.kr(Env([0, 1, 1, 0], [0.03, dursec - 0.06, 0.03]), doneAction:2);
		sig.dup * env * \amp.kr(vol);
	}.play
};

In order to play it as a Pbindef, I would also like to turn it into a SynthDef. I tried this

SynthDef(\playSlice, {
	|buf, index=0, vol=1|
	var startsamp, stopsamp, phs, sig, dursec, env;
	startsamp = Index.kr(NamedControl.kr(\slice, (0..100)), index);
	stopsamp = Index.kr(NamedControl.kr(\slice, (0..100)), index + 1);
	phs = Phasor.ar(0, BufRateScale.ir(buf), startsamp, stopsamp);
	sig = BufRd.ar(1, buf, phs);
	dursec = (stopsamp - startsamp)/BufSampleRate.ir(buf);
	env = EnvGen.kr(Env([0, 1, 1, 0], [0.03, dursec - 0.06, 0.03]), doneAction:2);
	sig = Pan2.ar(sig * env * \amp.kr(vol), \pan.kr(0));
	Out.ar(\out.kr(0), sig);

}).add;

When I execute the code I get this error

ERROR: SynthDef: could not write def: DoesNotUnderstandError

Before I make a Pbindef, I run the FluCoMa AmpSlicer and thus create the ~slice Buffer, which I provide to the Pbindef.

(
var buf = ~buffers[\lecture][0];

~slice = Buffer.new(s);
FluidBufAmpSlice.process(s, buf, indices: ~slice, fastRampUp: 300, fastRampDown: 900 ,slowRampUp: 3000,slowRampDown: 2000, onThreshold: 10,offThreshold: 5,floor: -40,minSliceLength: 4410,highPassFreq: 20)
)

Pbindef(\slice, \instrument, \playSlice, \buf, ~buffers[\lecture][0], \slice, ~slice, \index, Pseq((0..~slice.numFrames), inf), \amp, Pwhite(0,1, 0.9, inf), \dur, 2 );

I suspect I am providing a wrong initial value for \slice in the SynthDef. But how can I make this work? Or is it just not possible. The ~playSlice Funktion works, though.

thanks for the attention

1 Like

In the original SynthDef, this produces one slice’s endpoints – thus, one phasor, one buffer reader, one envelope etc.

Note that the specific slice is chosen by index – this SynthDef plays one slice at a time, but it can be any slice.

Here, your version produces an array of 101 start and stop points – thus 101 simultaneous phasors, buffer readers, envelopes etc. (Btw it’s not a good idea to have 101 envelopes all with doneAction:2 because the shortest one will cut off the entire synth – you lose independent envelope control.)

But then, in the Pbindef, you provide only one slice buffer, not the 101 slice buffers expected by the SynthDef: \slice, ~slice. Because there’s a mismatch between the SynthDef and the usage, I suspect that the (0..100) is simply a misunderstanding, and you should replace those with just 0.

Also, a typo: \slice, \instrument is backward.

hjh

1 Like

thank you, @jamshark70,
right, of course I don’t want to raise hell with 101 instances of the Synth. :wink:
The thing is that I don’t know if I should put an initial value for the \slice argument in the SynthDef. If I just declare the argument without a value, I still get the

ERROR: SynthDef: could not write def: DoesNotUnderstandError

SynthDef(\playSlice, {
	|buf, slice, index=0, vol=1|
	var startsamp, stopsamp, phs, sig, dursec, env;
	startsamp = Index.kr(slice, index);
	stopsamp = Index.kr(slice, index + 1);
	phs = Phasor.ar(0, BufRateScale.ir(buf), startsamp, stopsamp);
	sig = BufRd.ar(1, buf, phs);
	dursec = (stopsamp - startsamp)/BufSampleRate.ir(buf);
	env = EnvGen.kr(Env([0, 1, 1, 0], [0.03, dursec - 0.06, 0.03]), doneAction:2);
	sig = Pan2.ar(sig * env * \amp.kr(vol), \pan.kr(0));
	Out.ar(\out.kr(0), sig);
}).add();

Why can’t SC make the Synth?

Hello @qurgl

You can’t use an argument to define default value of another argument:
In your arg list you have vol = 1, then later you write \amp.kr(vol).
This synthdef works:

(
SynthDef(\playSlice, {
	|buf, slice=0, index=0|
	var startsamp, stopsamp, phs, sig, dursec, env;
	startsamp = Index.kr(slice, index);
	stopsamp = Index.kr(slice, index + 1);
	phs = Phasor.ar(0, BufRateScale.ir(buf), startsamp, stopsamp);
	sig = BufRd.ar(1, buf, phs);
	dursec = (stopsamp - startsamp)/BufSampleRate.ir(buf);
	env = EnvGen.kr(Env([0, 1, 1, 0], [0.03, dursec - 0.06, 0.03]), doneAction:2);
	sig = Pan2.ar(sig * env * \amp.kr(1), \pan.kr(0));
	Out.ar(\out.kr(0), sig);
}).add;
)

Thank you, you’re right, the SynthDef works now.