-
Notifications
You must be signed in to change notification settings - Fork 4
/
snd_test.go
158 lines (132 loc) · 3.06 KB
/
snd_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
153
154
155
156
157
158
package snd
import (
"testing"
"time"
"dasa.cc/signal"
)
const epsilon = 0.0001
func equals(a, b float64) bool {
return equaleps(a, b, epsilon)
}
func equaleps(a, b float64, eps float64) bool {
return (a-b) < eps && (b-a) < eps
}
type unit struct{ *mono }
func newunit() *unit {
u := &unit{newmono(nil)}
for i := range u.out {
u.out[i] = DefaultAmpFac
}
return u
}
func (u *unit) Prepare(uint64) {}
func (u *unit) Inputs() []Sound { return nil }
type zeros struct{ *mono }
func newzeros() *zeros { return &zeros{newmono(nil)} }
func (z *zeros) Prepare(uint64) {
for i := range z.out {
if z.off {
z.out[i] = 0
} else {
z.out[i] = 1
}
}
}
func (z *zeros) Inputs() []Sound { return nil }
func BenchmarkZeros(b *testing.B) {
z := newzeros()
b.ReportAllocs()
b.ResetTimer()
for n := 1; n <= b.N; n++ {
z.Prepare(uint64(n))
}
}
// mksound returns a 12-key piano synth as might be found in an app.
func mksound() Sound {
mix := NewMixer()
for i := 0; i < 12; i++ {
oscil := NewOscil(signal.Sawtooth(), 440, NewOscil(signal.Sine(), 2, nil))
oscil.SetPhase(NewOscil(signal.Square(), 200, nil))
comb := NewComb(0.8, 10*time.Millisecond, oscil)
adsr := NewADSR(50*time.Millisecond, 500*time.Millisecond, 100*time.Millisecond, 350*time.Millisecond, 0.4, 1, comb)
instr := NewInstrument(adsr)
mix.Append(instr)
}
loop := NewLoop(5*time.Second, mix)
mixloop := NewMixer(mix, loop)
lp := NewLowPass(1500, mixloop)
return NewPan(0, lp)
}
// func mkthoughtsaboutcreatingstuff() Sound {
// What if these were Options passed in ?
// How many things actually have options?
// Would need a way that an "option" and an "instance" could
// interchangably be used here, such as implementing the same interface?
// or having some type of lazy initialization that would allow easy access
// to the instance pointers.
// sine := Sine()
// sawtooth := Sawtooth(4)
// square := Square(4)
// phaser := snd.Oscil{
// Harm: square,
// Freq: 200,
// }.New()
// osc := snd.Oscil{
// Harm: sawtooth,
// Freq: 440,
// Mod: snd.Oscil{Harm: sine, Freq: 2}, // every new osc has independent mod
// Phase: phaser, // every new osc reuses same phaser
// }.New()
// cmb := snd.Comb{
// Gain: 0.8,
// Dur: 10 * time.Millisecond,
// }.New()
// env := snd.ADSR{
// Attack: 50 * time.Millisecond,
// Decay: 500 * time.Millisecond,
// Sustain: 100 * time.Millisecond,
// Release: 350 * time.Millisecond,
// SusAmp: 0.4,
// MaxAmp: 1,
// }.New()
// cmb.SetInput(osc)
// env.SetInput(cmb)
// this is just bad for so many reasons.
// &Pan{
// Left: 1,
// Right: 1,
// In: &Waveform{
// Buf: 4,
// In: &LowPass{
// Cutoff: 1500,
// In: &Mixer{
// Ins: []Sound{
// &Loop{
// Duration: 5,
// },
// &Mixer{
// Ins: []Sound{}
// },
// },
// },
// },
// },
// }
// }
func TestDecibel(t *testing.T) {
tests := []struct {
db Decibel
amp float64
}{
{0, 1},
{1, 1.1220},
{3, 1.4125},
{6, 1.9952},
{10, 3.1622},
}
for _, test := range tests {
if !equals(test.db.Amp(), test.amp) {
t.Errorf("%s have %v, want %v", test.db, test.db.Amp(), test.amp)
}
}
}