-
Notifications
You must be signed in to change notification settings - Fork 0
/
wav.go
208 lines (181 loc) · 4.5 KB
/
wav.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
package gfmedia
import (
"bytes"
"encoding/binary"
"fmt"
"io"
)
const (
maxSubChunkCount = 20
maxSubChunkSize = 1 << 20
)
var (
ErrNonstandardWavFile = fmt.Errorf("nonstandard wav file")
)
type WavAudio struct {
raw []byte
head *WavHead
}
func NewWavAudio(raw []byte) *WavAudio {
return &WavAudio{raw: raw}
}
func (a *WavAudio) GetHead() (*WavHead, error) {
if a.head != nil {
return a.head, nil
}
head := WavHead{}
if err := head.Deserialize(a.raw); err != nil {
return nil, err
}
a.head = &head
return a.head, nil
}
func (a WavAudio) ToPcm() (*PcmAudio, error) {
head, err := a.GetHead()
if err != nil {
return nil, err
}
pcm := make([]byte, len(a.raw)-head.Len())
copy(pcm, a.raw[head.Len():])
return &PcmAudio{raw: pcm}, nil
}
type WavHead struct {
Riff
Fmt
Extras
Data
}
func NewWavHead(numChannels uint16, sampleRate uint32, bitsPerSample uint16,
bodyLen uint32, extras ...Extra) *WavHead {
return &WavHead{
Riff: newRiff(36 + bodyLen + uint32(Extras(extras).Len())),
Fmt: newFmt(numChannels, sampleRate, bitsPerSample),
Extras: extras,
Data: newData(bodyLen),
}
}
func (h *WavHead) ExtendFmtChunk(ext FmtExtra) *WavHead {
h.UnknownFmtBytes = append(h.UnknownFmtBytes, ext.UnknownFmtBytes...)
h.FmtChunkSize += uint32(len(ext.UnknownFmtBytes))
h.ChunkSize += uint32(len(ext.UnknownFmtBytes))
return h
}
func (h *WavHead) Serialize() ([]byte, error) {
ext, err := h.Extras.Marshal()
if err != nil {
return nil, err
}
buf, byteOrder := &bytes.Buffer{}, binary.LittleEndian
if err := binary.Write(buf, byteOrder, h.Riff); err != nil {
return nil, err
}
if err := binary.Write(buf, byteOrder, h.FmtStandard); err != nil {
return nil, err
}
if _, err := buf.Write(h.FmtExtra.UnknownFmtBytes); err != nil {
return nil, err
}
if _, err := buf.Write(ext); err != nil {
return nil, err
}
if err := binary.Write(buf, byteOrder, h.Data); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
func (h *WavHead) Deserialize(raw []byte) error {
wav, head := bytes.NewReader(raw), WavHead{}
if err := readRiff(wav, &head); err != nil {
return err
}
for i := 0; i < maxSubChunkCount; i++ {
chunk, err := readChunk(wav)
if err != nil {
return err
}
subChunkID := string(chunk.SubChunkID[:])
if subChunkID == "data" {
head.DataChunkID = chunk.SubChunkID
head.DataChunkSize = chunk.SubChunkSize
break
}
if subChunkID == "fmt " {
head.FmtChunkID = chunk.SubChunkID
head.FmtChunkSize = chunk.SubChunkSize
if head.FmtChunkSize < 16 {
return ErrChunkSizeInvalid
}
subChunk, byteOrder := chunk.SubChunkData, binary.LittleEndian
head.AudioFormat = byteOrder.Uint16(subChunk[:2])
head.NumChannels = byteOrder.Uint16(subChunk[2:4])
head.SampleRate = byteOrder.Uint32(subChunk[4:8])
head.ByteRate = byteOrder.Uint32(subChunk[8:12])
head.BlockAlign = byteOrder.Uint16(subChunk[12:14])
head.BitsPerSample = byteOrder.Uint16(subChunk[14:16])
head.UnknownFmtBytes = subChunk[16:]
continue
}
ext := Extra{
SubChunk: SubChunk{
SubChunkID: chunk.SubChunkID,
SubChunkSize: chunk.SubChunkSize,
SubChunkData: chunk.SubChunkData,
},
}
head.Extras = append(head.Extras, ext)
}
*h = head
return nil
}
func (h *WavHead) Len() int {
// ChunkSize不包含ChunkID和ChunkSize这8个字节,需要加回来
return int(h.ChunkSize + 8 - h.DataChunkSize)
}
func read(r io.Reader, s []byte) error {
n, e := r.Read(s)
if e != nil {
return e
} else if n < len(s) {
return io.EOF
}
return nil
}
func readRiff(r io.Reader, head *WavHead) error {
chunkSize := [4]byte{}
if err := read(r, head.ChunkID[:]); err != nil {
return err
}
if err := read(r, chunkSize[:]); err != nil {
return err
}
head.ChunkSize = binary.LittleEndian.Uint32(chunkSize[:])
if err := read(r, head.Format[:]); err != nil {
return err
}
if string(head.Format[:]) != "WAVE" {
return ErrNonstandardWavFile
}
return nil
}
func readChunk(r io.Reader) (*SubChunk, error) {
chunk := &SubChunk{}
if err := read(r, chunk.SubChunkID[:]); err != nil {
return nil, err
}
subChunkSize := [4]byte{}
if err := read(r, subChunkSize[:]); err != nil {
return nil, err
}
chunk.SubChunkSize = binary.LittleEndian.Uint32(subChunkSize[:])
if string(chunk.SubChunkID[:]) == "data" {
return chunk, nil
}
if chunk.SubChunkSize > maxSubChunkSize {
return nil, ErrChunkSizeTooLarge
}
chunk.SubChunkData = make([]byte, chunk.SubChunkSize)
if err := read(r, chunk.SubChunkData[:]); err != nil {
return nil, err
}
return chunk, nil
}