Repository navigation
Expand file tree
/
Copy pathdecoder.go
More file actions
287 lines (260 loc) · 8.59 KB
/
Copy pathdecoder.go
File metadata and controls
287 lines (260 loc) · 8.59 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
//go:build darwin || freebsd || linux || netbsd || windows
package mago
import (
"fmt"
"unsafe"
)
// DecoderConfig configures a decoder. Leaving Channels or SampleRate at zero
// keeps the stream's native values.
type DecoderConfig struct {
Format Format
Channels uint32
SampleRate uint32
ChannelMixMode ChannelMixMode
DitherMode DitherMode
SeekPointCount uint32
}
// DefaultDecoderConfig returns a decoder that outputs 32-bit float frames at the
// stream's native channel count and sample rate.
func DefaultDecoderConfig() DecoderConfig {
return DecoderConfig{
Format: FormatF32,
ChannelMixMode: ChannelMixModeRectangular,
DitherMode: DitherModeNone,
}
}
// Decoder decodes an encoded stream (WAV, FLAC or MP3) into PCM frames.
type Decoder struct {
lib *Library
handle *decoderHandle
dataRef any
format Format
channels uint32
sampleRate uint32
looping bool
}
// NewDecoderMemory decodes from an in-memory encoded stream. The data slice is
// retained so it cannot be collected while the decoder reads from it.
func (lib *Library) NewDecoderMemory(data []byte, config DecoderConfig) (*Decoder, error) {
if err := lib.ensureOpen(); err != nil {
return nil, err
}
if len(data) == 0 {
return nil, fmt.Errorf("mago: decoder memory is empty")
}
return lib.newDecoder("ma_decoder_init_memory", config, data, func(native *decoderConfigNative, handle *decoderHandle) Result {
return lib.bindings.maDecoderInitMemory(unsafe.Pointer(&data[0]), uintptr(len(data)), native, handle)
})
}
// NewDecoderFile decodes from a file on disk.
func (lib *Library) NewDecoderFile(path string, config DecoderConfig) (*Decoder, error) {
if err := lib.ensureOpen(); err != nil {
return nil, err
}
return lib.newDecoder("ma_decoder_init_file", config, nil, func(native *decoderConfigNative, handle *decoderHandle) Result {
return lib.bindings.maDecoderInitFile(path, native, handle)
})
}
func (lib *Library) newDecoder(op string, config DecoderConfig, dataRef any, init func(*decoderConfigNative, *decoderHandle) Result) (*Decoder, error) {
native := decoderConfigNative{
Format: config.Format,
Channels: config.Channels,
SampleRate: config.SampleRate,
ChannelMixMode: config.ChannelMixMode,
DitherMode: config.DitherMode,
SeekPointCount: config.SeekPointCount,
Resampling: resamplerConfigNative{
Algorithm: ResampleAlgorithmLinear,
Linear: resamplerLinearConfigNative{LPFOrder: 4},
},
}
handle := (*decoderHandle)(lib.bindings.magoAlloc(magoObjectDecoder))
if handle == nil {
return nil, fmt.Errorf("mago: allocate decoder: out of memory")
}
if result := init(&native, handle); result != Success {
lib.bindings.magoFree(unsafe.Pointer(handle))
return nil, lib.resultError(op, result)
}
decoder := &Decoder{lib: lib, handle: handle, dataRef: dataRef}
format, channels, sampleRate, err := decoder.DataFormat()
if err != nil {
lib.bindings.maDecoderUninit(handle)
lib.bindings.magoFree(unsafe.Pointer(handle))
return nil, err
}
decoder.format = format
decoder.channels = channels
decoder.sampleRate = sampleRate
return decoder, nil
}
// DataFormat reports the decoder's output sample format, channels and sample
// rate.
func (d *Decoder) DataFormat() (Format, uint32, uint32, error) {
if d == nil || d.handle == nil {
return FormatUnknown, 0, 0, fmt.Errorf("mago: nil decoder")
}
if err := d.lib.ensureOpen(); err != nil {
return FormatUnknown, 0, 0, err
}
var format Format
var channels, sampleRate uint32
if result := d.lib.bindings.maDecoderGetDataFormat(d.handle, &format, &channels, &sampleRate, nil, 0); result != Success {
return FormatUnknown, 0, 0, d.lib.resultError("ma_decoder_get_data_format", result)
}
return format, channels, sampleRate, nil
}
// ReadF32 decodes up to len(out) float32 samples into out, returning the number
// of frames read. The decoder must be configured for f32 output. Reaching the
// end of the stream is reported as a read of zero frames, not an error.
func (d *Decoder) ReadF32(out []float32) (uint64, error) {
if d == nil || d.handle == nil {
return 0, fmt.Errorf("mago: nil decoder")
}
if err := d.lib.ensureOpen(); err != nil {
return 0, err
}
if len(out) == 0 {
return 0, nil
}
if d.format != FormatF32 {
return 0, fmt.Errorf("mago: decoder output format is %d, not f32", d.format)
}
frameCount, err := d.frameCount(len(out))
if err != nil {
return 0, err
}
return d.readFrames(unsafe.Pointer(&out[0]), frameCount, d.channels)
}
func (d *Decoder) readFrames(out unsafe.Pointer, framesNeeded uint64, channels uint32) (uint64, error) {
var total uint64
rewound := false
for total < framesNeeded {
var read uint64
result := d.lib.bindings.maDecoderReadPCMFrames(d.handle, unsafe.Pointer(uintptr(out)+uintptr(total*uint64(channels)*4)), framesNeeded-total, &read)
if result != Success && result != AtEnd {
return total, d.lib.resultError("ma_decoder_read_pcm_frames", result)
}
total += read
if read > 0 {
rewound = false
continue
}
if !d.looping {
return total, nil
}
if rewound {
return total, nil
}
if err := d.SeekToPCMFrame(0); err != nil {
return total, err
}
rewound = true
}
return total, nil
}
// ReadS16 decodes up to len(out) signed 16-bit samples into out, returning the
// number of frames read. The decoder must be configured for s16 output. Reaching
// the end of the stream is reported as a read of zero frames, not an error.
func (d *Decoder) ReadS16(out []int16) (uint64, error) {
if d == nil || d.handle == nil {
return 0, fmt.Errorf("mago: nil decoder")
}
if err := d.lib.ensureOpen(); err != nil {
return 0, err
}
if len(out) == 0 {
return 0, nil
}
if d.format != FormatS16 {
return 0, fmt.Errorf("mago: decoder output format is %d, not s16", d.format)
}
frameCount, err := d.frameCount(len(out))
if err != nil {
return 0, err
}
return d.readFrames16(unsafe.Pointer(&out[0]), frameCount, d.channels)
}
func (d *Decoder) readFrames16(out unsafe.Pointer, framesNeeded uint64, channels uint32) (uint64, error) {
var total uint64
rewound := false
for total < framesNeeded {
var read uint64
result := d.lib.bindings.maDecoderReadPCMFrames(d.handle, unsafe.Pointer(uintptr(out)+uintptr(total*uint64(channels)*2)), framesNeeded-total, &read)
if result != Success && result != AtEnd {
return total, d.lib.resultError("ma_decoder_read_pcm_frames", result)
}
total += read
if read > 0 {
rewound = false
continue
}
if !d.looping {
return total, nil
}
if rewound {
return total, nil
}
if err := d.SeekToPCMFrame(0); err != nil {
return total, err
}
rewound = true
}
return total, nil
}
func (d *Decoder) frameCount(sampleCount int) (uint64, error) {
if d.channels == 0 {
return 0, fmt.Errorf("mago: decoder reports zero channels")
}
return uint64(sampleCount) / uint64(d.channels), nil
}
// SeekToPCMFrame moves the decode position.
func (d *Decoder) SeekToPCMFrame(frameIndex uint64) error {
if d == nil || d.handle == nil {
return fmt.Errorf("mago: nil decoder")
}
if err := d.lib.ensureOpen(); err != nil {
return err
}
return d.lib.resultError("ma_decoder_seek_to_pcm_frame", d.lib.bindings.maDecoderSeekToPCMFrame(d.handle, frameIndex))
}
// CursorInPCMFrames reports the current decode position.
func (d *Decoder) CursorInPCMFrames() (uint64, error) {
return d.readFrameCount("ma_decoder_get_cursor_in_pcm_frames", d.lib.bindings.maDecoderGetCursorInPCMFrames)
}
// LengthInPCMFrames reports the total length of the stream.
func (d *Decoder) LengthInPCMFrames() (uint64, error) {
return d.readFrameCount("ma_decoder_get_length_in_pcm_frames", d.lib.bindings.maDecoderGetLengthInPCMFrames)
}
// AvailableFrames reports how many frames remain before the end of the stream.
func (d *Decoder) AvailableFrames() (uint64, error) {
return d.readFrameCount("ma_decoder_get_available_frames", d.lib.bindings.maDecoderGetAvailableFrames)
}
func (d *Decoder) readFrameCount(op string, fn func(*decoderHandle, *uint64) Result) (uint64, error) {
if d == nil || d.handle == nil {
return 0, fmt.Errorf("mago: nil decoder")
}
if err := d.lib.ensureOpen(); err != nil {
return 0, err
}
var value uint64
if result := fn(d.handle, &value); result != Success {
return 0, d.lib.resultError(op, result)
}
return value, nil
}
// Close uninitializes the decoder and frees it. Any retained input data is
// released.
func (d *Decoder) Close() error {
if d == nil || d.handle == nil {
return nil
}
if err := d.lib.ensureOpen(); err != nil {
return err
}
d.lib.bindings.maDecoderUninit(d.handle)
d.lib.bindings.magoFree(unsafe.Pointer(d.handle))
d.handle = nil
d.dataRef = nil
return nil
}