thestk / rtaudio

A set of C++ classes that provide a common API for realtime audio input/output across Linux (native ALSA, JACK, PulseAudio and OSS), Macintosh OS X (CoreAudio and JACK), and Windows (DirectSound, ASIO, and WASAPI) operating systems.
Other
1.49k stars 318 forks source link

Fix audio glitch on Linux + PulseAudio. #382

Closed cosinekitty closed 1 year ago

cosinekitty commented 1 year ago

This change resolves clicking and popping that occurs when using rtaudio to render output audio via PulseAudio on Linux systems. I discovered this issue using VCV Rack versions 2.1.0 and above, which uses a fork of rtaudio. Andrew Belt suggested I send this pull request upstream to the original rtaudio repo, because it will likely help more people that way.

Code to demonstrate reproducing the glitch and validating the fix, along with details, are available at:

https://github.com/cosinekitty/pulseaudio_glitch_demo

I will be happy to provide more help and info as needed.

Thank you!

dromer commented 1 year ago

@cosinekitty The project in your url does not exist, or is not public.

cosinekitty commented 1 year ago

@cosinekitty The project in your url does not exist, or is not public.

Oops! I just changed it to be public. Thank you for letting me know.

garyscavone commented 1 year ago

It seems that the attr_ptr structure allows one to refine aspects of buffer that might be useful to some people. And it is only used if someone specifies the numberOfBuffers parameter as an option. Did you try changing the values of numberOfBuffers to assess what was going on? If you use a very low number, it is not surprising that there may be some glitching when switching windows.

cosinekitty commented 1 year ago

OK, I did some more experiments tonight. I added some extra code to my demo so when I press ENTER, it intentionally causes a click to the audio. That way I can get a sense of latency. I can press ENTER and hear if there is a perceptible time delay before the click.

I found that my suggested change to RtAudio.cpp, i.e. passing NULL instead of attr_ptr to pa_simple_new, noticeably increases latency. It sounds like an extra quarter second or half second, something like that. I don't think VCV Rack musicians are going to like that, especially when using MIDI keyboards, etc.

So I reverted my changes to RtAudio.cpp. Now it matches your master branch. As expected, I confirmed the glitches still happen with numberOfBuffers = 2. There are fewer of them when numberOfBuffers = 3. When I set it to 4, I haven't yet heard any glitches, plus the latency is imperceptible.

@AndrewBelt So the evidence now is that it would be better to fix this in VCV Rack by either

(EDIT for clarification) Someone asked if I can eliminate the problem by increasing the block size in VCV Rack's UI (the Audio module dropdown). That's the first thing I tried back in November when I first ran into this issue. Larger block sizes don't fix the PulseAudio glitch; but increasing the number of buffers does.

garyscavone commented 1 year ago

This does not seem like an RtAudio issue (or there is a way to fix it by specifying the number of buffers to use) ... closing.