11use std:: io:: { Read , Seek , SeekFrom } ;
22use std:: time:: Duration ;
3- use std:: vec;
43
54use crate :: source:: SeekError ;
65use crate :: Source ;
1312 R : Read + Seek ,
1413{
1514 stream_reader : OggStreamReader < R > ,
16- current_data : vec:: IntoIter < i16 > ,
15+ current_data : Vec < i16 > ,
16+ next : usize ,
1717}
1818
1919impl < R > VorbisDecoder < R >
4343
4444 VorbisDecoder {
4545 stream_reader,
46- current_data : data. into_iter ( ) ,
46+ current_data : data,
47+ next : 0 ,
4748 }
4849 }
4950 pub fn into_inner ( self ) -> OggStreamReader < R > {
@@ -83,13 +84,20 @@ where
8384 // first few frames (packets) sometimes fail to decode, if
8485 // that happens just retry a few times.
8586 let mut last_err = None ;
86- for _ in 0 ..10 { // 10 seemed to work best in testing
87+ for _ in 0 ..10 {
88+ // 10 seemed to work best in testing
8789 let res = self . stream_reader . read_dec_packet_itl ( ) ;
8890 match res {
8991 Ok ( data) => {
9092 match data {
91- Some ( d) => self . current_data = d. into_iter ( ) ,
92- None => self . current_data = Vec :: new ( ) . into_iter ( ) ,
93+ Some ( d) => self . current_data = d,
94+ None => self . current_data = Vec :: new ( ) ,
95+ }
96+ // make sure the next seek returns the
97+ // sample for the correct speaker
98+ let to_skip = self . next % self . channels ( ) as usize ;
99+ for _ in 0 ..to_skip {
100+ self . next ( ) ;
93101 }
94102 return Ok ( ( ) ) ;
95103 }
@@ -111,24 +119,29 @@ where
111119
112120 #[ inline]
113121 fn next ( & mut self ) -> Option < i16 > {
114- if let Some ( sample) = self . current_data . next ( ) {
122+ if let Some ( sample) = self . current_data . get ( self . next ) . copied ( ) {
123+ self . next += 1 ;
115124 if self . current_data . len ( ) == 0 {
116125 if let Ok ( Some ( data) ) = self . stream_reader . read_dec_packet_itl ( ) {
117- self . current_data = data. into_iter ( ) ;
126+ self . current_data = data;
127+ self . next = 0 ;
118128 }
119129 }
120130 Some ( sample)
121131 } else {
122132 if let Ok ( Some ( data) ) = self . stream_reader . read_dec_packet_itl ( ) {
123- self . current_data = data. into_iter ( ) ;
133+ self . current_data = data;
134+ self . next = 0 ;
124135 }
125- self . current_data . next ( )
136+ let sample = self . current_data . get ( self . next ) . copied ( ) ;
137+ self . next += 1 ;
138+ sample
126139 }
127140 }
128141
129142 #[ inline]
130143 fn size_hint ( & self ) -> ( usize , Option < usize > ) {
131- ( self . current_data . size_hint ( ) . 0 , None )
144+ ( self . current_data . len ( ) , None )
132145 }
133146}
134147
0 commit comments