kira icon indicating copy to clipboard operation
kira copied to clipboard

how to stop a streaming sound?

Open robtfm opened this issue 2 years ago • 2 comments

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

robtfm avatar Mar 18 '24 15:03 robtfm

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?

robtfm avatar Mar 18 '24 15:03 robtfm

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.

tesselode avatar Mar 19 '24 04:03 tesselode

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.

eswartz avatar Sep 05 '25 16:09 eswartz

If you're doing realtime synthesis, I'd recommend implementing the Sound trait directly. Streaming sounds are more for streaming sounds from files.

tesselode avatar Sep 05 '25 22:09 tesselode

Good point! (It seems that I first need to implement SoundData to generate that Sound, but I get the idea..)

eswartz avatar Sep 05 '25 23:09 eswartz

Yes, SoundData splits into a Sound implementation and a corresponding handle upon passing it into a play function.

tesselode avatar Sep 06 '25 07:09 tesselode