Skip to contentSkip to conversation

sequencer queue PCM timing

DISCUSSION Chris Cannam 2 participants 6 messages May 13, 2004 – Jul 1, 2004

lore
Share & tools
lore
#0 lore raw

I asked about this on the JACK list just now and was recommended to bring the question here instead. So here we are.

If I'm running JACK with output to my first (and only) PCM device, and I have a JACK client running that also sets up an ALSA sequencer queue with its timer set to the ALSA PCM 0-0-0 playback timer, should I expect the ALSA queue's current time and the number of frames processed by the JACK client to correspond?

(Ignoring rounding error and any delays in startup time or
measurement: I simply mean that they shouldn't drift.)

I would expect them to remain in sync, and on both of my machines here they do, but I have a report from someone experiencing a quite considerable drift -- the JACK frame count ends up over a second and a half ahead of the ALSA timer over a ten minute period without xruns. How could that happen?

Chris

-------------------------------------------------------
This SF.Net email is sponsored by: SourceForge.net Broadband Sign-up now for SourceForge Broadband and get the fastest 6.0/768 connection for only $19.95/mo for the first 3 months!
http://ads.osdn.com/?ad_id=2562&alloc_id=6184&op=click

#1 lore raw
Quoted 9 lines · Chris Cannam

I asked about this on the JACK list just now and was recommended to bring the question here instead. So here we are.

If I'm running JACK with output to my first (and only) PCM device, and I have a JACK client running that also sets up an ALSA sequencer queue with its timer set to the ALSA PCM 0-0-0 playback timer, should I expect the ALSA queue's current time and the number of frames processed by the JACK client to correspond?

Quoted 8 lines

(Ignoring rounding error and any delays in startup time or
measurement: I simply mean that they shouldn't drift.)

I would expect them to remain in sync, and on both of my machines here they do, but I have a report from someone experiencing a quite considerable drift -- the JACK frame count ends up over a second and a half ahead of the ALSA timer over a ten minute period without xruns. How could that happen?

Maybe, lost interrupts can cause this. Actually, there is no correction in the PCM slave timer code for this situation. Can this problem be reproduced using a bigger period size for the JACK daemon?

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SuSE Labs

-------------------------------------------------------
This SF.Net email is sponsored by: SourceForge.net Broadband Sign-up now for SourceForge Broadband and get the fastest 6.0/768 connection for only $19.95/mo for the first 3 months!
http://ads.osdn.com/?ad_id=2562&alloc_id=6184&op=click

#2 lore raw
Quoted 2 lines · Jaroslav Kysela

Maybe, lost interrupts can cause this. Actually, there is no correction in the PCM slave timer code for this situation.

Looking over the figures again, it looks like (for the test case forwarded to me) the sequencer queue is actually consistently reporting more frames than JACK. Does that mean JACK might be losing, or can the sequencer queue ever gain?

For what it's worth (very little I imagine) both the sequencer queue and the JACK frame count are slightly behind the elapsed time reported by gettimeofday.

Quoted 3 lines

Can this
problem be reproduced using a bigger period size for the JACK daemon?

I'm not quite sure what you mean. Do you mean if I were to run JACK with a bigger period size, on my machine where this problem hasn't occurred before, it might cause something to start dropping interrupts? Why? I'm happy to give it a go, but I don't understand.

Chris

-------------------------------------------------------
This SF.Net email is sponsored by: SourceForge.net Broadband Sign-up now for SourceForge Broadband and get the fastest 6.0/768 connection for only $19.95/mo for the first 3 months!
http://ads.osdn.com/?ad_id=2562&alloc_id=6184&op=click

#3 lore raw

sequencer queue PCM timing drift

This is further to a problem I asked about a while ago but never really got anywhere with. At the time, I had seen it reported on the Rosegarden devel list but thought it didn't happen on my own hardware; now I realise it does.

I have a small test program that creates an ALSA sequencer queue timed from the PCM-0-0-0 timer as well as a JACK client, and that lets them both run and reports any drift between sequencer timing and JACK frame count. This is the program:

http://cvs.sf.net/viewcvs.py/rosegarden/base/test/seq/
queue-timer-jack.c?rev=1.1

If I run jackd at 48kHz and then let this program run for a few minutes, I see no appreciable drift.

However, if I run jackd at 44.1kHz (something I hardly ever do) and then start this program and let it run for a few minutes, I do get a predictable drift -- the ALSA queue timer is faster (i.e. claims to have seen more frames go by) by about 0.25%, or 0.15 seconds per minute of run time, with no xruns.

In both cases jackd is running on the hw:0 ALSA PCM device with 1024 frames per period, 2 periods/buffer; the soundcard is an M-Audio Audiophile 2496; /proc/asound/timers says

G0: system timer : 1000.000us (10000000 ticks)
P0-0-0: PCM playback 0-0-0 : SLAVE
Client sequencer queue 0 : running : lost interrupts 0
P0-0-1: PCM capture 0-0-1 : SLAVE

and /proc/asound/seq/timer says (in the 44.1 case)

Timer for queue 0 : PCM playback 0-0-0
Period time : 0.046545454
Skew : 65536 / 65536

What could cause this drift? Where should I look next?

Chris

-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training. Attend Black Hat Briefings & Training, Las Vegas July 24-29 - digital self defense, top technical experts, no vendor pitches, unmatched networking opportunities. Visit www.blackhat.com

#4 lore raw

Re: sequencer queue PCM timing drift

Quoted 5 lines · Chris Cannam

Timer for queue 0 : PCM playback 0-0-0
Period time : 0.046545454
Skew : 65536 / 65536

What could cause this drift? Where should I look next?

It looks like a rounding problem:

1024/44100 * 2 = 0.046439909

Look to snd_pcm_timer_resolution_change() in pcm_timer.c for more details.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SUSE Labs

-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training. Attend Black Hat Briefings & Training, Las Vegas July 24-29 - digital self defense, top technical experts, no vendor pitches, unmatched networking opportunities. Visit www.blackhat.com

#5 lore raw

Re: sequencer queue PCM timing drift

Quoted 4 lines · Jaroslav Kysela

It looks like a rounding problem:
[...]
Look to snd_pcm_timer_resolution_change() in pcm_timer.c for more details.

Right, that function doesn't work well for 44100/1024 in 32 bits -- it ends up trying to calculate 1e7 * 1024 / 441 and having to divide both numerator and denominator by 4 (thus losing the rather crucial 1 at the end of 441) before it can do the calculation without overflow.

Attached is a patch against 1.0.5a that gets better results in this instance, by leaving the denominator alone and instead doubling the result back up by the same number of times as the multiplier had to be halved by.

Chris

pcm_timer.c renamed from pcm_timer.c.~1~ +4 -3
@@ -49,7 +49,7 @@
 
 void snd_pcm_timer_resolution_change(snd_pcm_substream_t *substream)
 {
-	unsigned long rate, mult, fsize, l;
+	unsigned long rate, mult, fsize, l, post;
 	snd_pcm_runtime_t *runtime = substream->runtime;
 	
         mult = 1000000000;
@@ -63,16 +63,17 @@
 	l = gcd(rate, fsize);
 	rate /= l;
 	fsize /= l;
+	post = 1;
 	while ((mult * fsize) / fsize != mult) {
 		mult /= 2;
-		rate /= 2;
+		post *= 2;
 	}
 	if (rate == 0) {
 		snd_printk(KERN_ERR "pcm timer resolution out of range (rate = %u, period_size = %lu)\n", runtime->rate, runtime->period_size);
 		runtime->timer_resolution = -1;
 		return;
 	}
-	runtime->timer_resolution = mult * fsize / rate;
+	runtime->timer_resolution = (mult * fsize / rate) * post;
 }
 
 static unsigned long snd_pcm_timer_resolution(snd_timer_t * timer)