snd_pcm_writei() takes to much time to write data
I am using the ALSA open-source library to read and write audio data for my project. To write audio data, I am calling the snd_pcm_writei function. Initially, it works as expected, but after writing 2-3 frames, the function starts taking significantly more time (around 1 second) to complete.
Normally, snd_pcm_writei should execute within 1-3 ms, but this delay is affecting my application's overall performance, causing audio chopping issues.
Here are the details of my setup:
Frame size: ~8000 bytes (writing 8000 bytes per call)
Plug PCM: Route conversion PCM (sformat=S16_LE) Transformation table: 0 <- 0 1 <- 0 Its setup is: stream : PLAYBACK access : RW_INTERLEAVED format : S16_LE subformat : STD channels : 1 rate : 8000 exact rate : 8000 (8000/1) msbits : 16 buffer_size : 4000 period_size : 1000 period_time : 125000 tstamp_mode : NONE tstamp_type : MONOTONIC period_step : 1 avail_min : 1000 period_event : 0 start_threshold : 4000 stop_threshold : 4000 silence_threshold: 0 silence_size : 0 boundary : 9007199254740992000 Slave: Soft volume PCM Control: amba-playback min_dB: -20 max_dB: 20 resolution: 256 Its setup is: stream : PLAYBACK access : MMAP_INTERLEAVED format : S16_LE subformat : STD channels : 2 rate : 8000 exact rate : 8000 (8000/1) msbits : 16 buffer_size : 4000 period_size : 1000 period_time : 125000 tstamp_mode : NONE tstamp_type : MONOTONIC period_step : 1 avail_min : 1000 period_event : 0 start_threshold : 4000 stop_threshold : 4000 silence_threshold: 0 silence_size : 0 boundary : 9007199254740992000 Slave: Hardware PCM card 0 'es8389' device 0 subdevice 0 Its setup is: stream : PLAYBACK access : MMAP_INTERLEAV[ 22.206911] ambarella-i2c e400a000.i2c: No ACK from address 0x6c, 0:0! ED format [ 22.214533] os04c10: probe of 2-0001 failed with error -5 : S16_LE subformat : STD channels : 2 rate : 8000 exact rate : 8000 (8000/1) msbits : 16 buffer_size : 4000 period_size : 1000 period_time : 125000 tstamp_mode : NONE tstamp_type : MONOTONIC period_step : 1 avail_min : 1000 period_event : 0 start_threshold : 4000 stop_threshold : 4000 silence_threshold: 0 silence_size : 0 boundary : 9007199254740992000 appl_ptr : 0 hw_ptr : 0
I would appreciate any guidance or suggestions on why this delay is happening and how I can resolve it. Let me know if you need any additional details or logs.
If you write 8000 frames and rate is 8000Hz then it's exactly one second. If you want to write frames to the remaining space (buffer), then use snd_pcm_avail()/snd_pcm_avail_update() to identify the available frames or set the I/O mode to non-block snd_pcm_nonblock(). Otherwise the driver wait's until all frames are consumed (can be put to the buffer).