This repository has been archived by the owner on Aug 25, 2021. It is now read-only.
forked from youpy/go-wav
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
225 lines (178 loc) · 4.15 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
package wav
import (
"bufio"
"encoding/binary"
"errors"
"math"
"github.com/youpy/go-riff"
"github.com/zaf/g711"
)
type Reader struct {
r *riff.Reader
riffChunk *riff.RIFFChunk
format *WavFormat
*WavData
}
func NewReader(r riff.RIFFReader) *Reader {
riffReader := riff.NewReader(r)
return &Reader{r: riffReader}
}
func (r *Reader) Format() (format *WavFormat, err error) {
if r.format == nil {
format, err = r.readFormat()
if err != nil {
return
}
r.format = format
} else {
format = r.format
}
return
}
func (r *Reader) Read(p []byte) (n int, err error) {
if r.WavData == nil {
data, err := r.readData()
if err != nil {
return n, err
}
r.WavData = data
}
return r.WavData.Read(p)
}
func (r *Reader) ReadSamples(params ...uint32) (samples []Sample, err error) {
var bytes []byte
var numSamples, b, n int
if len(params) > 0 {
numSamples = int(params[0])
} else {
numSamples = 2048
}
format, err := r.Format()
if err != nil {
return
}
numChannels := int(format.NumChannels)
blockAlign := int(format.BlockAlign)
bitsPerSample := int(format.BitsPerSample)
bytes = make([]byte, numSamples*blockAlign)
n, err = r.Read(bytes)
if err != nil {
return
}
numSamples = n / blockAlign
r.WavData.pos += uint32(numSamples * blockAlign)
samples = make([]Sample, numSamples)
offset := 0
for i := 0; i < numSamples; i++ {
for j := 0; j < numChannels; j++ {
soffset := offset + (j * bitsPerSample / 8)
switch format.AudioFormat {
case AudioFormatIEEEFloat:
bits :=
uint32((int(bytes[soffset+3]) << 24) +
(int(bytes[soffset+2]) << 16) +
(int(bytes[soffset+1]) << 8) +
int(bytes[soffset]))
samples[i].Values[j] = int(math.MaxInt32 * math.Float32frombits(bits))
case AudioFormatALaw:
var val uint
pcm := g711.DecodeAlaw(bytes[soffset : soffset+(bitsPerSample/8)])
for b = 0; b < len(pcm); b++ {
val += uint(pcm[b]) << uint(b*8)
}
samples[i].Values[j] = toInt(val, bitsPerSample*2)
case AudioFormatMULaw:
var val uint
pcm := g711.DecodeUlaw(bytes[soffset : soffset+(bitsPerSample/8)])
for b = 0; b < len(pcm); b++ {
val += uint(pcm[b]) << uint(b*8)
}
samples[i].Values[j] = toInt(val, bitsPerSample*2)
default:
var val uint
for b = 0; b*8 < bitsPerSample; b++ {
val += uint(bytes[soffset+b]) << uint(b*8)
}
samples[i].Values[j] = toInt(val, bitsPerSample)
}
}
offset += blockAlign
}
return
}
func (r *Reader) IntValue(sample Sample, channel uint) int {
return sample.Values[channel]
}
func (r *Reader) FloatValue(sample Sample, channel uint) float64 {
return float64(r.IntValue(sample, channel)) / math.Pow(2, float64(r.format.BitsPerSample)-1)
}
func (r *Reader) readFormat() (fmt *WavFormat, err error) {
var riffChunk *riff.RIFFChunk
fmt = new(WavFormat)
if r.riffChunk == nil {
riffChunk, err = r.r.Read()
if err != nil {
return
}
r.riffChunk = riffChunk
} else {
riffChunk = r.riffChunk
}
fmtChunk := findChunk(riffChunk, "fmt ")
if fmtChunk == nil {
err = errors.New("Format chunk is not found")
return
}
err = binary.Read(fmtChunk, binary.LittleEndian, fmt)
if err != nil {
return
}
return
}
func (r *Reader) readData() (data *WavData, err error) {
var riffChunk *riff.RIFFChunk
if r.riffChunk == nil {
riffChunk, err = r.r.Read()
if err != nil {
return
}
r.riffChunk = riffChunk
} else {
riffChunk = r.riffChunk
}
dataChunk := findChunk(riffChunk, "data")
if dataChunk == nil {
err = errors.New("Data chunk is not found")
return
}
data = &WavData{bufio.NewReader(dataChunk), dataChunk.ChunkSize, 0}
return
}
func findChunk(riffChunk *riff.RIFFChunk, id string) (chunk *riff.Chunk) {
for _, ch := range riffChunk.Chunks {
if string(ch.ChunkID[:]) == id {
chunk = ch
break
}
}
return
}
func toInt(value uint, bits int) int {
var result int
switch bits {
case 32:
result = int(int32(value))
case 16:
result = int(int16(value))
case 8:
result = int(value)
default:
msb := uint(1 << (uint(bits) - 1))
if value >= msb {
result = -int((1 << uint(bits)) - value)
} else {
result = int(value)
}
}
return result
}