From Fedora Project Wiki

vhost-device-sound

vhost-device-sound from rust-vmm is an emulation of the virtio-sound device using the vhost-user protocol.
The vhost-device-sound crate is based on the virtio-specification standard.
This device implements the virtio-sound emulation with mono and stereo audio support which can be used with either a Pipewire or Alsa audio backend for playback or capture.
This VIRTIO sound device works on VMMs that support VIRTIO device emulation. A typical use case is QEMU.

More information about this device can be found in:
1. The vhost-device-sound crate: https://crates.io/crates/vhost-device-sound
2. The vhost-user-snd-pci device in QEMU: https://gitlab.com/qemu-project/qemu/-/blob/master/hw/virtio/vhost-user-snd-pci.c

Setting up the virtual image for the system

1. Download a Fedora image from the download site i.e: https://eu.edge.kernel.org/fedora/releases/40/Server/x86_64/images/ it's a regular persistent Fedora image, instead of booting a live iso every time.
2. Start the Fedora qcow image using the QEMU command:

    $ qemu-system-x86_64 -smp 4 -m 4G -enable-kvm \
     -drive file=../Fedora-Server-KVM-40-1.14.x86_64.qcow2,format=qcow2

At first boot, it ask for a user creation:

   Select 5 for user creation,
   Select 1 to create user
   Select the following options accordingly to set username and set password
   Press c to continue

Then a login prompt will appear: login with your already created user name and password

Check if the virtio_snd module is available. Run lsmod | grep virtio_snd to see if the module exists on your guest system. If it doesn't, you may need to install the appropriate kernel headers or a specific package that provides this module, also ensure the correct kernel version is being used.
For a Fedora image, run this command to install the necessary modules:

   $ sudo dnf install kernel-modules-extra-$(uname -r)
   $ sudo modprobe virtio_snd
   $ lsmod | grep virtio_snd

To test a simple playback or capture, install alsa-utils package:

   $ sudo dnf install alsa-utils -y

Now shutdown the qemu window and launch your new OS with QEMU, this time with the vhost-device-sound daemon already running in another terminal.

Testing the vhost-device-sound with QEMU sound-pci device.

1. In one terminal install the vhost-device-sound package:

    $ sudo dnf install vhost-device-sound

2. Run the following command to start the vhost-device-sound daemon:

    $ vhost-device-sound --socket=/tmp/mysnd.socket --backend pipewire

The alsa backend is also available for use. you can use --help to view options

3. If you have a QEMU version older than 9.0.0 you need to get a newer version. You can get 9.0.0 sources with:

   $ git clone https://gitlab.com/qemu-project/qemu.git
   $ git checkout v9.0.0

Once you have a recent enough QEMU version, the following command will connect the QEMU pci device frontend to the vhost-device-sound backend via the unix socket:

   $ qemu-system-x86_64  -snapshot -mem-prealloc     \
    -object memory-backend-memfd,share=on,id=mem0,size=4G \
    -machine q35,memory-backend=mem0,accel=kvm \
    -chardev socket,id=char0,reconnect=0,path=/tmp/mysnd.socket  \
    -device vhost-user-snd-pci,chardev=char0         \
    -drive file=Fedora-Server-KVM-39-1.5.x86_64.qcow2,format=qcow2

to be specific this is the QEMU options that tell qemu to use the device backend's socket

    -chardev socket,id=vsnd,path=/tmp/mysnd.sock \
    -device vhost-user-snd-pci,chardev=vsnd

If QEMU fails with an error like:

   Failed initializing vhost-user memory map, consider using -object memory-backend-memfd share=on

the following options will solve it:

    -object memory-backend-memfd,share=on,id=mem0,size=4G, \
    -machine q35,memory-backend=mem0,accel=kvm \

4. now the guest OS is running in QEMU, you can play a sound from the guest or perform a simple speaker-test

Notes:

qemu-kvm 9.0.0 has been released in Fedora ELN and CentOS Stream 9 but as of May 2024 they have vhost-user-snd-pci device disabled. A build with that device enabled should come to Fedora ELN and CentOS Stream 10 soon.