title: Why do ALSA examples use a loop for playing/capturing?
body: <p>I'm working on linux alsa (advanced linux sound architecture) library in "C" language on an ARM pc that runs Debian OS. I'm trying to record and play sound. The example projects for my purpose (one for recording and one for playing) do the job within <code>for</code> loop. I mean, reading or writing to the buffer. What is the purpose of this for loop usage? </p>
  
  <p>For example, in <a href="http://www.alsa-project.org/alsa-doc/alsa-lib/_2test_2pcm_min_8c-example.html" rel="nofollow noreferrer">http://www.alsa-project.org/alsa-doc/alsa-lib/_2test_2pcm_min_8c-example.html</a>, official example for playing sound. They create a buffer for the sound data and fill it randomly. That is okey, but they write this buffer array to sound device's buffer within <code>for</code> loop that loops 16 times. And there is nothing done with the loop counter <code>i</code> within loop:</p>
  
  <pre><code>for (i = 0; i &lt; 16; i++) {
      frames = snd_pcm_writei(handle, buffer, sizeof(buffer));
      if (frames &lt; 0)
          frames = snd_pcm_recover(handle, frames, 0);
      if (frames &lt; 0) {
          printf("snd_pcm_writei failed: %s\n", snd_strerror(frames));
          break;
      }
      if (frames &gt; 0 &amp;&amp; frames &lt; (long)sizeof(buffer))
          printf("Short write (expected %li, wrote %li)\n", (long)sizeof(buffer), frames);
  }
  </code></pre>
  
  <p>Same as <a href="https://gist.github.com/albanpeignier/104902" rel="nofollow noreferrer">recording sample</a>:</p>
  
  <pre><code>for (i = 0; i &lt; 10; ++i) {
     if ((err = snd_pcm_readi (capture_handle, buffer, buffer_frames)) != buffer_frames) {
        fprintf (stderr, "read from audio interface failed (%s)\n",
                 err, snd_strerror (err));
        exit (1);
     }
     fprintf(stdout, "read %d done\n", i);
  }
  </code></pre>