Skip to contentSkip to conversation

Click after draining

DISCUSSION Giuliano Pochini 2 participants 18 messages Sep 21, 2004 – Nov 9, 2004

lore
Share & tools
lore
#0 lore raw

With both the powermac driver and my echoaudio driver, playback ends with a "click". With properly choosen buffer/period/file sizes I can make the card play almost a whole period of old data before the substream is actually stopped. It seems that periods after the last one are not silenced, but I'm not having luck at findind the cause. This is not a recent problem.

For example, I made a test sound that is a pure tone with a quick fade to silence at the end:
-rw-rw-r-- 1 Giu Giu 606884 set 19 18:56 t2.wav

I played it with: "aplay -B1486077 -F600000 -v t2.wav" because it's slow enough I can follow debug messages on the screen. I got this trace from the irq handler (printed just before calling snd_pcm_period_elapsed()):

pcm_trigger start
irq per=1 state=3 bytes=105984, t=1095614506322223 irq per=2 state=3 bytes=211840, t=1095614506921131 irq per=0 state=3 bytes=262272, t=1095614507206088 irq per=1 state=3 bytes=368128, t=1095614507804997 irq per=2 state=3 bytes=473984, t=1095614508403906 irq per=0 state=5 bytes=524416, t=1095614508688863 irq per=1 state=5 bytes=630272, t=1095614509287772 *** irq per=2 state=5 bytes=736128, t=1095614509886681 pcm_trigger stop

per is the period, state is the runtime state (3=running, 5=draining), bytes are the number of bytes the DSP read from memory, t is the time_t in useconds.

The line marked with "***" is the period that should be silent (it's past the end of the file) but still contains old data. Also I don't understand why it didn't stop the card at that point.

I also looked for this problem on a PC with an intel8x0 chip, but it worked fine. Any hint to debug this issue is VERY welcome because I'm stuck.

--
Giuliano.

-------------------------------------------------------
This SF.Net email is sponsored by: YOU BE THE JUDGE. Be one of 170 Project Admins to receive an Apple iPod Mini FREE for your judgement on who ports your project to Linux PPC the best. Sponsored by IBM.
Deadline: Sept. 24. Go here: http://sf.net/ppc_contest.php

#1 lore raw
Quoted 6 lines · On Tue, 21 Sep 2004 22:13:18 +0200 Giuliano Pochini <[email protected]> wrote:

With both the powermac driver and my echoaudio driver, playback ends with a "click". With properly choosen buffer/period/file sizes I can make the card play almost a whole period of old data before the substream is actually stopped. It seems that periods after the last one are not silenced, but I'm not having luck at findind the cause. This is not a recent problem. [...]

I reply myself to add that I asked about this issue at one of the betatesters and he can hear the click also. It's not a mac-only problem... Any ideas ?

--
Giuliano.

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl

#2 lore raw

At Thu, 7 Oct 2004 20:58:01 +0200,

Quoted 11 lines · Giuliano Pochini
Quoted 6 lines · On Tue, 21 Sep 2004 22:13:18 +0200 Giuliano Pochini <[email protected]> wrote:

With both the powermac driver and my echoaudio driver, playback ends with a "click". With properly choosen buffer/period/file sizes I can make the card play almost a whole period of old data before the substream is actually stopped. It seems that periods after the last one are not silenced, but I'm not having luck at findind the cause. This is not a recent problem. [...]

Quoted 3 lines

I reply myself to add that I asked about this issue at one of the betatesters and he can hear the click also. It's not a mac-only problem... Any ideas ?

ALSA seems not putting silence properly unless silence_size > 0. How about the attached patch?
(it's an untested and quick fix, as usual :)

Takashi

Index: alsa-kernel/include/pcm.h
===================================================================
RCS file: /suse/tiwai/cvs/alsa/alsa-kernel/include/pcm.h,v retrieving revision 1.50

include/pcm.h +1 -0
diff -u -r1.50 pcm.h
@@ -875,6 +875,7 @@
 int snd_pcm_playback_xrun_asap(snd_pcm_substream_t *substream);
 int snd_pcm_capture_xrun_asap(snd_pcm_substream_t *substream);
 void snd_pcm_playback_silence(snd_pcm_substream_t *substream, snd_pcm_uframes_t new_hw_ptr);
+int snd_pcm_lib_fill_silence(snd_pcm_substream_t *substream, unsigned int ofs, snd_pcm_uframes_t frames);
 int snd_pcm_playback_ready(snd_pcm_substream_t *substream);
 int snd_pcm_capture_ready(snd_pcm_substream_t *substream);
 long snd_pcm_playback_ready_jiffies(snd_pcm_substream_t *substream);

Index: alsa-kernel/core/pcm_lib.c
===================================================================
RCS file: /suse/tiwai/cvs/alsa/alsa-kernel/core/pcm_lib.c,v retrieving revision 1.57

core/pcm_lib.c +40 -26

78 lines not on this page · raw message

Index: alsa-kernel/core/pcm_native.c
===================================================================
RCS file: /suse/tiwai/cvs/alsa/alsa-kernel/core/pcm_native.c,v retrieving revision 1.84

core/pcm_native.c +12 -1
diff -u -r1.84 pcm_native.c
@@ -1296,7 +1296,18 @@
 			runtime->status->state = SNDRV_PCM_STATE_DRAINING;
 			break;
 		default:
-			break;
+			return 0; /* do nothing */
+		}
+		if (! runtime->silence_size) {
+			/* fill silence until the priod end */
+			snd_pcm_uframes_t period_end = runtime->hw_ptr_interrupt + runtime->period_size;
+			snd_pcm_uframes_t appl_ptr = runtime->control->appl_ptr;
+			snd_pcm_sframes_t delta;
+			delta = (snd_pcm_sframes_t)(period_end - appl_ptr);
+			if (delta > 0)
+				snd_pcm_lib_fill_silence(substream,
+							 appl_ptr % runtime->buffer_size,
+							 delta);
 		}
 	} else {
 		/* stop running stream */
#3 lore raw

At Fri, 08 Oct 2004 17:34:45 +0200,

Quoted 14 lines · I wrote:

At Thu, 7 Oct 2004 20:58:01 +0200,

Quoted 11 lines · Giuliano Pochini
Quoted 6 lines · On Tue, 21 Sep 2004 22:13:18 +0200 Giuliano Pochini <[email protected]> wrote:

With both the powermac driver and my echoaudio driver, playback ends with a "click". With properly choosen buffer/period/file sizes I can make the card play almost a whole period of old data before the substream is actually stopped. It seems that periods after the last one are not silenced, but I'm not having luck at findind the cause. This is not a recent problem. [...]

Quoted 7 lines
Quoted 3 lines

I reply myself to add that I asked about this issue at one of the betatesters and he can hear the click also. It's not a mac-only problem... Any ideas ?

ALSA seems not putting silence properly unless silence_size > 0. How about the attached patch?
(it's an untested and quick fix, as usual :)

And broken, as usual :)
The corrected on is below.

Takashi

Index: alsa-kernel/include/pcm.h
===================================================================
RCS file: /suse/tiwai/cvs/alsa/alsa-kernel/include/pcm.h,v retrieving revision 1.50

include/pcm.h +1 -0
diff -u -r1.50 pcm.h
@@ -875,6 +875,7 @@
 int snd_pcm_playback_xrun_asap(snd_pcm_substream_t *substream);
 int snd_pcm_capture_xrun_asap(snd_pcm_substream_t *substream);
 void snd_pcm_playback_silence(snd_pcm_substream_t *substream, snd_pcm_uframes_t new_hw_ptr);
+int snd_pcm_lib_fill_silence(snd_pcm_substream_t *substream, unsigned int ofs, snd_pcm_uframes_t frames);
 int snd_pcm_playback_ready(snd_pcm_substream_t *substream);
 int snd_pcm_capture_ready(snd_pcm_substream_t *substream);
 long snd_pcm_playback_ready_jiffies(snd_pcm_substream_t *substream);

Index: alsa-kernel/core/pcm_lib.c
===================================================================
RCS file: /suse/tiwai/cvs/alsa/alsa-kernel/core/pcm_lib.c,v retrieving revision 1.57

core/pcm_lib.c +40 -26

78 lines not on this page · raw message

Index: alsa-kernel/core/pcm_native.c
===================================================================
RCS file: /suse/tiwai/cvs/alsa/alsa-kernel/core/pcm_native.c,v retrieving revision 1.84

core/pcm_native.c +10 -1
diff -u -r1.84 pcm_native.c
@@ -1296,7 +1296,16 @@
 			runtime->status->state = SNDRV_PCM_STATE_DRAINING;
 			break;
 		default:
-			break;
+			return 0; /* do nothing */
+		}
+		if (! runtime->silence_size) {
+			/* fill silence until the priod end */
+			snd_pcm_uframes_t appl_ptr = runtime->control->appl_ptr;
+			snd_pcm_uframes_t delta = appl_ptr % runtime->period_size;
+			if (delta > 0)
+				snd_pcm_lib_fill_silence(substream,
+							 appl_ptr % runtime->buffer_size,
+							 runtime->period_size - delta);
 		}
 	} else {
 		/* stop running stream */
#4 lore raw
Quoted 11 lines · On Fri, 08 Oct 2004 18:07:53 +0200 Takashi Iwai <[email protected]> wrote:
Quoted 11 lines
Quoted 10 lines
Quoted 6 lines

With both the powermac driver and my echoaudio driver, playback ends with a "click". With properly choosen buffer/period/file sizes I can make the card play almost a whole period of old data before the substream is actually stopped. It seems that periods after the last one are not silenced, but I'm not having luck at findind the cause. This is not a recent problem. [...]

I reply myself to add that I asked about this issue at one of the betatesters and he can hear the click also. It's not a mac-only problem... Any ideas ?

Quoted 6 lines
Quoted 3 lines

ALSA seems not putting silence properly unless silence_size > 0. How about the attached patch?
(it's an untested and quick fix, as usual :)

And broken, as usual :)
The corrected on is below.

No changes at all, sorry. I'll investigate further tomorrow.

--
Giuliano.

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl

#5 lore raw
In reply to Takashi Iwai
Quoted 6 lines · On Fri, 08 Oct 2004 18:07:53 +0200 Takashi Iwai <[email protected]> wrote:
Quoted 3 lines

ALSA seems not putting silence properly unless silence_size > 0. How about the attached patch?
(it's an untested and quick fix, as usual :)

And broken, as usual :)
The corrected on is below.

The patch doesn't fix the problem.

With your patch snd_pcm_do_drain_init() does:

	if (! runtime->silence_size) {
		/* fill silence until the priod end */
		snd_pcm_uframes_t appl_ptr = runtime->control->appl_ptr;
		snd_pcm_uframes_t delta = appl_ptr % runtime->period_size;
		if (delta > 0)
			snd_pcm_lib_fill_silence(substream,
					appl_ptr % runtime->buffer_size,
					runtime->period_size - delta);
	}

In my case delta is always 0. It seems ALSA aligns appl_ptr to the offset of the next period without silencing the current one. I could not find where. Anyway, calling _fill_silence() with length=runtime->period_size - delta is wrong IMHO because not all periods have have the same size. The last one can be smaller and in that case it will overflow.

Also, I don't think filling with silence until the end of the period is enough. ALSA stops the card some time it gets the interrupt, thus it should silence also the subsequent period, or its beginning part.

Replacing the block above with the following code fixes the problem for me:

	if (!runtime->silence_size) {
		snd_pcm_uframes_t start, end;

		start = runtime->control->appl_ptr % runtime->buffer_size;
		end = runtime->status->hw_ptr % runtime->buffer_size;

		if (end < start) {
			snd_pcm_lib_fill_silence(substream, start, runtime->buffer_size - start);
			snd_pcm_lib_fill_silence(substream, 0, end);
		} else {
			snd_pcm_lib_fill_silence(substream, start, end - start);
		}
	}

* BUT it not the right fix *

It works fine when there are 3 or more periods, and in that case it writes more stuff than needed (easily fixable). When there are only 2 periods it's not enough. For example:

per 0 per 1
Buffer: |---------------|---------------|
^^
appl_ptr / \ hw_ptr

Suppose period 0 is the last period to be played, so draining starts at its end. My code fills just an handful of frames, and they may not be enough to avoid the click when the card will reach the end of period 0 again. ALSA should silence period 1 at the end of period 1. I don't know how to do it.

--
Giuliano.

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl

#6 lore raw

At Sat, 16 Oct 2004 21:31:13 +0200,

Quoted 25 lines · Giuliano Pochini
Quoted 6 lines · On Fri, 08 Oct 2004 18:07:53 +0200 Takashi Iwai <[email protected]> wrote:
Quoted 3 lines

ALSA seems not putting silence properly unless silence_size > 0. How about the attached patch?
(it's an untested and quick fix, as usual :)

And broken, as usual :)
The corrected on is below.

The patch doesn't fix the problem.

With your patch snd_pcm_do_drain_init() does:

	if (! runtime->silence_size) {
		/* fill silence until the priod end */
		snd_pcm_uframes_t appl_ptr = runtime->control->appl_ptr;
		snd_pcm_uframes_t delta = appl_ptr % runtime->period_size;
		if (delta > 0)
			snd_pcm_lib_fill_silence(substream,
					appl_ptr % runtime->buffer_size,
					runtime->period_size - delta);
	}
Quoted 2 lines

In my case delta is always 0. It seems ALSA aligns appl_ptr to the offset of the next period without silencing the current one.

If so, it has to be in alsa-lib. Not a bug of ALSA driver.

Quoted 4 lines

I could not find where.
Anyway, calling _fill_silence() with length=runtime->period_size - delta is wrong IMHO because not all periods have have the same size. The last one can be smaller and in that case it will overflow.

The period size is always same even for the last period, AFAIK.

Quoted 3 lines

Also, I don't think filling with silence until the end of the period is enough. ALSA stops the card some time it gets the interrupt, thus it should silence also the subsequent period, or its beginning part.

I thought of that, too, but concluded that it's a questionary behavior.

Usually, it's not needed if the interrupt is taken fast enough at the moment the last period is processed. The click noise comes from the garbage data in the last period. Also, the app can specify silence_size if it needs this behavior explicitly.

However, there can be a case that the irq isn't handled fast enough, or the DMA and the interrupt isn't synchronized well (e.g. using a timer interrupt because of lack of DMA interrupt). In such a case, auto-silencing would be needed for the proper work...

Takashi

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl

#7 lore raw
Quoted 4 lines · On 18-Oct-2004 Takashi Iwai wrote:
Quoted 2 lines

In my case delta is always 0. It seems ALSA aligns appl_ptr to the offset of the next period without silencing the current one.

If so, it has to be in alsa-lib. Not a bug of ALSA driver.

Ok, I'll check that one.

Quoted 5 lines
Quoted 3 lines

Anyway, calling _fill_silence() with length=runtime->period_size - delta is wrong IMHO because not all periods have have the same size. The last one can be smaller and in that case it will overflow.

The period size is always same even for the last period, AFAIK.

Only if the driver explicitly asks for it by calling snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS).

Quoted 9 lines
Quoted 3 lines

Also, I don't think filling with silence until the end of the period is enough. ALSA stops the card some time it gets the interrupt, thus it should silence also the subsequent period, or its beginning part.

I thought of that, too, but concluded that it's a questionary behavior.

Usually, it's not needed if the interrupt is taken fast enough at the moment the last period is processed.

"Fast" means <22us at 44100Hz...

Quoted 1 line

The click noise comes from the garbage data in the last period.

Indeed.

--
Giuliano.

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl

#8 lore raw

At Mon, 18 Oct 2004 16:56:11 +0200 (CEST),

Quoted 9 lines · Giuliano Pochini
Quoted 6 lines
Quoted 4 lines

Anyway, calling _fill_silence() with length=runtime->period_size - delta is wrong IMHO because not all periods have have the same size. The last one can be smaller and in that case it will overflow.

The period size is always same even for the last period, AFAIK.

Only if the driver explicitly asks for it by calling snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS).

No, this means that buffer_size = N * period_size, where N is integer. Without this constraint, N doesn't have to be an integer.

Takashi

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl

#9 lore raw
Quoted 7 lines · On 18-Oct-2004 Takashi Iwai wrote:
Quoted 4 lines
Quoted 1 line

The period size is always same even for the last period, AFAIK.

Only if the driver explicitly asks for it by calling snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS).

No, this means that buffer_size = N * period_size, where N is integer. Without this constraint, N doesn't have to be an integer.

Yes, exactly. If N isn't integer, the buffer is formed by bufsize\persize periods (== the integer part of N) plus one short period which is bufsize%persize long.

--
Giuliano.

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl

#10 lore raw

At Mon, 18 Oct 2004 17:34:02 +0200 (CEST),

Quoted 9 lines · Giuliano Pochini
Quoted 5 lines · On 18-Oct-2004 Takashi Iwai wrote:
Quoted 4 lines
Quoted 1 line

The period size is always same even for the last period, AFAIK.

Only if the driver explicitly asks for it by calling snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS).

Quoted 7 lines
Quoted 2 lines

No, this means that buffer_size = N * period_size, where N is integer. Without this constraint, N doesn't have to be an integer.

Yes, exactly. If N isn't integer, the buffer is formed by bufsize\persize periods (== the integer part of N) plus one short period which is bufsize%persize long.

The period size is constant regardless of N because it simply defines the interval of interrupts. When N isn't integer (say, 2.5), the transfer point moves like:

	0, 1, 2, 0.5, 1.5, 0, 1, ...

That is, when the point is at 2, the transfer is split to two parts (2-2.5 and 0-0.5).

Takashi

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl

#11 lore raw
Quoted 12 lines · On 19-Oct-2004 Takashi Iwai wrote:
Quoted 11 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

The period size is always same even for the last period, AFAIK.

Only if the driver explicitly asks for it by calling snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS).

No, this means that buffer_size = N * period_size, where N is integer. Without this constraint, N doesn't have to be an integer.

Yes, exactly. If N isn't integer, the buffer is formed by bufsize\persize periods (== the integer part of N) plus one short period which is bufsize%persize long.

Quoted 8 lines

The period size is constant regardless of N because it simply defines the interval of interrupts. When N isn't integer (say, 2.5), the transfer point moves like:

      0, 1, 2, 0.5, 1.5, 0, 1, ...

That is, when the point is at 2, the transfer is split to two parts (2-2.5 and 0-0.5).

Currently my driver uses a short period. To fix it the driver should use a special feature of the DSP that allows the driver to build the s-g list on the fly. It also has the bonus that the DSP automatically stops the trasport when it reaches the last programmed period, so draining and xruns stop exactly where expected. It's more complex than the current ring buffer implementation.

Out of curiosity, is there any driver that supports non-integer N ? And why everything works fine even if the driver uses unequal periods ? It should skip all the time.

Anyway, _fill_silence() can still overflow.

--
Giuliano.

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl

#12 lore raw

At Tue, 19 Oct 2004 17:09:25 +0200 (CEST),

Quoted 20 lines · Giuliano Pochini
Quoted 16 lines · On 19-Oct-2004 Takashi Iwai wrote:
Quoted 11 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

The period size is always same even for the last period, AFAIK.

Only if the driver explicitly asks for it by calling snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS).

No, this means that buffer_size = N * period_size, where N is integer. Without this constraint, N doesn't have to be an integer.

Yes, exactly. If N isn't integer, the buffer is formed by bufsize\persize periods (== the integer part of N) plus one short period which is bufsize%persize long.

The period size is constant regardless of N because it simply defines the interval of interrupts. When N isn't integer (say, 2.5), the transfer point moves like:

Quoted 6 lines
Quoted 4 lines
      0, 1, 2, 0.5, 1.5, 0, 1, ...

That is, when the point is at 2, the transfer is split to two parts (2-2.5 and 0-0.5).

Currently my driver uses a short period.

Well, then this is not a supported feature. period_size must be constant in the current implementation.

Quoted 10 lines

To fix it the
driver should use a special feature of the DSP that allows the driver to build the s-g list on the fly. It also has the bonus that the DSP automatically stops the trasport when it reaches the last programmed period, so draining and xruns stop exactly where expected. It's more complex than the current ring buffer implementation.

Out of curiosity, is there any driver that supports non-integer N ?

Yes, many drivers. As mentioned, the period_size is nothing but the interrupt interval. On many chips, the interrupt interval is not bound to the certain buffer positions.

Quoted 2 lines

And why everything works fine even if the
driver uses unequal periods ? It should skip all the time.

Maybe it has worked casually :)

In fact, period_size is referred rarely in the code. Only snd_pcm_update_hw_ptr_interrupt() refers it, and changing period_size only once per buffer isn't critical here since hw_ptr_interrupt is corrected at each time from the last hw_ptr.

Quoted 1 line

Anyway, _fill_silence() can still overflow.

In your case, yes. But it's exceptional.

Usually, the DMA is stopped in the interrupt handler which processes the last period. That is, the resolution of control is in the interrupt interval (= period_size). This means that the whole period data is transferred even if only the short data has been written to the buffer.

Takashi

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl

#13 lore raw
Quoted 18 lines · On 19-Oct-2004 Takashi Iwai wrote:
Quoted 18 lines
Quoted 18 lines
Quoted 11 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

The period size is always same even for the last period, AFAIK.

Only if the driver explicitly asks for it by calling snd_pcm_hw_constraint_integer(runtime, SNDRV_PCM_HW_PARAM_PERIODS).

No, this means that buffer_size = N * period_size, where N is integer. Without this constraint, N doesn't have to be an integer.

Yes, exactly. If N isn't integer, the buffer is formed by bufsize\persize periods (== the integer part of N) plus one short period which is bufsize%persize long.

The period size is constant regardless of N because it simply defines the interval of interrupts. When N isn't integer (say, 2.5), the transfer point moves like:

      0, 1, 2, 0.5, 1.5, 0, 1, ...
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

That is, when the point is at 2, the transfer is split to two parts (2-2.5 and 0-0.5).

Currently my driver uses a short period.

Well, then this is not a supported feature. period_size must be constant in the current implementation.

Ok, I'll add that constraint. I'm not sure if using that DSP feature is worth the effort because those extra features do not come for free. The driver has to do more work in the irq handler and it has to send send one more command to the DSP, which may be pretty slow on old 20-bits cards. I'll evaluate it later. I would like to get the driver included in alsa-driver asap, before starting to add support for the new "3G" cards.

Quoted 3 lines
Quoted 1 line

Anyway, _fill_silence() can still overflow.

In your case, yes. But it's exceptional.

No, it also happens with you example when
period_offs+period_len > buffer_size:

N=3.5:

first round:
0000000011111111222222223333

2nd:
3333444444445555555566666666
....

fill_silence(per=3, period_len) overflows.

Quoted 5 lines

Usually, the DMA is stopped in the interrupt handler which processes the last period. That is, the resolution of control is in the interrupt interval (= period_size). This means that the whole period data is transferred even if only the short data has been written to the buffer.

Yes, I know, but the part from the last frame written by the app and the end of the period is not silenced *and* appl_ptr is moved to the end of the period before the call to drain_init(). I'm still looking for the cause. Sorry for my slowness, but I don't have a lot of time right now :(

--
Giuliano.

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl

#14 lore raw

At Wed, 20 Oct 2004 10:35:34 +0200 (CEST),

Quoted 10 lines · Giuliano Pochini
Quoted 4 lines
Quoted 2 lines

Anyway, _fill_silence() can still overflow.

In your case, yes. But it's exceptional.

No, it also happens with you example when
period_offs+period_len > buffer_size:

N=3.5:

Quoted 8 lines

first round:
0000000011111111222222223333

2nd:
3333444444445555555566666666
....

fill_silence(per=3, period_len) overflows.

Oh, yeah, you're right. This has to be handled correctly in fill_silence().

Takashi

-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal Use IT products in your business? Tell us what you think of them. Give us Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl

#15 lore raw
In reply to Takashi Iwai
Quoted 12 lines · On Mon, 18 Oct 2004 13:14:58 +0200 Takashi Iwai <[email protected]> wrote:
Quoted 12 lines

With your patch snd_pcm_do_drain_init() does:

	if (! runtime->silence_size) {
		/* fill silence until the priod end */
		snd_pcm_uframes_t appl_ptr = runtime->control->appl_ptr;
		snd_pcm_uframes_t delta = appl_ptr % runtime->period_size;
		if (delta > 0)
			snd_pcm_lib_fill_silence(substream,
					appl_ptr % runtime->buffer_size,
					runtime->period_size - delta);
	}
Quoted 4 lines
Quoted 2 lines

In my case delta is always 0. It seems ALSA aligns appl_ptr to the offset of the next period without silencing the current one.

If so, it has to be in alsa-lib. Not a bug of ALSA driver.

No, the bug is in my analysis. I had some time to look at this issue again. Your patch is ok. I was fooled by aplay that explicitly fills the last part of each block before writing it in chunks that have always the same size and my driver, which used unequally-sized periods, contributed to mess up the numbers.

The patch below is mostly the one you wrote, with these changes: it handles the case when the period to be silences crosses the buffer boundary and it fills with silence the last played period while draining. It's not the right thing of course but it work for me. Actually, it should fill only the first x frames of the period following the last one, but I need another variable.

Quoted 5 lines
Quoted 3 lines

Also, I don't think filling with silence until the end of the period is enough. ALSA stops the card some time it gets the interrupt, thus it should silence also the subsequent period, or its beginning part.

I thought of that, too, but concluded that it's a questionary behavior.

It's a minor problem because it only shows up during playback and if the song has a quick fade out. In my case the card reads at least 32 frames before it's stopped.

core/pcm_lib.c renamed from home/Giu/soft/ad/alsa-kernel/core/pcm_lib.c +72 -32
@@ -95,32 +95,10 @@
 	ofs = (runtime->silence_start + runtime->silence_filled) % runtime->buffer_size;
 	while (frames > 0) {
 		transfer = ofs + frames > runtime->buffer_size ? runtime->buffer_size - ofs : frames;
-		if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
-		    runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
-			if (substream->ops->silence) {
-				int err;
-				err = substream->ops->silence(substream, -1, ofs, transfer);
-				snd_assert(err >= 0, );
-			} else {
-				char *hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
-				snd_pcm_format_set_silence(runtime->format, hwbuf, transfer * runtime->channels);
-			}
-		} else {
-			unsigned int c;
-			unsigned int channels = runtime->channels;
-			if (substream->ops->silence) {
-				for (c = 0; c < channels; ++c) {
-					int err;
-					err = substream->ops->silence(substream, c, ofs, transfer);
-					snd_assert(err >= 0, );
-				}
-			} else {
-				size_t dma_csize = runtime->dma_bytes / channels;
-				for (c = 0; c < channels; ++c) {
-					char *hwbuf = runtime->dma_area + (c * dma_csize) + samples_to_bytes(runtime, ofs);
-					snd_pcm_format_set_silence(runtime->format, hwbuf, transfer);
-				}
-			}
+
+		if (snd_pcm_lib_fill_silence(substream, ofs, transfer) < 0) {
+			snd_printk(KERN_ERR "fill_silence error\n");
+			break;
 		}
 		runtime->silence_filled += transfer;
 		frames -= transfer;
@@ -128,6 +106,60 @@
 	}
 }
 
+static int snd_pcm_lib_fill_silence1(snd_pcm_substream_t *substream, unsigned int ofs, snd_pcm_uframes_t frames)
+{
+	snd_pcm_runtime_t *runtime = substream->runtime;
+	int c, channels = runtime->channels;
+	char *hwbuf;
+	size_t dma_csize;
+
+	//snd_printk("snd_pcm_lib_fill_silence(ofs=%d, frames=%d)\n", ofs, (int)frames);
+	if (substream->ops->silence) {
+		int err = 0;
+		if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
+		    runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED)
+			err = substream->ops->silence(substream, -1, ofs, frames);
+		else {
+			for (c = 0; c < channels; c++) {
+				if ((err = substream->ops->silence(substream, c, ofs, frames)) < 0)
+					break;
+			}
+		}
+		return err;
+	}
+
+	if (runtime->access == SNDRV_PCM_ACCESS_RW_INTERLEAVED ||
+	    runtime->access == SNDRV_PCM_ACCESS_MMAP_INTERLEAVED) {
+		hwbuf = runtime->dma_area + frames_to_bytes(runtime, ofs);
+		//snd_printk("snd_pcm_lib_fill_silence() set_sil()\n");
+		snd_pcm_format_set_silence(runtime->format, hwbuf, frames * channels);
+	} else {
+		dma_csize = runtime->dma_bytes / channels;
+		hwbuf = runtime->dma_area + samples_to_bytes(runtime, ofs);
+		for (c = 0; c < channels; c++) {
+			snd_pcm_format_set_silence(runtime->format, hwbuf, frames);
+			hwbuf += dma_csize;
+		}
+	}
+	return 0;
+}
+
+int snd_pcm_lib_fill_silence(snd_pcm_substream_t *substream, unsigned int ofs, snd_pcm_uframes_t frames)
+{
+	int err;
+
+	if (ofs + frames <= substream->runtime->buffer_size)
+		return snd_pcm_lib_fill_silence1(substream, ofs, frames);
+	else { /* Uncommon case: the part to be silenced goes beyond the end of the buffer */
+		err = snd_pcm_lib_fill_silence1(substream, ofs, substream->runtime->buffer_size - ofs);
+		if (err)
+			return err;
+		return snd_pcm_lib_fill_silence1(substream, 0, ofs + frames - substream->runtime->buffer_size);
+	}
+};
+
+
+
 static void xrun(snd_pcm_substream_t *substream)
 {
 	snd_pcm_stop(substream, SNDRV_PCM_STATE_XRUN);
@@ -222,10 +254,18 @@
 		new_hw_ptr = runtime->hw_ptr_base + pos;
 	}
 
-	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK &&
-	    runtime->silence_size > 0)
-		snd_pcm_playback_silence(substream, new_hw_ptr);
-
+	if (substream->stream == SNDRV_PCM_STREAM_PLAYBACK) {
+		if (runtime->silence_size > 0)
+			snd_pcm_playback_silence(substream, new_hw_ptr);
+#if 1
+		if (runtime->status->state == SNDRV_PCM_STATE_DRAINING) {
+			snd_pcm_uframes_t start;
+			start = runtime->status->hw_ptr % runtime->buffer_size;
+			start -= start % runtime->period_size;
+			snd_pcm_lib_fill_silence(substream, start, runtime->period_size);
+		}
+	}
+#endif
 	runtime->status->hw_ptr = new_hw_ptr;
 	runtime->hw_ptr_interrupt = new_hw_ptr - new_hw_ptr % runtime->period_size;
 
@@ -2080,12 +2120,12 @@
 	}
 	return 0;
 }
- 
+
 typedef int (*transfer_f)(snd_pcm_substream_t *substream, unsigned int hwoff,
 			  unsigned long data, unsigned int off,
 			  snd_pcm_uframes_t size);
 
-static snd_pcm_sframes_t snd_pcm_lib_write1(snd_pcm_substream_t *substream, 
+static snd_pcm_sframes_t snd_pcm_lib_write1(snd_pcm_substream_t *substream,
 					    unsigned long data,
 					    snd_pcm_uframes_t size,
 					    int nonblock,
core/pcm_native.c renamed from home/Giu/soft/ad/alsa-kernel/core/pcm_native.c +10 -1
@@ -1296,7 +1296,16 @@
 			runtime->status->state = SNDRV_PCM_STATE_DRAINING;
 			break;
 		default:
-			break;
+			return 0; /* do nothing */
+		}
+		if (!runtime->silence_size) {
+			/* fill silence until the period end */
+			snd_pcm_uframes_t appl_ptr = runtime->control->appl_ptr;
+			snd_pcm_uframes_t delta = appl_ptr % runtime->period_size;
+			if (delta > 0)
+				snd_pcm_lib_fill_silence(substream,
+							 appl_ptr % runtime->buffer_size,
+							 runtime->period_size - delta);
 		}
 	} else {
 		/* stop running stream */

--
Giuliano.

-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click

#16 lore raw

At Mon, 1 Nov 2004 20:29:49 +0100,

Quoted 6 lines · Giuliano Pochini

The patch below is mostly the one you wrote, with these changes: it handles the case when the period to be silences crosses the buffer boundary and it fills with silence the last played period while draining. It's not the right thing of course but it work for me. Actually, it should fill only the first x frames of the period following the last one, but I need another variable.

The first is the right fix, but how about the latter one (filling silence at the last position in snd_pcm_update_hw_ptr_interrupt()) ? Does it fix something?

Takashi

-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click

#17 lore raw
Quoted 10 lines · On 09-Nov-2004 Takashi Iwai wrote:

At Mon, 1 Nov 2004 20:29:49 +0100,

Quoted 6 lines · Giuliano Pochini

The patch below is mostly the one you wrote, with these changes: it handles the case when the period to be silences crosses the buffer boundary and it fills with silence the last played period while draining. It's not the right thing of course but it work for me. Actually, it should fill only the first x frames of the period following the last one, but I need another variable.

The first is the right fix

It's the part you wrote, with some minor tweaks.

Quoted 2 lines

but how about the latter one (filling silence at the last position in snd_pcm_update_hw_ptr_interrupt()) ? Does it fix something?

Yes, it fixes the remaining small click. It happens because the card does not stop immediately when the last interrupt is delivered. In my case it plays the first 32 frames of the next period that still contains old data. It's a very small problem compared to the big click you fixed.
My patch fills way too much data than needed of course. It's a just a test and it's not supposed to be applied.

--
Giuliano.

-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click