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
pub use error::Error;
pub use minimp3_sys as ffi;
use slice_deque::SliceDeque;
use std::{io, marker::Send, mem};
mod error;
pub const MAX_SAMPLES_PER_FRAME: usize = ffi::MINIMP3_MAX_SAMPLES_PER_FRAME as usize;
const BUFFER_SIZE: usize = MAX_SAMPLES_PER_FRAME * 15;
const REFILL_TRIGGER: usize = MAX_SAMPLES_PER_FRAME * 8;
pub struct Decoder<R> {
reader: R,
buffer: SliceDeque<u8>,
buffer_refill: Box<[u8; MAX_SAMPLES_PER_FRAME * 5]>,
decoder: Box<ffi::mp3dec_t>,
}
unsafe impl<R: Send> Send for Decoder<R> {}
#[derive(Debug, Clone)]
pub struct Frame {
pub data: Vec<i16>,
pub sample_rate: i32,
pub channels: usize,
pub layer: usize,
pub bitrate: i32,
}
impl<R> Decoder<R> {
pub fn new(reader: R) -> Self {
let mut minidec = unsafe { Box::new(mem::zeroed()) };
unsafe { ffi::mp3dec_init(&mut *minidec) }
Self {
reader,
buffer: SliceDeque::with_capacity(BUFFER_SIZE),
buffer_refill: Box::new([0; MAX_SAMPLES_PER_FRAME * 5]),
decoder: minidec,
}
}
pub fn reader(&self) -> &R {
&self.reader
}
pub fn reader_mut(&mut self) -> &mut R {
&mut self.reader
}
pub fn into_inner(self) -> R {
self.reader
}
fn decode_frame(&mut self) -> Result<Frame, Error> {
let mut frame_info = unsafe { mem::zeroed() };
let mut pcm = Vec::with_capacity(MAX_SAMPLES_PER_FRAME);
let samples: usize = unsafe {
ffi::mp3dec_decode_frame(
&mut *self.decoder,
self.buffer.as_ptr(),
self.buffer.len() as _,
pcm.as_mut_ptr(),
&mut frame_info,
) as _
};
if samples > 0 {
unsafe {
pcm.set_len(samples * frame_info.channels as usize);
}
}
let frame = Frame {
data: pcm,
sample_rate: frame_info.hz,
channels: frame_info.channels as usize,
layer: frame_info.layer as usize,
bitrate: frame_info.bitrate_kbps,
};
let current_len = self.buffer.len();
self.buffer
.truncate_front(current_len - frame_info.frame_bytes as usize);
if samples == 0 {
if frame_info.frame_bytes > 0 {
Err(Error::SkippedData)
} else {
Err(Error::InsufficientData)
}
} else {
Ok(frame)
}
}
}
#[cfg(feature = "async_tokio")]
impl<R: tokio::io::AsyncRead + std::marker::Unpin> Decoder<R> {
pub async fn next_frame_future(&mut self) -> Result<Frame, Error> {
loop {
let bytes_read = if self.buffer.len() < REFILL_TRIGGER {
Some(self.refill_future().await?)
} else {
None
};
match self.decode_frame() {
Ok(frame) => return Ok(frame),
Err(Error::InsufficientData) | Err(Error::SkippedData) => {
if let Some(0) = bytes_read {
return Err(Error::Eof);
}
}
Err(e) => return Err(e),
}
}
}
async fn refill_future(&mut self) -> Result<usize, io::Error> {
use tokio::io::AsyncReadExt;
let read_bytes = self.reader.read(&mut self.buffer_refill[..]).await?;
self.buffer.extend(self.buffer_refill[..read_bytes].iter());
Ok(read_bytes)
}
}
impl<R: io::Read> Decoder<R> {
pub fn next_frame(&mut self) -> Result<Frame, Error> {
loop {
let bytes_read = if self.buffer.len() < REFILL_TRIGGER {
Some(self.refill()?)
} else {
None
};
match self.decode_frame() {
Ok(frame) => return Ok(frame),
Err(Error::InsufficientData) | Err(Error::SkippedData) => {
if let Some(0) = bytes_read {
return Err(Error::Eof);
}
}
Err(e) => return Err(e),
}
}
}
fn refill(&mut self) -> Result<usize, io::Error> {
let read_bytes = self.reader.read(&mut self.buffer_refill[..])?;
self.buffer.extend(self.buffer_refill[..read_bytes].iter());
Ok(read_bytes)
}
}