Repository navigation
Expand file tree
/
Copy pathdecoder_test.go
More file actions
188 lines (162 loc) · 5.37 KB
/
Copy pathdecoder_test.go
File metadata and controls
188 lines (162 loc) · 5.37 KB
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
package mago
import (
"bytes"
"encoding/binary"
"math"
"os"
"testing"
)
func decoderTestWAV(t *testing.T, samples []float32, channels int, sampleRate int) []byte {
t.Helper()
var pcm bytes.Buffer
for _, sample := range samples {
scaled := math.Max(-32768, math.Min(32767, float64(sample)*32767))
if err := binary.Write(&pcm, binary.LittleEndian, int16(scaled)); err != nil {
t.Fatalf("write pcm: %v", err)
}
for ch := 1; ch < channels; ch++ {
if err := binary.Write(&pcm, binary.LittleEndian, int16(scaled)); err != nil {
t.Fatalf("write pcm: %v", err)
}
}
}
var wav bytes.Buffer
wav.WriteString("RIFF")
_ = binary.Write(&wav, binary.LittleEndian, uint32(36+pcm.Len()))
wav.WriteString("WAVE")
wav.WriteString("fmt ")
_ = binary.Write(&wav, binary.LittleEndian, uint32(16))
_ = binary.Write(&wav, binary.LittleEndian, uint16(1))
_ = binary.Write(&wav, binary.LittleEndian, uint16(channels))
_ = binary.Write(&wav, binary.LittleEndian, uint32(sampleRate))
_ = binary.Write(&wav, binary.LittleEndian, uint32(sampleRate*channels*2))
_ = binary.Write(&wav, binary.LittleEndian, uint16(channels*2))
_ = binary.Write(&wav, binary.LittleEndian, uint16(16))
wav.WriteString("data")
_ = binary.Write(&wav, binary.LittleEndian, uint32(pcm.Len()))
wav.Write(pcm.Bytes())
return wav.Bytes()
}
func TestDecoderReadsWAVFromMemory(t *testing.T) {
lib := newNullLibrary(t)
samples := make([]float32, 64)
for i := range samples {
samples[i] = float32(0.5 * math.Sin(2*math.Pi*440*float64(i)/48_000))
}
data := decoderTestWAV(t, samples, 1, 48_000)
decoder, err := lib.NewDecoderMemory(data, DefaultDecoderConfig())
if err != nil {
t.Fatalf("NewDecoderMemory: %v", err)
}
defer func() { _ = decoder.Close() }()
format, channels, sampleRate, err := decoder.DataFormat()
if err != nil {
t.Fatalf("DataFormat: %v", err)
}
if format != FormatF32 || channels != 1 || sampleRate != 48_000 {
t.Fatalf("format = %v channels = %d rate = %d", format, channels, sampleRate)
}
length, err := decoder.LengthInPCMFrames()
if err != nil {
t.Fatalf("LengthInPCMFrames: %v", err)
}
if length != uint64(len(samples)) {
t.Fatalf("length = %d, want %d", length, len(samples))
}
out := make([]float32, len(samples))
read, err := decoder.ReadF32(out)
if err != nil {
t.Fatalf("ReadF32: %v", err)
}
if read != uint64(len(samples)) {
t.Fatalf("read %d frames, want %d", read, len(samples))
}
if math.Abs(float64(out[1]-samples[1])) > 0.001 {
t.Fatalf("decoded sample %v, want %v", out[1], samples[1])
}
}
func TestDecoderSeekAndCursor(t *testing.T) {
lib := newNullLibrary(t)
samples := make([]float32, 32)
for i := range samples {
samples[i] = float32(i) / 32
}
decoder, err := lib.NewDecoderMemory(decoderTestWAV(t, samples, 1, 48_000), DefaultDecoderConfig())
if err != nil {
t.Fatalf("NewDecoderMemory: %v", err)
}
defer func() { _ = decoder.Close() }()
first := make([]float32, 4)
if _, err := decoder.ReadF32(first); err != nil {
t.Fatalf("ReadF32: %v", err)
}
if cursor, err := decoder.CursorInPCMFrames(); err != nil || cursor != 4 {
t.Fatalf("cursor = %d (%v), want 4", cursor, err)
}
if err := decoder.SeekToPCMFrame(0); err != nil {
t.Fatalf("SeekToPCMFrame: %v", err)
}
if cursor, err := decoder.CursorInPCMFrames(); err != nil || cursor != 0 {
t.Fatalf("cursor after seek = %d (%v), want 0", cursor, err)
}
again := make([]float32, 4)
if _, err := decoder.ReadF32(again); err != nil {
t.Fatalf("ReadF32: %v", err)
}
if math.Abs(float64(again[1]-first[1])) > 0.001 {
t.Fatalf("re-read sample %v, want %v", again[1], first[1])
}
}
func TestDecoderSetLooping(t *testing.T) {
lib := newNullLibrary(t)
samples := []float32{0.1, 0.2, 0.3, 0.4}
decoder, err := lib.NewDecoderMemory(decoderTestWAV(t, samples, 1, 48_000), DefaultDecoderConfig())
if err != nil {
t.Fatalf("NewDecoderMemory: %v", err)
}
defer func() { _ = decoder.Close() }()
first := make([]float32, len(samples))
if read, err := decoder.ReadF32(first); err != nil || read != uint64(len(samples)) {
t.Fatalf("ReadF32 = %d (%v), want %d", read, err, len(samples))
}
residual := make([]float32, 2)
if read, err := decoder.ReadF32(residual); err != nil || read != 0 {
t.Fatalf("residual read = %d (%v), want 0", read, err)
}
if err := decoder.SetLooping(true); err != nil {
t.Fatalf("SetLooping: %v", err)
}
wrapped := make([]float32, len(samples))
if read, err := decoder.ReadF32(wrapped); err != nil || read != uint64(len(samples)) {
t.Fatalf("looped ReadF32 = %d (%v), want %d", read, err, len(samples))
}
for i := range samples {
if math.Abs(float64(wrapped[i]-samples[i])) > 0.001 {
t.Fatalf("looped sample %d = %v, want %v", i, wrapped[i], samples[i])
}
}
}
func TestDecoderReadsWAVFromFile(t *testing.T) {
lib := newNullLibrary(t)
samples := make([]float32, 16)
for i := range samples {
samples[i] = float32(i) / 16
}
path := t.TempDir() + "/tone.wav"
if err := os.WriteFile(path, decoderTestWAV(t, samples, 1, 48_000), 0o600); err != nil {
t.Fatalf("write test wav: %v", err)
}
decoder, err := lib.NewDecoderFile(path, DefaultDecoderConfig())
if err != nil {
t.Fatalf("NewDecoderFile: %v", err)
}
defer func() { _ = decoder.Close() }()
out := make([]float32, len(samples))
read, err := decoder.ReadF32(out)
if err != nil {
t.Fatalf("ReadF32: %v", err)
}
if read != uint64(len(samples)) {
t.Fatalf("read %d frames, want %d", read, len(samples))
}
}