-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathdecode.go
215 lines (201 loc) · 5.18 KB
/
decode.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
package minimp3
/*
#define MINIMP3_IMPLEMENTATION
#include "minimp3.h"
#include <stdlib.h>
#include <stdio.h>
int decode(mp3dec_t *dec, mp3dec_frame_info_t *info, unsigned char *data, int *length, unsigned char *decoded, int *decoded_length) {
int samples;
short pcm[MINIMP3_MAX_SAMPLES_PER_FRAME];
samples = mp3dec_decode_frame(dec, data, *length, pcm, info);
*decoded_length = samples * info->channels * 2;
*length -= info->frame_bytes;
unsigned char buffer[samples * info->channels * 2];
memcpy(buffer, (unsigned char*)&(pcm), sizeof(short) * samples * info->channels);
memcpy(decoded, buffer, sizeof(short) * samples * info->channels);
return info->frame_bytes;
}
*/
import "C"
import (
"context"
"io"
"sync"
"time"
"unsafe"
)
const maxSamplesPerFrame = 1152 * 2
// Decoder decode the mp3 stream by minimp3
type Decoder struct {
readerLocker *sync.Mutex
data []byte
decoderLocker *sync.Mutex
decodedData []byte
decode C.mp3dec_t
info C.mp3dec_frame_info_t
context context.Context
contextCancel context.CancelFunc
SampleRate int
Channels int
Kbps int
Layer int
originalEof bool // if the original reader is EOF, set this to true
}
// BufferSize Decoded data buffer size.
var BufferSize = 1024 * 10
// WaitForDataDuration wait for the data time duration.
var WaitForDataDuration = time.Millisecond * 10
// NewDecoder decode mp3 stream and get a Decoder for read the raw data to play.
func NewDecoder(reader io.Reader) (dec *Decoder, err error) {
dec = new(Decoder)
dec.readerLocker = new(sync.Mutex)
dec.decoderLocker = new(sync.Mutex)
dec.context, dec.contextCancel = context.WithCancel(context.Background())
dec.decode = C.mp3dec_t{}
C.mp3dec_init(&dec.decode)
dec.info = C.mp3dec_frame_info_t{}
go func() {
for {
select {
case <-dec.context.Done():
return
default:
}
if len(dec.data) > BufferSize {
<-time.After(WaitForDataDuration)
continue
}
var data = make([]byte, 512)
var n int
n, err = reader.Read(data)
dec.readerLocker.Lock()
dec.data = append(dec.data, data[:n]...)
dec.readerLocker.Unlock()
if err == io.EOF {
dec.originalEof = true
break
}
if err != nil {
dec.originalEof = true
break
}
}
}()
go func() {
for {
select {
case <-dec.context.Done():
return
default:
}
if len(dec.decodedData) > BufferSize {
<-time.After(WaitForDataDuration)
continue
}
var decoded = [maxSamplesPerFrame * 2]byte{}
var decodedLength = C.int(0)
var length = C.int(len(dec.data))
if len(dec.data) == 0 {
<-time.After(WaitForDataDuration)
continue
}
frameSize := C.decode(&dec.decode, &dec.info,
(*C.uchar)(unsafe.Pointer(&dec.data[0])),
&length, (*C.uchar)(unsafe.Pointer(&decoded[0])),
&decodedLength)
if int(frameSize) == 0 {
<-time.After(WaitForDataDuration)
continue
}
dec.SampleRate = int(dec.info.hz)
dec.Channels = int(dec.info.channels)
dec.Kbps = int(dec.info.bitrate_kbps)
dec.Layer = int(dec.info.layer)
dec.readerLocker.Lock()
dec.decoderLocker.Lock()
dec.decodedData = append(dec.decodedData, decoded[:decodedLength]...)
if int(frameSize) <= len(dec.data) {
dec.data = dec.data[int(frameSize):]
}
dec.decoderLocker.Unlock()
dec.readerLocker.Unlock()
}
}()
return
}
// Started check the record mp3 stream started ot not.
func (dec *Decoder) Started() (channel chan bool) {
channel = make(chan bool)
go func() {
for {
select {
case <-dec.context.Done():
channel <- false
default:
}
if len(dec.decodedData) != 0 {
channel <- true
} else {
<-time.After(time.Millisecond * 100)
}
}
}()
return
}
// Read read the raw stream
func (dec *Decoder) Read(data []byte) (n int, err error) {
for {
select {
case <-dec.context.Done(): // if the decoder is stopped, then here should return EOF
err = io.EOF
return
default:
}
if len(dec.data) == 0 && len(dec.decodedData) == 0 && dec.originalEof {
err = io.EOF
return
} else if len(dec.decodedData) > 0 {
break
}
<-time.After(WaitForDataDuration)
}
dec.decoderLocker.Lock()
defer dec.decoderLocker.Unlock()
n = copy(data, dec.decodedData[:])
dec.decodedData = dec.decodedData[n:]
return
}
// Close stop the decode mp3 stream cycle.
func (dec *Decoder) Close() {
if dec.contextCancel != nil {
dec.contextCancel()
}
}
// DecodeFull put all of the mp3 data to decode.
func DecodeFull(mp3 []byte) (dec *Decoder, decodedData []byte, err error) {
dec = new(Decoder)
dec.decode = C.mp3dec_t{}
C.mp3dec_init(&dec.decode)
info := C.mp3dec_frame_info_t{}
var length = C.int(len(mp3))
for {
var decoded = [maxSamplesPerFrame * 2]byte{}
var decodedLength = C.int(0)
frameSize := C.decode(&dec.decode,
&info, (*C.uchar)(unsafe.Pointer(&mp3[0])),
&length, (*C.uchar)(unsafe.Pointer(&decoded[0])),
&decodedLength)
if int(frameSize) == 0 {
break
}
decodedData = append(decodedData, decoded[:decodedLength]...)
if int(frameSize) < len(mp3) {
mp3 = mp3[int(frameSize):]
}
dec.SampleRate = int(info.hz)
dec.Channels = int(info.channels)
dec.Kbps = int(info.bitrate_kbps)
dec.Layer = int(info.layer)
}
return
}