cpal icon indicating copy to clipboard operation
cpal copied to clipboard

[Request] Sleep thread on pause

Open hinto-janai opened this issue 1 year ago • 0 comments

Setup

  • Linux x86_64
  • cpal 0.15.2
  • ALSA

Request

On Stream's that can be and are paused, it would be nice if cpal did not consume any CPU time.

There are a few loop {}'s that I've patched by adding Mutex<bool> + Condvar, e.g:

https://github.com/RustAudio/cpal/blob/d4ba28df733fbb5e5a4d78f7a8fd789206a44cbe/src/host/alsa/mod.rs#L636-L644

A Condvar::wait() would be placed here to see if pause is true/false before continuing.

Current behavior

Every half second or so, a thread from cpal will consume some CPU time (half the % it actually takes to play audio) even when it is paused and not doing anything.

This behavior also exists in rodio::Sink due to periodic_access() although that is also easily patch-able with a Condvar::wait().

Even after doing this though, it seems cpal::Stream is still doing something in the background. The thread pops-up momentarily in top even when there is no audio playing. That thread should be sleeping.

hinto-janai avatar May 29 '23 16:05 hinto-janai