how to stop a streaming sound?
sorry if i'm missing something obvious.
i have a struct that implements Decoder which i wrap into a StreamingSoundData and send to AudioManager::play.
when the stream ends i return an error from decode, but kira continues to busy-wait poll the stream, burning cpu pretty hard.
i can't see a way to tell the audio manager to stop the handle, or to reply from the decoder that the stream has ended.
what should i do?
thanks
i now see i can stop the handle, sorry.
would it make sense for this to be automatic if the decoder returns an error? or have another mechanism for the decoder to report that the stream is finished?
Indeed, that is an oversight. I'm not sure if there's any realistic scenarios where a decoder returns an error, but it can still continue decoding. So it might be best to just stop the sound on the first error.
I ran into the same issue in 0.10.8, but handle.stop() doesn't stop the CPU burn.
In my case, I'm using a StreamingSoundHandle and a custom kira::sound::streaming::Decoder to wrap a synthesizer engine. For these, num_frames needs to report it has usize::MAX frames -- because it's really an unknown number, and the infrastructure only asks once. So I try to return an error when the stream ends, like the OP.
Fortunately, calling handle.seek_to(usize::MAX as f64) before calling handle.stop() works around this. (I know usize::MAX "seconds" is specious, but it at least works to terminate the inner thread, whereas seeking to (usize::MAX/sample_rate) as f64 doesn't help...)
IMO it would be nicer overall if the kira::sound::streaming::Decoder could expose a trait method like is_finished(&self) -> bool since the nature of a stream implies "unknown length" in many cases.
If you're doing realtime synthesis, I'd recommend implementing the Sound trait directly. Streaming sounds are more for streaming sounds from files.
Good point! (It seems that I first need to implement SoundData to generate that Sound, but I get the idea..)
Yes, SoundData splits into a Sound implementation and a corresponding handle upon passing it into a play function.