rodio icon indicating copy to clipboard operation
rodio copied to clipboard

how to play the same sound at a list of intervals

Open caente opened this issue 5 years ago • 4 comments

this not an issue, sorry, but didn't find where to ask about it, this is my use case: I have a small sound in a wav file, and a list of intervals, e.g.

let file = std::fs::File::open("data/beat.wav").unwrap();
let source = rodio::Decoder::new(BufReader::new(file)).unwrap();
let intervals: Vec<u64> = vec![400, 400, 401]; // in milliseconds

I need to play beat after each interval, it is also possible to have a Vec with the actual timestamps in which the sounds most play.

any hints will be appreciated

caente avatar Feb 06 '20 22:02 caente

this is what I ended up doing, but I wonder if there is a better way, and/or what at the trade offs of this approach, I really don't like the source.clone() part...

fn play_beats(sink: &Sink, intervals: Vec<u64>) {
    let file = std::fs::File::open("data/beat.wav").unwrap();
    let source = rodio::Decoder::new(BufReader::new(file))
        .unwrap()
        .buffered();
    intervals.iter().for_each(|interval| {
        let s = source.clone().delay(Duration::from_millis(*interval));
        sink.append(s);
    });
}

caente avatar Feb 07 '20 01:02 caente

someone in discord suggested this:

can you decode the wave to a vec of samples and use a slice reference as your source?

but it is not clear to me how to do this... any pointers?

caente avatar Feb 07 '20 14:02 caente

I have a question. does source have clone function?
I'm new to this crate, thanks.

SKTT1Ryze avatar Nov 22 '20 02:11 SKTT1Ryze

I know that. Thanks.

SKTT1Ryze avatar Nov 22 '20 02:11 SKTT1Ryze