julian+alberto classlib merges for 3.4 (10125, 10130, 10133)
[supercollider.git] / common / build / SCClassLibrary / Common / Audio / Mix.sc
blob7ec5c7a99623d6f8c56257cc0a9a17481bcfa914
2 Mix {
3         *new { arg array;
4                 var sum;
5                 array.asArray.do({ arg val, i;
6                         if (sum.isNil, { sum = val },{ sum = sum + val });
7                 });
8                 ^sum
9         }
10         // support this common idiom
11         *fill { arg n, function;
12                 var val, sum;
13                 n.do({ arg i;
14                         val = function.value(i);
15                         if (sum.isNil, { sum = val },{ sum = sum + val });
16                 });
17                 ^sum;
18         }
22 NumChannels {
24         *ar { arg input, numChannels = 2, mixdown = true;
25                 
26                 if(input.size > 1) { // collection
27                    ^input
28                         .clump(input.size / numChannels)
29                         .collect { arg chan, i;
30                                 if(chan.size == 1) {
31                                         chan.at(0)
32                                 } {
33                                         if(mixdown) {
34                                                 Mix.ar(chan)
35                                         } {
36                                                 chan.at(0)
37                                         }
38                                 }
39                         }
40                 } { 
41                         // single ugen or single item collection
42                         if(input.isSequenceableCollection) {
43                                 input = input.at(0);
44                         };
45                         
46                         if(numChannels == 1) {
47                                 ^input
48                         } {
49                                 ^Array.fill(numChannels, input)
50                         }
51                 }
52         }