Merge pull request #506 from andrewcsmith/patch-2
[supercollider.git] / SCClassLibrary / Common / Audio / BufIO.sc
blobff9f24dd2a625ed493648bcf6b1a59b9ab2c7d41
1 /*
2         PlayBuf - sample player
3 */
5 PlayBuf : MultiOutUGen {
6         *ar { arg numChannels, bufnum=0, rate=1.0, trigger=1.0, startPos=0.0, loop = 0.0, doneAction=0;
7                 ^this.multiNew('audio', numChannels, bufnum, rate, trigger, startPos, loop, doneAction)
8         }
10         *kr { arg numChannels, bufnum=0, rate=1.0, trigger=1.0, startPos=0.0, loop = 0.0, doneAction=0;
11                 ^this.multiNew('control', numChannels, bufnum, rate, trigger, startPos, loop, doneAction)
12         }
14         init { arg argNumChannels ... theInputs;
15                 inputs = theInputs;
16                 ^this.initOutputs(argNumChannels, rate);
17         }
18         argNamesInputsOffset { ^2 }
21 TGrains : MultiOutUGen {
22         *ar { arg numChannels, trigger=0, bufnum=0, rate=1, centerPos=0,
23                         dur=0.1, pan=0, amp=0.1, interp=4;
24                 if (numChannels < 2) {
25                          "TGrains needs at least two channels.".error;
26                          ^nil
27                 }
28                 ^this.multiNew('audio', numChannels, trigger, bufnum, rate, centerPos,
29                                 dur, pan, amp, interp)
30         }
31         init { arg argNumChannels ... theInputs;
32                 inputs = theInputs;
33                 ^this.initOutputs(argNumChannels, rate);
34         }
35         argNamesInputsOffset { ^2 }
40 // exception in GrafDef_Load: UGen 'SimpleLoopBuf' not installed.
42 SimpleLoopBuf : MultiOutUGen {
43         *ar { arg numChannels, bufnum=0, loopStart=0.0, loopEnd=99999.0, trigger=0.0;
44                 ^this.multiNew('audio', numChannels, bufnum, loopStart, loopEnd, trigger)
45         }
47         init { arg argNumChannels ... theInputs;
48                 inputs = theInputs;
49                 ^this.initOutputs(argNumChannels, rate);
50         }
54 BufRd : MultiOutUGen {
55         *ar { arg numChannels, bufnum=0, phase=0.0, loop=1.0, interpolation=2;
56                 ^this.multiNew('audio', numChannels, bufnum, phase, loop, interpolation)
57         }
58         *kr { arg numChannels, bufnum=0, phase=0.0, loop=1.0, interpolation=2;
59                 ^this.multiNew('control', numChannels, bufnum, phase, loop, interpolation)
60         }
62         init { arg argNumChannels ... theInputs;
63                 inputs = theInputs;
64                 ^this.initOutputs(argNumChannels, rate);
65         }
66         argNamesInputsOffset { ^2 }
67         checkInputs {
68                 if (rate == 'audio' and: {inputs.at(1).rate != 'audio'}, {
69                         ^("phase input is not audio rate: " + inputs.at(1) + inputs.at(1).rate);
70                 });
71                 ^this.checkValidInputs
72         }
75 BufWr : UGen {
76         *ar { arg inputArray, bufnum=0, phase=0.0, loop=1.0;
77                 ^this.multiNewList(['audio', bufnum, phase,
78                         loop] ++ inputArray.asArray)
80         }
81         *kr { arg inputArray, bufnum=0, phase=0.0, loop=1.0;
82                 ^this.multiNewList(['control', bufnum, phase,
83                         loop] ++ inputArray.asArray)
84         }
85         checkInputs {
86                 if (rate == 'audio' and: {inputs.at(1).rate != 'audio'}, {
87                         ^("phase input is not audio rate: " + inputs.at(1) + inputs.at(1).rate);
88                 });
89                 ^this.checkValidInputs
90         }
94 RecordBuf : UGen {
95         *ar { arg inputArray, bufnum=0, offset=0.0, recLevel=1.0, preLevel=0.0,
96                         run=1.0, loop=1.0, trigger=1.0, doneAction=0;
97                 ^this.multiNewList(
98                         ['audio', bufnum, offset, recLevel, preLevel, run, loop, trigger, doneAction ]
99                         ++ inputArray.asArray
100                 )
101         }
102         *kr { arg inputArray, bufnum=0, offset=0.0, recLevel=1.0, preLevel=0.0,
103                         run=1.0, loop=1.0, trigger=1.0, doneAction=0;
104                 ^this.multiNewList(
105                         ['control', bufnum, offset, recLevel, preLevel, run, loop, trigger, doneAction ]
106                         ++ inputArray.asArray
107                 )
108         }
112 ScopeOut : UGen {
113         *ar { arg inputArray , bufnum=0;
114                 this.multiNewList(['audio', bufnum] ++ inputArray.asArray);
115                 ^0.0
116         }
117         *kr { arg inputArray , bufnum=0;
118                 this.multiNewList(['control', bufnum] ++ inputArray.asArray);
119                 ^0.0
120         }
123 ScopeOut2 : UGen {
124         *ar { arg inputArray, scopeNum=0, maxFrames = 4096, scopeFrames;
125                 this.multiNewList(['audio', scopeNum, maxFrames, scopeFrames ? maxFrames] ++ inputArray.asArray);
126                 ^0.0
127         }
128         *kr { arg inputArray, scopeNum=0, maxFrames = 4096, scopeFrames;
129                 this.multiNewList(['control', scopeNum, maxFrames, scopeFrames ? maxFrames] ++ inputArray.asArray);
130                 ^0.0
131         }
134 Tap : UGen {
135         *ar { arg bufnum = 0, numChannels = 1, delaytime = 0.2;
136                 var n;
137                 n = delaytime * SampleRate.ir.neg; // this depends on the session sample rate, not buffer.
138                 ^PlayBuf.ar(numChannels, bufnum, 1, 0, n, 1);
139         }
142 LocalBuf : WidthFirstUGen {
144         *new { arg numFrames = 1, numChannels = 1;
145                 ^this.multiNew('scalar', numChannels, numFrames)
146         }
148         *new1 { arg rate ... args;
149                 var maxLocalBufs = UGen.buildSynthDef.maxLocalBufs;
150                 if(maxLocalBufs.isNil) {
151                         maxLocalBufs = MaxLocalBufs.new;
152                         UGen.buildSynthDef.maxLocalBufs = maxLocalBufs;
153                 };
154                 maxLocalBufs.increment;
155                 ^super.new.rate_(rate).addToSynth.init( *args ++ maxLocalBufs )
156         }
158         *newFrom { arg list;
159                 var shape, buf;
160                 shape = list.shape;
161                 if(shape.size == 1) { shape = [1, list.size] };
162                 if(shape.size > 2) { Error("LocalBuf: list has not the right shape").throw };
163                 buf = this.new(*shape.reverse);
164                 buf.set(list.flop.flat);
165                 ^buf
166         }
168         numFrames { ^inputs[1] }
169         numChannels { ^inputs[0] }
171         set { arg values, offset = 0;
172                 SetBuf(this, values.asArray, offset);
173         }
174         clear {
175                 ClearBuf(this);
176         }
180 MaxLocalBufs : UGen {
181         *new {
182                 ^this.multiNew('scalar', 0);
183         }
184         increment {
185                 inputs[0] = inputs[0] + 1;
186         }
189 SetBuf : WidthFirstUGen {
190         *new { arg buf, values, offset = 0;
191                 ^this.multiNewList(['scalar', buf, offset, values.size] ++ values)
192         }
195 ClearBuf : WidthFirstUGen {
196         *new { arg buf;
197                 ^this.multiNew('scalar', buf)
198         }