Latency and choppy sound with PC Speaker on QEMU on WSL

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
MarkD
Posts: 12
Joined: Tue Jul 16, 2024 6:33 pm

Latency and choppy sound with PC Speaker on QEMU on WSL

Post by MarkD »

I'm running my own home brew OS in QEMU on Ubuntu on WSL on Windows 11. I have code that sends a repeating scale of notes to the PC speaker (port 0x61). When I play a scale, it starts out fine, but the latency gets greater and greater until the sound starts to break up into short fragments. The fragmented sounds start 10-20 seconds after starting the scale. I expected some delay due to QEMU/WSL/Windows, but I wasn't expecting the increasing latency or the breaking up.

I run qemu as:
qemu-system-i386 -drive file=myos_disk.fat,index=0,media=disk,format=raw -debugcon stdio -audiodev pa,id=pcspkr -machine pcspk-audiodev=pcspkr

qemu: 8.2.2 (same result with 10.0.3 compiled from source)
Ubuntu: 24.04.3 LTS
WSL: 2.5.10.0

I don't understand how qemu uses pulseaudio. I don't have pulseaudio installed and yet I get the scale sounds. I did install pulseaudio-utils so I could use
pactl set-sink-volume RDPSink 25%
which does change the volume of the scale notes.

There is one sink
$ pactl list sinks
Sink #1
State: SUSPENDED
Name: RDPSink
Description: RDP Sink
Driver: module-rdp-sink.c
Sample Specification: s16le 2ch 44100Hz
Channel Map: front-left,front-right
Owner Module: 19
Mute: no
Volume: front-left: 16384 / 25% / -36.12 dB, front-right: 16384 / 25% / -36.12 dB
balance 0.00
Base Volume: 65536 / 100% / 0.00 dB
Monitor Source: RDPSink.monitor
Latency: 0 usec, configured 0 usec
Flags: DECIBEL_VOLUME LATENCY
Properties:
device.description = "RDP Sink"
device.class = "abstract"
device.icon_name = "audio-card"
Formats:
pcm
$ pulseaudio --version
Command 'pulseaudio' not found, but can be installed with:
sudo apt install pulseaudio

Can someone please explain how qemu uses pulseaudio to play port 0x61 speaker sounds without pulseaudio being installed and how to fix the latency/breakup issues? I'm wondering about a sample rate mismatch, but I don't know where they are set.

Thanks,
Mark
Octocontrabass
Member
Member
Posts: 6245
Joined: Mon Mar 25, 2013 7:01 pm

Re: Latency and choppy sound with PC Speaker on QEMU on WSL

Post by Octocontrabass »

Does the same thing happen with a Windows build of QEMU?
MarkD
Posts: 12
Joined: Tue Jul 16, 2024 6:33 pm

Re: Latency and choppy sound with PC Speaker on QEMU on WSL

Post by MarkD »

Good news. Bad news.
It works perfectly running in QEMU on Windows. However, it is now running perfectly in QEMU on WSL. Perhaps a reboot fixed it though I did many of those while the problem occurred. Perhaps something else was interfering, a browser tab?, and causing issues. I don't know. Wasted two days on this. Oh well. Such is osdev!

Thanks for the suggestion of QEMU on Windows. I'll keep that around.

I still don't understand how QEMU uses pulse audio without pulse audio being installed. But I can now complete my code and move on.

Thanks.
MarkD
Posts: 12
Joined: Tue Jul 16, 2024 6:33 pm

Re: Latency and choppy sound with PC Speaker on QEMU on WSL

Post by MarkD »

Sigh. Did another (Windows) reboot and the problem has returned.

It works fine with QEMU running directly on Windows, but QEMU on WSL is back to running ~20secs with increasing latency and then breaks up into fragments of tones. Before the reboot, it ran for 20 minutes without problem.

So I'm pretty sure my code is fine and it's a QEMU, pulseaudio, WSL issue.

Such is osdev.
Post Reply