title: PortAudio unreliable: Expression '...' failed
body: <p>I'm currently experimenting with real-time signal processing, so I went and tried out PortAudio (from C).</p>
  
  <p>I have two audio interfaces on my computer, onboard sound (Intel HD Audio) and a USB audio interface. Both generally work fine under ALSA on Linux. I also tried the USB audio interface under JACK on Linux and this also works perfectly.</p>
  
  <p><strong>What I do:</strong></p>
  
  <p>My code just initializes PortAudio, opens and starts a stream (one channel, <code>paInt32</code> sample format, <code>defaultLowInputLatency</code> / <code>defaultLowOutputLatency</code>, though I tried changing to <code>paFloat32</code> or <code>defaultHighInputLatency</code> / <code>defaultHighOutputLatency</code>, which didn't improve anything).</p>
  
  <p>On each invocation of the callback, it copies <code>sizeof(int32_t) * frameCount</code> bytes via <code>memcpy</code> from the input to the output buffer, then returns <code>paContinue</code>. It does nothing else in the callback. No memory allocation, no system calls, nothing it could block on. It just outputs what it has read. The code is very simple, still I can't get it running.</p>
  
  <p>Replacing the <code>memcpy</code> with a loop copying <code>frameCount</code> elements of type <code>int32_t</code> over from the input to the output buffer didn't change anything.</p>
  
  <p><strong>What I've tried:</strong></p>
  
  <p>The following scenarios were tried out with PortAudio.</p>
  
  <ol>
  <li>Input and output via USB audio interface, callback mechanism on PortAudio, ALSA backend.</li>
  <li>Input and output via USB audio interface, blocking I/O on PortAudio with 1024 samples buffer size, ALSA backend.</li>
  <li>Input via USB audio interface, output via onboard sound, callback mechanism on PortAudio, ALSA backend.</li>
  <li>Input via USB audio interface, output via onboard sound, blocking I/O on PortAudio with 1024 samples buffer size, ALSA backend.</li>
  <li>Input and output via USB audio interface, callback mechanism on PortAudio, JACK backend.</li>
  <li>Input and output via USB audio interface, blocking I/O on PortAudio with 1024 samples buffer size, JACK backend.</li>
  </ol>
  
  <p><strong>The problems I encountered:</strong></p>
  
  <p>The results were as follows. (Numbers represent the scenarios described above.)</p>
  
  <ol>
  <li>No output from device.</li>
  <li>Output from device unsteady (interrupted). All the time lots of buffer underruns.</li>
  <li>No output from device.</li>
  <li>Output from device unrealiable. Sometimes it works, sometimes it doesn't. (Without changing anything, just running the executable multiple times.) When it works, latency starts off low, but increases over time and gets very noticeable.</li>
  <li>No output from device.</li>
  <li>No output from device.</li>
  </ol>
  
  <p>Between each try, ALSA has been tested if it's still responsive (sometimes it got completely "locked up", so that no application could output sound any longer) and rebooted the system in case ALSA got "locked up", then continued the testing.</p>
  
  <p><strong>More details, that might be useful when tracking the problem down:</strong></p>
  
  <p>In the scenarios where there is no output at all, I get the following error messages when using ALSA as backend.</p>
  
  <pre><code>Expression 'err' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 3350
  Expression 'ContinuePoll( self, StreamDirection_In, &amp;pollTimeout, &amp;pollCapture )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 3876
  Expression 'PaAlsaStream_WaitForFrames( stream, &amp;framesAvail, &amp;xrun )' failed in 'src/hostapi/alsa/pa_linux_alsa.c', line: 4248
  </code></pre>
  
  <p>I get the following error message when using JACK as backend.</p>
  
  <pre><code>Cannot lock down 42435354 byte memory area (Cannot allocate memory)
  </code></pre>
  
  <p>In addition, no matter what method I use, I always get these warnings.</p>
  
  <pre><code>ALSA lib pcm.c:2267:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear
  ALSA lib pcm.c:2267:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe
  ALSA lib pcm.c:2267:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side
  ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
  ALSA lib pcm_route.c:867:(find_matching_chmap) Found no matching channel map
  </code></pre>
  
  <p>When using ALSA, I also get one or two complaints about underruns.</p>
  
  <pre><code>ALSA lib pcm.c:7905:(snd_pcm_recover) underrun occurred
  </code></pre>
  
  <p>The PortAudio functions that I call (<code>Pa_Initialize</code>, <code>Pa_OpenStream</code>, <code>Pa_StartStream</code>, <code>Pa_StopStream</code>, <code>Pa_CloseStream</code>, <code>Pa_Terminate</code>, in this order), all return <code>paNoError</code>.</p>
  
  <p>The <code>paex_read_write_wire.c</code> (blocking I/O) example that comes with PortAudio can usually access the device, but also experiences lots of underruns (like my test case 2).</p>
  
  <p>In either case, there's nothing interesting showing up in <code>dmesg</code>. (Just checked that, since ALSA has a kernel-level component.)</p>
  
  <p><strong>My question:</strong></p>
  
  <p>Anyone knows what's the problem here and how I could fix it? Or at least, how I could narrow it down a bit more?</p>