linux-mips.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Lauri Kasanen <cand@gmx.com>
To: Takashi Iwai <tiwai@suse.de>
Cc: linux-mips@vger.kernel.org, tsbogend@alpha.franken.de, perex@perex.cz
Subject: Re: [PATCH 5/6 v2] sound: Add n64 driver
Date: Mon, 11 Jan 2021 17:51:43 +0200	[thread overview]
Message-ID: <20210111175143.9cf93be4478eca67cf081649@gmx.com> (raw)
In-Reply-To: <s5hczybts17.wl-tiwai@suse.de>

On Mon, 11 Jan 2021 16:25:08 +0100
Takashi Iwai <tiwai@suse.de> wrote:

> On Mon, 11 Jan 2021 13:02:22 +0100,
> Lauri Kasanen wrote:
> >
> > On Mon, 11 Jan 2021 11:11:39 +0100
> > Takashi Iwai <tiwai@suse.de> wrote:
> > > Oh yes, at the last IRQ, the push should be avoided.
> > > I guess that the code order should be changed to the following way:
> > >
> > >   1. advance the position for a period size
> > >   2. call snd_pcm_period_elapsed()
> > >   3. check if the stream is still running
> > >   4. copy the next chunk and update nextpos
> >
> > This order gives correct pointer advancing etc, but now it's hitting a
> > new problem: the pcm core is reusing the buffer from under the audio
> > card. It's writing new data to the area that is currently being read by
> > DMA.
>
> Could you elaborate?  I still don't get what's going on there.

The audio plays partially out of order. At the point when the IRQ
fires, the data for the next period is correct, but it becomes
incorrect during playing it.

This is clear because if I memcpy it in the isr, like in the first
patch, the playback is correct. When copied to a private buffer, the
pcm core can't overwrite it when the card is reading it.

However the playback speed is correct, and when I print out the events,
the pointer advances correctly, one period at a time, and there are no
continuous stops and restarts.

> > I assume the core expects DMA to be instant, but in this card's case
> > it's ondemand, reading bytes as needed.
>
> No, PCM core doesn't expect it.  The only expectation is that the data
> from the current pointer to the next period boundary will be
> transferred until the next period-elapsed call.

Curious. This problem shouldn't exist then if everything is correct.
I'm thankful for your help in solving these; I haven't done alsa
hacking before.

In the current code, everything should be in sync with how you wrote.

period size 1026 frames
irq fires for the first time, pos becomes 1026*4=4104 bytes
snd_pcm_period_elapsed gets called, pointer returns 4104/4 frames
stream is running, so we call push()
push() starts playing at 4104 bytes into alsa's dma buffer

So the driver is playing the second period, and the core shouldn't be
writing into it as the pointer points to the start of that period.

> > By restoring the memcpy buffer, I get good audio with this new order
> > (sans occasional crackling due to the memcpy taking too long).
>
> The overwriting problem has been already present in the original patch
> version as I already argued.  The driver copies the full next period
> to be updated, but this is never guaranteed to have been filled by the
> application.  Maybe this didn't surface obviously with the original
> version because it essentially gives one more period available on the
> buffer -- i.e. it might be a matter of number of periods.

Yes, this startup issue was present, but I believe it's different to
the current issue. I could be wrong though.

- Lauri

  reply	other threads:[~2021-01-11 15:51 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-08  8:35 [PATCH 5/6 v2] sound: Add n64 driver Lauri Kasanen
2021-01-08  9:06 ` Takashi Iwai
2021-01-08 10:13   ` Lauri Kasanen
2021-01-09  7:23   ` Lauri Kasanen
2021-01-09  8:16     ` Takashi Iwai
2021-01-09 17:46       ` Lauri Kasanen
2021-01-09 18:17         ` Takashi Iwai
2021-01-09 20:54           ` Takashi Iwai
2021-01-10  7:15             ` Lauri Kasanen
2021-01-10 10:24               ` Takashi Iwai
2021-01-10 17:03                 ` Lauri Kasanen
2021-01-10 17:22                   ` Takashi Iwai
2021-01-10 17:41                     ` Lauri Kasanen
2021-01-11  8:05                       ` Takashi Iwai
2021-01-11  9:43                         ` Lauri Kasanen
2021-01-11 10:11                           ` Takashi Iwai
2021-01-11 12:02                             ` Lauri Kasanen
2021-01-11 15:25                               ` Takashi Iwai
2021-01-11 15:51                                 ` Lauri Kasanen [this message]
2021-01-13 11:57                                 ` Lauri Kasanen
2021-01-13 12:04                                   ` Takashi Iwai
2021-01-13 12:14                                     ` Lauri Kasanen
2021-01-13 12:38                                       ` Takashi Iwai
2021-01-13 12:49                                         ` Lauri Kasanen

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=20210111175143.9cf93be4478eca67cf081649@gmx.com \
    --to=cand@gmx.com \
    --cc=linux-mips@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=tiwai@suse.de \
    --cc=tsbogend@alpha.franken.de \
    /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;
as well as URLs for NNTP newsgroup(s).