title: How to use ALSA's snd_pcm_writei()?
body: <p>Can someone explain how <a href="http://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m.html#gf13067c0ebde29118ca05af76e5b17a9" rel="noreferrer">snd_pcm_writei</a></p>
  
  <pre><code>snd_pcm_sframes_t snd_pcm_writei(snd_pcm_t *pcm, const void *buffer,
                                   snd_pcm_uframes_t size)
  </code></pre>
  
  <p>works?</p>
  
  <p>I have used it like so:</p>
  
  <pre><code>for (int i = 0; i &lt; 1; i++) {
     f = snd_pcm_writei(handle, buffer, frames);
  
     ...
  }
  </code></pre>
  
  <p>Full source code at <a href="http://pastebin.com/m2f28b578" rel="noreferrer">http://pastebin.com/m2f28b578</a></p>
  
  <p>Does this mean, that I shouldn't give <code>snd_pcm_writei()</code> the number of
  all the frames in <code>buffer</code>, but only</p>
  
  <p>sample_rate * latency = <code>frames</code></p>
  
  <p>?</p>
  
  <p>So if I e.g. have:
  sample_rate = 44100
  latency = 0.5 [s]
  all_frames = 100000</p>
  
  <p>The number of frames that I should give to <code>snd_pcm_writei()</code> would be</p>
  
  <p>sample_rate * latency = frames
  44100*0.5 = 22050</p>
  
  <p>and the number of iterations the for-loop should be?:</p>
  
  <p>(int) 100000/22050 = 4; with frames=22050</p>
  
  <p>and one extra, but only with</p>
  
  <p>100000 mod 22050 = 11800</p>
  
  <p>frames?</p>
  
  <p>Is that how it works?</p>
  
  <p>Louise</p>
  
  <p><a href="http://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m.html#gf13067c0ebde29118ca05af76e5b17a9" rel="noreferrer">http://www.alsa-project.org/alsa-doc/alsa-lib/group___p_c_m.html#gf13067c0ebde29118ca05af76e5b17a9</a></p>