If a packet arrives very late (and one will, eventually), you will underrun. That's unavoidable. You can check for SND_PCM_STATE_XRUN from snd_pcm_state(). It's undocumented, but you need to call snd_pcm_avail_update() first to get an accurate reading from snd_pcm_state(). When an underrun occurs, recover with snd_pcm_recover() and then start writing data again.
Ok, this my first attempt at recovery, still haven't solved buffering problem so it underruns for each and every write.
Does this look roughly correct ?
Thanks, Jon
#ifdef ALSA // snd_pcm_writen - writes non interleaved (mono) samples // bufs an explicit void* pointer type set to point at real sample buffer "audio" // broken pipe indicates buffer underrun
numrecs=sizeof(audio)/2; // 1k buffer, 512 samples //printf("%d \n", snd_pcm_wait(playback_handle[activeslot],1)); err = snd_pcm_writen (playback_handle[activeslot], &bufs, numrecs); if (err<numrecs) { fflush(stdout); if (err>0) { fprintf (stderr, "snd_pcm_writen: wrote only %d\n",err); fflush(stderr); } else { if (err == EPIPE) { errb=snd_pcm_recover(playback_handle[activeslot],err,0); // error passed is resullt of writen if (errb<0) { fprintf(stderr,"Failed to recover from underrun\n"); fflush(stderr); exit(1); } } else { fprintf (stderr, "snd_pcm_writen: write to audio interface failed (%s) (%d)\n", snd_strerror (err),err); fflush(stdout); fflush(stderr); exit (1); } } } #endif