This page is about configuring the audio subsystem on a Raspberry Pi installed with RaspiOS, to be used as NAS and mediacenter. See the main index at Home Mediacenter and NAS with the Raspberry Pi.
I want to use Kodi for HDMI video and audio playback, but I want also occasionally to play music using Music Player Daemon, routing the audio to an external amplifier which has RCA stereo connectors. Once the cables are connected, it is also desiderable to play Kodi videos routing video to HDMI monitor and routing audio to the stereo amplifier.
For the software part, the recommended way is to use Pulseaudio as the backend. In this way, both Kodi and MDP can share the audio hardware at the same time, you are free also to use HDMI audio for Kodi (video) and analog RCA for MPD (audio).
For the hardware part, you can use the digital HDMI audio or the analog 3.5 mm jack (which carries video and stereo audio signals over a CTIA 4 poles connector). For a better audio quality you have to connect and audio hat or an external USB audio sound card; as explained below the integrated audio devices are of unacceptable quality also for an occasional user, let alone an audiophile one!
There are several possibilities of getting audio from a Raspberry Pi 4.
This is the first natuarl choiche, at least when watching videos using Kodi. I use an old Raspberry Pi 3 as a Kodi media center, connected to an HDMI monitor capable of audio too; the sound quality is acceptable and it is aligned to the medium quality of the speakers inside the monitor. Unfortunately I discovered that the Raspberry Pi 4 has a worse audio quality than the Pi 3: I ear continously pops, clicks and little gaps in the music. I tried to swap the power supply and the HDMI cable with better quality ones, but the problem remains. It seems that the problem is not hardware related, but it is introduced when I add Pulseaudio in the software stack. If I select direct Raspberry PI:HDMI as sound device in Kodi, the audio playback is OK.
I prefer to avoid the HDMI audio output mainly because the audio of the HDMI monitor is of poor quality, but another problem is that if the monitor goes into standby (as Kodi does, when you don't play anything), also the audio goes standby. In this condition even the Music Player Damon cannot play to HDMI.
In my opinion, HDMI audio on the Raspberry Pi 4 with Pulseaudio is of unacceptable quality.
The second choice is to use the analog audio through the 3.5 mm jack. I purchased a CTIA 4 poles jack mic/phones splitter and an audio cable with 3.5 mm stereo jack at one end and RCA connecttors at the other. In this way I can connect the Raspberry Pi 4 to my stereo amplifier. The resulting audio volume is too low that I have to push the amplifier volume to the max, just to ear something. The result is unacceptable; pushing the volume so high you start earing the electrical noise and it is impossible to listen to at even medium loudness. The 100% volume level exiting from the Raspberry Pi 4 is equivalent to approximately 50% of the volume of my smartphone, when attached at the same cable.
As far as I know, and according to many articles on the net, there is not solution to this problem.
For a decent audio quality, I decided that the only acceptable solution is to relay on an external USB sound card connected to my stereo amplifier. I choosed a model based on the C-Media Electronics chip (USB Id 0d8c:0024), which comes with two standard RCA connectors. The device is compatible with ALSA so the Pulseaudio system can select it per default, setting the following in /etc/pulse/client.conf:
# Audio to USB audio device: default-sink = alsa_output.usb-C-Media_Electronics_Inc._USB_Advanced_Audio_Device-00.analog-stereo
As stated above, I want to play audio both with Kodi and with MPD. Both requires libpulse0, so the rigth way should be to use the pulseaudio subsystem. I installed also mplayer for testing purpose:
apt-get install pulseaudio mplayer
The Kodi program will be run using the kodi Unix user, so add the user to the required groups to grant the required permissions:
adduser kodi audio adduser kodi pulse adduser kodi pulse-access
Become the kodi user, start the PulseAudio server and list the available sinks (audio output devices):
su - kodi pulseaudio --start pacmd list-sinks
WARNING: after the pulseaudio --start command, the daemon is running by the kodi user; the daemon will eventually die if the user log-out or if not-used for a while. You can inspect the daemon activity by adding the options --log-target=newfile:/home/kodi/pulseaudio.log --log-level=debug.
On the Raspberry Pi I get only the HDMI port and the analog jack:
index: 0 name: <alsa_output.platform-bcm2835_audio.digital-stereo> ... ports: hdmi-output-0: HDMI ... index: 1 name: <alsa_output.platform-bcm2835_audio.analog-stereo> ... ports: analog-output: Analog Output ...
To set the default audio device used by PylseAudio clients, add the following lines to /etc/pulse/client.conf (need root):
# Audio to the HDMI port: default-sink = alsa_output.platform-bcm2835_audio.digital-stereo # Audio to the analog (jack) port: #default-sink = alsa_output.platform-bcm2835_audio.analog-stereo # Audio to USB audio device: #default-sink = alsa_output.usb-C-Media_Electronics_Inc._USB_Advanced_Audio_Device-00.analog-stereo
To check the current volume use the pactl list sinks command:
pactl list sinks Sink #0 State: SUSPENDED Name: alsa_output.platform-bcm2835_audio.digital-stereo ... Mute: no Volume: front-left: 65536 / 100% / 0.00 dB, front-right: 65536 / 100% / 0.00 dB balance 0.00 ...
Eventually turn off the mute status and set the required volume:
# Set Mute: no pacmd set-sink-mute "alsa_output.platform-bcm2835_audio.digital-stereo" 0 # Set Volume: 50% pacmd set-sink-volume "alsa_output.platform-bcm2835_audio.digital-stereo" 32768
Finally check that the audio is working as kodi user:
mplayer -ao pulse /usr/share/sounds/alsa/Front_Center.wav
PulseAudio developers say that running PulseAudio as a system-wide service is a bad idea, but in this case I think it is what is needed, because:
We need a Systemd unit file to run the daemon /etc/systemd/system/pulseaudio.service:
[Unit] Description=PulseAudio system server [Service] Type=notify ExecStart=/usr/bin/pulseaudio --daemonize=no --system --realtime --log-target=journal [Install] WantedBy=multi-user.target kodi.service mpd.service
Systemd service can be of type forking, simple or notify, which one is the best? From the pulseaudio(1) man page we can know that there is the --system option to be used for a system-wide instance, in that case we should use also the --daemonize=no for systemd notification to work. So the proper way seems to be the Type=notify
Then we can enable and start the service (verify that the pulseaudio daemon is running by the pulse user):
systemctl daemon-reload systemctl enable pulseaudio.service systemctl start pulseaudio.service
From now on, every user with group access to audio, pulse and pulse-access, can play to PulseAudio system.
: When Pulseaudio is running as a system user, some commands like pacmd does not work for the user, because they search files under /run/user/<UID>/pulse/, which instead are under /run/pulse/. It is possibile to make symlinks to solve the problem or…
To enable PulseAudio as a system-wide service, several recipes exist on the Net, many of them are plain wrong:
With the PulseAudio running as a system service, you can start Kodi and go to Settings ⇒ System ⇒ Audio = Audio output device and select PULSE: Default. Here you can also switch between HDMI and stereo jack:
The audio devices PI:HDMI, PI:Analogue and PI:HDMI and Analogue should not be longer used, because they can conflict with the PulseAudio process which is using them.
To play music we choosed to install the Music Player Daemon (mpd) and a text based client : ncmpc or ncmpcpp, which one?
The mpd configuration file is /etc/mpd.conf, the relevant settings are:
music_directory "/var/lib/mpd/music" bind_to_address "::" audio_output { type "pulse" name "pulse audio" }
The mpd user must be addedd to the relevant groups: audio, pulse and pulse-access.