class library: DUGen - the server now handles audio-rate inputs correctly
[supercollider.git] / SCClassLibrary / Common / Audio / Mix.sc
blobd4828a1600df8c3f5487c74b00e1cb6b49d806b5
1 Mix {
2         *new { arg array;
3                 var sum;
4                 array.asArray.do({ arg val, i;
5                         if (sum.isNil, { sum = val },{ sum = sum + val });
6                 });
7                 ^sum
8         }
9         // support this common idiom
10         *fill { arg n, function;
11                 var val, sum;
12                 n.do({ arg i;
13                         val = function.value(i);
14                         if (sum.isNil, { sum = val },{ sum = sum + val });
15                 });
16                 ^sum;
17         }
21 NumChannels {
23         *ar { arg input, numChannels = 2, mixdown = true;
25                 if(input.size > 1) { // collection
26                    ^input
27                         .clump(input.size / numChannels)
28                         .collect { arg chan, i;
29                                 if(chan.size == 1) {
30                                         chan.at(0)
31                                 } {
32                                         if(mixdown) {
33                                                 Mix.new(chan)
34                                         } {
35                                                 chan.at(0)
36                                         }
37                                 }
38                         }
39                 } {
40                         // single ugen or single item collection
41                         if(input.isSequenceableCollection) {
42                                 input = input.at(0);
43                         };
45                         if(numChannels == 1) {
46                                 ^input
47                         } {
48                                 ^Array.fill(numChannels, input)
49                         }
50                 }
51         }