aboutsummaryrefslogtreecommitdiff
path: root/sc4pd/headers/plugin_interface/SC_InlineUnaryOp.h
blob: 9ea2e0b52b4fc8153e62d6b27dc638b7d3dd3642 (plain)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
	SuperCollider real time audio synthesis system
    Copyright (c) 2002 James McCartney. All rights reserved.
	http://www.audiosynth.com

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#ifndef _UnaryOpUGen_
#define _UnaryOpUGen_

#include "SC_Types.h"
#include "SC_Constants.h"

///////////////////////////////////////////////////////////////////////////////////////

inline bool sc_isnan(float x)
{
	return (!(x >= 0.f || x <= 0.f));
}

///////////////////////////////////////////////////////////////////////////////////////

// versions provided for float32 and float64
// did not supply template because do not want to instantiate for integers.
// all constants explicitly cast to prevent PowerPC frsp instruction generation.

///////////////////////////////////////////////////////////////////////////////////////

// this is a function for preventing pathological math operations in ugens.
// can be used at the end of a block to fix any recirculating filter values.
inline float32 zapgremlins(float32 x)
{
	float32 absx = fabs(x);
	// very small numbers fail the first test, eliminating denormalized numbers
	//    (zero also fails the first test, but that is OK since it returns zero.)
	// very large numbers fail the second test, eliminating infinities
	// Not-a-Numbers fail both tests and are eliminated.
	return (absx > (float32)1e-15 && absx < (float32)1e15) ? x : (float32)0.;
}

inline float32 sc_log2(float32 x)
{
	return log(fabs(x)) * rlog2;
}

inline float32 sc_log10(float32 x)
{
	return log10(fabs(x));
}

inline float32 sc_midicps(float32 note)
{
	return (float32)440. * pow((float32)2., (note - (float32)69.) * (float32)0.083333333333);
}

inline float32 sc_cpsmidi(float32 freq)
{
	return sc_log2(freq * (float32)0.0022727272727) * (float32)12. + (float32)69.;
}

inline float32 sc_midiratio(float32 midi)
{
	return pow((float32)2. , midi * (float32)0.083333333333);
}

inline float32 sc_ratiomidi(float32 ratio)
{
	return (float32)12. * sc_log2(ratio);
}

inline float32 sc_octcps(float32 note)
{
	return (float32)440. * pow((float32)2., note - (float32)4.75);
}

inline float32 sc_cpsoct(float32 freq)
{
	return sc_log2(freq * (float32)0.0022727272727) + (float32)4.75;
}

inline float32 sc_ampdb(float32 amp)
{
	return log10(amp) * (float32)20.;
}

inline float32 sc_dbamp(float32 db)
{
	return pow((float32)10., db * (float32).05);
}

inline float32 sc_squared(float32 x)
{
	return x * x;
}

inline float32 sc_cubed(float32 x)
{
	return x * x * x;
}

inline float32 sc_sqrt(float32 x)
{
	return x < (float32)0. ? -sqrt(-x) : sqrt(x);
}


inline float32 sc_hanwindow(float32 x)
{
	if (x < (float32)0. || x > (float32)1.) return (float32)0.;
	return (float32)0.5 - (float32)0.5 * cos(x * twopi);
}

inline float32 sc_welwindow(float32 x)
{
	if (x < (float32)0. || x > (float32)1.) return (float32)0.;
	return sin(x * pi);
}

inline float32 sc_triwindow(float32 x)
{
	if (x < (float32)0. || x > (float32)1.) return (float32)0.;
	if (x < (float32)0.5) return (float32)2. * x;
	else return (float32)-2. * x + (float32)2.;
}

inline float32 sc_bitriwindow(float32 x)
{
	float32 ax = (float32)1. - fabs(x);
	if (ax <= (float32)0.) return (float32)0.;
	return ax;
}

inline float32 sc_rectwindow(float32 x)
{
	if (x < (float32)0. || x > (float32)1.) return (float32)0.;
	return (float32)1.;
}

inline float32 sc_scurve(float32 x)
{
	if (x <= (float32)0.) return (float32)0.;
	if (x >= (float32)1.) return (float32)1.;
	return x * x * ((float32)3. - (float32)2. * x);
}

inline float32 sc_scurve0(float32 x)
{
	// assumes that x is in range
	return x * x * ((float32)3. - (float32)2. * x);
}

inline float32 sc_ramp(float32 x)
{
	if (x <= (float32)0.) return (float32)0.;
	if (x >= (float32)1.) return (float32)1.;
	return x;
}

inline float32 sc_distort(float32 x)
{
	return x / ((float32)1. + fabs(x));
}

inline float32 sc_softclip(float32 x)
{
	float32 absx = fabs(x);
	if (absx <= (float32)0.5) return x;
	else return (absx - (float32)0.25) / x;
}

// Taylor expansion out to x**9/9! factored  into multiply-adds
// from Phil Burk.
inline float32 taylorsin(float32 x)
{
	// valid range from -pi/2 to +3pi/2
	x = pi2 - fabs(pi2 - x);
	float32 x2 = x * x;
  	return x*(x2*(x2*(x2*(x2*(1.0/362880.0) 
  			- (1.0/5040.0))
            + (1.0/120.0))
            - (1.0/6.0))
            + 1.0);
}

inline float32 sc_trunc(float32 x)
{
	// truncFloat is a number which causes a loss of precision of 
	// the fractional part.
	// NOTE: this will only work if the FPU is set to round downward.
	// That is NOT the default rounding mode. SC sets it to this mode.
	float32 tmp1 = x + truncFloat;
	float32 tmp2 = tmp1 - truncFloat;
	return tmp2;
}

inline float32 sc_frac(float32 x)
{
	return x - sc_trunc(x);
}

inline float32 sc_lg3interp(float32 x1, float32 a, float32 b, float32 c, float32 d)
{
	// cubic lagrange interpolator
	float32 x0 = x1 + 1.f;
	float32 x2 = x1 - 1.f;
	float32 x3 = x1 - 2.f;
	
	float32 x03 = x0 * x3 * 0.5f;
	float32 x12 = x1 * x2 * 0.16666666666666667f;
	
	return x12 * (d * x0 - a * x3) + x03 * (b * x2 - c * x1);
}

inline float32 sc_CalcFeedback(float32 delaytime, float32 decaytime)
{
	if (delaytime == 0.f) {
		return 0.f;
	} else if (decaytime > 0.f) {
		return exp(log001 * delaytime / decaytime);
	} else if (decaytime < 0.f) {
		return -exp(log001 * delaytime / -decaytime);
	} else {
		return 0.f;
	}
}

inline float32 sc_wrap1(float32 x)
{
	if (x >= (float32) 1.) return x + (float32)-2.;
	if (x <  (float32)-1.) return x + (float32) 2.;
	return x;
}

inline float32 sc_fold1(float32 x)
{
	if (x >= (float32) 1.) return (float32) 2. - x;
	if (x <  (float32)-1.) return (float32)-2. - x;
	return x;
}


///////////////////////////////////////////////////////////////////////////////////////

inline float64 zapgremlins(float64 x)
{
	float64 absx = fabs(x);
	// very small numbers fail the first test, eliminating denormalized numbers
	//    (zero also fails the first test, but that is OK since it returns zero.)
	// very large numbers fail the second test, eliminating infinities
	// Not-a-Numbers fail both tests and are eliminated.
	return (absx > (float64)1e-15 && absx < (float64)1e15) ? x : (float64)0.;
}

inline float64 sc_log2(float64 x)
{
	return log(fabs(x)) * rlog2;
}

inline float64 sc_log10(float64 x)
{
	return log10(fabs(x));
}

inline float64 sc_midicps(float64 note)
{
	return (float64)440. * pow((float64)2., (note - (float64)69.) * (float64)0.083333333333);
}

inline float64 sc_cpsmidi(float64 freq)
{
	return sc_log2(freq * (float64)0.0022727272727) * (float64)12. + (float64)69.;
}

inline float64 sc_midiratio(float64 midi)
{
	return pow((float64)2. , midi * (float64)0.083333333333);
}

inline float64 sc_ratiomidi(float64 ratio)
{
	return (float64)12. * sc_log2(ratio);
}

inline float64 sc_octcps(float64 note)
{
	return (float64)440. * pow((float64)2., note - (float64)4.75);
}

inline float64 sc_cpsoct(float64 freq)
{
	return sc_log2(freq * (float64)0.0022727272727) + (float64)4.75;
}

inline float64 sc_ampdb(float64 amp)
{
	return log10(amp) * (float64)20.;
}

inline float64 sc_dbamp(float64 db)
{
	return pow((float64)10., db * (float64).05);
}

inline float64 sc_squared(float64 x)
{
	return x * x;
}

inline float64 sc_cubed(float64 x)
{
	return x * x * x;
}

inline float64 sc_sqrt(float64 x)
{
	return x < (float64)0. ? -sqrt(-x) : sqrt(x);
}

inline float64 sc_hanwindow(float64 x)
{
	if (x < (float64)0. || x > (float64)1.) return (float64)0.;
	return (float64)0.5 - (float64)0.5 * cos(x * twopi);
}

inline float64 sc_welwindow(float64 x)
{
	if (x < (float64)0. || x > (float64)1.) return (float64)0.;
	return sin(x * pi);
}

inline float64 sc_triwindow(float64 x)
{
	if (x < (float64)0. || x > (float64)1.) return (float64)0.;
	if (x < (float64)0.5) return (float64)2. * x;
	else return (float64)-2. * x + (float64)2.;
}

inline float64 sc_bitriwindow(float64 x)
{
	float64 ax = fabs(x);
	if (ax > (float64)1.) return (float64)0.;
	return (float64)1. - ax;
}

inline float64 sc_rectwindow(float64 x)
{
	if (x < (float64)0. || x > (float64)1.) return (float64)0.;
	return (float64)1.;
}

inline float64 sc_scurve(float64 x)
{
	if (x <= (float64)0.) return (float64)0.;
	if (x >= (float64)1.) return (float64)1.;
	return x * x * ((float64)3. - (float64)2. * x);
}

inline float64 sc_scurve0(float64 x)
{
	// assumes that x is in range
	return x * x * ((float64)3. - (float64)2. * x);
}

inline float64 sc_ramp(float64 x)
{
	if (x <= (float64)0.) return (float64)0.;
	if (x >= (float64)1.) return (float64)1.;
	return x;
}

inline float64 sc_distort(float64 x)
{
	return x / ((float64)1. + fabs(x));
}

inline float64 sc_softclip(float64 x)
{
	float64 absx = fabs(x);
	if (absx <= (float64)0.5) return x;
	else return (absx - (float64)0.25) / x;
}

// Taylor expansion out to x**9/9! factored  into multiply-adds
// from Phil Burk.
inline float64 taylorsin(float64 x)
{
	x = pi2 - fabs(pi2 - x);
	float64 x2 = x * x;
  	return x*(x2*(x2*(x2*(x2*(1.0/362880.0) 
  			- (1.0/5040.0))
            + (1.0/120.0))
            - (1.0/6.0))
            + 1.0);
}

inline float64 sc_trunc(float64 x)
{
	// truncDouble is a number which causes a loss of precision of 
	// the fractional part.
	// NOTE: this will only work if the FPU is set to round downward.
	// That is NOT the default rounding mode. SC sets it to this mode.
	float64 tmp1 = x + truncDouble;
	float64 tmp2 = tmp1 - truncDouble;
	return tmp2;
}

inline float64 sc_frac(float64 x)
{
	return x - sc_trunc(x);
}

inline float64 sc_wrap1(float64 x)
{
	if (x >= (float64) 1.) return x + (float64)-2.;
	if (x <  (float64)-1.) return x + (float64) 2.;
	return x;
}

inline float64 sc_fold1(float64 x)
{
	if (x >= (float64) 1.) return (float64) 2. - x;
	if (x <  (float64)-1.) return (float64)-2. - x;
	return x;
}

inline int32 sc_grayCode(int32 x)
{
	return x ^ (x >> 1);
}



///////////////////////////////////////////////////////////////////////////////////////

#endif