rodio panic when trying to play Sink with reverb effect

UPDATED: This issue is officially a bug.

I’m adding a reverb effect following this example, but panic when trying to play the sink.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>thread '<unnamed>' panicked at /feelslikehome/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rodio-0.19.0/src/conversions/channels.rs:100:13:
attempt to subtract with overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: Rust panics must be rethrown
</code>
<code>thread '<unnamed>' panicked at /feelslikehome/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rodio-0.19.0/src/conversions/channels.rs:100:13: attempt to subtract with overflow note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace fatal runtime error: Rust panics must be rethrown </code>
thread '<unnamed>' panicked at /feelslikehome/.cargo/registry/src/index.crates.io-6f17d22bba15001f/rodio-0.19.0/src/conversions/channels.rs:100:13:
attempt to subtract with overflow
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: Rust panics must be rethrown

This is what I did:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>
fn main() {
// This snippet is from the example
let (_stream, handle) = rodio::OutputStream::try_default().unwrap();
let sink = rodio::Sink::try_new(&handle).unwrap();
let file = std::fs::File::open("assets/music.ogg").unwrap();
// This is originally a function that returns (Mode, Sink, Stream) tuple
match Decoder::new(BufReader::new(file)) {
Ok(decoder) => {
let mode = get_mode();
if mode == Mode::Bass {
// Duration is just an arbitrary number, I tried 0, milliseconds, etc.
let reverb = decoder.buffered().reverb(Duration::from_secs(30), 1.0);
sink.append(reverb);
} else {
sink.append(decoder);
}
// Try playing it right away, it panicked!
sink.play();
Some((mode, sink, stream))
}
Err(e) => {
info!("Failed to create decoder: {:?}", e);
None
}
}
}
fn get_mode() -> Mode {
Mode::Bass
}
enum Mode {
case Bass
}
</code>
<code> fn main() { // This snippet is from the example let (_stream, handle) = rodio::OutputStream::try_default().unwrap(); let sink = rodio::Sink::try_new(&handle).unwrap(); let file = std::fs::File::open("assets/music.ogg").unwrap(); // This is originally a function that returns (Mode, Sink, Stream) tuple match Decoder::new(BufReader::new(file)) { Ok(decoder) => { let mode = get_mode(); if mode == Mode::Bass { // Duration is just an arbitrary number, I tried 0, milliseconds, etc. let reverb = decoder.buffered().reverb(Duration::from_secs(30), 1.0); sink.append(reverb); } else { sink.append(decoder); } // Try playing it right away, it panicked! sink.play(); Some((mode, sink, stream)) } Err(e) => { info!("Failed to create decoder: {:?}", e); None } } } fn get_mode() -> Mode { Mode::Bass } enum Mode { case Bass } </code>
        
    fn main() {
        
        // This snippet is from the example
        let (_stream, handle) = rodio::OutputStream::try_default().unwrap();
        let sink = rodio::Sink::try_new(&handle).unwrap();
        let file = std::fs::File::open("assets/music.ogg").unwrap();

        // This is originally a function that returns (Mode, Sink, Stream) tuple
        match Decoder::new(BufReader::new(file)) {
            Ok(decoder) => {
                let mode = get_mode();
                if mode == Mode::Bass {
                    // Duration is just an arbitrary number, I tried 0, milliseconds, etc.
                    let reverb = decoder.buffered().reverb(Duration::from_secs(30), 1.0);
                    sink.append(reverb);
                } else {
                    sink.append(decoder);
                }
                // Try playing it right away, it panicked!
                sink.play();
                Some((mode, sink, stream))
            }
            Err(e) => {
                info!("Failed to create decoder: {:?}", e);
                None
            }
        }
     }

     fn get_mode() -> Mode {
         Mode::Bass
     }

     enum Mode {
        case Bass
     }
     

I suspect calling the buffered() is the problem, but it is needed because the reverb requires a clone-able source. There’s no API to unbuffered the decoder. When I tried to play the sink right away, it was also panic.

Any pointers?

4

Using the linked example and the code you provided, I tried to put together a minimal reproducible example, but it’s working with a sample song. I suspect there is some arithmetic overflow when applying reverb to the specific song you’re playing. Could you check if it happens with the linked audio file too?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>use rodio::{Decoder, Source};
use std::io::BufReader;
use std::time::Duration;
fn main() {
let (_stream, handle) = rodio::OutputStream::try_default().unwrap();
let sink = rodio::Sink::try_new(&handle).unwrap();
let file = std::fs::File::open("assets/music.ogg").unwrap();
match Decoder::new(BufReader::new(file)) {
Ok(decoder) => {
let reverb = decoder.buffered().reverb(Duration::from_millis(70), 1.0);
sink.append(reverb);
}
Err(e) => {
panic!("Failed to create decoder: {:?}", e);
}
}
sink.sleep_until_end();
}
</code>
<code>use rodio::{Decoder, Source}; use std::io::BufReader; use std::time::Duration; fn main() { let (_stream, handle) = rodio::OutputStream::try_default().unwrap(); let sink = rodio::Sink::try_new(&handle).unwrap(); let file = std::fs::File::open("assets/music.ogg").unwrap(); match Decoder::new(BufReader::new(file)) { Ok(decoder) => { let reverb = decoder.buffered().reverb(Duration::from_millis(70), 1.0); sink.append(reverb); } Err(e) => { panic!("Failed to create decoder: {:?}", e); } } sink.sleep_until_end(); } </code>
use rodio::{Decoder, Source};
use std::io::BufReader;
use std::time::Duration;

fn main() {
    let (_stream, handle) = rodio::OutputStream::try_default().unwrap();
    let sink = rodio::Sink::try_new(&handle).unwrap();

    let file = std::fs::File::open("assets/music.ogg").unwrap();

    match Decoder::new(BufReader::new(file)) {
        Ok(decoder) => {
            let reverb = decoder.buffered().reverb(Duration::from_millis(70), 1.0);
            sink.append(reverb);
        }
        Err(e) => {
            panic!("Failed to create decoder: {:?}", e);
        }
    }
    
    sink.sleep_until_end();
}

This is officially a bug. I posted the solution here to solve the panic. The bug is in the size_hint function of the channels.rs rodio. This line:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>(size + consumed) / self.from as usize * self.to as usize
- self.next_output_sample_pos as usize
</code>
<code>(size + consumed) / self.from as usize * self.to as usize - self.next_output_sample_pos as usize </code>
(size + consumed) / self.from as usize * self.to as usize
    - self.next_output_sample_pos as usize

Becomes:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>((size + consumed) / self.from as usize * self.to as usize).saturating_sub(self.next_output_sample_pos as usize)
</code>
<code>((size + consumed) / self.from as usize * self.to as usize).saturating_sub(self.next_output_sample_pos as usize) </code>
((size + consumed) / self.from as usize * self.to as usize).saturating_sub(self.next_output_sample_pos as usize)

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật