-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound_test.go
267 lines (235 loc) · 7.39 KB
/
sound_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
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
package sound
import (
"github.com/splace/signals"
"os"
"testing"
"time"
)
func TestSaveTone(t *testing.T) {
wavFile, err := os.Create("./test output/tone.wav")
if err != nil {
panic(err)
}
defer wavFile.Close()
s1 := signals.Sine{signals.X(.005)}
signals.Encode(wavFile, 2, 44100, signals.X(1), s1)
}
func TestSaveSound(t *testing.T) {
wavFile, err := os.Create("./test output/noise.wav")
if err != nil {
panic(err)
}
defer wavFile.Close()
s1 := NewSound(signals.NewNoise(), time.Second/2)
Encode(wavFile, 2, 44100, s1)
}
func TestSaveFlattenedSound(t *testing.T) {
wavFile, err := os.Create("./test output/fnoise.wav")
if err != nil {
panic(err)
}
defer wavFile.Close()
s1 := NewSound(&signals.Segmented{Signal:signals.NewNoise(), Width:signals.X(.0005)}, time.Second/2)
Encode(wavFile, 2, 44100, s1)
}
func TestSaveNote(t *testing.T) {
noteNumber := MidiNote("one-line", "C")
wavFile, err := os.Create("./test output/middlec.wav")
if err != nil {
panic(err)
}
defer wavFile.Close()
s1 := NewMidiNote(noteNumber, 2000*ms, 1)
Encode(wavFile, 1, 8000, s1)
}
func TestLoad(t *testing.T) {
stream, err := os.Open("middlec.wav")
if err != nil {
panic(err)
}
defer stream.Close()
noises, err := signals.Decode(stream)
if err != nil {
panic(err)
}
if len(noises) != 1 {
t.Error("middlec.wav not reported as mono.")
}
}
func TestLoadChannels(t *testing.T) {
stream, err := os.Open("pcm0808s.wav")
if err != nil {
panic(err)
}
defer stream.Close()
noises, err := signals.Decode(stream)
if err != nil {
panic(err)
}
if len(noises) != 2 {
t.Error("middlec.wav not reported as stereo.")
}
}
func TestEncodeSignal(t *testing.T) {
wavFile, err := os.Create("./test output/delayedlimitedsquaresignal.wav")
if err != nil {
panic(err)
}
defer wavFile.Close()
s1 := Delayed(NewSound(signals.Square{signals.X(.1)}, 1000*ms), 2000*ms)
Encode(wavFile, 1, 8000, s1)
}
func TestSaveSignal(t *testing.T) {
err := signals.SaveGOB("./test output/delayedlimitedsquaresignal", Delayed(NewSound(signals.Square{signals.X(.1)}, 1000*ms), 2000*ms))
if err != nil {
panic(err)
}
}
func TestSaveModifiedNote(t *testing.T) {
wavFile2, err := os.Create("./test output/NoteModded.wav")
if err != nil {
panic(err)
}
defer wavFile2.Close()
s2 := Spedup(NewSound(NewTone(time.Millisecond, 1), time.Second), .264) // makes a middle c
Encode(wavFile2, 1, 8000, s2)
}
func TestSaveModifiedWav(t *testing.T) {
stream, err := os.Open("8k8bitpcm.wav")
if err != nil {
panic(err)
}
defer stream.Close()
wavFile, err := os.Create("./test output/8k8bitpcmSpedup.wav")
if err != nil {
panic(err)
}
defer wavFile.Close()
noises, err := signals.Decode(stream)
//noises[0].Interpolate = true // interpolation because the save frequency, 44.1k, is going to be much more than stored, 8k.
Encode(wavFile, 1, 44100, Spedup(noises[0].(Sound), 1.2))
//wav.Encode(noises[0], wavFile, 44100,1)
}
func TestSaveWavSoundAfterSound(t *testing.T) {
wavFile, err := os.Create("./test output/tones.wav")
if err != nil {
panic(err)
}
defer wavFile.Close()
s1 := NewNote(NewTone(Period(4, "D"), 1), time.Second/3)
s2 := After(s1, NewNote(NewTone(Period(4, "E"), 1), time.Second/3))
s3 := After(s2, NewNote(NewTone(Period(4, 0), 1), time.Second/3))
s4 := After(s3, NewNote(NewTone(Period("small", 0), 1), time.Second/3))
s5 := After(s4, NewNote(NewTone(Period("small", "G"), 1), time.Second*2/3))
Encode(wavFile, 1, 44100, NewCompositor(s1, s2, s3, s4, s5))
}
func TestSaveWavSoundSequence(t *testing.T) {
wavFile, err := os.Create("./test output/tones2.wav")
if err != nil {
panic(err)
}
defer wavFile.Close()
notes:=[]Sound{
NewNote(NewTone(Period(4, "D"), 1),time.Second/3),
NewNote(NewTone(Period(4, "E"), 1), time.Second/3),
NewNote(NewTone(Period(4, 0), 1), time.Second/3),
NewNote(NewTone(Period("small", 0), 1), time.Second/3),
NewNote(NewTone(Period("small", "G"), 1), time.Second*2/3),
}
Encode(wavFile, 1, 44100, NewSequencer(notes...))
}
func TestSaveVibrato(t *testing.T) {
wavFile, err := os.Create("./test output/vibrato.wav")
if err != nil {
panic(err)
}
defer wavFile.Close()
s := NewMidiNote(MidiNote("one-line", "C"), 2000*ms, 1)
sm := NewMidiNote(MidiNote("great", "C"), 2000*ms, 1)
Encode(wavFile, 1, 8000, Modulated(s, sm, 1*ms))
}
func TestSaveADSRModulate(t *testing.T) {
wavFile, err := os.Create("./test output/ADSRModulate.wav")
if err != nil {
panic(err)
}
defer wavFile.Close()
sm := signals.Looped{signals.NewADSREnvelope(signals.X(.1), signals.X(.1), signals.X(.1), signals.Y(.7), signals.X(.1)), signals.X(.4)}
s := NewMidiNote(MidiNote("two-line", "C"), 3500*ms, 100)
Encode(wavFile, 1, 8000, Modulated(s, sm, 20*ms))
}
func TestSaveSequences(t *testing.T) {
wavFile, err := os.Create("./test output/noteSequence.wav")
if err != nil {
panic(err)
}
defer wavFile.Close()
noteLength:=300*ms
// uses reference to which notes are double
TwinkleTwinkleLittleStar := struct{
Notes []int8
Doubles map[int]struct{}
} {
[]int8{60, 60, 67, 67, 69, 69, 67, 65, 65, 64, 64, 62, 62, 60},
map[int]struct{}{ 7:{}, 14:{}},
}
// use sequencer to append notes
notes := make([]Sound, len(TwinkleTwinkleLittleStar.Notes))
for i, midiNumber := range TwinkleTwinkleLittleStar.Notes {
if _,ok:=TwinkleTwinkleLittleStar.Doubles[i+1];ok{
notes[i] = NewMidiNote(midiNumber,noteLength*2 ,.7)
}else{
notes[i] = NewMidiNote(midiNumber,noteLength ,.7)
}
}
Encode(wavFile, 1, 44100, NewSequencer(SoundsSeparated(Silence(80*ms),notes...)...))
}
func TestSaveHarmonicNotes(t *testing.T) {
stream, err := os.Open("sample.wav")
if err != nil {
panic(err)
}
sample, err := signals.Decode(stream)
if err != nil {
panic(err)
}
defer stream.Close()
wavFile, err := os.Create("./test output/hNotes.wav")
if err != nil {
panic(err)
}
defer wavFile.Close()
sustainedEnv := func(length time.Duration) signals.LimitedSignal {
return signals.NewADSREnvelope(signals.X(.025), signals.X(.1), signals.X(length.Seconds()), signals.Y(.5), signals.X(.5))
}
noteDuration := 180 * ms
sustainDuration := 50 * ms
// use compositor, so each note doesn't just start when previous ended, they can overlap.
// this way decay of previous note can still exist beyond the start of the next note.
notes := Compositor{}
addMidiNote := func(t Compositor, noteNum int8, length, gap uint8) Compositor {
//noteAndGap := Sound{signals.Product{NewToneMidi(noteNum, 80), sustainedEnv(signals.MultiplyInterval(length, sustainDuration))}, 140*ms+signals.MultiplyInterval(length+gap, noteDuration)}
envNoteAndGap := NewSound(signals.Modulated{NewSampledMidiTone(noteNum, sample[0], .7), sustainedEnv(time.Duration(length) * sustainDuration)}, 140*ms+time.Duration(length+gap)*noteDuration)
if len(t.Composite) == 0 {
return NewCompositor(append(t.Composite, envNoteAndGap))
}
return NewCompositor(append(t.Composite, AfterPlusOffset(t.Composite[len(t.Composite)-1].(Sound), envNoteAndGap, -140*ms)))
}
// use -ve to indicate double note
TwinkleTwinkleLittleStar := []int8{60, 60, 67, 67, 69, 69, -67, 65, 65, 64, 64, 62, 62, -60}
var noteLength uint8 = 1
for _, note := range TwinkleTwinkleLittleStar {
if note < 0 {
notes = addMidiNote(notes, -note, noteLength*2+2, 0)
} else {
notes = addMidiNote(notes, note, noteLength*2, 0)
}
}
// measured frequencies: 261 392 440 392 349 330 293.5 261 checked
Encode(wavFile, 1, 44100, notes)
}
/*
func BenchmarkOne(b *testing.B) {
b.StopTimer()
}
*/