How to enable sound in qemu?

Solution 1:

To ease the QEMU setup process you can use QuickEMU.

sudo apt-get install git
git clone https://github.com/wimpysworld/quickemu.git

sudo apt-get install snapd
snap install qemu-virgil --edge
snap connect qemu-virgil:audio-record
snap connect qemu-virgil:kvm
snap connect qemu-virgil:raw-usb
snap connect qemu-virgil:removable-media

cat << EOF > ~/vm.conf
boot="legacy"
guest_os="linux"
iso="/path/to/file.iso"
disk_img="/home/$USER/path/to/disk.qcow2"
disk=16G
EOF

And then run it

./quickemu --vm ~/vm.conf

The resulting VM will have sound, SMB, SSH, USB, good video performance and other features.


Note: under the hood QuickEMU sets -audiodev option with corresponding parameters.

Solution 2:

N0rbert's utility didn't do exactly what I wanted however by inspecting the sourcecode I was able to make my commandline work like this:

BOOT_USB="/dev/sdb"
sudo cp ~/.config/pulse/cookie /root/.config/pulse/cookie
sudo qemu-system-x86_64 -machine accel=kvm:tcg -m 4096 -bios /usr/share/ovmf/OVMF.fd -audiodev pa,id=pa,server=unix:${XDG_RUNTIME_DIR}/pulse/native,out.stream-name=foobar,in.stream-name=foobar -device intel-hda -device hda-duplex,audiodev=pa,mixer=off -rtc base=localtime,clock=host -smp 16 -vga virtio -display gtk,gl=on -drive file=$BOOT_USB,format=raw,index=0,media=disk

I don't think I can explain all the details on how this works but I'll leave my solution here for others. In the command at the beginning is a fix for pulse audio. Sometimes it works sometimes it doesn't but with the cp command it works fairly consistantly. A couple days after finding the solution I discovered another problem with my handling of the format of the disk. The new command is safer and there are no warnings or errors. The command will boot a uefi enabled USB drive with my copy of rEFInd. It applies accelerated graphics and audio. You can add a data drive to this commandline with this option

-drive file=/dev/sdc,format=raw,if=virtio

OBS captured footage looks stunningly clear and smooth from the vm. Even playing full hd 60fps youtube video.