Skip to content

Commit

Permalink
revert to upstream non-seekable minimp3, comment out minimp3-seek sup…
Browse files Browse the repository at this point in the history
…port
  • Loading branch information
dvdsk committed Oct 10, 2023
1 parent 95c8315 commit a0cd112
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 18 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ cpal = "0.15"
claxon = { version = "0.4.2", optional = true }
hound = { version = "3.3.1", optional = true }
lewton = { version = "0.10", optional = true }
# minimp3_fixed = { version = "0.5.4", optional = true}
minimp3 = { git = "https://github.com/dvdsk/minimp3-rs.git", optional = true }
minimp3_fixed = { version = "0.5.4", optional = true}
symphonia = { version = "0.5.2", optional = true, default-features = false }
crossbeam-channel = { version = "0.5.8", optional = true }
thiserror = "1.0.49"
Expand All @@ -27,7 +26,7 @@ flac = ["claxon"]
vorbis = ["lewton"]
wav = ["hound"]
mp3 = ["symphonia-mp3"]
minimp3 = ["dep:minimp3"]
minimp3 = ["dep:minimp3_fixed"]
wasm-bindgen = ["cpal/wasm-bindgen"]
symphonia-aac = ["symphonia/aac"]
symphonia-all = ["symphonia-aac", "symphonia-flac", "symphonia-isomp4", "symphonia-mp3", "symphonia-vorbis", "symphonia-wav"]
Expand Down
36 changes: 24 additions & 12 deletions src/decoder/mp3.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
use std::io::{Read, Seek, SeekFrom};
use std::time::Duration;

use crate::Source;
use crate::source::SeekError;
use crate::Source;

use minimp3_fixed as minimp3;
use minimp3::Frame;
use minimp3::{Decoder, SeekDecoder};
use minimp3::Decoder;
// use minimp3::{Decoder, SeekDecoder};

pub struct Mp3Decoder<R>
where
R: Read + Seek,
{
decoder: SeekDecoder<R>,
// decoder: SeekDecoder<R>,
decoder: Decoder<R>,
current_frame: Frame,
current_frame_offset: usize,
}
Expand All @@ -24,11 +27,13 @@ where
if !is_mp3(data.by_ref()) {
return Err(data);
}
let mut decoder = SeekDecoder::new(data)
// let mut decoder = SeekDecoder::new(data)
let mut decoder = Decoder::new(data);
// paramaters are correct and minimp3 is used correctly
// thus if we crash here one of these invariants is broken:
.expect("should be able to allocate memory, perform IO");
let current_frame = decoder.decode_frame()
// .expect("should be able to allocate memory, perform IO");
// let current_frame = decoder.decode_frame()
let current_frame = decoder.next_frame()
// the reader makes enough data availible therefore
// if we crash here the invariant broken is:
.expect("data should not corrupt");
Expand Down Expand Up @@ -69,11 +74,17 @@ where
}

fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
let pos = (pos.as_secs_f32() * 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
self.decoder.seek_samples(pos)?;
Ok(())
// TODO waiting for PR in minimp3_fixed or minimp3

// let pos = (pos.as_secs_f32() * 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
// self.decoder.seek_samples(pos)?;
// Ok(())

Err(SeekError::NotSupported {
underlying_source: std::any::type_name::<Self>(),
})
}
}

Expand All @@ -85,7 +96,8 @@ where

fn next(&mut self) -> Option<i16> {
if self.current_frame_offset == self.current_frame_len().unwrap() {
if let Ok(frame) = self.decoder.decode_frame() {
if let Ok(frame) = self.decoder.next_frame() {
// if let Ok(frame) = self.decoder.decode_frame() {
self.current_frame = frame;
self.current_frame_offset = 0;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/decoder/symphonia.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io::ErrorKind, time::Duration};
use std::time::Duration;
use symphonia::{
core::{
audio::{AudioBufferRef, SampleBuffer, SignalSpec},
Expand Down
2 changes: 1 addition & 1 deletion src/source/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ pub enum SeekError {
LewtonDecoder(#[from] lewton::VorbisError),
#[cfg(all(feature = "minimp3", not(feature = "symphonia-mp3")))]
#[error("Error seeking in mp3 source: {0}")]
Minimp3Decorder(#[from] minimp3::Error),
Minimp3Decorder(#[from] minimp3_fixed::Error),
#[error("An error occured")]
Other(Box<dyn std::error::Error + Send>),
}
Expand Down
2 changes: 1 addition & 1 deletion tests/seek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ fn sink_and_decoder(format: &str) -> (Sink, Decoder<impl Read + Seek>) {
fn format_decoder_info() -> &'static [(&'static str, bool, &'static str)] {
&[
#[cfg(feature = "minimp3")]
("mp3", true, "minimp3"),
("mp3", false, "minimp3"),
#[cfg(feature = "symphonia-mp3")]
("mp3", true, "symphonia"),
#[cfg(feature = "hound")]
Expand Down

0 comments on commit a0cd112

Please sign in to comment.