File: Bus.sc

package info (click to toggle)
supercollider 1%3A3.6.6~repack-2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 23,792 kB
  • ctags: 25,269
  • sloc: cpp: 177,129; lisp: 63,421; ansic: 11,297; python: 1,787; perl: 766; yacc: 311; sh: 286; lex: 181; ruby: 173; makefile: 168; xml: 13
file content (268 lines) | stat: -rw-r--r-- 6,849 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
Bus {

	var <rate,<index,<numChannels,<server;
	var mapSymbol;

	*control { arg server,numChannels=1;
		var alloc;
		server = server ? Server.default;
		alloc = server.controlBusAllocator.alloc(numChannels);
		if(alloc.isNil,{
			error("Meta_Bus:control: failed to get a control bus allocated."
				+ "numChannels:" + numChannels + "server:" + server.name);
			^nil
		});
		^this.new(\control,alloc,numChannels,server)
	}
	*audio { arg server,numChannels=1;
		var alloc;
		server = server ? Server.default;
		alloc = server.audioBusAllocator.alloc(numChannels);
		if(alloc.isNil,{
			error("Meta_Bus:audio: failed to get an audio bus allocated."
			+ "numChannels:" + numChannels + "server:" + server.name);
			^nil
		});
		^this.new(\audio,alloc,numChannels,server)
	}
	*alloc { arg rate,server,numChannels=1;
		^this.perform(rate ? \audio,server,numChannels);
	}

	*new { arg rate=\audio,index=0,numChannels=2,server;
		^super.newCopyArgs(rate,index,numChannels,server ? Server.default)
	}

	isSettable {
		^rate != \audio
	}

	set { arg ... values; // shouldn't be larger than this.numChannels
		if(this.isSettable, {
			server.sendBundle(nil,(["/c_set"]
				++ values.collect({ arg v,i; [index + i ,v] }).flat));
		}, {error("Cannot set an audio rate bus")});
	}
	setMsg { arg ... values;
		if(this.isSettable, {
			^["/c_set"]
				++ values.collect({ arg v,i; [index + i ,v] }).flat
		}, {error("Cannot set an audio rate bus"); ^nil});
	}

	setn { arg values;
		// could throw an error if values.size > numChannels
		if(this.isSettable, {
			server.sendBundle(nil,
				["/c_setn",index,values.size] ++ values);
		}, {error("Cannot set an audio rate bus")});
	}
	setnMsg { arg values;
		if(this.isSettable, {
			^["/c_setn",index,values.size] ++ values;
		}, {error("Cannot set an audio rate bus"); ^nil});
	}
	setAt { |offset ... values|
		// shouldn't be larger than this.numChannels - offset
		if(this.isSettable, {
			server.sendBundle(nil,(["/c_set"]
				++ values.collect({ arg v,i; [index + offset + i ,v] }).flat));
		}, {error("Cannot set an audio rate bus")});
	}
	setnAt { |offset, values|
		// could throw an error if values.size > numChannels
		if(this.isSettable, {
			server.sendBundle(nil,
				["/c_setn",index + offset, values.size] ++ values);
		}, {error("Cannot set an audio rate bus")});
	}
	setPairs { | ... pairs|
		if(this.isSettable, {
			server.sendBundle(nil,(["/c_set"]
				++ pairs.clump(2).collect({ arg pair; [pair[0] + index, pair[1]] }).flat));
		}, {error("Cannot set an audio rate bus")});
	}

	get { arg action;
		if(numChannels == 1) {
			action = action ? { |vals| "Bus % index: % value: %.\n".postf(rate, index, vals); };
			OSCpathResponder(server.addr,['/c_set',index], { arg time, r, msg;
				action.value(msg.at(2)); r.remove }).add;
			server.listSendMsg(["/c_get",index]);
		} {
			this.getn(numChannels, action)
		};
	}

	getn { arg count, action;
		action = action ? { |vals| "Bus % index: % values: %.\n".postf(rate, index, vals); };
		OSCpathResponder(server.addr,['/c_setn',index],{arg time, r, msg;
			action.value(msg.copyToEnd(3)); r.remove } ).add;
		server.listSendMsg(this.getnMsg(count));
	}
	getMsg {
		^["/c_get",index];
	}
	getnMsg { arg count;
		^["/c_getn",index, count ? numChannels];
	}

	getSynchronous {
		if (not(this.isSettable)) {
			Error("Bus-getSynchronous only works for control-rate busses").throw;
		} {
			^server.getControlBusValue(index);
		}
	}

	getnSynchronous {|count|
		if (not(this.isSettable)) {
			Error("Bus-getnSynchronous only works for control-rate busses").throw;
		} {
			^server.getControlBusValues(index, count ? numChannels);
		}
	}

	setSynchronous { |... values|
		if (not(this.isSettable)) {
			Error("Bus-setSynchronous only works for control-rate busses").throw;
		} {
			if (values.size == 1) {
				server.setControlBusValue(index, values[0])
			} {
				server.setControlBusValues(index, values)
			}
		}
	}

	setnSynchronous {|values|
		if (not(this.isSettable)) {
			Error("Bus-setnSynchronous only works for control-rate busses").throw;
		} {
			server.setControlBusValues(index, values)
		}
	}

	fill { arg value,numChans;
		// could throw an error if numChans > numChannels
		server.sendBundle(nil,
			["/c_fill",index,numChans,value]);
	}

	fillMsg { arg value;
		^["/c_fill",index,numChannels,value];
	}


	free {
		if(index.isNil,{ (this.asString + " has already been freed").warn; ^this });
		if(rate == \audio,{
			server.audioBusAllocator.free(index);
		},{
			server.controlBusAllocator.free(index);
		});
		index = nil;
		numChannels = nil;
		mapSymbol = nil;
	}

	// allow reallocation

	alloc {
		if(rate === 'audio', {
			index = server.audioBusAllocator.alloc(numChannels);
		}, {
			index = server.controlBusAllocator.alloc(numChannels);
		});
		mapSymbol = nil;
	}

	realloc {
		var r, n;
		if(index.notNil, {
			r = rate; n = numChannels;
			this.free;
			rate = r; numChannels = n;
			this.alloc;
		})
	}

	// alternate syntaxes
	setAll { arg value;
		this.fill(value,numChannels);
	}

	value_ { arg value;
		this.fill(value,numChannels);
	}

	printOn { arg stream;
		stream << this.class.name << "(" <<*
			[rate, index, numChannels, server]  <<")"
	}

	storeOn { arg stream;
		stream << this.class.name << "(" <<*
			[rate.asCompileString, index, numChannels, server.asCompileString]  <<")"
	}
	== { arg aBus;
		^this.compareObject(aBus, #[\index, \numChannels, \rate, \server])
	}
	hash {
		^this.instVarHash(#[\index, \numChannels, \rate, \server])
	}

	isAudioOut { // audio interface
		^(rate === \audio and: {index < server.options.firstPrivateBus})
	}

	ar { |numChannels=(this.numChannels), offset=0|
		if(rate == \audio,{
			^In.ar(index + offset, numChannels)
		},{
			//"Bus converting control to audio rate".inform;
			^K2A.ar( In.kr(index + offset, numChannels) )
		})
	}
	kr { |numChannels=(this.numChannels), offset=0|
		if(rate == \audio,{
			^A2K.kr( In.ar(index + offset, numChannels) )
		},{
			^In.kr(index + offset, numChannels)
		})
	}

	play { arg target=0, outbus, fadeTime, addAction=\addToTail;
		if(this.isAudioOut.not,{ // returns a Synth
			^{ this.ar }.play(target, outbus, fadeTime, addAction);
		});
	}

	asUGenInput { ^this.index }
	asControlInput { ^this.index }

	asMap {
		^mapSymbol ?? {
			if(index.isNil) { MethodError("bus not allocated.", this).throw };
			mapSymbol = if(rate == \control) { "c" } { "a" };
			mapSymbol = (mapSymbol ++ index).asSymbol;
		}
	}

	// added by nescivi; gets a subbus from another bus

	*newFrom { |bus, offset, numChannels=1|
		if ( offset > bus.numChannels or: {numChannels + offset >
			bus.numChannels} )
		{
			Error( "Bus:newFrom tried to reach outside the channel range of
%".format( bus )).throw
		};
		^Bus.new( bus.rate, bus.index + offset, numChannels);
	}

	subBus{|offset, numChannels=1|
		^Bus.newFrom( this, offset, numChannels );
	}

}