-
Notifications
You must be signed in to change notification settings - Fork 1
/
reader.go
176 lines (153 loc) · 3.69 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
package mpg123
import (
"errors"
"io"
"io/ioutil"
)
// Reader implements on-the-fly mp3 decoding
// for an io.Reader
type Reader struct {
input io.Reader
h *Handle
feedBuf []byte
nextOffset int64
totalRead int64
bytesSinceOk int
maxBadBytes int
needMore bool
offset Offset
frameInfo FrameInfo
outputFormat OutputFormat
meta Meta
}
type ReaderConfig struct {
// Output sample format of the decoded audio
OutputFormat *OutputFormat
// Internal buffer size
BufferSize int
}
var DefaultConfig = ReaderConfig{
OutputFormat: nil,
BufferSize: 16 * 1024,
}
// NewReaderConfig returns a new Reader
// configured with supplied config
func NewReaderConfig(r io.Reader, config ReaderConfig) *Reader {
h, _ := NewDefaultHandle()
if config.OutputFormat != nil {
h.SetOutputFormat(*config.OutputFormat)
}
if config.BufferSize == 0 {
config.BufferSize = DefaultConfig.BufferSize
}
h.OpenFeed()
return &Reader{
input: r,
h: h,
maxBadBytes: 4096,
feedBuf: make([]byte, config.BufferSize),
}
}
// NewReader returns a new Reader
// configured with the supplied config
func NewReader(r io.Reader) *Reader {
return NewReaderConfig(r, ReaderConfig{})
}
// Offset returns current stream offset
func (r *Reader) Offset() Offset {
return r.offset
}
// FrameInfo returns the most recent frame info
func (r *Reader) FrameInfo() FrameInfo {
return r.frameInfo
}
// OutputFormat returns the decoder output format
func (r *Reader) OutputFormat() OutputFormat {
return r.outputFormat
}
// Meta returns stream metadata
func (r *Reader) Meta() Meta {
return r.meta
}
// Read implements io.Reader, reading decoded
// samples from its underlying Reader.
// The sample format depends on the configuration.
// Read(nil) allows to find the beginning the stream
// without consuming any samples
func (r *Reader) Read(buf []byte) (int, error) {
defer func() {
r.offset = r.h.Offset()
r.frameInfo = r.h.FrameInfo()
f := r.h.MetaCheck()
switch {
case f&MetaNewID3 != 0:
id3v2, err := r.h.MetaID3()
if id3v2 != nil && err == nil {
r.meta.ID3v2 = id3v2
}
}
}()
if r.nextOffset > r.totalRead {
n, err := io.CopyN(ioutil.Discard, r.input, r.nextOffset-r.totalRead)
r.totalRead += n
if err != nil {
return 0, err
}
}
for r.bytesSinceOk < r.maxBadBytes {
var feed []byte
if r.needMore {
r.needMore = false
feedLen, err := r.input.Read(r.feedBuf)
r.totalRead += int64(feedLen)
r.nextOffset = r.totalRead
if feedLen == 0 && err != nil {
return 0, err
}
feed = r.feedBuf[:feedLen]
r.bytesSinceOk += feedLen
}
switch n, err := r.h.Decode(feed, buf); err {
case ErrNewFormat:
r.outputFormat = r.h.OutputFormat()
r.bytesSinceOk = 0
if len(buf) == 0 {
return n, nil
}
case ErrNeedMore:
r.needMore = true
if n > 0 {
r.bytesSinceOk = 0
return n, nil
}
case ErrDone:
return n, io.EOF
default:
r.bytesSinceOk = 0
return n, nil
}
}
r.bytesSinceOk = 0
return 0, errors.New("No valid data found")
}
// Seek sets the offset in samples of the next Read.
// If underlying reader is not io.Seeker only io.SeekStart and io.SeekCurrent
// with positive offsets are supported
func (r *Reader) Seek(sampleOffset int64, whence int) (int64, error) {
newOffset, inputBytesOffset, err := r.h.FeedSeek(sampleOffset, whence)
if err != nil {
return 0, err
}
switch ir := r.input.(type) {
case io.Seeker:
if _, err := ir.Seek(int64(inputBytesOffset), io.SeekStart); err != nil {
return 0, err
}
default:
if inputBytesOffset < r.totalRead {
return 0, errors.New("Seeking back is not supported")
}
r.nextOffset = inputBytesOffset
}
return newOffset, nil
}