Capturing audio in Linux with low latency
I want to record low latency audio in Linux in a program I am writing.
I did some experiments using the ALSA API, using snd_pcm_readi () to capture audio, then immediately using snd_pcm_writei () to play it back.
I tried to play around with the number of frames captured and the buffer size, but I can't seem to get the latency in less than a second or so.
Should I use PulseAudio or JACK? Can they be used to play the captured audio?
source to share
To reduce the capture latency, reduce the period size of the capture device. To reduce playback latency, reduce the buffer size of the playback device.
The jack can play captured audio (just connect the input ports to the output ports), but you still need to adjust its periods / buffers.
Also see Relationship Between Speaker and Microphone Period Sizes and Recording with ALSA - Understanding Memory Mapping .
source to share
I am doing some low latency programming work,
My experience: First, your capture buffer should be small, like a 10ms period buffer. (assuming you are using a 512 frame buffer and 48000 samples).
Then you must set the start_track of the output device to at least 2 * frames (frame size 1 * if you don't have a lot of data written).
For a recorder such as CL. said, use a relatively small period size is better, but not too small to avoid too many irqs.
Alternatively, you can change the process schedule to FIFO schedule.
Then hopefully you get a full 20ms latency.
source to share
I believe you should first make sure that you are using a Linux kernel that actually achieves the lowest typical latency.
There are several kernel compile-time configuration options that you can look into:
-
CONFIG_HZ_1000
-
CONFIG_IRQ_FORCED_THREADING
-
CONFIG_PREEMPT
-
CONFIG_PREEMPT_RT_FULL
(only available with RT patch)
Plus, you can do more things to optimize audio latency on Linux. There are some starting points to be found here:
source to share