underrun problems after setting parameters with snd_pcm_set_params()

robert rozee rozee at mail.com
Tue Apr 28 09:43:17 CEST 2020


hi,
    i'm having some problems using ALSA to stream simple sounds on one 
particular netbook running EasyOS, a cut-down 64-bit linux distro that is 
rather slow.
 

the problem only occurs with SHORT sound clips. i found that clips longer than 
1111ms played without flaw. clips less that 500ms generally failed to play to 
the end, but there was no error code returned.

clips between 501ms and 1111ms generated the error message: 
"ALSA lib pcm.c:8424:(snd_pcm_recover) underrun occurred".

my buffer size is equivalent to 500ms.

the code essentially does the following:
 
-----------------------------------------------------------------
snd_pcm_open(@handle, "default" , SND_PCM_STREAM_PLAYBACK, 0);
snd_pcm_set_params(handle, SND_PCM_FORMAT_U8,
                           SND_PCM_ACCESS_RW_INTERLEAVED,
                           1,                           // number of channels
                           48000,                       // bitrate (bps)
                           1,                           // resampling on/off
                           min(500000, sample_length)); // latency
while (frames_left_to_write>0)
{
//
// CODE IN HERE TO: refill buffer, and set frames_to_write_now appropriately
//
// send out current buffer content
  frames=snd_pcm_writei(handle, @buffer, frames_to_write_now);
  if frames<0 then frames=snd_pcm_recover(handle, frames, 0);
  if frames<0 then break;
}
-----------------------------------------------------------------
******************************************************************************** 
the problem, as far as i can tell, is with snd_pcm_writei() failing to play the
portion of the sound clip in the buffer, but returning NO error code. the error 
-EFILE is only generated on the attempt to write a SECOND buffer load.

the sound sample i'm using is a 440Hz sine wave tone, amplitude of +/-100, 
centred around 128. ends are feathered in/out, so any short playback is very 
obvious.

after doing a bit of a search on similar problems, i'd like to speculate that 
snd_pcm_set_params() is choosing an inappropriate small period size. for 
low-end computers this is causing the underrun issue. if snd_pcm_set_params() 
is used for configuration, is there any way to change the period size after the 
call? can it be set between opening the handle and setting the other parameters?


cheers,
rob :-)


More information about the Alsa-devel mailing list