-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathenv_test.go
152 lines (137 loc) · 3.06 KB
/
env_test.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
package sc
import (
"math"
"os"
"testing"
)
// All of the expected values in these tests were generated with sclang using SCIDE.
func TestPerc(t *testing.T) {
var (
e = EnvPerc{}
expect = []float32{0, 2, -99, -99, 1, 0.01, 5, -4, 0, 1, 5, -4}
)
verifyInputs(t, expect, e.Inputs())
}
func TestLinen(t *testing.T) {
t.Run("default", func(t *testing.T) {
var (
e = EnvLinen{}
expect = []float32{0, 3, -99, -99, 1, 0.01, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0}
)
verifyInputs(t, expect, e.Inputs())
})
t.Run("welch", func(t *testing.T) {
var (
f = EnvLinen{Curve: "welch"}
expectWelch = []float32{0, 3, -99, -99, 1, 0.01, 4, 0, 1, 1, 4, 0, 0, 1, 4, 0}
)
verifyInputs(t, expectWelch, f.Inputs())
})
}
func TestTriangle(t *testing.T) {
var (
e = EnvTriangle{}
expect = []float32{0, 2, -99, -99, 1, 0.5, 1, 0, 0, 0.5, 1, 0}
)
verifyInputs(t, expect, e.Inputs())
}
func TestSine(t *testing.T) {
var (
e = EnvSine{}
expect = []float32{0, 2, -99, -99, 1, 0.5, 3, 0, 0, 0.5, 3, 0}
)
verifyInputs(t, expect, e.Inputs())
}
func TestPairs(t *testing.T) {
var (
e = EnvPairs{
Pairs: Pairs([][2]float32{
{0, 1},
{2.1, 0.5},
{3, 1.4},
}),
Curve: "exponential",
}
expect = []float32{1, 2, -99, -99, 0.5, 2.1, 2, 0, 1.4, 0.9, 2, 0}
)
verifyInputs(t, expect, e.Inputs())
}
func TestTLC(t *testing.T) {
var (
e = EnvTLC([]TLC{
{0, 1, "sine"},
{2.1, 0.5, "lin"},
{3, 1.4, "lin"},
})
expect = []float32{1, 2, -99, -99, 0.5, 2.1, 3, 0, 1.4, 0.9, 1, 0}
)
verifyInputs(t, expect, e.Inputs())
}
func TestTHX(t *testing.T) {
var (
// I wrote this test after encountering problems with Env
// while implementing the THX deep note.
env = Env{
Levels: []Input{
C(0),
C(0.1),
C(1),
},
Times: []Input{
C(5),
C(8),
},
Curve: []Input{
CurveExp,
CurveCustom,
},
}
expect = []float32{0, 2, -99, -99, 0.1, 5, 5, 2, 1, 8, 5, 5}
)
verifyInputs(t, expect, env.Inputs())
}
func TestEnvADSR(t *testing.T) {
const name = "TestEnvADSR"
def := NewSynthdef(name, func(params Params) Ugen {
var (
bus = C(0)
sig = SinOsc{}.Rate(AR).Mul(EnvGen{
Env: EnvADSR{},
Done: FreeEnclosing,
}.Rate(KR))
)
return Out{bus, sig}.Rate(AR)
})
f, err := os.Create("testdata/" + name + ".gosyndef")
if err != nil {
t.Fatal(err)
}
if err := def.Write(f); err != nil {
t.Fatal(err)
}
same, err := def.CompareToFile("testdata/" + name + ".scsyndef")
if err != nil {
t.Fatal(err)
}
if !same {
t.Fatalf("synthdef different from sclang version")
}
}
const epsilon = 1e-6
func verifyInputs(t *testing.T, expect []float32, inputs []Input) {
if len(expect) != len(inputs) {
t.Fatalf("expected: %+v\n got: %+v", expect, inputs)
}
for i, in := range inputs {
var val float32
switch v := in.(type) {
case C:
val = float32(v)
default:
t.Fatalf("input %d was not a float or int (%v)", i, in)
}
if delta := float64(val - expect[i]); math.Abs(delta) > epsilon {
t.Fatalf("expected: %+v\n got: %+v", expect, inputs)
}
}
}