A Raspberry Pi is a great small computer that can run 24 hours with no problem, and just consumes a few watts, so it’s a fine candidate for a modern flexible media player.
The default high quality sound output of a Raspberry Pi is HDMI, which is great for video, but not so great for classic stereo systems. The standard analogue out is near CD quality. Fine for system sounds and video calling, but not enough for HiFi. Remember the Pi is designed as a cheap computer for educational purposes.
HiFi audiophiles probably want to add a dedicated USB-DAC (you lose an USB port) or dedicated HAT like HiFiBerry or IQAudio.
ALSA and PulseAudio soundserver
Then we have the software thing. Raspbian always used Advanced Linux Sound Architecture (ALSA) as the driver and skipped the layer of PulseAudio, which is the default sound server that lays on top of ALSA in modern Debian and Ubuntu desktops.
Since the Pi400 was introduced, and marketed as a serious affordable desktop, the now renamed Raspberry Pi OS, started to offer PulseAudio as well in December 2020. The big win of PulseAudio, better support for Bluetooth and more programs can output sound at the same time. With ALSA output would be locked by one program: you can’t mix.
Well with a caveat. Raspberry Pi OS with desktop is shipped with PulseAudio, but Raspberry Pi OS Lite is not shipped with PulseAudio, because shipping it with PulseAudio would make it a Raspberry Pi OS Medium. ;(
So Raspberry Pi OS Lite uses just ALSA, Raspberry Pi OS with desktop uses also ALSA, but on top of that the PulseAudio layer.
Debug audio on a Raspberry Pi
Check the hardware, it doesn’t matter in this step if you use PulseAudio or not.
Start with listing all audio outputs:
aplay -l
This should output something like:
**** List of PLAYBACK Hardware Devices ****
card 0: b1 [bcm2835 HDMI 1], device 0: bcm2835 HDMI 1 [bcm2835 HDMI 1]
Subdevices: 4/4
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
card 1: Headphones [bcm2835 Headphones], device 0: bcm2835 Headphones [bcm2835 Headphones]
Subdevices: 4/4
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
card 2: IQaudIODAC [IQaudIODAC], device 0: IQaudIO DAC HiFi pcm512x-hifi-0 [IQaudIO DAC HiFi pcm512x-hifi-0]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 3: DAC [USB AUDIO DAC], device 0: USB Audio [USB Audio]
Subdevices: 1/1
Subdevice #0: subdevice #0
Here you see the four outputs: HDMI output (0), analogue out (1), IQAudio HAT (2), and an USB-DAC (3)
To test your hardware, upload a test.wav
file to your Pi (aplay can’t play mp3 files), and try the different outputs.
aplay -D plughw:0 test.wav # HDMI
aplay -D plughw:1 test.wav #headphones/audio out
aplay -D plughw:2 test.wav # iqaudio
aplay -D plughw:3 test.wav # USB DAC
Of course you need to have the outputs properly connected to an amplifier, monitor or headphone. Now you know your hardware is working OK.
Check for a faulty power supply
If you have hisses, or distortion, also always check your Raspberry Pi for throttling and a possible power or heating problem:
vcgencmd get_throttled
throttled=0x0
If you see a different output, that suggests you have a faulty power supply. Phone chargers typically are not stable PSU’s. Better use the official Raspberry Pi power supply. Also the quality of the cable really does matter.
If that is also OK, it’s just about configuring the right output in your software.
Configuring the Music Player Daemon
To set the IQAudio HAT as the default ALSA output in /etc/mpd.conf
audio_output {
type "alsa"
name "IQAudio"
device "hw:2" # set hw:3 for DAC out
}
To set PulseAudio as the default output:
audio_output {
type "pulse" name "pulse audio" }
Do not edit boot/config.txt by default
In general there is no need to edit /boot/config.txt
. Official HATS will configure automatically and the same goes for USB-DACS. Plug and play. You see older tutorials or manuals instructing you to edit /boot/config.txt
for configuration. IMHO that is only needed in special cases. If you don’t know what you’re doing, don’t edit boot/config.txt
.