request pos now uses mutable self

This commit is contained in:
dskleingeld 2021-01-13 23:31:05 +01:00 committed by dvdsk
parent fafe4ba1af
commit 023e833b01
No known key found for this signature in database
GPG key ID: 6CF9D20C5709A836
7 changed files with 49 additions and 41 deletions

View file

@ -5,10 +5,13 @@ fn main() {
let sink = rodio::Sink::try_new(&handle).unwrap();
let file = std::fs::File::open("examples/music.mp3").unwrap();
sink.append(rodio::Decoder::new(BufReader::new(file)).unwrap());
sink.append_seekable(rodio::Decoder::new(BufReader::new(file)).unwrap());
std::thread::sleep(std::time::Duration::from_secs(2));
sink.set_pos(1.0);
loop {
std::thread::sleep(std::time::Duration::from_secs(2));
sink.set_pos(2.0);
dbg!("setting pos");
}
sink.sleep_until_end();
}

View file

@ -372,7 +372,7 @@ impl<R> Decoder<R>
where
R: Read + Seek,
{
fn request_pos(&self, pos: f32) -> bool {
fn request_pos(&mut self, pos: f32) -> bool {
match self.0 {
#[cfg(feature = "wav")]
DecoderImpl::Wav(ref source) => false,
@ -381,31 +381,31 @@ where
#[cfg(feature = "flac")]
DecoderImpl::Flac(ref source) => false,
#[cfg(feature = "mp3")]
DecoderImpl::Mp3(ref source) => source.request_pos(pos),
DecoderImpl::Mp3(ref mut source) => source.request_pos(pos),
DecoderImpl::None(_) => false,
}
}
}
// impl<R> SourceExt for Decoder<R>
// where
// R: Read + Seek,
// {
// fn request_pos(&self, pos: f32) -> bool {
// match self.0 {
// #[cfg(feature = "wav")]
// DecoderImpl::Wav(ref source) => false,
// #[cfg(feature = "vorbis")]
// DecoderImpl::Vorbis(ref source) => false,
// #[cfg(feature = "flac")]
// DecoderImpl::Flac(ref source) => false,
// #[cfg(feature = "mp3")]
// DecoderImpl::Mp3(ref source) => {source.test(); false},
// DecoderImpl::None(_) => false,
// };
// todo!();
// }
// }
impl<R> SourceExt for Decoder<R>
where
R: Read + Seek,
{
fn request_pos(&mut self, pos: f32) -> bool {
match self.0 {
#[cfg(feature = "wav")]
DecoderImpl::Wav(ref source) => false,
#[cfg(feature = "vorbis")]
DecoderImpl::Vorbis(ref source) => false,
#[cfg(feature = "flac")]
DecoderImpl::Flac(ref source) => false,
#[cfg(feature = "mp3")]
DecoderImpl::Mp3(ref mut source) => {source.request_pos(pos)},
DecoderImpl::None(_) => false,
}
// todo!();
}
}
impl<R> Iterator for LoopedDecoder<R>
where

View file

@ -3,8 +3,8 @@ use std::time::Duration;
use crate::{Source, SourceExt};
use minimp3::{Frame, Decoder};
// use minimp3::SeekDecoder as Decoder;
use minimp3::Frame;
use minimp3::SeekDecoder as Decoder;
pub struct Mp3Decoder<R>
where
@ -23,8 +23,10 @@ where
if !is_mp3(data.by_ref()) {
return Err(data);
}
let mut decoder = Decoder::new(data);
let current_frame = decoder.next_frame().unwrap();
let mut decoder = Decoder::new(data).map_err(|_| ())?;
// TODO: figure out decode_frame vs next_frame <dvdsk noreply@davidsk.dev>
// let current_frame = decoder.next_frame().unwrap();
let current_frame = decoder.decode_frame().map_err(|_| ())?;
Ok(Mp3Decoder {
decoder,
@ -66,9 +68,12 @@ impl<R> SourceExt for Mp3Decoder<R>
where
R: Read + Seek,
{
fn request_pos(&self, pos: f32) -> bool {
// self.seek_samples(0); //TODO
true
fn request_pos(&mut self, pos: f32) -> bool {
let pos = (pos * self.sample_rate() as f32) as u64;
// do not trigger a sample_rate, channels and frame len update
// as the seek only takes effect after the current frame is done
dbg!("seek");
self.decoder.seek_samples(pos).is_ok()
}
}
@ -78,14 +83,14 @@ where
{
type Item = i16;
#[inline]
fn next(&mut self) -> Option<i16> {
if self.current_frame_offset == self.current_frame.data.len() {
match self.decoder.next_frame() {
Ok(frame) => self.current_frame = frame,
_ => return None,
if self.current_frame_offset == self.current_frame_len().unwrap() {
if let Ok(frame) = self.decoder.decode_frame() {
self.current_frame = frame;
self.current_frame_offset = 0;
} else {
return None;
}
self.current_frame_offset = 0;
}
let v = self.current_frame.data[self.current_frame_offset];

View file

@ -101,7 +101,7 @@ where
I: SourceExt,
I::Item: Sample,
{
fn request_pos(&self, pos: f32) -> bool {
fn request_pos(&mut self, pos: f32) -> bool {
self.input.request_pos(pos)
}
}

View file

@ -430,5 +430,5 @@ where
pub trait SourceExt {
/// Seek to pos and whether the seek succeeded
fn request_pos(&self, pos: f32) -> bool;
fn request_pos(&mut self, pos: f32) -> bool;
}

View file

@ -123,7 +123,7 @@ where
I: SourceExt,
I::Item: Sample,
{
fn request_pos(&self, pos: f32) -> bool {
fn request_pos(&mut self, pos: f32) -> bool {
self.input.request_pos(pos)
}
}

View file

@ -96,7 +96,7 @@ where
I: SourceExt,
I::Item: Sample,
{
fn request_pos(&self, pos: f32) -> bool {
fn request_pos(&mut self, pos: f32) -> bool {
self.input.request_pos(pos)
}
}