Here are some tips for reducing audio latency:
- Ensure sample rates and data formats are matched between the libasound client
and the audio hardware, so that there's no need for Soft SRC or any other data
conversion in libasound.
- Make sure the libasound client's reads and writes are in the exact audio fragments/block
size, and disable the libasound sub-buffering plugin by calling
snd_pcm_plugin_set_disable():
snd_pcm_plugin_set_disable (pcm_handle, PLUGIN_DISABLE_BUFFER_PARTIAL_BLOCKS);
- In the client application, configure the audio interface to use a smaller audio
fragment size. If playback is using the PCM software mixer, then the fragment size
can be any multiple of the PCM software mixer fragment size. By default
this is 1024 samples per channel. So if your audio configuration uses 16-bit stereo
(2 channel ) then the fragment size would be 4KB. Here's how the fragment size is calculated:
fragment size (1024 samples per channel in this case) x 2 bytes per sample (16-bit)
x 2 channels
= 4096 bytes
Note:
Make sure to look at the fragment size returned via the
snd_pcm_plugin_setup()
call, because the audio interface may not be able to exactly
satisfy your fragment size request, depending on various factors such as DMA
alignment requirements, sample size (configured using the
sw_mixer_samples option or
sw_mixer_ms),
potentially required data conversions, and so on. For more information, see
PCM software mixer
- In the libasound client, set the playback start mode to be
SND_PCM_START_GO, and then issue the go command (by calling
snd_pcm_playback_go())
after you've written two audio fragments/blocks into the interface.
For capture, use SND_PCM_START_DATA, which enables capture to the client as soon as one fragment
of data is available.
- The PCM Software Mixer is automatically used unless you explicitly disable it.
When the PCM Software is enabled (default behavior), you must wait until three audio
fragments/blocks are written into the audio interface before issuing the
go command or else you will risk an underrun.
Otherwise, if the PCM Software Mixer is disabled, you can
wait until two audio fragments are written before issuing the
go command.