Multichannel Expansion

Multiple channels of audio are represented as Arrays.


// one channel

{,4,0.1) }.play;

// two channels

{ [,4,0.1), ] }.play;

Each channel of output will go out a different speaker, so your limit here is two for a stereo output. If you have a supported multi channel audio interface or card then you can output as many channels as the card supports.

All UGens have only a single output. This uniformity facilitates the use of array operations to perform manipulation of multi channel structures.

In order to implement multichannel output, UGens create a separate UGen known as an OutputProxy for each output. An OutputProxy is just a place holder for the output of a multichannel UGen. OutputProxies are created internally, you never need to create them yourself, but it is good to be aware that they exist so you'll know what they are when you run across them.

// look at the outputs of Pan2:,;

play({,; });

When an Array is given as an input to a unit generator it causes an array of multiple copies of that unit generator to be made, each with a different value from the input array. This is called multichannel expansion. All but a few special unit generators perform multichannel expansion. Only Arrays are expanded, no other type of Collection, not even subclasses of Array.

{,8,0.1) }.play // one channel

// the array in the freq input causes an Array of 2 Blips to be created :

{[499,600],8,0.1) }.play // two channels,8,0.1).postln // one unit generator created.[500,601],8,0.1).postln // two unit generators created.

Multichannel expansion will propagate through the expression graph. When a unit generator constructor is called with an array of inputs, it returns an array of instances. If that array is the input to another constructor, then another array is created, and so on.

{[100,250],0.05),,400,5), 0.05) }.play;

// the [100,250] array of frequency inputs to Saw causes to return 

// an array of two Saws, that array causes to create two RLPFs.

// Both RLPFs share a single instance of XLine.

When a constructor is parameterized by two or more arrays, then the number of channels created is equal to the longest array, with parameters being pulled from each array in parallel. The shorter arrays will wrap.

for example, the following:[400, 500, 600],[0.5, 0.1], 0.2)

is equivalent to:

[,0.5,0.2),,0.1,0.2),,0.5,0.2) ]

A more complex example based on the Saw example above is given below. In this example, the XLine is expanded to two instances, one going from 8000 Hz to 400 Hz and the other going in the opposite direction from 500 Hz to 7000 Hz. These two XLines are 'married' to the two Saw oscillators and used to parameterize two copies of RLPF. So on the left channel a 100 Hz Saw is filtered from 8000 Hz to 400 Hz and on the right channel a 250 Hz Saw is filtered from 500 Hz to 7000 Hz.

{[100,250],0.05),[8000,500],[400,7000],5), 0.05) }.play;

Protecting arrays against expansion

Some unit generators such as Klank require arrays of values as inputs. Since all arrays are expanded, you need to protect some arrays by a Ref object. A Ref instance is an object with a single slot named 'value' that serves as a holder of an object. one way to create a Ref, but there is a syntactic shortcut. The backquote ` is a unary operator that is equivalent to calling So to protect arrays that are inputs to a Klank or similar UGens you write:`[[400,500,600],[1,2,1]], z)

You can still create multiple Klanks by giving it an array of Ref'ed arrays.[ `[[400,500,600],[1,2,1]],  `[[700,800,900],[1,2,1]] ], z)

is equivalent to:

[`[[400,500,600],[1,2,1]], z),`[[700,800,900],[1,2,1]], z)]

Reducing channel expansion with Mix

The Mix object provides the means for reducing multichannel arrays to a single channel.[a, b, c]) // array of channels

is equivalent to:

a + b + c  // mixed to one

Mix is more efficient than using + since it can perform multiple additions at a time. But the main advantage is that it can deal with situations where the number of channels is arbitrary or determined at runtime.

// three channels of Pulse are mixed to one channel

{[400, 501, 600], [0.5, 0.1], 0.1) ) }.play

Multi channel expansion works differently for Mix. Mix takes one input which is an array (one not protected by a Ref). That array does not cause copies of Mix to be made. All elements of the array are mixed together in a single Mix object. On the other hand if the array contains one or more arrays then multi channel expansion is 

performed one level down. This allows you to mix an array of stereo (two element) arrays resulting in one two channel array. For example: [ [a, b], [c, d], [e, f] ] ) // input is an array of stereo pairs

is equivalent to:

// mixed to a single stereo pair

[ [a, c, e] ), [b, d, f] ) ] 

Currently it is not recursive. You cannot use Mix on arrays of arrays of arrays.

Here's a final example illustrating multi channel expansion and Mix. By changing the variable 'n' you can change the number of voices in the patch. How many voices can your machine handle?



var n;

n = 8; // number of 'voices' // mix all stereo pairs down. // pan the voice to a stereo position // a comb filter used as a string resonator // random impulses as an excitation function

// an array to cause expansion of Dust to n channels

// 1 means one impulse per second on average

Array.fill(n, 1), 

0.3 // amplitude


0.01, // max delay time in seconds

// array of different random lengths for each 'string'

Array.fill(n, {0.004.rand+0.0003}), 

4 // decay time in seconds


Array.fill(n,{1.0.rand2}) // give each voice a different pan position





Using flop for multichannel expansion

The method flop swaps columns and rows, allowing to derive series of argument sets:


SynthDef("help_multichannel", { |out=0, freq=440, mod=0.1, modrange=20|,, 0, modrange) + freq

) * EnvGate(0.1)





var freq, mod, modrange;

freq = Array.exprand(8, 400, 5000);

mod = Array.exprand(8, 0.1, 2);

modrange = Array.rand(8, 0.1, 40);

fork {

[\freq, freq, \mod, mod, \modrange, modrange] { |args|


Synth("help_multichannel", args);





Similarly, Function-flop returns an unevaluated function that will expand to its arguments when evaluated:


SynthDef("blip", { |freq|,, 0, 0.05, 1, 0, 2) 

* * [1, 1.02])) }).send(s);

a = { |dur=1, x=1, n=10, freq=400|

fork { {

if(x.coin) { Synth("blip", [\freq, freq]) };

(dur / n).wait;

} }



a.value(5, [0.3, 0.3, 0.2], [12, 32, 64], [1000, 710, 700]);