# 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 ?

//~unmixed=Bus.audio(s,10);
~count=5;
~unmixed=Bus.audio(s,2*~count);

SynthDef(\mixAll, { |in, count=10, out=0|
// var all=in.ar; // 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
var all=In.ar(in,c);

// Some future processing
// ...

// Mixing it all
Out.ar(out,Mix.ar(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 = \bus.kr(0!6); // NamedControl arg, as always the number of busses has to be hardcoded an cannot be an argument
var someParam = In.ar(bus[0]);
var someOtherParam = In.ar(bus[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 = In.ar(bus[0]);
var someOtherParam = In.ar(bus[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: \bus.kr(0!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.

hjh

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 )

SynthDef(\mixAll, { | out=0|
var bus=\channels.kr(-1!40); // choose some max
var amp=\amp.kr(0.8);
var count=0;
var sig;

// count how many bus are filled-in
bus.do{|v,i|
count=count+Select.kr(bus[i]+1,[0,1]);
};

// then play with this
Poll.kr(Impulse.kr(10),count,"count");
amp=amp/count;
Poll.kr(Impulse.kr(10),amp,"amp");

sig= bus.collect{|v,i|
Select.ar(bus[i]+1,[DC.ar(0),In.ar(bus[i])*amp]);
};

Out.ar(out,Mix.ar(sig));