rustrodio

Avoid dropping the variable when wrapped into a struct


I have a Rodio's Sink wrapper in HAudioSink. I also implement a try_new_from_haudio function that, in short, creates a Sink instance, wrap it in HAudioSink and already starts playing the first audio.
In Sink's docs it states: "Dropping the Sink stops all sounds. You can use detach if you want the sounds to continue playing". So when try_new_from_haudiois returning, it drops the original sink and the sound is stopping when it shouldn't.

So my question here is: what should I do to avoid it dropping when I create an instance of HAudioSink? Is ManuallyDrop the way to go?

struct HAudioSink {
    sink: Sink,
}

impl HAudioSink {
    pub fn try_new_from_haudio<T>(haudio: HAudio<T>) -> HResult<Self>
    where
        T: NativeType + Float + ToPrimitive,
    {
        let (_stream, stream_handle) = OutputStream::try_default()?;
        let sink = Sink::try_new(&stream_handle).unwrap();
        let nchannels = haudio.nchannels();
        let nframes = haudio.nframes();
        let sr = haudio.sr();
        let mut data_interleaved: Vec<f32> = Vec::with_capacity(nchannels * nframes);
        let values = haudio
            .inner()
            .inner()
            .values()
            .as_any()
            .downcast_ref::<PrimitiveArray<T>>()
            .unwrap();

        for f in 0..nframes {
            for ch in 0..nchannels {
                data_interleaved.push(values.value(f + ch * nframes).to_f32().unwrap());
            }
        }

        let source = SamplesBuffer::new(u16::try_from(nchannels).unwrap(), sr, data_interleaved);

        sink.append(source);

        Ok(HAudioSink { sink })
    }

    // Sleeps the current thread until the sound ends.
    pub fn sleep_until_end(&self) {
        self.sink.sleep_until_end();
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    //this test doesn't work
    #[test]
    fn play_test() {
        let sink = HAudioSink::try_new_from_file("../testfiles/gs-16b-2c-44100hz.wav").unwrap();

        sink.append_from_file("../testfiles/gs-16b-2c-44100hz.wav")
            .unwrap();

        sink.sleep_until_end();
    }
}

If I put sink.sleep_until_end() inside try_new_from_haudio, just before returning Ok, it works.

check the following link for the reproducible example of this issue: https://github.com/RustAudio/rodio/issues/476


Solution

  • The problem is that for the _stream too "If this is dropped playback will end & attached OutputStreamHandles will no longer work." see the docs on OutputStream. So you have to store it alongside your Sink:

    pub struct SinkWrapper {
        pub sink: Sink,
        pub stream: OutputStream,
    }
    
    impl SinkWrapper {
        pub fn new() -> Self {
            let (stream, stream_handle) = OutputStream::try_default().unwrap();
            let sink = Sink::try_new(&stream_handle).unwrap();
    
            // Add a dummy source of the sake of the example.
            let source = SineWave::new(440.0)
                .take_duration(Duration::from_secs_f32(1.))
                .amplify(2.);
            sink.append(source);
    
            Self { sink, stream }
        }
    }