All of lore.kernel.org
 help / color / mirror / Atom feed
* Non-blocking snd_pcm_drain?
@ 2012-11-10 17:08 Daniel Sanz
  2012-11-10 17:28 ` Clemens Ladisch
  0 siblings, 1 reply; 4+ messages in thread
From: Daniel Sanz @ 2012-11-10 17:08 UTC (permalink / raw)
  To: alsa-devel

Hello, I'm new to the ALSA API and I have a question:

I've developed a small program that plays audio from a buffer using
snd_pcm_writei and snd_pcm_drain but 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? I've tried calling snd_pcm_nonblock(handle, 1)
before the call to snd_pcm_drain but it does not work.

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.

What would be the best way to achieve this?

Thanks,

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Non-blocking snd_pcm_drain?
  2012-11-10 17:08 Non-blocking snd_pcm_drain? Daniel Sanz
@ 2012-11-10 17:28 ` Clemens Ladisch
  2012-11-10 17:57   ` Daniel Sanz
  0 siblings, 1 reply; 4+ messages in thread
From: Clemens Ladisch @ 2012-11-10 17:28 UTC (permalink / raw)
  To: Daniel Sanz; +Cc: alsa-devel

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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Non-blocking snd_pcm_drain?
  2012-11-10 17:28 ` Clemens Ladisch
@ 2012-11-10 17:57   ` Daniel Sanz
  2012-11-11 18:10     ` Daniel Sanz
  0 siblings, 1 reply; 4+ messages in thread
From: Daniel Sanz @ 2012-11-10 17:57 UTC (permalink / raw)
  To: Clemens Ladisch; +Cc: alsa-devel

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@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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Non-blocking snd_pcm_drain?
  2012-11-10 17:57   ` Daniel Sanz
@ 2012-11-11 18:10     ` Daniel Sanz
  0 siblings, 0 replies; 4+ messages in thread
From: Daniel Sanz @ 2012-11-11 18:10 UTC (permalink / raw)
  To: Clemens Ladisch; +Cc: alsa-devel

Removing the last line, snd_pcm_close(handle), plays the entire sound,
but I don't think it is a good solution.

It is like there is somewhere a huge latency between the moment I call
snd_pcm_writei and the moment I hear sound, but I don't know what's
causing this. I've even tried reducing the period size to 32 frames or
padding the last buffer with zeros, but no luck either.

I'm out of ideas.


On Sat, Nov 10, 2012 at 6:57 PM, Daniel Sanz <daniellsanz2@gmail.com> wrote:
> 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@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

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2012-11-11 18:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-11-10 17:08 Non-blocking snd_pcm_drain? Daniel Sanz
2012-11-10 17:28 ` Clemens Ladisch
2012-11-10 17:57   ` Daniel Sanz
2012-11-11 18:10     ` Daniel Sanz

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.