lieff / minimp3

Minimalistic MP3 decoder single header library
Creative Commons Zero v1.0 Universal
1.6k stars 214 forks source link

Minimp3 fails to cal function next_frame(). #120

Open GameBakerStudio opened 3 months ago

GameBakerStudio commented 3 months ago

While developing an audio-centered application, I came across an issue with the minimp3/minimp3_fixed crate when using it to decode mp3 files. The error/output looks like this:

Decoding MP3 File.
Iterating through PCM data.
thread 'main' panicked at library/core/src/panicking.rs:220:5:
unsafe precondition(s) violated: slice::get_unchecked_mut requires that the index is within the slice
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread caused non-unwinding panic. aborting
The error occurs when trying to call the decoder.next_frame() method.

[Uploading space_walk.txt…]()

Here's my code:

pub(crate) fn decode_mp3_file(file_path: &str) -> Vec<f32> {

    println!("Decoding MP3 File.");

    let mut decoder = Decoder::new(File::open(&file_path).unwrap());

    let mut output_data: Vec<f32> = Vec::new();

    println!("Iterating through PCM data.");

    loop {

        match decoder.next_frame() { // ERROR HERE
            Ok(Frame { data, sample_rate, channels, .. }) => {
                println!("Reading PCM data: {} samples, {} Hz, {} channels", data.len(), sample_rate, channels);
                for sample in data.iter() {
                    output_data.push(*sample as f32 / 32767.0); // Convert i16 to f32 PCM.
                }
            },
            Err(Error::Eof) => {
                println!("Reached end of file.");
                break;
            },
            Err(e) => {
                eprintln!("Error decoding MP3 frame: {:?}", e);
                break;
            },
        }

    }

    output_data

}

I previously used the crate creak, but that broke after updating cargo, so I decided that was a sign to revamp the mp3 loading to something more dedicated. This is important as it's evidence that there is nothing wrong with the file_path parameter, but rather with minimp3 directly.

I also tried switching to minimp3_fixed, which I am continuing to use, but had the exact same error.

What's truly strange is that I have looked over my code SO many times, and not once found any difference from the example code in the minimp3 and minimp3_fixed examples and docs on docs.rs, crates.io, and github.

What's also strange is that this is not a return from the error check? I thought an error with next_frame() would run the Err function I put there with "Error decoding MP3 frame".

I attached the MP3 file, but removed the extension as GitHub doesn't allow that.

mcclure commented 2 months ago

On my Linux machine I get this failure with ALL input mp3 files I have tested. I only get it in debug mode. In release mode I get no error. I am using rust 1.79.0.