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
use std::io::{Read, Seek, SeekFrom};
use std::time::Duration;
use std::vec;
use Source;
use lewton::inside_ogg::OggStreamReader;
pub struct VorbisDecoder<R>
where
R: Read + Seek,
{
stream_reader: OggStreamReader<R>,
current_data: vec::IntoIter<i16>,
}
impl<R> VorbisDecoder<R>
where
R: Read + Seek,
{
pub fn new(mut data: R) -> Result<VorbisDecoder<R>, R> {
if !is_vorbis(data.by_ref()) {
return Err(data);
}
let mut stream_reader = OggStreamReader::new(data).unwrap();
let mut data = match stream_reader.read_dec_packet_itl().ok().and_then(|v| v) {
Some(d) => d,
None => Vec::new(),
};
match stream_reader.read_dec_packet_itl().ok().and_then(|v| v) {
Some(mut d) => data.append(&mut d),
None => (),
};
Ok(VorbisDecoder {
stream_reader: stream_reader,
current_data: data.into_iter(),
})
}
}
impl<R> Source for VorbisDecoder<R>
where
R: Read + Seek,
{
#[inline]
fn current_frame_len(&self) -> Option<usize> {
Some(self.current_data.len())
}
#[inline]
fn channels(&self) -> u16 {
self.stream_reader.ident_hdr.audio_channels as u16
}
#[inline]
fn sample_rate(&self) -> u32 {
self.stream_reader.ident_hdr.audio_sample_rate
}
#[inline]
fn total_duration(&self) -> Option<Duration> {
None
}
}
impl<R> Iterator for VorbisDecoder<R>
where
R: Read + Seek,
{
type Item = i16;
#[inline]
fn next(&mut self) -> Option<i16> {
if let Some(sample) = self.current_data.next() {
if self.current_data.len() == 0 {
if let Some(data) = self
.stream_reader
.read_dec_packet_itl()
.ok()
.and_then(|v| v)
{
self.current_data = data.into_iter();
}
}
return Some(sample);
} else {
if let Some(data) = self
.stream_reader
.read_dec_packet_itl()
.ok()
.and_then(|v| v)
{
self.current_data = data.into_iter();
}
return self.current_data.next();
}
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
(self.current_data.size_hint().0, None)
}
}
fn is_vorbis<R>(mut data: R) -> bool
where
R: Read + Seek,
{
let stream_pos = data.seek(SeekFrom::Current(0)).unwrap();
if OggStreamReader::new(data.by_ref()).is_err() {
data.seek(SeekFrom::Start(stream_pos)).unwrap();
return false;
}
data.seek(SeekFrom::Start(stream_pos)).unwrap();
true
}