class library: Volume - remove debug message
[supercollider.git] / SCClassLibrary / Common / Math / Signal.sc
blob624ad62c9df84ebe878b1c73e136efd03a865dfd
1 Signal[float] : FloatArray {
3         *sineFill { arg size, amplitudes, phases;
4                 ^Signal.newClear(size).sineFill(amplitudes, phases).normalize
5         }
6         *chebyFill { arg size, amplitudes, normalize=true;
7                 ^Signal.newClear(size).chebyFill(amplitudes, normalize); //.normalizeTransfer //shouldn't normalize by default!
8         }
9         *hammingWindow { arg size, pad=0;
10                 if (pad == 0, {
11                         ^this.newClear(size).fill(0.5).addSine(1, 0.39, -0.5pi);
12                 },{
13                         ^this.newClear(size-pad).fill(0.5).addSine(1, 0.39, -0.5pi) ++ this.newClear(pad);
14                 });
15         }
16         *hanningWindow { arg size, pad=0;
17                 if (pad == 0, {
18                         ^this.newClear(size).fill(0.5).addSine(1, 0.5, -0.5pi);
19                 },{
20                         ^this.newClear(size-pad).fill(0.5).addSine(1, 0.5, -0.5pi) ++ this.newClear(pad);
21                 });
22         }
23         *welchWindow { arg size, pad=0;
24                 if (pad == 0, {
25                         ^this.newClear(size).addSine(0.5, 1, 0);
26                 },{
27                         ^this.newClear(size-pad).addSine(0.5, 1, 0) ++ this.newClear(pad);
28                 });
29         }
30         *rectWindow { arg size, pad=0;
31                 if (pad == 0, {
32                         ^this.newClear(size).fill(1.0);
33                 },{
34                         ^this.newClear(size-pad).fill(1.0) ++ this.newClear(pad);
35                 });
36         }
37         *readNew { arg file;
38                 ^file.readAllSignal;
39         }
41         // operations
42         fill { arg val; _SignalFill ^this.primitiveFailed }
43         scale { arg scale; _SignalScale ^this.primitiveFailed }
44         offset { arg offset; _SignalOffset ^this.primitiveFailed }
46         asWavetable {
47                 // Interpolating oscillators require wavetables in a special format.
48                 // This method returns a wavetable in that format.
49                 _SignalAsWavetable;
50                 ^this.primitiveFailed
51         }
53         asWavetableNoWrap {
54                 // Shaper requires wavetables without wrap.
55                 // This method returns a wavetable in that format.
56                 //To generate size N wavetable need N/2+1 signal values rather than N/2
57                 //because Buffer's add_wchebyshev calculates N/2+1 values whilst
58                 //Signal's _SignalAddChebyshev calculates N/2!
60                 var newsig = Signal.newClear((this.size-1)*2);
61                 var next, cur;
63                 cur= this[0];
64                 (this.size-1).do{|i|
65                 var index= 2*i;
66                 next= this[i+1];
68                 newsig[index]= 2*cur -next;
69                 newsig[index+1]= next-cur;
70                 cur=next;
72                 };
74                 ^newsig
75         }
77         peak { _SignalPeak; ^this.primitiveFailed }
79         normalize { arg beginSamp=0, endSamp;
80                 _SignalNormalize;
81                 ^this.primitiveFailed
82         }
83         normalizeTransfer {
84                 _SignalNormalizeTransferFn;
85                 ^this.primitiveFailed
86         }
88         invert { arg beginSamp=0, endSamp;
89                 _SignalInvert;
90                 ^this.primitiveFailed
91         }
92         reverse { arg beginSamp=0, endSamp;
93                 _SignalReverse;
94                 ^this.primitiveFailed
95         }
96         fade { arg beginSamp=0, endSamp, beginLevel=0.0, endLevel=1.0;
97                 _SignalFade;
98                 ^this.primitiveFailed
99         }
100         rotate { arg n=1;
101                 _SignalRotate
102                 ^this.primitiveFailed
103         }
105         zeroPad { arg minSize;
106                 var size = max(minSize ? 0, this.size).nextPowerOfTwo;
107                 ^this ++ Signal.newClear(size - this.size);
108         }
110         integral { _SignalIntegral; ^this.primitiveFailed }
111         overDub { arg aSignal, index=0;
112                 _SignalOverDub
113                 // add a signal to myself starting at the index
114                 // if the other signal is too long only the first part is overdubbed
115                 ^this.primitiveFailed
116         }
117         overWrite { arg aSignal, index=0;
118                 _SignalOverWrite
119                 // write a signal to myself starting at the index
120                 // if the other signal is too long only the first part is overwritten
121                 ^this.primitiveFailed
122         }
124         play { arg loop=false, mul=0.2, numChannels=1, server;
125                 var buf;
126                 buf = Buffer.sendCollection(server ? Server.default, this, numChannels, -1, { buf.play(loop, mul); });
127                 ^buf
128         }
130         waveFill { arg function, start = 0.0, end = 1.0;
131                 var i = 0, step, size, val, x;
133                 // evaluate a function for every sample over the interval from
134                 // start to end.
135                 size = this.size;
136                 if (size <= 0, { ^this });
138                 x = start;
139                 step = (end - start) / size;
140                 while ({ i < size }, {
141                         val = function.value(x, this.at(i), i);
142                         this.put(i, val);
143                         x = x + step;
144                         i = i + 1;
145                 });
146                 ^this
147         }
148         addSine { arg harmonicNumber = 1, amplitude = 1.0, phase = 0.0;
149                 _SignalAddHarmonic
150                 ^this.primitiveFailed
151         }
152         sineFill { arg amplitudes, phases;
153                 this.fill(0.0);
154                 if (phases.isNil, { phases = #[0]; });
155                 amplitudes.do({ arg amp, i; this.addSine(i+1, amp, phases @@ i) });
156         }
157         sineFill2 { arg list;
158                 this.fill(0.0);
159                 list.do({ arg item, i;
160                         var harm, amp, phase;
161                         # harm, amp, phase = item;
162                         this.addSine(harm, amp ? 1.0, phase ? 0.0);
163                 });
164         }
166         addChebyshev { arg harmonicNumber = 1, amplitude = 1.0;
167                 _SignalAddChebyshev
168                 ^this.primitiveFailed
169         }
170         chebyFill { arg amplitudes, normalize=true;
171                 this.fill(0.0);
172                 amplitudes.do({ arg amp, i; this.addChebyshev(i+1, amp); if(i%4==1,{this.offset(1)}); if(i%4==3,{this.offset(-1)}); }); //corrections for JMC DC offsets, as per Buffer:cheby
174                 if(normalize,{this.normalizeTransfer}); //no automatic cheby
175         }
177         //old version
178         chebyFill_old { arg amplitudes;
179                 this.fill(0.0);
180                 amplitudes.do({ arg amp, i; this.addChebyshev(i+1, amp) });
181                 this.normalizeTransfer
182         }
185         *fftCosTable { arg fftsize;
186                 ^this.newClear((fftsize/4) + 1).fftCosTable
187         }
188         fftCosTable {
189                 var harm;
190                 harm = this.size / ((this.size - 1) * 4);
191                 this.addSine(harm, 1, 0.5pi);
192         }
194         fft { arg imag, cosTable;
195                 // argCosTable must contain 1/4 cycle of a cosine (use fftCosTable)
196                 // fftsize is the next greater power of two than the receiver's length
197                 _Signal_FFT
198                 ^this.primitiveFailed
199         }
200         ifft { arg imag, cosTable;
201                 // argCosTable must contain 1/4 cycle of a cosine (use fftCosTable)
202                 // fftsize is the next greater power of two than the receiver's length
203                 _Signal_IFFT
204                 ^this.primitiveFailed
205         }
207         neg { _Neg; ^this.primitiveFailed }
208         abs { _Abs; ^this.primitiveFailed }
209         sign { _Sign; ^this.primitiveFailed }
210         squared { _Squared; ^this.primitiveFailed }
211         cubed { _Cubed; ^this.primitiveFailed }
212         sqrt { _Sqrt; ^this.primitiveFailed }
213         exp { _Exp; ^this.primitiveFailed }
214         //reciprocal { _Recip; ^this.primitiveFailed }
215         //midicps { _MIDICPS; ^this.primitiveFailed }
216         //cpsmidi { _CPSMIDI; ^this.primitiveFailed }
217         //midiratio { _MIDIRatio; ^this.primitiveFailed }
218         //ratiomidi { _RatioMIDI; ^this.primitiveFailed }
219         //ampdb { _AmpDb; ^this.primitiveFailed }
220         //dbamp { _DbAmp; ^this.primitiveFailed }
221         //octcps { _OctCPS; ^this.primitiveFailed }
222         //cpsoct { _CPSOct; ^this.primitiveFailed }
223         log { _Log; ^this.primitiveFailed }
224         log2 { _Log2; ^this.primitiveFailed }
225         log10 { _Log10; ^this.primitiveFailed }
226         sin { _Sin; ^this.primitiveFailed }
227         cos { _Cos; ^this.primitiveFailed }
228         tan { _Tan; ^this.primitiveFailed }
229         asin { _ArcSin; ^this.primitiveFailed }
230         acos { _ArcCos; ^this.primitiveFailed }
231         atan { _ArcTan; ^this.primitiveFailed }
232         sinh { _SinH; ^this.primitiveFailed }
233         cosh { _CosH; ^this.primitiveFailed }
234         tanh { _TanH; ^this.primitiveFailed }
235         distort { _Distort; ^this.primitiveFailed }
236         softclip { _SoftClip; ^this.primitiveFailed }
238         rectWindow { _RectWindow; ^this.primitiveFailed }
239         hanWindow { _HanWindow; ^this.primitiveFailed }
240         welWindow { _WelchWindow; ^this.primitiveFailed }
241         triWindow { _TriWindow; ^this.primitiveFailed }
243         scurve { _SCurve; ^this.primitiveFailed }
244         ramp { _Ramp; ^this.primitiveFailed }
246         + { arg aNumber; _Add; ^aNumber.performBinaryOpOnSignal('+', this) }
247         - { arg aNumber; _Sub; ^aNumber.performBinaryOpOnSignal('-', this) }
248         * { arg aNumber; _Mul; ^aNumber.performBinaryOpOnSignal('*', this) }
249         / { arg aNumber; _FDiv; ^aNumber.performBinaryOpOnSignal('/', this) }
250         mod { arg aNumber; _Mod; ^aNumber.performBinaryOpOnSignal('mod', this) }
251         div { arg aNumber; _IDiv; ^aNumber.performBinaryOpOnSignal('div', this) }
252         pow { arg aNumber; _Pow; ^aNumber.performBinaryOpOnSignal('pow', this) }
253         min { arg aNumber; _Min; ^aNumber.performBinaryOpOnSignal('min', this) }
254         max { arg aNumber; _Max; ^aNumber.performBinaryOpOnSignal('max', this) }
255         ring1 { arg aNumber; _Ring1; ^aNumber.performBinaryOpOnSignal('ring1', this) }
256         ring2 { arg aNumber; _Ring2; ^aNumber.performBinaryOpOnSignal('ring2', this) }
257         ring3 { arg aNumber; _Ring3; ^aNumber.performBinaryOpOnSignal('ring3', this) }
258         ring4 { arg aNumber; _Ring4; ^aNumber.performBinaryOpOnSignal('ring4', this) }
259         difsqr { arg aNumber; _DifSqr; ^aNumber.performBinaryOpOnSignal('difsqr', this) }
260         sumsqr { arg aNumber; _SumSqr; ^aNumber.performBinaryOpOnSignal('sumsqr', this) }
261         sqrsum { arg aNumber; _SqrSum; ^aNumber.performBinaryOpOnSignal('sqrsum', this) }
262         sqrdif { arg aNumber; _SqrDif; ^aNumber.performBinaryOpOnSignal('sqrdif', this) }
263         absdif { arg aNumber; _AbsDif; ^aNumber.performBinaryOpOnSignal('absdif', this) }
264         thresh { arg aNumber; _Thresh; ^aNumber.performBinaryOpOnSignal('thresh', this) }
265         amclip { arg aNumber; _AMClip; ^aNumber.performBinaryOpOnSignal('amclip', this) }
266         scaleneg { arg aNumber; _ScaleNeg; ^aNumber.performBinaryOpOnSignal('scaleneg', this) }
267         clip2 { arg aNumber=1; _Clip2; ^aNumber.performBinaryOpOnSignal('clip2', this) }
268         fold2 { arg aNumber; _Fold2; ^aNumber.performBinaryOpOnSignal('fold2', this) }
269         wrap2 { arg aNumber; _Wrap2; ^aNumber.performBinaryOpOnSignal('wrap2', this) }
270         excess { arg aNumber; _Excess; ^aNumber.performBinaryOpOnSignal('excess', this) }
271         firstArg { arg aNumber; _FirstArg; ^aNumber.performBinaryOpOnSignal('firstArg', this) }
273         == { arg aNumber; _EQ; ^aNumber.performBinaryOpOnSignal('==', this) }
274         != { arg aNumber; _NE; ^aNumber.performBinaryOpOnSignal('!=', this) }
276         clip { arg lo, hi; _ClipSignal; ^this.primitiveFailed }
277         wrap { arg lo, hi; _WrapSignal; ^this.primitiveFailed }
278         fold { arg lo, hi; _FoldSignal; ^this.primitiveFailed }
280         asInteger { _AsInt; ^this.primitiveFailed }
281         asFloat { _AsFloat; ^this.primitiveFailed }
282         asComplex { ^Complex.new(this, 0.0) }
283         asSignal { ^this }
285         // complex support
286         real { ^this }
287         imag { ^0.0 }
289         //PRIVATE:
290         performBinaryOpOnSignal { arg aSelector, aNumber, adverb;
291                 BinaryOpFailureError(this, aSelector, [aNumber, adverb]).throw;
292         }
295 Wavetable[float] : FloatArray {
296         // the only way to make a Wavetable is by Signal::asWavetable
297         *new {
298                 ^this.shouldNotImplement(thisMethod)
299         }
300         *newClear {
301                 ^this.shouldNotImplement(thisMethod)
302         }
304         *sineFill { arg size, amplitudes, phases;
305                 ^Signal.sineFill(size, amplitudes, phases).asWavetable
306         }
308         //size must be N/2+1 for N power of two; N is eventual size of wavetable
309         *chebyFill { arg size, amplitudes, normalize=true;
311                 ^Signal.chebyFill(size, amplitudes, normalize).asWavetableNoWrap; //asWavetable causes wrap here, problem
312         }
314         *chebyFill_old { arg size, amplitudes;
316                 //this.deprecated(thisMethod, Buffer.findRespondingMethodFor(\cheby));
318                 ^Signal.chebyFill(size, amplitudes).asWavetable; //asWavetable causes wrap here, problem
319         }
321         asSignal {
322                 _WavetableAsSignal
323                 ^this.primitiveFailed
324         }
326         blend { arg anotherWavetable, blendFrac=0.5;
327                 ^this.asSignal.blend(anotherWavetable.asSignal, blendFrac).asWavetable;
328         }
330         *readNew { arg file;
331                 ^file.readAllSignal.asWavetable;
332         }
333         write { arg path;
334                 var file;
335                 file = File.new(path, "wb");
336                 if (file.notNil, {
337                         file.write(this.asSignal);
338                         file.close;
339                 });
340         }
341         //libMenuAction { arg names;
342         //      this.plot(names.last);
343         //}