SampleRate outside of Synth does nothing?

I can feel that this has to do somehow with the Synth being created with the play method, and that the BufSampleRate and SampleRate Ugens are outside of that synth…, but can some one explain why is this code wrong (it doesn’t even start/stop the synth, the synth hangs around even though doneAction=2?

(
var numChannels = b.numChannels;
var ratio = BufSampleRate.ir(b) / SampleRate.ir;
var rate = 1;
var action = 2;
{
	PlayBuf.ar(
		numChannels: numChannels, 
		bufnum: b,
		rate: ratio * rate,
		doneAction: action)
}.play;
)

I can feel that this has to do somehow with the Synth being created with the play method, and that the BufSampleRate and SampleRate Ugens are outside of that synth…

Your feeling is 100% correct :slight_smile:

but can some one explain why is this code wrong

Because UGens only work within the context of a UGen graph function.

1 Like

You can wrap the UGens is a function and compose:

(
var numChannels = b.numChannels;
var ratio = {BufSampleRate.ir(b) / SampleRate.ir};
var rate = 1;
var action = 2;
{
	PlayBuf.ar(
		numChannels: numChannels, 
		bufnum: b,
		rate: ratio * rate,
		doneAction: action)
}.play;
)
1 Like

But then the function should be called later, right?!

1 Like

Absolutely! This method is efficient because UGens are treated as AbstractFunctions. They are a subclass of AbstractFunction, as Function. This allows any function external to the SynthDef to be postponed until it’s actually evaluated within the SynthDef.

It essentially acts as a mechanism to delay a ‘block’ (as it’s called in smalltalk) until it’s evaluated within a SynthDef context.

You can even play with this and write “proto synthdefs”, or smaller blocks, and delay their evaluation.

For more details, you can refer to the documentation here: AbstractFunction in SuperCollider.