-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathenv.go
655 lines (589 loc) · 14.1 KB
/
env.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
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
package sc
import (
"fmt"
"sort"
)
const (
// CurveStep is a flat envelope segment.
CurveStep = C(0)
// CurveLinear is a linear envelope segment.
CurveLinear = C(1)
// CurveExp is an exponential envelope segment.
CurveExp = C(2)
// CurveSine is a sinusoidal shaped envelope segment.
CurveSine = C(3)
// CurveWelch is a sinusoidal segment shaped like the sides of a welch window.
CurveWelch = C(4)
// CurveCustom is an undocumented (on doc.sccode.org) envelope segment shape.
CurveCustom = C(5)
// CurveSquared is a squared envelope segment.
CurveSquared = C(6)
// CurveCubed is a cubed envelope segment.
CurveCubed = C(7)
)
// shapeNames contains the names of the envelope curves.
// This mirrors the IdentityDictionary in SCClassLibrary/Common/Audio/Env.sc.
var shapeNames = map[string]int{
"step": 0,
"lin": 1,
"linear": 1,
"exp": 2,
"exponential": 2,
"sin": 3,
"sine": 3,
"wel": 4,
"welch": 4,
"sqr": 6,
"squared": 6,
"cub": 7,
"cubed": 7,
}
// Env is a specification for a breakpoint envelope
type Env struct {
// Levels is the array of levels
Levels []Input
// Times is the array of durations (in seconds).
// The length of this array should be one less than the
// Levels array.
Times []Input
// Curve determines the shape of each envelope segment.
// This could be a string, a float, a slice of strings, or a slice of floats.
Curve interface{}
ReleaseNode Input
LoopNode Input
}
func (env *Env) defaults() {
if env.Levels == nil {
env.Levels = []Input{C(0), C(1), C(0)}
}
if env.Times == nil {
env.Times = []Input{C(1), C(1)}
}
if env.ReleaseNode == nil {
env.ReleaseNode = C(-99)
}
if env.LoopNode == nil {
env.LoopNode = C(-99)
}
if env.Curve == nil {
env.Curve = "linear"
}
}
// Inputs returns the array of inputs that defines the Env.
func (env Env) Inputs() []Input {
(&env).defaults()
// This is how the inputs array is constructed:
// 0, 3, -99, -99, -- starting level, num segments, releaseNode, loopNode
// 1, 0.1, 5, 4, -- first segment: level, time, curve type, curvature
// 0.5, 1, 5, -4, -- second segment: level, time, curve type, curvature
// 0, 0.2, 5, 4 -- and so on
var (
curvesArray = env.curvesArray()
lc = len(curvesArray)
lt = len(env.Times)
)
if lc != lt {
panic(fmt.Errorf("%d curve types != %d times", lc, lt))
}
var (
numSegments = len(env.Levels) - 1
arr = make([]Input, 4*(numSegments+1))
)
arr[0] = env.Levels[0]
arr[1] = C(numSegments)
arr[2] = env.ReleaseNode
arr[3] = env.LoopNode
for i, t := range env.Times {
arr[(4*i)+4] = env.Levels[i+1]
arr[(4*i)+5] = t
arr[(4*i)+6] = shapeNumber(curvesArray[i])
arr[(4*i)+7] = curveValue(curvesArray[i])
}
return arr
}
// curvesArray returns the Curve as an array.
func (env Env) curvesArray() []interface{} {
switch val := env.Curve.(type) {
case int, string, float64, Input:
return arrayFromScalar(val, len(env.Times))
case []int:
return intsToEmpties(val)
case []float64:
return floatsToEmpties(val)
case []string:
return stringsToEmpties(val)
case []Input:
return inputsToEmpties(val)
case []interface{}:
return val
default:
panic(fmt.Sprintf("unsupported type for envelope curve: %T", env.Curve))
}
}
// intsToEmpties converts a int slice to a slice of the empty interface.
func intsToEmpties(arr []int) []interface{} {
ret := make([]interface{}, len(arr))
for i, ii := range arr {
ret[i] = ii
}
return ret
}
// floatsToEmpties converts a float slice to a slice of the empty interface.
func floatsToEmpties(arr []float64) []interface{} {
ret := make([]interface{}, len(arr))
for i, f := range arr {
ret[i] = f
}
return ret
}
// stringsToEmpties converts a string slice to a slice of the empty interface.
func stringsToEmpties(arr []string) []interface{} {
ret := make([]interface{}, len(arr))
for i, s := range arr {
ret[i] = s
}
return ret
}
// inputsToEmpties converts an Input slice to a slice of the empty interface.
func inputsToEmpties(arr []Input) []interface{} {
ret := make([]interface{}, len(arr))
for i, in := range arr {
ret[i] = in
}
return ret
}
// arrayFromScalar converts a scalar value to a slice of the empty interface.
func arrayFromScalar(val interface{}, length int) []interface{} {
ret := make([]interface{}, length)
for i := range ret {
ret[i] = val
}
return ret
}
// shapeNumber returns the mysterious shape number for the Env inputs array.
//
// The relevant sclang code (from Env.sc) is:
//
// *shapeNumber { arg shapeName;
// ^shapeName.asArray.collect { |name|
// var shape;
// if(name.isValidUGenInput) { 5 } {
// shape = shapeNames.at(name);
// if(shape.isNil) { Error("Env shape not defined.").throw };
// shape
// }
// }.unbubble
// }
//
// Note: we don't handle the case where shapeValue is an array since we force
// this func to be called with either string, float64, or Input.
//
func shapeNumber(shapeValue interface{}) Input {
if _, ok := isValidUgenInput(shapeValue); ok {
return C(5)
}
switch val := shapeValue.(type) {
case int:
return C(float32(val))
case float64:
return C(float32(val))
case float32:
return C(val)
case Input:
return val
case string:
shapeNum, ok := shapeNames[val]
if !ok {
panic(fmt.Sprintf("invalid curve: %s", val))
}
return C(shapeNum)
default:
panic(fmt.Sprintf("invalid curve type (must be float, string, or Input): %T", shapeValue))
}
}
// isValidUgenInput returns false if val is not a valid ugen input and true otherwise.
func isValidUgenInput(val interface{}) (Input, bool) {
switch x := val.(type) {
case int:
return C(float32(x)), true
case float64:
return C(float32(x)), true
case float32:
return C(x), true
case Input:
return x, true
default:
return nil, false
}
}
// curveValue returns the mysterious curve value for the Env inputs array.
//
// The relevant sclang code (from Env.sc) is:
//
// curveValue { arg curve;
// ^if(curve.isSequenceableCollection) {
// curve.collect { |x|
// if(x.isValidUGenInput) { x } { 0 }
// }
// } {
// if(curve.isValidUGenInput) { curve } { 0 }
// }
// }
func curveValue(curve interface{}) Input {
if input, ok := isValidUgenInput(curve); ok {
return input
}
return C(0)
}
// EnvLinen creates a new envelope which has a trapezoidal shape
type EnvLinen struct {
Attack, Sustain, Release, Level Input
Curve interface{}
}
func (linen *EnvLinen) defaults() {
if linen.Attack == nil {
linen.Attack = C(0.01)
}
if linen.Sustain == nil {
linen.Sustain = C(1)
}
if linen.Release == nil {
linen.Release = C(1)
}
if linen.Level == nil {
linen.Level = C(1)
}
if linen.Curve == nil {
linen.Curve = "lin"
}
}
// Inputs returns the array of inputs that defines the Env.
func (linen EnvLinen) Inputs() []Input {
(&linen).defaults()
return Env{
Levels: []Input{C(0), linen.Level, linen.Level, C(0)},
Times: []Input{linen.Attack, linen.Sustain, linen.Release},
Curve: linen.Curve,
}.Inputs()
}
// EnvTriangle creates a new envelope that has a triangle shape
type EnvTriangle struct {
Dur, Level Input
}
func (tri *EnvTriangle) defaults() {
if tri.Dur == nil {
tri.Dur = C(1)
}
if tri.Level == nil {
tri.Level = C(1)
}
}
// Inputs returns the array of inputs that defines the Env.
func (tri EnvTriangle) Inputs() []Input {
(&tri).defaults()
d := tri.Dur.Mul(C(0.5))
return Env{
Levels: []Input{C(0), tri.Level, C(0)},
Times: []Input{d, d},
}.Inputs()
}
// EnvSine creates a new envelope which has a hanning window shape
type EnvSine struct {
Dur, Level Input
}
func (sine *EnvSine) defaults() {
if sine.Dur == nil {
sine.Dur = C(1)
}
if sine.Level == nil {
sine.Level = C(1)
}
}
// Inputs returns the array of inputs that defines the Env.
func (sine EnvSine) Inputs() []Input {
(&sine).defaults()
d := sine.Dur.Mul(C(0.5))
return Env{
Levels: []Input{C(0), sine.Level, C(0)},
Times: []Input{d, d},
Curve: "sine",
}.Inputs()
}
// EnvPerc creates a new envelope that has a percussive shape
type EnvPerc struct {
Attack, Release, Level, Curve Input
}
func (perc *EnvPerc) defaults() {
if perc.Attack == nil {
perc.Attack = C(0.01)
}
if perc.Release == nil {
perc.Release = C(1)
}
if perc.Level == nil {
perc.Level = C(1)
}
if perc.Curve == nil {
perc.Curve = C(-4)
}
}
// Inputs returns the array of inputs that defines the Env.
func (perc EnvPerc) Inputs() []Input {
(&perc).defaults()
return Env{
Levels: []Input{C(0), perc.Level, C(0)},
Times: []Input{perc.Attack, perc.Release},
Curve: []Input{perc.Curve, perc.Curve},
}.Inputs()
}
// Pairs are pairs of floats: the first float is time,
// the second is level.
// They get sorted by time.
type Pairs [][2]float32
func (p Pairs) Len() int {
return len(p)
}
func (p Pairs) Less(i, j int) bool {
return p[i][0] < p[j][0]
}
func (p Pairs) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
// EnvPairs creates a new envelope from coordinates/pairs
type EnvPairs struct {
Pairs Pairs
Curve interface{}
}
// Inputs returns the array of inputs that defines the Env.
func (pairs EnvPairs) Inputs() []Input {
sort.Sort(&(pairs.Pairs))
var (
lp = len(pairs.Pairs)
levels = make([]Input, lp)
times = make([]Input, lp-1)
)
for i, p := range pairs.Pairs {
levels[i] = C(p[1])
if i > 0 {
times[i-1] = C(p[0] - pairs.Pairs[i-1][0])
}
}
return Env{
Levels: levels,
Times: times,
Curve: pairs.Curve,
}.Inputs()
}
// TLC (time, level, curve) triplet
type TLC struct {
Time, Level float32
Curve interface{}
}
// EnvTLC creates a new envelope from an array of (time, level, curve) triplets
// This is renamed from Env.xyc.
// The Curve value of the last triplet is ignored.
type EnvTLC []TLC
func (tlc EnvTLC) Len() int {
return len(tlc)
}
func (tlc EnvTLC) Less(i, j int) bool {
return tlc[i].Time < tlc[j].Time
}
func (tlc EnvTLC) Swap(i, j int) {
tlc[i], tlc[j] = tlc[j], tlc[i]
}
// Inputs returns the array of inputs that defines the Env.
func (tlc EnvTLC) Inputs() []Input {
sort.Sort(tlc)
var (
lp = len(tlc)
levels = make([]Input, lp)
times = make([]Input, lp-1)
curves = make([]interface{}, lp-1)
)
for i, t := range tlc {
levels[i] = C(t.Level)
if i > 0 {
times[i-1] = C(t.Time - tlc[i-1].Time)
curves[i-1] = tlc[i-1].Curve
}
}
return Env{
Levels: levels,
Times: times,
Curve: curves,
}.Inputs()
}
// EnvADSR represents the ever-popular ADSR envelope
type EnvADSR struct {
A, D, S, R, Peak, Curve, Bias Input
}
func (adsr *EnvADSR) defaults() {
if adsr.A == nil {
adsr.A = C(0.01)
}
if adsr.D == nil {
adsr.D = C(0.3)
}
if adsr.S == nil {
adsr.S = C(0.5)
}
if adsr.R == nil {
adsr.R = C(1)
}
if adsr.Peak == nil {
adsr.Peak = C(1)
}
if adsr.Curve == nil {
adsr.Curve = C(-4)
}
if adsr.Bias == nil {
adsr.Bias = C(0)
}
}
// Inputs returns the array of inputs that defines the Env.
func (adsr EnvADSR) Inputs() []Input {
(&adsr).defaults()
levels := []Input{
C(0).Add(adsr.Bias),
adsr.Peak.Add(adsr.Bias),
adsr.S.Add(adsr.Bias),
C(0).Add(adsr.Bias),
}
var (
times = []Input{adsr.A, adsr.D, adsr.R}
cts = []Input{C(-4), C(-4), C(-4)}
)
return Env{
Levels: levels,
Times: times,
Curve: cts,
ReleaseNode: C(2),
}.Inputs()
}
// EnvDADSR is EnvADSR with its onset delayed by D seconds
type EnvDADSR struct {
Delay, A, D, S, R, Peak, Curve, Bias Input
}
func (dadsr *EnvDADSR) defaults() {
if dadsr.Delay == nil {
dadsr.Delay = C(0.1)
}
if dadsr.A == nil {
dadsr.A = C(0.01)
}
if dadsr.D == nil {
dadsr.D = C(0.3)
}
if dadsr.S == nil {
dadsr.S = C(0.5)
}
if dadsr.R == nil {
dadsr.R = C(1)
}
if dadsr.Peak == nil {
dadsr.Peak = C(1)
}
if dadsr.Curve == nil {
dadsr.Curve = C(-4)
}
if dadsr.Bias == nil {
dadsr.Bias = C(0)
}
}
// Inputs returns the array of inputs that defines the Env.
func (dadsr EnvDADSR) Inputs() []Input {
(&dadsr).defaults()
levels := []Input{
C(0),
C(0).Add(dadsr.Bias),
dadsr.Peak.Add(dadsr.Bias),
dadsr.S.Add(dadsr.Bias),
C(0).Add(dadsr.Bias),
}
var (
times = []Input{dadsr.Delay, dadsr.A, dadsr.D, dadsr.R}
cts = []Input{CurveCustom, CurveCustom, CurveCustom, CurveCustom}
)
return Env{
Levels: levels,
Times: times,
Curve: cts,
ReleaseNode: C(3),
}.Inputs()
}
// EnvASR is an attack-sustain-release envelope
type EnvASR struct {
A, S, R, Curve Input
}
func (asr *EnvASR) defaults() {
if asr.A == nil {
asr.A = C(0.01)
}
if asr.S == nil {
asr.S = C(1)
}
if asr.R == nil {
asr.R = C(1)
}
if asr.Curve == nil {
asr.Curve = C(-4)
}
}
// Inputs returns the array of inputs that defines the Env.
func (asr EnvASR) Inputs() []Input {
(&asr).defaults()
var (
levels = []Input{C(0), asr.S, C(0)}
times = []Input{asr.A, asr.R}
cts = []Input{CurveCustom, CurveCustom}
)
return Env{
Levels: levels,
Times: times,
Curve: cts,
ReleaseNode: C(1),
}.Inputs()
}
// EnvCutoff creates an envelope with no attack segment.
// It simply sustains at the peak level until released.
type EnvCutoff struct {
R, Level, CurveType Input
}
func (cutoff *EnvCutoff) defaults() {
if cutoff.R == nil {
cutoff.R = C(0.1)
}
if cutoff.Level == nil {
cutoff.Level = C(1)
}
if cutoff.CurveType == nil {
cutoff.CurveType = CurveLinear
}
}
// Inputs returns the array of inputs that defines the Env.
func (cutoff EnvCutoff) Inputs() []Input {
(&cutoff).defaults()
var (
levels = []Input{cutoff.Level, C(0)}
times = []Input{cutoff.R}
cts = []Input{cutoff.CurveType}
)
return Env{
Levels: levels,
Times: times,
Curve: cts,
ReleaseNode: C(0),
}.Inputs()
}
// I don't understand Env.circle [briansorahan]
//
// Env.circle([0, 1, 0], [0.01, 0.5, 0.2]).asArray;
// => [ 0, 2, -99, -99, 1, 0.01, 1, 0, 0, 0.5, 1, 0 ]
//
// Shouldn't loopNode be set to one of the envelope breakpoints?
//
// type EnvCircle struct {
// Levels, Times []Input
// Curve Input
// }