-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreader.go
191 lines (178 loc) · 5.38 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
// mpa, an MPEG-1 Audio library
// Copyright (C) 2014 KORÁNDI Zoltán <korandi.z@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License, version 3 as
// published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
// Please note that, being hungarian, my last name comes before my first
// name. That's why it's in all caps, and not because I like to shout my
// name. So please don't start your emails with "Hi Korandi" or "Dear Mr.
// Zoltan", because it annoys the hell out of me. Thanks.
package mpa
// Named constants for Reader.Format.
const (
S16_LE = iota // Signed, 16-bit, little endian
S16_BE // Signed, 16-bit, big endian
U16_LE // Unsigned, 16-bit, little endian
U16_BE // Unsigned, 16-bit, big endian
S24_LE // Signed, 24-bit, little endian
S24_BE // Signed, 24-bit, big endian
U24_LE // Unsigned, 24-bit, little endian
U24_BE // Unsigned, 24-bit, big endian
S24_3LE // Signed, packed 24-bit, little endian
S24_3BE // Signed, packed 24-bit, big endian
U24_3LE // Unsigned, packed 24-bit, little endian
U24_3BE // Unsigned, packed 24-bit, big endian
S32_LE // Signed, 32-bit, little endian
S32_BE // Signed, 32-bit, big endian
U32_LE // Unsigned, 32-bit, little endian,
U32_BE // Unsigned, 32-bit, big endian
S8 // Signed, 8-bit
U8 // Unsigned, 8-bit
S20_3LE // Signed, packed 20-bit, little endian
S20_3BE // Signed, packed 20-bit, big endian
U20_3LE // Unsigned, packed 20-bit, little endian
U20_3BE // Unsigned, packed 20-bit, big endian
S18_3LE // Signed, packed 18-bit, little endian
S18_3BE // Signed, packed 18-bit, big endian
U18_3LE // Unsigned, packed 18-bit, little endian
U18_3BE // Unsigned, packed 18-bit, big endian
)
// A formatDescription specifies the signedness, endiannes and bit depth of a
// PCM stream.
type formatDescription struct {
signed bool
bits int
size int
le bool
}
// formatDescriptions lists all formats supported by Reader.
var formatDescriptions = [...]formatDescription{
S16_LE: {true, 16, 2, true},
S16_BE: {true, 16, 2, false},
U16_LE: {false, 16, 2, true},
U16_BE: {false, 16, 2, false},
S24_LE: {true, 24, 4, true},
S24_BE: {true, 24, 4, false},
U24_LE: {false, 24, 4, true},
U24_BE: {false, 24, 4, false},
S24_3LE: {true, 24, 3, true},
S24_3BE: {true, 24, 3, false},
U24_3LE: {false, 24, 3, true},
U24_3BE: {false, 24, 3, false},
S32_LE: {true, 32, 4, true},
S32_BE: {true, 32, 4, false},
U32_LE: {false, 32, 4, true},
U32_BE: {false, 32, 4, false},
S8: {true, 8, 1, false},
U8: {false, 8, 1, false},
S20_3LE: {true, 20, 3, true},
S20_3BE: {true, 20, 3, false},
U20_3LE: {false, 20, 3, true},
U20_3BE: {false, 20, 3, false},
S18_3LE: {true, 18, 3, true},
S18_3BE: {true, 18, 3, false},
U18_3LE: {false, 18, 3, true},
U18_3BE: {false, 18, 3, false},
}
// A Reader wraps a Decoder and converts its output into a byte stream.
type Reader struct {
Decoder *Decoder
Format int
Mono bool
Swap bool
buffer [8 * 1152]byte
unread []byte
}
// Read reads up to len(dst) bytes of PCM data into dst and returns the number
// of bytes read.
func (r *Reader) Read(dst []byte) (int, error) {
n := 0
for len(dst) > 0 {
if len(r.unread) == 0 {
if err := r.refill(); err != nil {
return n, err
}
}
m := copy(dst, r.unread)
n, dst, r.unread = n+m, dst[m:], r.unread[m:]
}
return n, nil
}
// refill fills the internal buffer with one frame's worth of data.
func (r *Reader) refill() error {
if err := r.Decoder.DecodeFrame(); err != nil {
return err
}
var samples [2][1152]float32
r.Decoder.ReadSamples(0, samples[0][:])
r.Decoder.ReadSamples(1, samples[1][:])
r.convert(&samples)
return nil
}
// convert converts the floating-point samples to the output format, writes the
// result into the buffer, and updates 'unread' accordingly.
func (r *Reader) convert(samples *[2][1152]float32) {
nSamples := r.Decoder.NSamples()
nChannels := 2
if r.Mono {
nChannels = 1
}
format := formatDescriptions[r.Format]
offset := float32(1)
if format.signed {
offset = 0
}
multiplier := float32(int(1) << uint(format.bits-1))
max := (1 + offset) * multiplier
le := format.le
size := format.size
jump := size
if !r.Mono {
jump *= 2
}
for outCh := 0; outCh < nChannels; outCh++ {
inCh := outCh
if r.Swap {
inCh = 1 - outCh
}
ptr := outCh * size
for t := 0; t < nSamples; t++ {
s := (samples[inCh][t] + offset) * multiplier
if s >= max {
s--
}
var si int
if s < 0 {
si = int(s - 0.5)
} else {
si = int(s + 0.5)
}
if le {
for i := 0; i < size; i++ {
r.buffer[ptr+i] = byte(si & 0xff)
si >>= 8
}
} else {
for i := 0; i < size; i++ {
r.buffer[ptr+size-1-i] = byte(si & 0xff)
si >>= 8
}
}
ptr += jump
}
if r.Mono {
break
}
}
r.unread = r.buffer[0 : jump*nSamples]
}