-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdecoder_test.go
359 lines (336 loc) · 10.1 KB
/
decoder_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
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
package aiff
import (
"bytes"
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"testing"
"time"
"github.com/go-audio/audio"
)
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile `file`")
// func init() {
// flag.Parse()
// if *cpuprofile != "" {
// f, err := os.Create(*cpuprofile)
// if err != nil {
// log.Fatal("could not create CPU profile: ", err)
// }
// if err := pprof.StartCPUProfile(f); err != nil {
// log.Fatal("could not start CPU profile: ", err)
// }
// defer pprof.StopCPUProfile()
// }
// // Debug = true
// }
func TestContainerAttributes(t *testing.T) {
expectations := []struct {
input string
id [4]byte
size uint32
format [4]byte
commSize uint32
numChans uint16
numSampleFrames uint32
sampleSize uint16
sampleRate int
totalFrames int64
encoding [4]byte
encodingName string
comments []string
}{
{"fixtures/kick.aif", formID, 9642, aiffID,
18, 1, 4484, 16, 22050, 4484, [4]byte{}, "", nil},
{"fixtures/ring.aif", formID, 354310, aiffID,
18, 2, 88064, 16, 44100, 88064, [4]byte{}, "", []string{"Creator: Logic"}},
{"fixtures/sowt.aif", formID, 17276, aifcID,
24, 2, 4064, 16, 44100, 4064, encSowt, "", nil},
// misaligned chunk sizes
{"fixtures/sowt2.aif", formID, 683420, aifcID,
24, 2, 166677, 16, 44100, 166677, encSowt, "", []string{"c) 2009 mutekki-media.de"}},
{"fixtures/ableton.aif", formID, 203316, aifcID, 38, 2, 33815, 24, 48000, 33815, encAble, "Ableton Content", nil},
}
for _, exp := range expectations {
path, _ := filepath.Abs(exp.input)
t.Log(path)
f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
d := NewDecoder(f)
buf, err := d.FullPCMBuffer()
if err != nil {
t.Fatal(fmt.Errorf("failed to read the full PCM buffer - %s", err))
}
if err := d.Drain(); err != nil {
t.Fatalf("draining %s failed - %s\n", path, err)
}
if int(d.BitDepth) != int(exp.sampleSize) {
t.Fatalf("%s of %s didn't match %d, got %d", "Clip bit depth", exp.input, exp.sampleSize, d.BitDepth)
}
if int(d.SampleRate) != exp.sampleRate {
t.Fatalf("%s of %s didn't match %d, got %d", "Clip sample rate", exp.input, exp.sampleRate, d.SampleRate)
}
if int(d.NumChans) != int(exp.numChans) {
t.Fatalf("%s of %s didn't match %d, got %d", "Clip sample channels", exp.input, exp.numChans, d.NumChans)
}
if buf.NumFrames() != int(exp.totalFrames) {
t.Fatalf("%s of %s didn't match %d, got %d", "Clip sample data size", exp.input, exp.totalFrames, buf.NumFrames())
}
if d.ID != exp.id {
t.Fatalf("%s of %s didn't match %s, got %s", "ID", exp.input, exp.id, d.ID)
}
if d.Size != exp.size {
t.Fatalf("%s of %s didn't match %d, got %d", "BlockSize", exp.input, exp.size, d.Size)
}
if d.Form != exp.format {
t.Fatalf("%s of %s didn't match %q, got %q", "Format", exp.input, exp.format, d.Form)
}
// comm chunk
if d.commSize != exp.commSize {
t.Fatalf("%s of %s didn't match %d, got %d", "comm size", exp.input, exp.commSize, d.commSize)
}
if d.NumChans != exp.numChans {
t.Fatalf("%s of %s didn't match %d, got %d", "NumChans", exp.input, exp.numChans, d.NumChans)
}
if d.NumSampleFrames != exp.numSampleFrames {
t.Fatalf("%s of %s didn't match %d, got %d", "NumSampleFrames", exp.input, exp.numSampleFrames, d.NumSampleFrames)
}
if d.BitDepth != exp.sampleSize {
t.Fatalf("%s of %s didn't match %d, got %d", "SampleSize", exp.input, exp.sampleSize, d.BitDepth)
}
if d.SampleRate != exp.sampleRate {
t.Fatalf("%s of %s didn't match %d, got %d", "SampleRate", exp.input, exp.sampleRate, d.SampleRate)
}
if d.Encoding != exp.encoding {
t.Fatalf("%s of %s didn't match %q, got %q", "Encoding", exp.input, exp.encoding, d.Encoding)
}
if d.EncodingName != exp.encodingName {
t.Fatalf("%s of %s didn't match %s, got %s", "EncodingName", exp.input, exp.encodingName, d.EncodingName)
}
if len(d.Comments) != len(exp.comments) {
t.Fatalf("%s of %s didn't match %d, got %d", "number of comments", exp.input, len(exp.comments), len(d.Comments))
}
for i, c := range d.Comments {
if c != exp.comments[i] {
t.Fatalf("expected comment of %s to be %q but was %q\n", exp.input, exp.comments[i], c)
}
}
}
}
func TestDecoder_Duration(t *testing.T) {
expectations := []struct {
input string
duration time.Duration
}{
{"fixtures/kick.aif", time.Duration(203356009)},
}
for _, exp := range expectations {
path, _ := filepath.Abs(exp.input)
f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
c := NewDecoder(f)
d, err := c.Duration()
if err != nil {
t.Fatal(err)
}
if d != exp.duration {
t.Fatalf("duration of %s didn't match %d milliseconds, got %d", exp.input, exp.duration, d)
}
}
}
func TestDecoder_FullPCMBuffer(t *testing.T) {
testCases := []struct {
input string
desc string
samples []int
numSamples int
}{
{"fixtures/kick.aif",
"1 ch, 22050 Hz, 'lpcm' (0x0000000E) 16-bit big-endian signed integer",
[]int{76, 76, 75, 75, 72, 71, 72, 69, 70, 68, 65, 73, 529, 1425, 2245, 2941, 3514, 3952, 4258, 4436, 4486, 4413, 4218, 3903, 3474, 2938, 2295, 1553, 711, -214, -1230, -2321, -3489, -4721, -6007, -7352, -8738, -10172, -11631, -13127, -14642, -16029, -17322, -18528, -19710, -20877},
4484,
},
{"fixtures/delivery.aiff",
"1 ch, 22050 Hz, 'lpcm' (0x0000000E) 16-bit big-endian signed integer",
[]int{132, 123, 53, 85, 20, -15, 14, -2, 47, 96, 115, 181, 207, 248, 284, 324, 358, 369, 375, 350, 335, 305, 250, 188, 164, 144, 138, 151, 151, 166, 159, 170, 178, 178, 164, 125, 112, 73, 49, 28, -24, -70, -129, -165, -177, -185, -186, -194, -198, -211, -217, -208, -222, -215, -205, -177, -119, -100, -61, -34, -35, 10, 43, 79, 130, 157, 198, 251, 297, 348, 399, 424},
17199,
},
}
for i, tc := range testCases {
t.Logf("%d - %s - %s\n", i, tc.input, tc.desc)
path, _ := filepath.Abs(tc.input)
f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
d := NewDecoder(f)
buf, err := d.FullPCMBuffer()
if err != nil {
t.Fatal(err)
}
if len(buf.Data) != tc.numSamples {
t.Fatalf("the length of the buffer (%d) didn't match what we expected (%d)", len(buf.Data), tc.numSamples)
}
for i := 0; i < len(tc.samples); i++ {
if buf.Data[i] != tc.samples[i] {
t.Fatalf("Expected %d at position %d, but got %d", tc.samples[i], i, buf.Data[i])
}
}
}
}
func TestDecoderPCMBuffer(t *testing.T) {
testCases := []struct {
input string
desc string
bitDepth int
samples []int
samplesAvailable int
}{
{"fixtures/kick.aif",
"1 ch, 22050 Hz, 16-bit",
16,
// test the extraction of some of the PCM content
[]int{76, 76, 75, 75, 72, 71, 72, 69, 70, 68, 65, 73, 529, 1425, 2245, 2941, 3514, 3952, 4258, 4436, 4486, 4413, 4218, 3903, 3474, 2938, 2295, 1553, 711, -214, -1230, -2321, -3489, -4721, -6007, -7352, -8738, -10172, -11631, -13127, -14642, -16029, -17322, -18528, -19710, -20877},
4484,
},
{
"fixtures/padded24b.aif",
"padded 24bit sample",
24,
nil,
22913,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
path, _ := filepath.Abs(tc.input)
f, err := os.Open(path)
if err != nil {
t.Fatal(err)
}
defer f.Close()
d := NewDecoder(f)
samples := []int{}
intBuf := make([]int, 255)
buf := &audio.IntBuffer{Data: intBuf}
var samplesAvailable int
var n int
for err == nil {
n, err = d.PCMBuffer(buf)
if n == 0 {
break
}
samplesAvailable += n
if err != nil {
t.Fatal(err)
}
samples = append(samples, buf.Data...)
}
if samplesAvailable != tc.samplesAvailable {
t.Fatalf("expected %d samples available, got %d", tc.samplesAvailable, samplesAvailable)
}
if buf.SourceBitDepth != tc.bitDepth {
t.Fatalf("expected source depth to be %d but got %d", tc.bitDepth, buf.SourceBitDepth)
}
// allow to test the first samples of the content
if tc.samples != nil {
for i, sample := range samples {
if i >= len(tc.samples) {
break
}
if sample != tc.samples[i] {
t.Fatalf("Expected %d at position %d, but got %d", tc.samples[i], i, sample)
}
}
}
})
}
}
func TestDecoder_IsValidFile(t *testing.T) {
testCases := []struct {
in string
isValid bool
}{
{"fixtures/bloop.aif", true},
{"fixtures/kick8b.aiff", true},
{"fixtures/zipper24b.aiff", true},
{"fixtures/sample.avi", false},
{"fixtures/kick.wav", false},
{"fixtures/ableton.aif", false},
}
for i, tc := range testCases {
f, err := os.Open(tc.in)
if err != nil {
t.Fatal(err)
}
defer f.Close()
d := NewDecoder(f)
if d.IsValidFile() != tc.isValid {
t.Fatalf("[%d] validation of the aiff files doesn't match expected %t, got %t - %#v", i, tc.isValid, d.IsValidFile(), d)
}
// make sure the consumer can rewind
if _, err = d.Seek(0, io.SeekStart); err != nil {
t.Fatal(err)
}
}
}
func TestDecoderRewind(t *testing.T) {
f, err := os.Open("fixtures/ableton.aif")
if err != nil {
t.Fatal(err)
}
defer f.Close()
d := NewDecoder(f)
d.ReadInfo()
buf := &audio.IntBuffer{Format: d.Format(), Data: make([]int, 512)}
n, err := d.PCMBuffer(buf)
if err != nil {
t.Fatal(err)
}
if n != 512 {
t.Fatalf("expected to read 512 samples but got %d", n)
}
if err := d.Rewind(); err != nil {
t.Fatal(err)
}
newBuf := &audio.IntBuffer{Format: d.Format(), Data: make([]int, 512)}
n, err = d.PCMBuffer(newBuf)
if err != nil {
t.Fatal(err)
}
if n != 512 {
t.Fatalf("expected to read 512 samples but got %d", n)
}
if !reflect.DeepEqual(buf.Data, newBuf.Data) {
t.Fatal("expected to read the same data after rewinding")
}
}
func TestJump(t *testing.T) {
chunk := Chunk{}
if err := chunk.Jump(1); err == nil {
t.Fatal(errors.New("Expected EOF"))
}
data := []byte("abcdefgh")
chunk.R = bytes.NewReader(data)
chunk.Size = len(data)
if err := chunk.Jump(1); err != nil {
t.Fatal(err)
}
c, err := chunk.ReadByte()
if err != nil {
t.Fatal(err)
}
if c != data[1] {
t.Errorf("Expected '%x' got '%x'", data[1], c)
}
}