-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathreader.go
267 lines (221 loc) · 5.48 KB
/
reader.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
package wave
import (
"bytes"
"encoding/binary"
"io"
"io/ioutil"
"math"
"os"
"strings"
)
// type aliases for conversion functions
type (
bytesToIntF func([]byte) int
bytesToFloatF func([]byte) float64
)
var (
// figure out which 'to int' function to use..
byteSizeToIntFunc = map[int]bytesToIntF{
16: bits16ToInt,
24: bits24ToInt,
32: bits32ToInt,
}
byteSizeToFloatFunc = map[int]bytesToFloatF{
16: bitsToFloat,
32: bitsToFloat,
64: bitsToFloat,
}
// max value depending on the bit size
maxValues = map[int]int{
8: math.MaxInt8,
16: math.MaxInt16,
32: math.MaxInt32,
64: math.MaxInt64,
}
)
// ReadWaveFile parses a .wave file into a Wave struct
func ReadWaveFile(f string) (Wave, error) {
// open as read-only file
file, err := os.Open(f)
if err != nil {
return Wave{}, err
}
defer file.Close()
return ReadWaveFromReader(file)
}
// ReadWaveFromReader parses an io.Reader into a Wave struct
func ReadWaveFromReader(reader io.Reader) (Wave, error) {
data, err := ioutil.ReadAll(reader)
if err != nil {
return Wave{}, err
}
data = deleteJunk(data)
hdr := readHeader(data)
wfmt := readFmt(data)
wavdata := readData(data, wfmt)
frames := parseRawData(wfmt, wavdata.RawData)
wavdata.Frames = frames
return Wave{
WaveHeader: hdr,
WaveFmt: wfmt,
WaveData: wavdata,
}, nil
}
// for our wave format we expect double precision floats
func bitsToFloat(b []byte) float64 {
var bits uint64
switch len(b) {
case 2:
bits = uint64(binary.LittleEndian.Uint16(b))
case 4:
bits = uint64(binary.LittleEndian.Uint32(b))
case 8:
bits = binary.LittleEndian.Uint64(b)
default:
panic("Can't parse to float..")
}
float := math.Float64frombits(bits)
return float
}
func bits16ToInt(b []byte) int {
if len(b) != 2 {
panic("Expected size 4!")
}
var payload int16
buf := bytes.NewReader(b)
err := binary.Read(buf, binary.LittleEndian, &payload)
if err != nil {
// TODO: make safe
panic(err)
}
return int(payload) // easier to work with ints
}
func bits24ToInt(b []byte) int {
if len(b) != 3 {
panic("Expected size 3!")
}
// add some padding to turn a 24-bit integer into a 32-bit integer
b = append([]byte{0x00}, b...)
var payload int32
buf := bytes.NewReader(b)
err := binary.Read(buf, binary.LittleEndian, &payload)
if err != nil {
// TODO: make safe
panic(err)
}
return int(payload) // easier to work with ints
}
// turn a 32-bit byte array into an int
func bits32ToInt(b []byte) int {
if len(b) != 4 {
panic("Expected size 4!")
}
var payload int32
buf := bytes.NewReader(b)
err := binary.Read(buf, binary.LittleEndian, &payload)
if err != nil {
// TODO: make safe
panic(err)
}
return int(payload) // easier to work with ints
}
func readData(b []byte, wfmt WaveFmt) WaveData {
wd := WaveData{}
start := 36 + wfmt.ExtraParamSize
subchunk2ID := b[start : start+4]
wd.Subchunk2ID = subchunk2ID
subsize := bits32ToInt(b[start+4 : start+8])
wd.Subchunk2Size = subsize
wd.RawData = b[start+8:]
return wd
}
// Should we do n-channel separation at this point?
func parseRawData(wfmt WaveFmt, rawdata []byte) []Frame {
bytesSampleSize := wfmt.BitsPerSample / 8
// TODO: sanity-check that this is a power of 2? I think only those sample sizes are
// possible
frames := []Frame{}
// read the chunks
for i := 0; i < len(rawdata); i += bytesSampleSize {
rawFrame := rawdata[i : i+bytesSampleSize]
unscaledFrame := byteSizeToIntFunc[wfmt.BitsPerSample](rawFrame)
scaled := scaleFrame(unscaledFrame, wfmt.BitsPerSample)
frames = append(frames, scaled)
}
return frames
}
func scaleFrame(unscaled, bits int) Frame {
maxV := maxValues[bits]
return Frame(float64(unscaled) / float64(maxV))
}
// deleteJunk will remove the JUNK chunks if they are present
func deleteJunk(b []byte) []byte {
var junkStart, junkEnd int
for i := 0; i < len(b)-4; i++ {
if strings.ToLower(string(b[i:i+4])) == "junk" {
junkStart = i
}
if strings.ToLower(string(b[i:i+3])) == "fmt" {
junkEnd = i
}
}
if junkStart != 0 {
cpy := make([]byte, len(b[0:junkStart]))
copy(cpy, b[0:junkStart])
cpy = append(cpy, b[junkEnd:]...)
return cpy
}
return b
}
// readFmt parses the FMT portion of the WAVE file
// assumes the entire binary representation is passed!
func readFmt(b []byte) WaveFmt {
wfmt := WaveFmt{}
subchunk1ID := b[12:16]
wfmt.Subchunk1ID = subchunk1ID
subchunksize := bits32ToInt(b[16:20])
wfmt.Subchunk1Size = subchunksize
format := bits16ToInt(b[20:22])
wfmt.AudioFormat = format
numChannels := bits16ToInt(b[22:24])
wfmt.NumChannels = numChannels
sr := bits32ToInt(b[24:28])
wfmt.SampleRate = sr
br := bits32ToInt(b[28:32])
wfmt.ByteRate = br
ba := bits16ToInt(b[32:34])
wfmt.BlockAlign = ba
bps := bits16ToInt(b[34:36])
wfmt.BitsPerSample = bps
// parse extra (optional) elements..
if subchunksize != 16 {
// only for compressed files (non-PCM)
extraSize := bits16ToInt(b[36:38])
wfmt.ExtraParamSize = extraSize
wfmt.ExtraParams = b[38 : 38+extraSize]
}
return wfmt
}
// TODO: make safe.
func readHeader(b []byte) WaveHeader {
// the start of the bte slice..
hdr := WaveHeader{}
hdr.ChunkID = b[0:4]
if string(hdr.ChunkID) != "RIFF" {
panic("Invalid file")
}
chunkSize := b[4:8]
var size uint32
buf := bytes.NewReader(chunkSize)
err := binary.Read(buf, binary.LittleEndian, &size)
if err != nil {
panic(err)
}
hdr.ChunkSize = int(size) // easier to work with ints
format := b[8:12]
if string(format) != "WAVE" {
panic("Format should be WAVE")
}
hdr.Format = string(format)
return hdr
}