4

I want to capture audio on Linux with low latency in a program I'm writing.

I've run some experiments using the ALSA API, using snd_pcm_readi() to capture sound, then immediately using snd_pcm_writei() to play it back.

I've tried playing with the number of frames captured, and the buffer size, but I don't seem to be able to get the latency down to less than a second or so.

Am I better off using PulseAudio or JACK? Can those be used to play the captured audio?

Paul Steckler
  • 617
  • 5
  • 19

3 Answers3

1

To reduce capture latency, reduce the period size of the capture device. To reduce playback latency, reduce the buffer size of the playback device.

Jack can play the captured audio (just connect the input ports to the output ports), but you still have to configure its periods/buffers.

Also see Relation between period size of speaker and mic and Recording from ALSA - understanding memory mapping.

Community
  • 1
  • 1
CL.
  • 173,858
  • 17
  • 217
  • 259
1

I've doing some work on low latency audio programming,

My experience is, first, your capture buffer should be small, like 10ms period buffer. (let's assuming you're using 512 frame buffer, and 48000 sample rate).

Then, you should config your Output device start_threshold to at least 2 * frame size ( 1 * frame size if your don't have much process of recorded data).

For record device, like CL. said, use a relative small period size is better, but not too small to avoid too much irq.

Also, you can change your process schedule to FIFO schedule.

Then, hopefully, you will get about 20ms total latency.

Jiejing Zhang
  • 1,030
  • 8
  • 16
1

I believe you should at first ensure that you are running a Linux kernel which actually allows you to achieve low typical latency.

There are several kernel compile-time configuration options which you might look into:

  • CONFIG_HZ_1000
  • CONFIG_IRQ_FORCED_THREADING
  • CONFIG_PREEMPT
  • CONFIG_PREEMPT_RT_FULL (available only with RT patch)

Apart from that, there are more things you can do in order to optimize your audio latency in Linux. Some starting reference points can be found there:

http://wiki.linuxaudio.org/wiki/real_time_info

Peter Bašista
  • 749
  • 9
  • 22