dimtpap / obs-pipewire-audio-capture

🔊 Audio device and application capture for OBS Studio using PipeWire
https://obsproject.com/forum/resources/pipewire-audio-capture.1458/
GNU General Public License v2.0
346 stars 12 forks source link

Distorted audio if multiple sampling rates are allowed #55

Closed yoqto closed 6 months ago

yoqto commented 6 months ago

Hi,

I adapted a single setting from the default pipewire config for my user:

$ diff -u /usr/share/pipewire/pipewire.conf pipewire.conf 
--- /usr/share/pipewire/pipewire.conf   2024-03-15 23:37:10.647762298 +0100
+++ pipewire.conf       2024-03-20 20:44:55.685176377 +0100
@@ -27,7 +27,7 @@

     ## Properties for the DSP configuration.
     #default.clock.rate          = 48000
-    #default.clock.allowed-rates = [ 48000 ]
+    default.clock.allowed-rates = [ 48000 44100 ]
     #default.clock.quantum       = 1024
     #default.clock.min-quantum   = 32
     #default.clock.max-quantum   = 2048

This prevents software resampling when listening to mixed 44.1kHz and 48kHz audio as my onboard sound chip supports both and switches between them just fine, depending on what media I consume at the moment.

When recording or streaming with sound played via native pipewire output and recorded via this plugin with OBS, only 48/96kHz sources sound fine in the recording/stream, 44.1kHz sources are heavily distorted. To me locally on my headphones everything sounds just fine, no matter the source material sampling rate.

Steps to reproduce:

  1. Generate three test signal files in Audacity, say a few hundred Hz for a few seconds, at 44.1, 48 and 96 kHz and export them to individual files
  2. Have no sound playing at all (check via pw-top)
  3. Start OBS, record -> pw-top says OBS is recording at 48kHz
  4. Play the three files via mpv's native pipewire output:
    $ mpv 300_44100.wav 400_48000.wav 500_96000.wav 
    Playing: 300_44100.wav
     (+) Audio --aid=1 (pcm_s16le 1ch 44100Hz)
    AO: [pipewire] 44100Hz mono 1ch s16
    Playing: 400_48000.wav
     (+) Audio --aid=1 (pcm_s16le 1ch 48000Hz)
    AO: [pipewire] 48000Hz mono 1ch s16
    Playing: 500_96000.wav
     (+) Audio --aid=1 (pcm_s16le 1ch 96000Hz)
    AO: [pipewire] 96000Hz mono 1ch s16
    Exiting... (End of file)

    testfiles.tar.gz

This is not limited to mpv, but seem to happen with other audio sources as well - maybe even somewhat randomly. pw-record -v somefile.wav does not appear to have this issue. I have this plugin set up to record everything except for vesktop.bin to exclude audio from discord calls. Only recording mpv via the plugin produces the same distorted results.

I've found two possible workarounds:

  1. resetting my pipewire.conf to the default (-> 48kHz only)
  2. Having pw-record -v somefile.wav running at the same time as recording through OBS

Gentoo Linux Pipewire 1.0.4 obs-pipewire-audio-capture 1.1.4, built from source Wireplumber 0.4.7 OBS 30.1 running on the host (no Flatpak or docker or anything) Kernel 6.8.1 KDE Plasma 6.0.2 Some generic onboard sound chip on AMD X570 -> module snd_hda_intel

If there's more info I can provide or a patch to try out, please let me know.

dimtpap commented 6 months ago

Can you try the latest commit?

yoqto commented 6 months ago

Sure: 5c47e1ab fixes it for me! :+1: Whatever I throw at it now, it records just fine, even when combining multiple sampling rates at random and from various source applications. Thank you so much, that was much faster than I'd hoped for!