Need Help Getting Sound From QEMU

First let’s address the crackling, etc… The technical term for this is a “buffer underrun” btw. I have found that the PA code in Qemu is whats lacking a this time and in my personal opinion a better workaround is to tell Qemu to use ALSA instead. It will still be using PA as there is a ALSA to PA shim, but using this method in my experience it completely resolves all guest audio issues without any custom software on the guest.

See if you can get aplay working as the user you’re running Qemu as.

aplay -L should list all the devices, one of which should be pulse.

If you can get this working you should be able to do what I suggested earlier and tell Qemu to use ALSA by setting the QEMU_AUDIO_DRV environment variable to alsa.

Here are the exact environment variables I am using with great success:

# tell qemu to use ALSA
QEMU_AUDIO_DRV=alsa

# tell ALSA to use the output device called "pulse" 
QEMU_ALSA_DAC_DEV=pulse           

# tell ALSA to use the input device called "pulse"
QEMU_ALSA_ADC_DEV=pulse           

# total size of the audio buffer to setup for DMA to the hardware
# this varies depending on your hardware, some can handle low values which = lower latency
QEMU_ALSA_DAC_BUFFER_SIZE=512

# how big each period inside the buffer is (512 / 128 = 4 periods)
# this affects how often the kernel will wake up your application for more data.
QEMU_ALSA_DAC_PERIOD_SIZE=128

Note though that the buffer and period sizes will likely need to be tweaked as these values are specific to your hardware. In simple terms these dictate how much audio data is to be buffered between each audio thread wake up. I have a professional audio card which can handle extremely low values so what you see here may already be too low (although they are conservative as I don’t need extremely low latency for my guest VM). Note that these values must be powers of 2.

For completeness the total latency is calculated using the following formula

latency = sample rate / buffer size

For example, at 48000Hz (48KHz), the latency would be 48000 / 512 = 93ms

For more detail on this specific subject see: https://www.alsa-project.org/main/index.php/FramesPeriods

Removing PA will break tons of stuff, most packages that have any kind of audio support now depend on PA and removing it will break audio for other applications. It also does increase latency and reduces quality (forced 48KHz re-sample by default) but not enough that you would care unless you were dealing with studio quality audio, and if you were you would already be using “JackAudio” instead.

Yeah, tonight actually I plan to have a go at the Division 2 now it’s out. I have been away on a camping trip and only just got back.

Welp, I get

alsa: Could not initialize ADC
alsa: Failed to open `pulse':
alsa: Reason: Connection refused
Failed to create secure directory (/home/cookie/.config/pulse): Permission denied

Like I said, start with aplay. What does it give you?
Btw, aplay is a wav file player, if you don’t care for the noise you can use random noise as it’s input… ie

cat /dev/urandom | aplay

This must work before qemu will

It shows the help menu.

Ohh, yeah it works, I can hear the static.

Excellent, so what user is Qemu running as? and I can’t remember, are you using libvirt?

QEMU is running as my user and I think so. Libvirt is the one that uses <> and not - in the XML file right?

libvirt has a GUI and xml configurations. Libvirt does some strange user switch stuff that causes permission issues when doing things like this, which is why I asked.

I am not sure why you’re getting a permission denied error there, I suggest you investigate that, as if you can fix it all your sound issues should be resolved, esp since aplay is giving you noise via pulse

This is a bit of a shot in the dark, but I tried recreating your config on my Fedora box and I ran into SELinux errors. Are you on a distro that uses SELinux?

Not sure what that is but I’m on ubuntu if that helps.

Oh. You’re probably running into AppArmor issues then.

When you boot the VM, watch your journal or syslog to see if there are any errors being thrown when you get the access denied.


To answer your question:


I’m about to leave for the evening, so I can’t help much, just thought I’d try to point you in the right direction.

1 Like

We spent some more time on this last night via twitch chat, we pretty much came to this same conclusion.

1 Like

Ah. Well, glad I was able to get in the same situation. It appears, from my understanding, that the qemu contexts aren’t allowed to mess with users home directories.

I gave up trying to switch over to ALSA with pulse as the source/sink because I kept getting connection refused errors too (no permission denied though) for pulse despite both unix socket and tcp modules being loaded with auth-anonymous, port is definitely open, etc all looks good, even manually running pulse with debug didn’t give me hints as to why I kept getting connection refused. I didn’t pursue debugging anything on the libvirt side because the VM is working just fine with PA arguments and I can’t perceive any latency and there’s no static or noise so I’m just going to leave it at that. System has been up for a couple days and several suspends and Cadence says only 3 Xruns and I don’t even remember hearing any pops or crackles, could have occurred during suspend maybe. I’m not an audiophile and probably wouldn’t be able to tell the difference in sound between the built in audio on the motherboard and an external DAC or some other professional audio gear, but I also haven’t tried so maybe mediocre is just fine for that reason. :stuck_out_tongue:

I hope you’re able to get it working with PA arguments, it might take creating your own systemd unit for running PA with some arguments to load a certain configuration but certainly possible.

1 Like

@gnif @SgtAwesomesauce and @lessaj thanks guys for all the help, been busy trying to fix another issue… For some reason after updating all the packages on the system. It broke the VM somehow, I can’t view it in Virt Manager. It just spins then after a little bit stops, to which looking glass is supposed to kick in. But I can’t connect, was able to get into safe mode and saw looking glass wasn’t in the start up folder anymore and the internet to it was broken. Also in Virt Manager I can no longer go into the right click menu and restart or reboot, only force shut down. At this point I don’t know what to do, really don’t think it’s going to work out, maybe dual boot instead? Wouldn’t have to deal with any of these issues anymore… When it was running, I’ve been dealing with issues such as mouse latency, sometimes the tops of windows would dissapear when starting looking glass, low updates per second (getting like 30 - 40 when the FPS is over 140). Don’t get me wrong, looking glass is a great piece of software but there is probably something up with the system that’s causing this. Seems like I’m the only one having these issues. :confused: