Unable to properly setup sound for QEMU-KVM

I recently had a fresh install of F41 with KDE on Wayland.
Everything looks fine and works as intended, except sound on my VMs.
At first, there was a bug with QEMU packages, I opened a thread here, where the bug is mentioned. I was able to make it work by blocking the update to version 9.1.1-1 and use version 9.1.0-2 instead.

However, I still have issues.
My VM freezes once every about 5-10 minutes, and that causes the sound in complete de-sync state, like 4-5 seconds delay. Having similar issues previously, with other distros, I tried to enable pipewire and/or pulseaudio, but haven’t been able to make them work.

Can somebody please guide me through the process of enabling either one, and setup my VM accordingly? Ideally I prefer pipewire, but if pulseaudio provides easier and better stability, I have no problem.

inxi -F

System:
  Host: wizzy-am5-f41-kde6 Kernel: 6.11.6-300.fc41.x86_64 arch: x86_64
    bits: 64
  Desktop: KDE Plasma v: 6.2.3 Distro: Fedora Linux 41 (KDE Plasma)
Machine:
  Type: Desktop Mobo: Gigabyte model: X670E AORUS MASTER
    serial: <superuser required> UEFI: American Megatrends LLC. v: F33d
    date: 10/16/2024
CPU:
  Info: 16-core model: AMD Ryzen 9 7950X3D bits: 64 type: MT MCP cache:
    L2: 16 MiB
  Speed (MHz): avg: 3559 min/max: 545/5759 cores: 1: 3559 2: 3559 3: 3559
    4: 3559 5: 3559 6: 3559 7: 3559 8: 3559 9: 3559 10: 3559 11: 3559 12: 3559
    13: 3559 14: 3559 15: 3559 16: 3559 17: 3559 18: 3559 19: 3559 20: 3559
    21: 3559 22: 3559 23: 3559 24: 3559 25: 3559 26: 3559 27: 3559 28: 3559
    29: 3559 30: 3559 31: 3559 32: 3559
Graphics:
  Device-1: NVIDIA AD103 [GeForce RTX 4080] driver: vfio-pci v: N/A
  Device-2: Advanced Micro Devices [AMD/ATI] Raphael driver: amdgpu
    v: kernel
  Device-3: Logitech HD Pro Webcam C920 driver: snd-usb-audio,uvcvideo
    type: USB
  Display: wayland server: Xwayland v: 24.1.4 compositor: kwin_wayland
    driver: N/A resolution: 1: 3440x1440 2: 1440x2560
  API: EGL v: 1.5 drivers: kms_swrast,radeonsi,swrast
    platforms: gbm,wayland,x11,surfaceless,device
  API: OpenGL v: 4.6 compat-v: 4.5 vendor: amd mesa v: 24.2.6 renderer: AMD
    Radeon Graphics (radeonsi raphael_mendocino LLVM 19.1.0 DRM 3.59
    6.11.6-300.fc41.x86_64)
  API: Vulkan v: 1.3.296 drivers: N/A surfaces: xcb,xlib,wayland
Audio:
  Device-1: NVIDIA driver: vfio-pci
  Device-2: Advanced Micro Devices [AMD] Family 17h/19h HD Audio
    driver: snd_hda_intel
  Device-3: Logitech HD Pro Webcam C920 driver: snd-usb-audio,uvcvideo
    type: USB
  Device-4: Creative Sound Blaster X5
    driver: cdc_acm,hid-generic,snd-usb-audio,usbhid type: USB
  Device-5: KTMicro USB_Audio_Device
    driver: hid-generic,snd-usb-audio,usbhid type: USB
  API: ALSA v: k6.11.6-300.fc41.x86_64 status: kernel-api
  Server-1: PipeWire v: 1.2.6 status: active
  Server-2: PulseAudio v: 17.0 status: active
Network:
  Device-1: Intel 82576 Gigabit Network driver: igb
  IF: enp12s0f0 state: up speed: 1000 Mbps duplex: full
    mac: 98:b7:85:01:e3:3e
  Device-2: Intel 82576 Gigabit Network driver: igb
  IF: enp12s0f1 state: down mac: 98:b7:85:01:e3:3f
  Device-3: Intel Ethernet I225-V driver: igc
  IF: enp14s0 state: up speed: 1000 Mbps duplex: full mac: 74:56:3c:4b:74:7e
  Device-4: Realtek RTL8153 Gigabit Ethernet Adapter driver: r8152 type: USB
  IF: enp22s0u1u2u3 state: up speed: 1000 Mbps duplex: full
    mac: 34:1b:22:83:0f:1f
  IF-ID-1: virbr0 state: down mac: 52:54:00:79:13:64
Bluetooth:
  Device-1: Intel AX210 Bluetooth driver: btusb type: USB
  Report: btmgmt ID: hci0 rfk-id: 0 state: down bt-service: enabled,running
    rfk-block: hardware: no software: yes address: F0:D4:15:03:8E:D1 bt-v: 5.3
Drives:
  Local Storage: total: 5.52 TiB used: 1.08 TiB (19.5%)
  ID-1: /dev/nvme0n1 vendor: Kingston model: SKC3000D2048G size: 1.86 TiB
  ID-2: /dev/nvme1n1 vendor: Seagate model: XPG GAMMIX S50 Lite
    size: 953.87 GiB
  ID-3: /dev/nvme2n1 vendor: Samsung model: SSD 970 EVO 500GB
    size: 465.76 GiB
  ID-4: /dev/sda vendor: Crucial model: CT2000BX500SSD1 size: 1.82 TiB
  ID-5: /dev/sdb vendor: Samsung model: SSD 850 EVO 500GB size: 465.76 GiB
Partition:
  ID-1: / size: 464.17 GiB used: 9.96 GiB (2.1%) fs: btrfs dev: /dev/nvme2n1p3
  ID-2: /boot size: 973.4 MiB used: 425.7 MiB (43.7%) fs: ext4
    dev: /dev/nvme2n1p2
  ID-3: /boot/efi size: 598.8 MiB used: 19.3 MiB (3.2%) fs: vfat
    dev: /dev/nvme2n1p1
  ID-4: /home size: 464.17 GiB used: 9.96 GiB (2.1%) fs: btrfs
    dev: /dev/nvme2n1p3
Swap:
  ID-1: swap-1 type: zram size: 8 GiB used: 0 KiB (0.0%) dev: /dev/zram0
Sensors:
  System Temperatures: cpu: 39.1 C mobo: N/A gpu: amdgpu temp: 34.0 C
  Fan Speeds (rpm): N/A
Info:
  Memory: total: 64 GiB note: est. available: 61.91 GiB
    used: 43.23 GiB (69.8%)
  Processes: 635 Uptime: 1h 10m Shell: Bash inxi: 3.3.36

Replying to myself, as I probably solved it.

My current settings:

<domain xmlns:qemu="http://libvirt.org/schemas/domain/qemu/1.0" type="kvm">
...
    <sound model="ich9">
      <address type="pci" domain="0x0000" bus="0x00" slot="0x1b" function="0x0"/>
    </sound>
    <audio id="1" type="pulseaudio"/>
...
  <qemu:commandline>
    <qemu:env name="XDG_RUNTIME_DIR" value="/run/user/1000"/>
    <qemu:env name="QEMU_AUDIO_DRV" value="pa"/>
    <qemu:env name="QEMU_PA_SAMPLES" value="8192"/>
    <qemu:env name="QEMU_AUDIO_TIMER_PERIOD" value="99"/>
    <qemu:env name="QEMU_PA_SERVER" value="/run/user/1000/pulse/native"/>
  </qemu:commandline>

I also set this

echo "export XDG_RUNTIME_DIR=/run/user/$(id -u)" >> ~/.bashrc

source ~/.bashrc       (to reload)

but not sure if is needed, as it didn’t work before I add the line in qemu commandline.

I wrote “probably”, as I am not 100% sure, but used it for several hours without any issues, no cracking sound, no freezes and no lag.

Next step, to try and enable USB 5.1 sound… :laughing: