-
Notifications
You must be signed in to change notification settings - Fork 2
/
wav_reader_test.go
153 lines (124 loc) · 4.33 KB
/
wav_reader_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
package agraph
import (
"fmt"
"os"
"testing"
)
func TestNewWaveReader(t *testing.T) {
file, err := os.OpenFile("examples/ringbackB.wav", os.O_RDWR, 066)
if err != nil {
t.Error(err)
}
reader, err := NewWaveReader(file)
if err != nil {
t.Error(err)
}
expectedChunkID := "RIFF"
expectedSize := uint32(160038) // take off the 8 bits??
expectedFormat := "WAVE"
expectedSubChunk1ID := "fmt "
expectedSubChunk1Size := uint32(16)
expectedAudioFormat := uint16(1)
expectedNumChannels := uint16(1)
expectedSampleRate := uint32(8000)
expectedByteRate := uint32(16000)
expectedBlockAlign := uint16(2)
expectedBitsPerSamp := uint16(16)
expectedSubchunk2ID := "data"
expectedSubchunk2Size := uint32(160002)
if string(reader.Riff.ChunkID) != expectedChunkID {
t.Errorf("Actual chunk ID %v is not equal to expected chunk ID %v", expectedChunkID, reader.Riff.ChunkID)
}
if reader.Riff.ChunkSize != expectedSize {
t.Errorf("Actual chunk size %v is not equal to expected chunk size %v", expectedSize, reader.Riff.ChunkSize)
}
if string(reader.Riff.Format) != expectedFormat {
t.Errorf("Actual format %v is not equal to expected formmat %v", expectedFormat, reader.Riff.Format)
}
if string(reader.Fmt.ID) != expectedSubChunk1ID {
t.Errorf("Actual subChunk1ID %v is not equal to expected subChunk1 ID %v", string(reader.Fmt.ID), expectedSubChunk1ID)
}
if reader.Fmt.Size != expectedSubChunk1Size {
t.Errorf("Actual subChunk1Size %v is not equal to expected subchunk1Size %v", reader.Fmt.Size, expectedSubChunk1Size)
}
if reader.Fmt.Data.AudioFormat != expectedAudioFormat {
t.Errorf("Actual audio-format %v is not equal to expected audio-format %v", reader.Fmt.Data.AudioFormat, expectedAudioFormat)
}
if reader.Fmt.Data.NumChannels != expectedNumChannels {
t.Errorf("Actual num-channels %v is not equal to expected num-channels %v", reader.Fmt.Data.NumChannels, expectedNumChannels)
}
if reader.Fmt.Data.SampleRate != expectedSampleRate {
t.Errorf("Actual sample-rate %v is not equal to expected sample-rate %v", reader.Fmt.Data.SampleRate, expectedSampleRate)
}
if reader.Fmt.Data.ByteRate != expectedByteRate {
t.Errorf("Actual byte-rate %v is not equal to expected byte-rate %v", reader.Fmt.Data.ByteRate, expectedByteRate)
}
if reader.Fmt.Data.BlockAlign != expectedBlockAlign {
t.Errorf("Actual block-align %v is not equal to expected block-align %v", reader.Fmt.Data.BlockAlign, expectedBlockAlign)
}
if reader.Fmt.Data.BitsPerSample != expectedBitsPerSamp {
t.Errorf("Actual bits-per-sample %v is not equal to expected bits-per-sample %v", reader.Fmt.Data.BitsPerSample, expectedBitsPerSamp)
}
if string(reader.data.ID) != expectedSubchunk2ID {
t.Errorf("Actual subchunk2ID %v is not equal to expected subchunk2ID %v", reader.data.ID, expectedSubchunk2ID)
}
if reader.data.Size != expectedSubchunk2Size {
t.Errorf("Actual subchunk2 size %v is not equal to expected size %v", reader.data.Size, expectedSubChunk1Size)
}
/*
fmt.Printf("AudioFormat: %v\n", reader.Fmt.Data.AudioFormat)
fmt.Printf("NumChannels: %v\n", reader.Fmt.Data.NumChannels)
fmt.Printf("SampleRate: %v\n", reader.Fmt.Data.SampleRate)
fmt.Printf("ByteRate: %v\n", reader.Fmt.Data.ByteRate)
fmt.Printf("BlockAlign: %v\n", reader.Fmt.Data.BlockAlign)
fmt.Printf("BitsPerSample: %v\n", reader.Fmt.Data.BitsPerSample)
*/
}
func TestRead(t *testing.T) {
file, err := os.OpenFile("examples/tone.wav", os.O_RDWR, 066)
if err != nil {
t.Error(err)
}
reader, err := NewWaveReader(file)
if err != nil {
t.Error(err)
}
expectedSize := 1024
b := make([]byte, expectedSize)
actualSize, err := reader.Read(b)
if err != nil {
t.Error(err)
}
if actualSize != expectedSize {
t.Errorf("Actual size %v != expected size %v", actualSize, expectedSize)
}
}
func TestReadSampleRaw(t *testing.T) {
file, err := os.OpenFile("examples/ringbackA.wav", os.O_RDWR, 066)
if err != nil {
t.Error(err)
}
reader, err := NewWaveReader(file)
if err != nil {
t.Error(err)
}
_, err = reader.ReadSampleRaw()
if err != nil {
t.Error(err)
}
}
func TestWaveReaderReadSampleInt16(t *testing.T) {
file, err := os.OpenFile("examples/ringbackA.wav", os.O_RDWR, 066)
if err != nil {
t.Error(err)
}
reader, err := NewWaveReader(file)
if err != nil {
t.Error(err)
}
samp, err := reader.ReadSampleInt16()
if err != nil {
t.Error(err)
}
fmt.Println(samp)
}