Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / SCClassLibrary / JITLib / ProxySpace / NodeMapSetting.sc
blobac4699d55ebc876aae0bf186cde8d38da36c28f4
1 NodeMapSetting {
3         var <>key, <>value, <>busNumChannels, <>isMultiChannel=false, <>isMapped=false, <>mappedRate;
5         *new { arg key, value, busNumChannels;
6                 ^super.newCopyArgs(key, value, busNumChannels)
7         }
9         map { arg index;
10                 value = index;
11                 isMultiChannel = false;
12                 isMapped = true;
13                 busNumChannels = 1;
14         }
15         mapn { arg index, numChannels;
16                 value = index;
17                 busNumChannels = numChannels;
18                 isMultiChannel = true;
19                 isMapped = true;
20         }
21         mapa { arg index;
22                 this.map(index);
23                 mappedRate = \audio;
24         }
25         mapan { arg index, numChannels;
26                 this.map(index, numChannels);
27                 mappedRate = \audio;
28         }
29         set { arg val;
30                 value = val;
31                 isMapped = false;
32                 isMultiChannel = val.isSequenceableCollection;
33         }
34         getValue { // unmapped, single channel only
35                 ^if(this.isMapped.not and: { this.isMultiChannel.not }) { value } { nil }
36         }
38         addToEvent { arg event;
39                 var mapPrefix;
40                 if(isMapped) {
41                         mapPrefix = if(mappedRate === \audio) { "a" } { "c" };
42                         event.put(key, { |i| mapPrefix ++ (i + this.index) }.dup(busNumChannels))
43                 } {
44                         event.put(key, value)
45                 }
47         }
49         updateNodeMap { arg nodeMap;
50                 if(isMapped) {
51                         if(this.isNeutral) { nodeMap.upToDate = false; ^this }; // ignore setting
52                         this.updateBusNumChannels;
53                         if(isMultiChannel) {
54                                 if(mappedRate === \audio) {
55                                         nodeMap.mapanArgs = nodeMap.mapanArgs.addAll([key,this.index,busNumChannels])
56                                 } {
57                                         nodeMap.mapnArgs = nodeMap.mapnArgs.addAll([key, this.index, busNumChannels])
58                                 }
59                         }{
60                                 if(mappedRate === \audio) {
61                                         nodeMap.mapaArgs = nodeMap.mapaArgs.addAll([key, this.index]);
62                                 } {
63                                         nodeMap.mapArgs = nodeMap.mapArgs.addAll([key, this.index]);
64                                 }
65                         }
66                 } {
67                         if(value.notNil) {
68                                 if(isMultiChannel) {
69                                         nodeMap.setnArgs = nodeMap.setnArgs.addAll([key, value.size]++value)
70                                 } {
71                                         nodeMap.setArgs = nodeMap.setArgs.addAll([key, value])
72                                 }
73                         };
74                 }
75         }
77         copy {
78                 ^this.class.prNew(key, value, busNumChannels, isMultiChannel, isMapped, mappedRate)
79         }
80         
81         asControlName {
82                 ^ControlName(key, nil, mappedRate, value)
83         }
85         isEmpty { ^value.isNil }
86         index { ^value }
87         isNeutral { ^false }
88         updateBusNumChannels {}
90         storeArgs { ^[key, value, busNumChannels] }
92         printOn { arg stream;
93                 stream << this.storeArgs
94         }
96         *prNew { arg key, value, busNumChannels, isMultiChannel=false, isMapped=false, mappedRate;
97                 ^super.newCopyArgs(key, value, busNumChannels, isMultiChannel, isMapped, mappedRate)
98         }
102 ProxyNodeMapSetting : NodeMapSetting {
103         var <>rate; // rate is the synthDef "rate" arg, value is a proxy !
105         index { ^value.index }
106         isEmpty {
107                 ^value.isNil and: { rate.isNil }
108         }
110         map { arg proxy;
111                 value = proxy;
112                 isMapped = true;
113                 // stays neutral if both mappedRate, busNumChannels are nil
114                 if(proxy.isNeutral) { proxy.initBus(mappedRate, busNumChannels) }; // could add a test
115                 busNumChannels = proxy.numChannels;
116                 busNumChannels !? { isMultiChannel = busNumChannels > 1 };
117                 mappedRate = proxy.rate; // here we determine the rate simply from the input proxy
118         }
120         isNeutral { ^value.isNeutral }
121         storeArgs { ^[value, busNumChannels, rate] }
122         updateBusNumChannels {
123                 if(isMultiChannel and: { busNumChannels.isNil }) {
124                         busNumChannels = value.numChannels;
125                 };
126         }