MusicPlayerDaemon / MPD

Music Player Daemon
https://www.musicpd.org/
GNU General Public License v2.0
2.15k stars 347 forks source link

[SOLVED] Pipewire : downsamples all hires files from MPD to 44.1kHz 16bit #1434

Closed robcollins55 closed 2 years ago

robcollins55 commented 2 years ago

Bug report

Describe the bug

All "hires" music downloaded from Qobuz gets down-sampled to 44.1kHz 16-bit format sometime during MPD's interraction with the pipewire sound server platform (ver 1:0.3.45-1). I know this because my USB DAC (Chord Electronics Mojo https://chordelectronics.co.uk/product/mojo) signals file type 44.1 (bright red led) during music playback. Otherwise, MPD basic functionally works as expected, using MALP android app as remote control. I am running MPD on Garuda Linux (Arch Linux distro) with linux kernel 5.16.7-zen1-1-zen 64 bit.

Any help with this greatly appreciated as I think MPD is the most robust and convenient music collection player out there; just I can't use it until I can play my hires music :(.

Expected Behavior

Music should be passed from MPD to pipewire and outputted to USB DAC untouched/natively without down-sampling. The Chord Mojo DAC has clear feadback via changing the colour of a multicoloured LED, signalling a particular colour depending on the sample rate being fed to it. Eg playing a 96kHz sample hires file from MPD should have the sample signal led glowing green per this chart from the manual: https://majorhifi.com/wp-content/uploads/Chord-Mojo-Sample-Rate-Color-Scheme.jpg This behavior works flawlessly with pipewire when playing the same 96kHz file from other music player packages on the same machine including Clementine, Strawberry, and Elisa.

Actual Behavior

Hires music played by MPD to pipewire appears to be always down-sampled to CD quality 44.1kHz 16-bit (Chord Mojo DAC glows red). Eg, a 96kHz 24-bit file will not play in its native format (led glowing green) and is instead down-sampled to 44.1kHz 16-bit (dac led glows red).

~/.config/mpd/mpd.conf :

music_directory "~/Music" playlist_directory "~/.config/mpd/playlists" db_file "~/.config/mpd/db" state_file "~/.config/mpd/state" sticker_file "~/.config/mpd/sticker.sql" bind_to_address "192.168.1.14" port "6600" restore_paused "no" auto_update "yes" zeroconf_enabled "yes" zeroconf_name "MPD Player @ %h" input { plugin "curl" } audio_output { type "pipewire" name "PipeWire Sound Server" } replaygain "album"

/etc/pipewire/pipewire.conf (all hashed elements removed) :

context.properties = { link.max-buffers = 16 core.daemon = true core.name = pipewire-0 default.clock.rate = 96000 default.clock.allowed-rates = [ 44100 48000 88200 96000 176000 192000 ] vm.overrides = { default.clock.min-quantum = 1024 } } context.spa-libs = { audio.convert. = audioconvert/libspa-audioconvert api.alsa. = alsa/libspa-alsa api.v4l2. = v4l2/libspa-v4l2 api.libcamera. = libcamera/libspa-libcamera api.bluez5. = bluez5/libspa-bluez5 api.vulkan. = vulkan/libspa-vulkan api.jack. = jack/libspa-jack support. = support/libspa-support } context.modules = [ { name = libpipewire-module-rtkit args = { } flags = [ ifexists nofail ] } { name = libpipewire-module-protocol-native } { name = libpipewire-module-profiler } { name = libpipewire-module-metadata } { name = libpipewire-module-spa-device-factory } { name = libpipewire-module-spa-node-factory } { name = libpipewire-module-client-node } { name = libpipewire-module-client-device } { name = libpipewire-module-portal flags = [ ifexists nofail ] } { name = libpipewire-module-access args = { } } { name = libpipewire-module-adapter } { name = libpipewire-module-link-factory } { name = libpipewire-module-session-manager } ] context.objects = [ { factory = spa-node-factory args = { factory.name = support.node.driver node.name = Dummy-Driver node.group = pipewire.dummy priority.driver = 20000 } } { factory = spa-node-factory args = { factory.name = support.node.driver node.name = Freewheel-Driver priority.driver = 19000 node.group = pipewire.freewheel node.freewheel = true } } ] context.exec = [ ]

MPD Version

Music Player Daemon 0.23.5 (0.23.5) Copyright........PURPOSE.

Database plugins: simple proxy upnp

Storage plugins: local udisks nfs curl

Neighbor plugins: upnp udisks

Decoders plugins: [mad] mp3 mp2 [mpg123] mp3 [vorbis] ogg oga [oggflac] ogg oga [flac] flac [opus] opus ogg oga [sndfile] wav aiff aif au snd paf iff svx sf voc w64 pvf xi htk caf sd2 [audiofile] wav au aiff aif [dsdiff] dff [dsf] dsf [hybrid_dsd] m4a [faad] aac [mpcdec] mpc [wavpack] wv [openmpt] mptm mod s3m xm it 669 amf ams c67 dbm digi dmf dsm dtm far imf ice j2b m15 mdl med mms mt2 mtm nst okt plm psm pt36 ptm sfx sfx2 st26 stk stm stp ult wow gdm mo3 oxm umx xpk ppm mmcmp [modplug] 669 amf ams dbm dfm dsm far it med mdl mod mtm mt2 okt s3m stm ult umx xm [mikmod] amf dsm far gdm imf it med mod mtm s3m stm stx ult uni xm [sidplay] sid mus str prg P00 [wildmidi] mid [fluidsynth] mid [ffmpeg] 16sv 3g2 3gp 4xm 8svx aa3 aac ac3 adx afc aif aifc aiff al alaw amr anim apc ape asf atrac au aud avi avm2 avs bap bfi c93 cak cin cmv cpk daud dct divx dts dv dvd dxa eac3 film flac flc fli fll flx flv g726 gsm gxfiss m1v m2v m2t m2ts m4a m4b m4v mad mj2 mjpeg mjpg mka mkv mlp mm mmf movmp+ mp1 mp2 mp3 mp4 mpc mpeg mpg mpga mpp mpu mve mvi mxf nc nsv nut nuv oga ogm ogv ogx oma ogg omg opus psp pva qcp qt r3d ra ram rl2 rm rmvb roq rpl rvc shn smk snd sol son spx str swf tak tgi tgq tgv thp ts tsp tta xa xvid uv uv2 vb vid vob voc vp6 vmd wav webm wma wmv wsaud wsvga wv wve rtp://rtsp:// rtsps:// [gme] ay gbs gym hes kss nsf nsfe rsn sap spc vgm vgz [pcm]

Filters: libsamplerate soxr

Tag plugins: id3tag

Output plugins: shout null fifo pipe alsa ao oss openal solaris pipewire pulse jack httpd snapcast recorder

Encoder plugins: null vorbis opus lame twolame wave flac

Archive plugins: [bz2] bz2 [zzip] zip [iso] iso

Input plugins: file io_uring archive alsa qobuz curl ffmpeg nfs mms cdio_paranoia

Playlist plugins: extm3u m3u pls xspf asx rss soundcloud flac cue embcue

Protocols: file:// alsa:// cdda:// ftp:// ftps:// gopher:// hls+http:// hls+https:// http:// https:// mms:// mmsh:// mmst:// mmsu:// nfs:// qobuz:// rtmp:// rtmpe:// rtmps:// rtmpt:// rtmpte:// rtmpts:// rtp:// rtsp:// rtsps:// scp:// sftp:// smb:// srtp://

Log

$ killall mpd && mpd --stderr --no-daemon --verbose config_file: loading file /home/rob/.config/mpd/mpd.conf libsamplerate: libsamplerate converter 'Fastest Sinc Interpolator' vorbis: Xiph.Org libVorbis 1.3.7 opus: libopus 1.3.1 sndfile: libsndfile-1.0.31 hybrid_dsd: The Hybrid DSD decoder is disabled because it was not explicitly enabled decoder: Decoder plugin 'wildmidi' is unavailable: configuration file does not exist: /etc/timidity/timidity.cfg simple_db: reading DB input: Input plugin 'qobuz' is not configured: No Qobuz app_id configured curl: version 7.81.0 curl: with OpenSSL/1.1.1m state_file: Loading state file /home/rob/.config/mpd/state playlist: queue song 1:"Classical/Marek Janowski with Camilla Tilling, Detlef Roth, Rundfunk-Sinfonieorchester Berlin, & Orchestra Berlin Radio Choir/Brahms — Ein Deutsches Requiem/02 - Ein Deutsches Requiem, Op. 45 — II. Denn alles Fleisch es ist wie Gras.flac" inotify: initializing inotify decoder_thread: probing plugin flac decoder: audio_format=96000:24:2, seekable=true output: opened "PipeWire Sound Server" (pipewire) audio_format=96000:24:2 replay_gain: replay gain mode has changed off->album replay_gain: scale=1 replay_gain: scale=1.0169082 inotify: watching music directory

MaxKellermann commented 2 years ago

If you can help me with this because due to MPD successfully running each boot all get is the following returned from mpd --verbose:

I have no idea what that means. How do you start MPD? Obviously, the way you said you started it didn't work. So is MPD really running? Which configuration of MPD is the one running on port 6600?

But I think, about your issue here: this is a Pipewire limitation. If you want full quality, use ALSA directly. Any other solution will reduce sound quality.

robcollins55 commented 2 years ago

MPD as described in the opening description does load and function properly with basic music playing functionality every time I boot (sytemd MPD loading). I get the "failed to bind to...." error because MPS is running normally and occupying that port. The problem here is that (also per my description) many other players on the same system pass music natively to pipewire and plays natively at hi resolution through the dac. MPD does not.

I already tried to use ALSA instead and all I got was the common error message posted here: https://mpd.readthedocs.io/en/stable/user.html#error-device-or-resource-busy

MaxKellermann commented 2 years ago

Without the log, I can't tell anything. That is very important. You did not answer my questions.

robcollins55 commented 2 years ago

Thanks for that. I successfully got a verbose log by doing killall mpd && mpd --verbose. Please see the output in the original (edited) bug report above.

MaxKellermann commented 2 years ago

Your log shows that you never did any playback, therefore there never was a problem with MPD using the wrong audio format.

robcollins55 commented 2 years ago

that's because i had to killall mpd to start it fresh to restart mpd to get verbose output. What does this mean - surely it is connected with sending wrong audio formats?: "hybrid_dsd: The Hybrid DSD decoder is disabled because it was not explicitly enabled"

robcollins55 commented 2 years ago

...also answering your first query - which address is MPD bind to? That's in the mpd.conf posted initially: bind_to_address "192.168.1.14" port "6600" MPD is running normally bound to that address, just not passing files in their native format to pipewire. Instead, downsampling is happening, per original description.

robcollins55 commented 2 years ago

Ok I got a more meaningful "in operation" MPD log by doing: mpd --stderr --no-daemon --verbose

Please see updated log section above.

MaxKellermann commented 2 years ago

According to your log file, MPD does not downsample anything. If there is any downsampling involved, it's for sure not by MPD.

robcollins55 commented 2 years ago

Thanks for your help and prodding me in the right direction. You were absolutely right - direct to alsa is the best setup and after a little more digging I found out how to achieve that. Everything is working as it should now; I have SPDIF bit-perfect sound out to alsa. For the sake of perhaps helping others with Pipewire dramas, I'll describe below what I did:

FIXED

What worked for me with my Chord Mojo dac set up was following the suggestion at https://mpd.fandom.com/wiki/Alsa to set the output section of mpd.conf to the generic SPDIF setting below to get digital passthrough direct to the dac, thereby preventing any resampling of the output stream:

audio_output {
      type                    "alsa"
      name                    "SPDIF"
      device                  "cards.pcm.iec958"
}

I first made sure that I actually had an SPDIF option on the dac by running aplay -L, and ofcourse I did, it's an audiophile grade dac. aplay -L returned:

iec958:CARD=Mojo,DEV=0
     Mojo, USB Audio
     IEC958 (S/PDIF) Digital Audio Output

However, this direct-to-alsa approach left a big problem in the form of pipewire having exclusive control of my alsa system and mpd failing to establish audio function, mpd instead giving me the alsa complaint (or similar) "Error opening alsa device "hw:0,0": Device or resource busy" in a verbose run of mpd.

Pipewire had to go. Some suggestions on the net say to merely mask pipewire and its pulseaudio plugins preventing pipewire from ever starting up at boot. The recipe for that is (pulseaudio masking also listed though you don't need that):

systemctl --user mask pulseaudio.socket
systemctl --user mask pipewire.socket
systemctl --user mask pipewire-pulse.socket
systemctl --user mask pulseaudio.service
systemctl --user mask pipewire.service
systemctl --user mask pipewire-pulse.service

While the above works and gets mpd running direct to alsa/SPDIF (after a reboot), it also prevents any other application having sound output unless the application has advanced sound settings of its own that you can adjust to select alsa output. This method therefore I felt was a bit too much like taking a sledge hammer to a tack.

Instead I decided to (gasp) install pulseaudio as the sound system, thereby removing pipewire entirely. If you just try to uninstall pipewire, dependencies of half your setup will likely go with it, if it will even let you do that. Instead I found that with just installing pulseaudio, the package manager gave me option to do a clean swap-out of pipewire, so just choosing a few pipewire uninstall options during pulseaudio installation worked for me. I'm on Garuda (Arch Linux based) so this terminal command did the trick for me: sudo pacman -Syu pulseaudio

After a reboot I was in a pulseaudio-based soundsystem - no more pipewire :) . Just to be sure I did a check: pactl info | grep "Server Name" Which returned: Server Name: PulseAudio Success!! (Prior to pulseaudio installation the same command returned: Server Name: PulseAudio (on PipeWire 0.3.45) )

Pulseaudio (like pipewire) still takes exclusive control of alsa (so mpd still won't get access to alsa directly), but that can be fixed as follows.

Tux's Idyllic Life has a good method for releasing alsa from being exclusive slave to pulseaudio on the page https://idyllictux.wordpress.com/2009/04/21/ubuntu-904-jaunty-keeping-the-beast-pulseaudio-at-bay/ . Open ‘/etc/pulse/default.pa’ in an editor and look for lines similar to the following two lines (also commented out in default.pa):

#load-module module-alsa-sink
#load-module module-alsa-source device=hw:1,0

Add the following two lines just after those two lines quoted above, save and reboot:

load-module module-alsa-sink device=dmix
load-module module-alsa-source device=dsnoop

Upon reboot, alsa (for me at least) was no longer under exclusive control of pulseaudio and mpd worked to bit-perfect SPDIF output (no resampling) - yay! mpd now works for playing my hires music to my dac in native hires format.

EDIT two weeks later.....

I discovered that pulseaudio was still trying to impose itself on my sound system so I needed to disable it:

systemctl --user mask pulseaudio socket
systemctl --user mask pulseaudio.service

Then unset pulseaudio as the sound system and set the Chord Mojo as the default soundcard:

asoundconf unset-pulseaudio
asoundconf set-default-card Mojo

Final thing. Made sure my libao.conf was using alsa (check that default_driver=alsa) :

$ sudo nano /etc/libao.conf