title: ALSA: Ways to prevent underrun for speaker
body: <p>I am playing a single channel audio in non-interleaved mode. I am getting underrun when I am writing audio data into speaker : <code>ALSA lib pcm.c:7339:(snd_pcm_recover) underrun occurred</code></p>
  
  <p>Here is how I write:</p>
  
  <pre><code>    printf("%d",snd_pcm_avail (spkhandle));  
      ret = snd_pcm_writen(spkhandle, pSpeakerBuf , framesIn18Millisec);
      if(ret &lt; 0)
      {
          snd_pcm_recover(spkhandle, ret, 0);
      }
  </code></pre>
  
  <p>What are the different ways/parameter configurations to prevent ALSA under run ?</p>
  
  <p>(I am using Linux 3.0, ARM )</p>
  
  <p>Edit:
  Here is a buffer measurement using snd_pcm_avail() API </p>
  
  <pre><code>      snd_pcm_avail = 2304      &lt;&lt; snd_pcm_writen call 1 success
        snd_pcm_avail = 2160      &lt;&lt; snd_pcm_writen call 2 success
        snd_pcm_avail = 2016      &lt;&lt; snd_pcm_writen call 3 success
        snd_pcm_writen error -32 Broken pipe  &lt;&lt; snd_pcm_writen call 4 failure
        ALSA lib pcm.c:7339:(snd_pcm_recover) underrun occurred  &lt;&lt; And displays this message
  </code></pre>
  
  <p>Here is the output that Marko requested for:</p>
  
  <pre><code>snd_output_t* out;
  ....
  // Do alsa parameters init .... 
  ....
  snd_output_stdio_attach(&amp;out, stderr, 0);
  snd_pcm_dump_sw_setup(spkhandle, out);
  
    tstamp_mode  : NONE
    period_step  : 1
    avail_min    : 144
    period_event : 0
    start_threshold  : 288
    stop_threshold   : 2304
    silence_threshold: 0
    silence_size : 0
    boundary     : 1207959552
  </code></pre>