RustAudio / cpal

Cross-platform audio I/O library in pure Rust
Apache License 2.0
2.7k stars 359 forks source link

Entire system sound mutes while debugging #872

Open ValonPrime opened 7 months ago

ValonPrime commented 7 months ago

Hello everyone,

my entire system sounds mutes when debugging while a sound plays.

using gdb to break in the Beep-Example at line 123 causes all other programs (Firefox...) to mute / no longer output any audio: rust-gdb target b beep.rs:123 r https://github.com/RustAudio/cpal/blob/2ec761d30b35dbbacfecb41a2f5985781a7d52d1/examples/beep.rs#L123

I dont expect that my programs audio continues (because execution is suspended). But at least other programs should be able to continue to output audio.

j-n-f commented 3 months ago

It's surprising that the audio stops when you pause on a sleep call, but this is because every single thread in your program gets paused whenever a breakpoint is hit.

If you're using GDB, you may want to look at Non-Stop Mode.

I'm using ASIO on Windows and I don't have the issue where audio from other programs is blocked.

To troubleshoot this further you could look at a few different things:

  1. What device are you using and how is it configured? (the output of beep.rs should tell you this)
  2. Can you use that same device with something other than cpal and get the behaviour you want? (i.e. isolate issue to library or backend; e.g. if you're using PA/PipeWire, find another program that talks to those and try to set a breakpoint in it)

If you are in fact using PipeWire, it would be helpful to know how PipeWire is expected to behave when you block in the callback.

ValonPrime commented 3 months ago

My system sound (as well as the beep) continues when breaking while Non-Stop Mode is on.

The output of beep.rs:

Output device: default
Default output config: SupportedStreamConfig { channels: 2, sample_rate: SampleRate(44100), buffer_size: Range { min: 1, max: 4194304 }, sample_format: F32 }

Process finished with exit code 0

I also tried this with separate devices (external interface, BT headphones and frontpanel audio). All yield the same behavior.

About trying different libs: Using "Soloud" with the following code worked as expected. The mp3 stopped playing but my system (Firefox) continued to output sound.

use soloud::*;

fn main() {
    let sl = Soloud::default().unwrap();
    let mut wav = Wav::default();
    wav.load_mem(include_bytes!("../creepypasta.mp3")).unwrap();
    sl.play(&wav);
    while sl.voice_count() > 0 {
        // Breakpoint here
        std::thread::sleep(std::time::Duration::from_millis(100));
    }
}

I dont have nearly enough knowledge to pinpoint the source of the Problem (CPAL or PipeWire or ...). But I think using Non-Stop mode is just a workaround. Especially because I dont know how to enable it in Rustrover ... 😭

sullyj3 commented 3 months ago

It seems like cpal doesn't suppport pulseaudio. From the readme:

Currently, supported hosts include:

Linux (via ALSA or JACK)

edit: ref #259

I've run into the same thing, I think when you connect to ALSA, it commandeers the whole audio system and prevents other programs from using it.

GiantBlargg commented 1 month ago

This seems to have been fixed in Pipewire at some point. I was able to reproduce this bug back in August but now I'm on Pipewire 1.2.5 and other programs continue to play sound when debugging.

kjarosh commented 1 month ago

~It's been fixed for me as well with pipewire 1.0.8 I suspect (it wasn't working with 1.0.7).~

Turns out it has stopped working again despite having pipewire 1.0.8.