ALSA: usb-audio: lowlatency playback runs 25% slow, no xrun (Scarlett 2i2)
Hi,
On a Focusrite Scarlett 2i2 3rd Gen (1235:8210), playback with lowlatency=1 (the default) and a two-period ring runs at about 75% of the nominal rate when the application does some work between snd_pcm_wait() and snd_pcm_writei(). No xrun is reported; hw_ptr just advances slower than real time. With lowlatency=0 every case runs at the full rate.
Setup: S32_LE, 2 ch, 48 kHz on hw:CARD=USB,DEV=0 (EP 0x01 OUT, sync type SYNC, 125 us packets). Kernel 6.14.0-24-generic (Ubuntu 24.04), alsa-lib 1.2.9.
The test loop is: snd_pcm_wait() for a free period, busy-wait N us, then write one period of silence. The rate is frames written minus snd_pcm_delay(), over 5 s. Frames consumed per second:
period x n work lowlatency=1 lowlatency=0
128 x 2 0 us 47,999 47,999 128 x 2 300 us 36,159 47,997 128 x 2 1500 us 36,569 47,999 128 x 3 300 us 48,002 47,999 256 x 2 300 us 41,090 47,999 512 x 2 300 us 48,010 47,999
Other observations:
- SCHED_RR priority 20 doesn't help, so it isn't late wakeups.
- Writing the previous period first and computing afterwards holds 48k.
- The loss doesn't scale with the work: a run is either at 48k or at about 36k (one period in four lost). Near 50 us, runs flip between the two.
- At 128x2, delay is 510 frames with lowlatency=1 and 512 with lowlatency=0, so low-latency mode gains nothing on this card.
From reading sound/usb in v6.14 (not traced): snd_usb_pcm_playback_ack() only queues pending output URBs when !ep->active_mask. If the write lands after one URB has completed short of data but while the other is still in flight, the new data waits for a whole URB. With two URBs per ring the stream loses time, but submitted_urbs never reaches zero, so notify_xrun() never fires. Is it intended that ack only queues URBs when none are in flight?
I'm happy to test a patch or collect traces. The reproducer is a short alsa-lib program, and I can send it if it helps.
This was investigated with Claude Code (Anthropic's Claude Opus 5.5 model). It ran the measurements, wrote the test program, read the sound/usb source and drafted this mail. I steered and approved each step, ran the lowlatency=0 tests myself and reviewed this mail before sending it. No fix has been written, no trace taken, and only this one device has been tested.
Thanks,
Alexander Toth
Quoted 46 lines · Alexander Toth
Hi,
On a Focusrite Scarlett 2i2 3rd Gen (1235:8210), playback with lowlatency=1 (the default) and a two-period ring runs at about 75% of the nominal rate when the application does some work between
snd_pcm_wait()andsnd_pcm_writei(). No xrun is reported;hw_ptrjust advances slower than real time. With lowlatency=0 every case runs at the full rate.Setup:
S32_LE, 2 ch, 48 kHz on hw:CARD=USB,DEV=0 (EP0x01OUT, sync type SYNC, 125 us packets). Kernel 6.14.0-24-generic (Ubuntu 24.04), alsa-lib 1.2.9.The test loop is:
snd_pcm_wait()for a free period, busy-wait N us, then write one period of silence. The rate is frames written minussnd_pcm_delay(), over 5 s. Frames consumed per second:period x n work lowlatency=1 lowlatency=0
128 x 2 0 us 47,999 47,999 128 x 2 300 us 36,159 47,997 128 x 2 1500 us 36,569 47,999 128 x 3 300 us 48,002 47,999 256 x 2 300 us 41,090 47,999 512 x 2 300 us 48,010 47,999Other observations:
-SCHED_RRpriority 20 doesn't help, so it isn't late wakeups.
- Writing the previous period first and computing afterwards holds 48k.
- The loss doesn't scale with the work: a run is either at 48k or at about 36k (one period in four lost). Near 50 us, runs flip between the two.
- At 128x2, delay is 510 frames with lowlatency=1 and 512 with lowlatency=0, so low-latency mode gains nothing on this card.From reading
sound/usbin v6.14 (not traced):snd_usb_pcm_playback_ack()only queues pending output URBs when !ep->active_mask. If the write lands after one URB has completed short of data but while the other is still in flight, the new data waits for a whole URB. With two URBs per ring the stream loses time, butsubmitted_urbsnever reaches zero, sonotify_xrun()never fires. Is it intended that ack only queues URBs when none are in flight?I'm happy to test a patch or collect traces. The reproducer is a short alsa-lib program, and I can send it if it helps.
Quoted 6 lines
This was investigated with Claude Code (Anthropic's Claude Opus 5.5 model). It ran the measurements, wrote the test program, read the
sound/usbsource and drafted this mail. I steered and approved each step, ran the lowlatency=0 tests myself and reviewed this mail before sending it. No fix has been written, no trace taken, and only this one device has been tested.
Thanks for the report. While the numbers sound interesting, let's see whether we're looking at the right things.
First off, the low-latency mode doesn't mean that it'll give lower latency at the wakeup. It's for avoiding the latency at the stream start. Only speaking about the wake-up timing, the non-low-latency mode is even better.
For the playback in the traditional (non-low-latency) mode, the driver submits URBs of silent data already at the PCM prepare phase before actually starting the playback. When the playback is triggered, then the driver starts submitting the URBs with the actual audio data. That is, for small period buffer cases like the above, almost a full buffer will be the latency *before* starting the first sample.
This behavior is to align with the implicit feedback mode that needs the capture packet to be fed to the playback. Also, this makes the wakeup timing easier; because the stream has been already running, the queued URB will return exactly after the given samples have been actually fed to the hardware. Since the driver processes the period-elapsed procedure at each URB complete callback, the timing to call the period-elapsed is fairly close to the finish of the audio period.
OTOH, in the low-latency mode, the URB submissions starts at the very start of the playback. For a configuration with small period and buffer, it means that the driver may get the URB complete callbacks immediately right after submission (they are on hardware queue). In the API level, this already allows user-space to fill further audio data, hence it returns. But the actual audio playback is far behind it, hence the delay is reported much higher. Depending on the application, this difference might be a problem, indeed. But for normal applications like PipeWire or such that does manage the timing to fill up by itself, the reduction of the start-up latency would be a bigger win. Also, for an engine like JACK, this wouldn't matter much, as it's also just filling up the next period when available.
So, the whole results depend on what you're seeing and asking for.
Of course, there can be any driver code bugs and logic failures, too. In that case, I'd happily correct the flaw. Please investigate further and report back.
thanks,
Takashi