andrewrk / libsoundio

C library for cross-platform real-time audio input and output
http://libsound.io/
MIT License
1.94k stars 230 forks source link

PulseAudio backend should use PA_STREAM_ADJUST_LATENCY #149

Closed genosse-einhorn closed 5 years ago

genosse-einhorn commented 7 years ago

Without this flag, PulseAudio may ignore the requested latency.

Whether PA_STREAM_ADJUST_LATENCY has any observable effect appears to depend on the hardware and on what other applications are currently running and accessing the device. My webcam needs this flag, otherwise it produces auto data in buffers of ~1s, while my other microphone is happily delivering low-latency audio without it.

As a side effect of enabling this flag, the sio_microphone example produces a never ending stream of buffer underruns, which I could fix by increasing the ring buffer size slightly. That might be another quirk of my hardware, though, so I can't quite say if it is a general problem that needs to be fixed.

Please check out libsoundio-pa-latency.patch.txt

(Silly github only lets me attach .txt files, no .patch)

mdsitton commented 7 years ago

There is likely a reason github blocks .patch files from being uploading(I'd take a guess its to encourage people to do a pull request)

And as the author of this library(and plenty others) have previously mentioned in other issues please submit a pull request :)

hvenev commented 7 years ago

https://www.freedesktop.org/wiki/Software/PulseAudio/Documentation/Developer/Clients/LatencyControl/

For me not setting PA_STREAM_ADJUST_LATENCY means that sio_microphone has consistent underflows if the latency is less than that of pulseaudio. I think the flag should be set iff software_latency has been adjusted.

For playback I think frames_min should be based on minreq (the minimum amount that can be sent to the hardware; as provided by pa_stream_get_buffer_attr) and frames_max should be based on nbytes (amount of buffer space provided by pulseaudio). For recording, they should be based on nbytes + fragsize - maxlength (amount of data that must be read so that there are at least fragsize bytes free in the buffer) and nbytes (amount of data available) respectively.

Should I start working on a patch?