fix(seek) vorbis decoder now respects channel order

This commit is contained in:
dvdsk 2024-04-03 12:29:31 +02:00
parent 04c6957dac
commit 1c821362fd
No known key found for this signature in database
GPG key ID: 6CF9D20C5709A836

View file

@ -1,6 +1,5 @@
use std::io::{Read, Seek, SeekFrom}; use std::io::{Read, Seek, SeekFrom};
use std::time::Duration; use std::time::Duration;
use std::vec;
use crate::source::SeekError; use crate::source::SeekError;
use crate::Source; use crate::Source;
@ -13,7 +12,8 @@ where
R: Read + Seek, R: Read + Seek,
{ {
stream_reader: OggStreamReader<R>, stream_reader: OggStreamReader<R>,
current_data: vec::IntoIter<i16>, current_data: Vec<i16>,
next: usize,
} }
impl<R> VorbisDecoder<R> impl<R> VorbisDecoder<R>
@ -43,7 +43,8 @@ where
VorbisDecoder { VorbisDecoder {
stream_reader, stream_reader,
current_data: data.into_iter(), current_data: data,
next: 0,
} }
} }
pub fn into_inner(self) -> OggStreamReader<R> { pub fn into_inner(self) -> OggStreamReader<R> {
@ -83,13 +84,20 @@ where
// first few frames (packets) sometimes fail to decode, if // first few frames (packets) sometimes fail to decode, if
// that happens just retry a few times. // that happens just retry a few times.
let mut last_err = None; let mut last_err = None;
for _ in 0..10 { // 10 seemed to work best in testing for _ in 0..10 {
// 10 seemed to work best in testing
let res = self.stream_reader.read_dec_packet_itl(); let res = self.stream_reader.read_dec_packet_itl();
match res { match res {
Ok(data) => { Ok(data) => {
match data { match data {
Some(d) => self.current_data = d.into_iter(), Some(d) => self.current_data = d,
None => self.current_data = Vec::new().into_iter(), None => self.current_data = Vec::new(),
}
// make sure the next seek returns the
// sample for the correct speaker
let to_skip = self.next % self.channels() as usize;
for _ in 0..to_skip {
self.next();
} }
return Ok(()); return Ok(());
} }
@ -111,24 +119,29 @@ where
#[inline] #[inline]
fn next(&mut self) -> Option<i16> { fn next(&mut self) -> Option<i16> {
if let Some(sample) = self.current_data.next() { if let Some(sample) = self.current_data.get(self.next).copied() {
self.next += 1;
if self.current_data.len() == 0 { if self.current_data.len() == 0 {
if let Ok(Some(data)) = self.stream_reader.read_dec_packet_itl() { if let Ok(Some(data)) = self.stream_reader.read_dec_packet_itl() {
self.current_data = data.into_iter(); self.current_data = data;
self.next = 0;
} }
} }
Some(sample) Some(sample)
} else { } else {
if let Ok(Some(data)) = self.stream_reader.read_dec_packet_itl() { if let Ok(Some(data)) = self.stream_reader.read_dec_packet_itl() {
self.current_data = data.into_iter(); self.current_data = data;
self.next = 0;
} }
self.current_data.next() let sample = self.current_data.get(self.next).copied();
self.next += 1;
sample
} }
} }
#[inline] #[inline]
fn size_hint(&self) -> (usize, Option<usize>) { fn size_hint(&self) -> (usize, Option<usize>) {
(self.current_data.size_hint().0, None) (self.current_data.len(), None)
} }
} }