0

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

daniellga
  • 1,142
  • 6
  • 16
  • If you want to keep using the `Sink` just keep it around, if not use `detach` `ManuallyDrop` is a very niche struct rarely needed by high level code. – cafce25 Feb 16 '23 at 11:33
  • But how would I do that? Sink appears to drop, and so music stops, when `try_new_from_audio` closes its scope – daniellga Feb 16 '23 at 12:51
  • Store the result in a variable, `let sink = HAudioSink::try_new_from_audio(…)`. – cafce25 Feb 16 '23 at 13:04
  • Ok I edited my original post so you can understand it better.. I was already doing this step you meantioned. The test only works when I call sleep inside `try_new_from_audio`. When I call sleep inside the test, it doesn't work. I suspect it's the original sink being dropped that does this effect. – daniellga Feb 16 '23 at 13:20
  • @cafce25 I created an issue with a reproducible example in Rodio. https://github.com/RustAudio/rodio/issues/476 – daniellga Feb 16 '23 at 14:10
  • You should add the [mre] you produced for the issue here, too, next time start with that, would have saved both of us some time. – cafce25 Feb 16 '23 at 14:33

1 Answers1

1

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 }
    }
}
cafce25
  • 15,907
  • 4
  • 25
  • 31