Alsa-Devel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Grant Likely <grant.likely@secretlab.ca>
To: Jon Smirl <jonsmirl@gmail.com>
Cc: John Bonesio <bones@secretlab.ca>,
	alsa-devel@alsa-project.org, broonie@opensource.wolfsonmicro.com,
	linuxppc-dev@lists.ozlabs.org, lrg@slimlogic.co.uk
Subject: Re: [PATCH 2/6] ASoC/mpc5200: get rid of the appl_ptr tracking nonsense
Date: Sat, 7 Nov 2009 11:51:16 -0700	[thread overview]
Message-ID: <fa686aa40911071051y3cf63e55ibe7fbc65c30bd15e@mail.gmail.com> (raw)
In-Reply-To: <9e4733910911070451y28073dbeke6ced6246f5a5c24@mail.gmail.com>

On Sat, Nov 7, 2009 at 5:51 AM, Jon Smirl <jonsmirl@gmail.com> wrote:
> On Sat, Nov 7, 2009 at 3:34 AM, Grant Likely <grant.likely@secretlab.ca> wrote:
>> Sound drivers PCM DMA is supposed to free-run until told to stop
>> by the trigger callback.  The current code tries to track appl_ptr,
>> to avoid stale buffer data getting played out at the end of the
>> data stream.  Unfortunately it also results in race conditions
>> which can cause the audio to stall.
>
> I leave in an hour and I will be off net for a week so I can't look at these.

Okay, no problem.  I can be patient.

> The problem at end of stream works like this:
>
> last buffer containing music plays
> this buffer has been padded with zero to make a full sample
> interrupt occurs at end of buffer
>  --- at this point the next chained buffer starts playing, it is full of junk
>  --- this chaining happens in hardware
> Alsa processes the callback and sends stop stream
> --- oops, too late, buffer full of noise has already played several samples
> --- these samples of noise are clearly audible.
> --- they are usually a fragment from earlier in the song.

I'm not yet convinced that this sequence is correct.  Well, I mean,
I'm not convinced about the buffer only being filled to top up the
current period.  My understanding of ALSA is that the application is
supposed to make sure there is enough silence in the buffer to handle
the lag between notification that the last period with valid data has
been played out and the stop trigger.

> Using aplay with short clips like the action sounds for pidgin, etc
> makes these noise bursts obviously visible.

Yup, I've got a bunch of clips that I can reproduce the problem with,
and I can reproduce it reliably using aplay.  However, the problem I'm
seeing seems to be related to a dev_dbg() call in the trigger stop
path.  When KERN_DEBUG messages get sent to the console, and the
console is one of the PSC ports, then I get the replayed sample
artifact at the end.  However, if I 'tail -f /var/log/kern.log', then
I still get to see the debug output, but the audible artifact doesn't
occur.  That says to me that the real problem is an unbounded latency
caused by another part of the kernel (the tty console in this case).

It seems to me that aplay doesn't silence out very many buffers past
the end of the sample, but I won't know for sure until I instrument it
to see what data is present in the buffers.  I'll do that next week.
>
> To fix this you need a mechanism to determine where the valid data in
> the buffering system ends and noise starts. Once you know the extent
> of the valid data we can keep the DMA channel programmed to not play
> invalid data. You can play off the end of valid data two ways; under
> run when ALSA doesn't supply data fast enough and end of buffer.

Underrun is a realtime failure.  ALSA handles it by triggering STOP
and START of the stream AFAIKT.  Just about all ALSA drivers using DMA
will end up replaying buffers if the kernel cannot keep up with
hardware.

End of buffer seems to be the responsibility of userspace, but I need
to investigate this more.

My experiments to this point seems to suggest that when you hear the
artifacts it is due to both the end of buffer condition, and a
realtime failure in executing the stop trigger.

> ALSA does not provide information on where valid data ends in easily
> consumable form so I've been trying to reconstruct it from appl_ptr.
> A much cleaner solution would be for ALSA to provide a field that
> indicates the last valid address in the ring buffer system. Then in
> the driver's buffer complete callback I could get that value and
> reprogram the DMA engine not to run off the end of valid data. As each
> buffer completes I would reread the value and update the DMA stop
> address. You also need the last valid address field when DMA is first
> started.

... assuming that audio needs to stop exactly at the end of valid
data.  But if the last few periods are silence, then this assumption
isn't true.

g.

-- 
Grant Likely, B.Sc., P.Eng.
Secret Lab Technologies Ltd.

  parent reply	other threads:[~2009-11-07 18:51 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-07  8:33 [PATCH 0/6] Fixups to MPC5200 ASoC drivers Grant Likely
2009-11-07  8:33 ` [PATCH 1/6] ASoC/mpc5200: Track DMA position by period number instead of bytes Grant Likely
2009-11-07 10:35   ` Liam Girdwood
2009-11-07 16:50     ` Grant Likely
2009-11-07  8:34 ` [PATCH 2/6] ASoC/mpc5200: get rid of the appl_ptr tracking nonsense Grant Likely
2009-11-07 12:51   ` Jon Smirl
2009-11-07 13:04     ` Jon Smirl
2009-11-07 18:53       ` Grant Likely
2009-11-07 18:51     ` Grant Likely [this message]
2009-11-07 20:12       ` Mark Brown
2009-11-11 16:38         ` [alsa-devel] " Jon Smirl
2009-11-11 18:37           ` Mark Brown
2009-11-11 19:24             ` Grant Likely
2009-11-11 20:03               ` Mark Brown
2009-11-11 21:34               ` Jon Smirl
2009-11-11 21:57                 ` [alsa-devel] " Grant Likely
2009-11-11 23:13                   ` Jon Smirl
2009-11-12 12:10                     ` Mark Brown
2009-11-11 21:26             ` Jon Smirl
2009-11-07  8:34 ` [PATCH 3/6] ASoC/mpc5200: Improve printk debug output for trigger Grant Likely
2009-11-07  8:34 ` [PATCH 4/6] ASoC/mpc5200: add to_psc_dma_stream() helper Grant Likely
2009-11-07 12:33   ` Mark Brown
2009-11-07  8:34 ` [PATCH 5/6] ASoC/mpc5200: fix enable/disable of AC97 slots Grant Likely
2009-11-07  8:34 ` [PATCH 6/6] ASoC/mpc5200: Add fudge factor to value reported by .pointer() Grant Likely
2009-11-07 18:11   ` Mark Brown
2009-11-07 18:19     ` Grant Likely
2009-11-07 19:33       ` Mark Brown
2009-11-07 19:46         ` Grant Likely
2009-11-07 12:57 ` [PATCH 0/6] Fixups to MPC5200 ASoC drivers Mark Brown
2009-11-07 16:52   ` Grant Likely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=fa686aa40911071051y3cf63e55ibe7fbc65c30bd15e@mail.gmail.com \
    --to=grant.likely@secretlab.ca \
    --cc=alsa-devel@alsa-project.org \
    --cc=bones@secretlab.ca \
    --cc=broonie@opensource.wolfsonmicro.com \
    --cc=jonsmirl@gmail.com \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=lrg@slimlogic.co.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox