Retrieving the number of channels of a Bus within a SynthDef

I have a SynthDef receiving an audio bus as input with multiple channels.

How can I retrieve the number of channels of that bus within the SynthDef itself ?


SynthDef(\mixAll, { |in, count=10, out=0|
	// var; // ERROR: Message 'ar' not understood. RECEIVER: an OutputProxy
	// var c=in.numChannels; // returns 1
	// var c=count*2; // ERROR: Non Boolean in test, RECEIVER: a BinaryOpUGen
	// var c=~count*2;  // required an external variable :-(
	var c=20; // hardcoded :-(

	// Reading the right number of channels from the input bus 

	// Some future processing
	// ...

	// Mixing it all,*0.5);

//~mixall=Synth(\mixAll, [\in, ~unmixed, \out, 0, \count, ~count]);
~mixall=Synth(\mixAll, [\in, ~unmixed, \out, 0]);


I like to use NamedControl in SynthDefs for arrays - the pros and cons of NamedControl are discussed in many threads on the forum. Here is the outline of a SynthDef with 6 input busses.

SynthDef(\test, {
	var bus = \!6); // NamedControl arg, as always the number of busses has to be hardcoded an cannot be an argument
	var someParam =[0]);
	var someOtherParam =[1]);

note that variable ‘bus’ in the SynthDef is independent of argument ‘bus’, I just chose to use the same name.

With the old way of declaring arguments the SynthDef would look like this:

SynthDef(\test, {|bus = #[0,0,0,0,0,0]|
	var someParam =[0]);
	var someOtherParam =[1]);

It is hopefully obvious from the above why NamedControl is a shorter and more concise syntax for arrays in SynthDefs. Imagine you wanted 20 busses. NamedControl: \!20). Old way: bus = #[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]

This would change the synthdef structure at runtime, which is never allowed.

Further, in scsynth, buses are not associated with a number of channels. The server has no concept of a multichannel bus, hence, no “number of channels” to retrieve.

It needs to be managed from the language.


Not in scsynth, but well in sclang. Hence the confusion.

-> Bus(audio, 246, 10, localhost)

From a post of mine of 2 years ago : ( I think I stumble more or less on the same limitation :person_facepalming: )

With your comments I rewrote my Synth like this:

SynthDef(\mixAll, { | out=0|
    var bus=\!40); // choose some max
    var amp=\;
    var count=0;
    var sig;

    // count how many bus are filled-in{|v,i|[i]+1,[0,1]);
    // then play with this,count,"count");

    sig= bus.collect{|v,i|[i]+1,[,[i])*amp]);