title: what is the difference between ALSA threshold, avail_min and period?
body: <p>I am working on an ALSA capture routine on a quad core debian box and I am baffled.</p>
  
  <p>Basic ALSA capture, hw:0 interface, 16 bits, 44.1 KHz, using a blocking thread with poll(), controlled by <code>snd_pcm_start()</code> and <code>snd_pcm_drop()</code>. I check the return values of all ALSA API calls.  Basic capture is working fine but I cannot figure out how to set the poll rate (that is, how often ALSA notifies my poll() routine to return because frames are available to be read).</p>
  
  <p>I use <code>snd_pcm_sw_params_set_avail_min()</code> to set this to 2048 frames and poll() returns when there are about 2048 frames available to read. I call <code>snd_pcm_avail_delay()</code> before I call <code>snd_pcm_readi()</code> and it all appears reasonable. But if I set it to anything less than 2048 I get inconsistent results. If I set the avail_min to 1024 half of the time poll() returns when there is around 1024 frames available and half of the time not until 1500 frames are available. When I set the avail_min to 512, the very 1st read is 512 but then 95% of the subsequent reads are 1024 frames or larger. When I call <code>snd_pcm_hw_params_get_period_size_min/max()</code> this device reports a valid range of 16-8192 frames, so a period of 512 frames does not seem unreasonable--the driver reports that the hw supports it.</p>
  
  <p>I have tried every combination of <code>snd_pcm_sw_params_set_avail_min()</code>, <code>snd_pcm_hw_params_set_period_size()</code> and <code>snd_pcm_sw_params_set_start_threshold()</code>, but still cannot
  get reasonable results. </p>
  
  <p>I have tried nailing this thread up to a single CPU core with the pthread setaffinity. I have tried <code>sched_setschedule()</code> to give the process 'soft' real time priorities. Nothing seems to allow me to get consistent poll() returns except setting the avail_min to at least 2048.</p>
  
  <p>So here are some ALSA questions:</p>
  
  <ol>
  <li><p>Is the start threshold (using <code>snd_pcm_sw_params_set_start_threshold()</code> only relevant for async
  capture? That is, the start threshold does not seem applicable for poll() based capture. Is this 
  correct?</p></li>
  <li><p>What is the difference between the period size in frames and the avail_min size in frames? 
  The documentation seems to imply that the avail_min sets the point at which the poll() will return because there are at least that many frames available to be read. The period size is a hw parameter but the API still provides a minimum and maximum value and a way to set it. It seems to me that they are really the same thing but I cannot get my poll() to return according to the period I set.</p></li>
  <li><p>Have I misread the documentation when I interpret the intent of the <code>snd_pcm_sw_params_set_avail_min()</code> to set the target number of available captured frames for triggering the poll() routine?  </p></li>
  <li><p>Is it reasonable for me to expect that poll() should return more consistently than whenever there is anywhere between 500 and 1500 frames available for reading? or is this normal behavior and I should expect wide variations in the number of frames that are available for reading?</p></li>
  <li><p>If wide variations are to be expected, is it best to always read the total available frames as reported by <code>snd_pcm_avail()</code> or is it best to read a set number of frames each time poll() returns?</p></li>
  </ol>