[alsa-devel] Non-blocking snd_pcm_drain?

Daniel Sanz daniellsanz2 at gmail.com
Sat Nov 10 18:57:11 CET 2012


If I don't drain the buffer, then I don't hear the entire sound, even
though I think I'm writing correctly all the frames.

Here's the relevant code (playback of 2 seconds of a sound file
represented by file pointer "fp"):

	snd_pcm_hw_params_get_period_size(params, &frames, 0);
	buffer_size = frames * channels * 2;
	buffer = (char*)malloc(buffer_size);

	snd_pcm_hw_params_get_period_time(params, &period_time, NULL);

	for (loops = (2 * 1000000) / period_time; loops > 0; loops--)
	{
                fread(buffer, sizeof(char), buffer_size, fp);

		if ((err = snd_pcm_writei(handle, buffer, frames)) == -EPIPE)
		{
			printf("XRUN.\n");
			snd_pcm_prepare(handle);
		}
		else if (err < 0)
		{
			printf("ERROR. Can't write to the device. %s\n", snd_strerror(err));
		}
       }

       snd_pcm_close(handle);

Thanks,

On Sat, Nov 10, 2012 at 6:28 PM, Clemens Ladisch <clemens at ladisch.de> wrote:
>
> Daniel Sanz wrote:
> > I noticed that the snd_pcm_drain call blocks until the buffer is
> > completely drained. Is there any way to drain the buffer without
> > a block?
>
> Try calling snd_pcm_nonblock(handle, 1).
>
> > I've tried calling snd_pcm_nonblock(handle, 1) before the call to
> > snd_pcm_drain but it does not work.
>
> How so?
>
> > The context is that this is an application that plays a particular WAV
> > sound repeatedly, so I don't want to wait until the sound is played
> > completely before it is played again.
>
> Then why do you drain at all?  Just write the new data into the buffer.
>
>
> Regards,
> Clemens


More information about the Alsa-devel mailing list