-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathc.go
637 lines (550 loc) · 13.3 KB
/
c.go
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
package sc
import (
"math"
"math/rand"
)
// C wraps a float32 and implements the Input interface.
type C float32
// Abs computes the absolute value of a signal.
func (c C) Abs() Input {
return C(float32(math.Abs(float64(c))))
}
// Absdif returns the absolute value of the difference of two inputs.
func (c C) Absdif(val Input) Input {
return c.Add(val.Neg()).Abs()
}
// Acos computes the arccosine of a signal.
func (c C) Acos() Input {
return C(float32(math.Acos(float64(c))))
}
// Add adds another input to the constant.
func (c C) Add(val Input) Input {
if v, ok := val.(C); ok {
return C(float32(v) + float32(c))
}
return val.Add(c)
}
// Amclip returns 0 when b <= 0, a*b when b > 0.
func (c C) Amclip(val Input) Input {
return val.GT(C(0)).Mul(c.Mul(val))
}
// AmpDb converts linear amplitude to decibels.
func (c C) AmpDb() Input {
return C(float32(20 * math.Log10(float64(c))))
}
// Asin computes the arcsine of a signal.
func (c C) Asin() Input {
return C(float32(math.Asin(float64(c))))
}
// Atan computes the arctangent of a signal.
func (c C) Atan() Input {
return C(float32(math.Atan(float64(c))))
}
// Atan2 returns the arctangent of y/x.
func (c C) Atan2(val Input) Input {
if v, ok := val.(C); ok {
return C(float32(math.Atan2(float64(c), float64(v))))
}
return val.Reciprocal().Atan2(c.Reciprocal())
}
// Bilinrand returns a linearly distributed random value between [+in ... -in].
func (c C) Bilinrand() Input {
return c.Rand2()
}
// Ceil computes the ceiling (next highest integer) of a signal.
func (c C) Ceil() Input {
return C(float32(math.Ceil(float64(c))))
}
// Clip2 clips input wave a to +/- b
func (c C) Clip2(val Input) Input {
return c.Max(val.Neg()).Min(val)
}
// Coin returns one or zero with the probability given by the input.
func (c C) Coin() Input {
if rand.Float64() < float64(c) {
return C(1)
}
return C(0)
}
// Cos computes the cosine of an Input.
func (c C) Cos() Input {
return C(float32(math.Cos(float64(c))))
}
// Cosh computes the hyperboliccosine of an Input.
func (c C) Cosh() Input {
return C(float32(math.Cosh(float64(c))))
}
// Cpsmidi converts frequency in Hz to midi note values.
func (c C) Cpsmidi() Input {
return C(Cpsmidi(float32(c)))
}
// Cpsoct converts cycles per second to decimal octaves.
func (c C) Cpsoct() Input {
return C(Cpsoct(float32(c)))
}
// Cubed computes the cube of a signal.
func (c C) Cubed() Input {
return C(float32(c * c * c))
}
// DbAmp converts decibels to linear amplitude.
func (c C) DbAmp() Input {
return C(float32(math.Pow(10, float64(c)/20)))
}
// Difsqr returns the value of (a*a) - (b*b).
func (c C) Difsqr(val Input) Input {
return c.Squared().Add(val.Squared().Neg())
}
// Distort performs non-linear distortion on a signal.
func (c C) Distort() Input {
return C(float32(c) / float32(1+math.Abs(float64(c))))
}
// Div divides one input by another.
// This will panic if val is C(0)
func (c C) Div(val Input) Input {
if v, ok := val.(C); ok {
return C(c / v)
}
return val.Reciprocal().Mul(c)
}
// Exp computes the exponential of a signal.
func (c C) Exp() Input {
return C(float32(math.Exp(float64(c))))
}
// Expon raises a constant to the power of another input.
// If val is not a C the this method just returns c.
// TODO: fix this
func (c C) Expon(val Input) Input {
if v, ok := val.(C); ok {
return C(float32(math.Pow(float64(c), float64(v))))
}
return c
}
// Floor computes the floor of a constant.
func (c C) Floor() Input {
return C(float32(math.Floor(float64(c))))
}
// Fold2 folds input wave a to +/- b
func (c C) Fold2(val Input) Input {
if v, ok := val.(C); ok {
return C(fold2(float32(c), float32(v)))
}
return c // TODO: fix this
}
// Frac returns the fractional part of a constant.
func (c C) Frac() Input {
return C(float32(float64(c) - math.Trunc(float64(c))))
}
// GCD computes the gcd of one Input and another.
func (c C) GCD(val Input) Input {
if v, ok := val.(C); ok {
return C(gcd(float32(c), float32(v)))
}
return val.GCD(c)
}
// GT computes x > y.
func (c C) GT(val Input) Input {
if v, ok := val.(C); ok {
if c > v {
return C(1)
}
return C(0)
}
return val.LT(c)
}
// GTE computes x >= y.
func (c C) GTE(val Input) Input {
if v, ok := val.(C); ok {
if c >= v {
return C(1)
}
return C(0)
}
return val.LTE(c)
}
// Hypot returns the square root of the sum of the squares of a and b.
// Or equivalently, the distance from the origin to the point (x, y).
func (c C) Hypot(val Input) Input {
if v, ok := val.(C); ok {
return C(float32(math.Hypot(float64(c), float64(v))))
}
return val.Hypot(c)
}
// HypotApx returns an approximation of the square root of the sum of the squares of x and y.
// This uses the formula:
// abs(x) + abs(y) - ((sqrt(2) - 1) * min(abs(x), abs(y)))
func (c C) HypotApx(val Input) Input {
if v, ok := val.(C); ok {
var (
x = float32(math.Abs(float64(c))) + float32(math.Abs(float64(v)))
y = float32(math.Min(math.Abs(float64(c)), math.Abs(float64(v))))
)
return C(x - ((float32(math.Sqrt(2)) - 1) * y))
}
return val.HypotApx(c)
}
// LCM computes the least common multiple of one Input and another.
func (c C) LCM(val Input) Input {
if v, ok := val.(C); ok {
return C(lcm(float32(c), float32(v)))
}
return val.LCM(c)
}
// LT computes x < y.
func (c C) LT(val Input) Input {
if v, ok := val.(C); ok {
if c < v {
return C(1)
}
return C(0)
}
return val.GT(c)
}
// LTE computes x <= y.
func (c C) LTE(val Input) Input {
if v, ok := val.(C); ok {
if c <= v {
return C(1)
}
return C(0)
}
return val.GTE(c)
}
// Linrand returns a linearly distributed random value between in and zero.
func (c C) Linrand() Input {
return c.Rand()
}
// Log computes a natural logarithm.
func (c C) Log() Input {
return C(math.Log(float64(c)))
}
// Log10 computes a natural logarithm.
func (c C) Log10() Input {
return C(math.Log10(float64(c)))
}
// Log2 computes a natural logarithm.
func (c C) Log2() Input {
return C(math.Log2(float64(c)))
}
// Max returns the maximum of one input and another.
func (c C) Max(other Input) Input {
if v, ok := other.(C); ok {
return C(maxFloat32(float32(c), float32(v)))
}
return other.Max(c)
}
// Midicps converts MIDI note number to cycles per second.
func (c C) Midicps() Input {
return C(Midicps(float32(c)))
}
// Midiratio converts an interval in MIDI notes into a frequency ratio.
func (c C) Midiratio() Input {
return C(float32(math.Pow(2, float64(c)/12)))
}
// Min returns the minimum of one signal and another.
func (c C) Min(other Input) Input {
if v, ok := other.(C); ok {
return C(minFloat32(float32(c), float32(v)))
}
return other.Min(c)
}
// Moddif returns the smaller of the great circle distances between the two points.
func (c C) Moddif(y, mod Input) Input {
var (
diff = c.Absdif(y).Modulo(mod)
modhalf = mod.Mul(C(0.5))
)
return modhalf.Add(diff.Absdif(modhalf).Neg())
}
// Modulo computes the modulo of one signal and another.
// If val is not a C, then this method just returns the receiver.
// I'm not sure what a constant modulo a ugen should be.
// Note that Go only supports integers for the modulo operator.
func (c C) Modulo(val Input) Input {
if v, ok := val.(C); ok {
return C(float32(int(c) % int(v)))
}
return c
}
// Mul multiplies the constant by another input.
func (c C) Mul(val Input) Input {
if v, ok := val.(C); ok {
return C(float32(v) * float32(c))
}
return val.Mul(c)
}
// MulAdd multiplies and adds at the same time.
func (c C) MulAdd(mul, add Input) Input {
if m, mok := mul.(C); mok {
if a, aok := add.(C); aok {
return C((float32(m) * float32(c)) + float32(a))
}
return add.MulAdd(c, mul)
}
return mul.MulAdd(c, add)
}
// Neg is a convenience operator that multiplies a signal by -1.
func (c C) Neg() Input {
return C(float32(c) * -1)
}
// Octcps converts decimal octaves to cycles per second.
func (c C) Octcps() Input {
return C(Octcps(float32(c)))
}
// Pow raises a constant to the power of another input.
// If val is not a C the this method just returns c.
// TODO: fix this
func (c C) Pow(val Input) Input {
if v, ok := val.(C); ok {
return C(float32(math.Pow(float64(c), float64(v))))
}
return c
}
// Rand returns an evenly distributed random value between this and zero.
func (c C) Rand() Input {
return C(float32(math.Trunc(float64(rand.Float32() * float32(c)))))
}
// Rand2 returns an evenly distributed random value between [+this ... - this].
func (c C) Rand2() Input {
v := (rand.Float32() * 2 * float32(c)) - float32(c)
return C(float32(math.Trunc(float64(v))))
}
// Ratiomidi converts a frequency ratio to an interval in MIDI notes.
func (c C) Ratiomidi() Input {
return C(float32(12 * math.Log2(float64(c))))
}
// Reciprocal computes the reciprocal of a signal.
func (c C) Reciprocal() Input {
return C(1 / float32(c))
}
// Ring1 returns the value of ((a*b) + a).
func (c C) Ring1(val Input) Input {
if v, ok := val.(C); ok {
return C((c * v) + c)
}
return val.Ring1(c)
}
// Ring2 returns the value of ((a*b) + a + b).
func (c C) Ring2(val Input) Input {
if v, ok := val.(C); ok {
return C((c * v) + c + v)
}
return val.Ring2(c)
}
// Ring3 returns the value of (a*a*b).
func (c C) Ring3(val Input) Input {
if v, ok := val.(C); ok {
return C((c * v) * v)
}
return val.Ring3(c)
}
// Ring4 returns the value of ((a*a *b) - (a*b*b)).
func (c C) Ring4(val Input) Input {
if v, ok := val.(C); ok {
return C(((c * v) * v) - (c * v * v))
}
return val.Ring4(c)
}
// Round performs quantization by rounding. Rounds a to the nearest multiple of b.
func (c C) Round(val Input) Input {
if v, ok := val.(C); ok {
return C(Roundf(float32(c), float32(v)))
}
return c
}
// Scaleneg returns a*b when a < 0, otherwise a.
func (c C) Scaleneg(val Input) Input {
if c < 0 {
return c.Mul(val)
}
return c
}
// Sign computes the sign of the constant.
func (c C) Sign() Input {
if c > 0 {
return C(1)
} else if c < 0 {
return C(-1)
}
return C(0)
}
// Sin computes the sine of an Input.
func (c C) Sin() Input {
return C(float32(math.Sin(float64(c))))
}
// Sinh computes the hyperbolic sine of an Input.
func (c C) Sinh() Input {
return C(float32(math.Sinh(float64(c))))
}
// SoftClip clips the constant to the range [-0.5, 0.5]
func (c C) SoftClip() Input {
if float32(c) < -0.5 {
return C(-0.5)
} else if float32(c) > 0.5 {
return C(0.5)
}
return c
}
// Sqrt computes the square root of a constant.
func (c C) Sqrt() Input {
return C(math.Sqrt(float64(c)))
}
// Sqrdif computes the square of the difference between the two inputs.
func (c C) Sqrdif(val Input) Input {
return c.Add(val.Neg()).Squared()
}
// Sqrsum computes the square of the sum of the two inputs.
func (c C) Sqrsum(val Input) Input {
return c.Add(val).Squared()
}
// Squared computes the square of a signal.
func (c C) Squared() Input {
return C(float32(c * c))
}
// Sum3rand returns a value from a gaussian-like random distribution between in and zero.
func (c C) Sum3rand() Input {
return C(rand.NormFloat64())
}
// Sumsqr returns the value of (a*a) + (b*b).
func (c C) Sumsqr(val Input) Input {
return c.Squared().Add(val.Squared())
}
// Tan computes the tangent of an Input.
func (c C) Tan() Input {
return C(float32(math.Tan(float64(c))))
}
// Tanh computes the hyperbolic tangent of an Input.
func (c C) Tanh() Input {
return C(float32(math.Tanh(float64(c))))
}
// Thresh returns 0 when c < val, otherwise c.
func (c C) Thresh(val Input) Input {
if v, ok := val.(C); ok {
if c < v {
return C(0)
}
return c
}
return val.GTE(c).Mul(c)
}
// Trunc performs quantization by truncation. Truncate c to a multiple of val.
// If val is not a constant, c is returned.
func (c C) Trunc(val Input) Input {
if v, ok := val.(C); ok {
return C(Truncf(float32(c), float32(v)))
}
return c
}
// Wrap2 wraps input wave to +/-b
func (c C) Wrap2(val Input) Input {
if v, ok := val.(C); ok {
return C(wrap2(float32(c), float32(v)))
}
return c // TODO: fix this
}
// Roundf rounds a to the nearest multiple of b.
func Roundf(a, b float32) float32 {
if b == 0 {
return 0
}
var m, v float32
for {
if b*m <= a {
if b*(m+1) > a {
if math.Abs(float64(a-b*m)) < math.Abs(float64(a-b*(m+1))) {
v = b * m
} else {
v = b * (m + 1)
}
break
}
m++
continue
}
m--
if b*m <= a {
if math.Abs(float64(a-b*m)) < math.Abs(float64(a-b*(m+1))) {
v = b * m
} else {
v = b * (m + 1)
}
break
}
}
return v
}
// Truncf returns the next highest multiple of b that is < a.
func Truncf(a, b float32) float32 {
if b == 0 {
return 0
}
var m, v float32
for {
if b*m <= a {
if b*(m+1) > a {
v = b * m
break
}
m++
continue
}
m--
if b*m <= a {
v = b * m
break
}
}
return v
}
func fold2(x, y float32) float32 {
if y < 0 {
y *= -1
} else if y == 0 {
return 0
}
if x <= y && x >= -y {
return x
}
if x > y {
return fold2(y-(x-y), y)
}
return fold2(-y+(-y-x), y)
}
func gcd(x, y float32) float32 {
a, b := int(x), int(y)
for b != 0 {
t := b
b = a % b
a = t
}
return float32(a)
}
func lcm(x, y float32) float32 {
a, b := int(x), int(y)
return float32(a*b) / gcd(x, y)
}
func maxFloat32(f1, f2 float32) float32 {
if f1 > f2 {
return f1
}
return f2
}
func minFloat32(f1, f2 float32) float32 {
if f1 < f2 {
return f1
}
return f2
}
func wrap2(x, y float32) float32 {
if y < 0 {
y *= -1
} else if y == 0 {
return 0
}
if x <= y && x >= -y {
return x
}
if x > y {
return wrap2(x-(2*y), y)
}
return wrap2(x+(2*y), y)
}