working audio in the Raspberry Pi 4 through the headphone jack
As it is said in the Debian wiki, headphone audio does not work out-of-the-box with the generated images. In my case, with a Raspberry Pi 4B 8GB, and the image rpi4-20210726, I managed to make it work, persistently across reboots. The procedure boils down to this:
# amixer -c 2 cset numid=2 on
# amixer -c 2 cset numid=3 1
The first one is to enable PCM, and the second one is to route audio to the jack (0=auto 1=jack 2=hdmi).
This should be done, if possible, before attempting to output any sound, and before logging into the desktop.
I think doing it as root is enough; if it isn't, it can be done as normal user too.
Assuming it is the card "2". In my case it is, as seen in the output of aplay -l
:
user@rpi4-20210726:~$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: vc4hdmi0 [vc4-hdmi-0], device 0: MAI PCM vc4-hdmi-hifi-0 [MAI PCM vc4-hdmi-hifi-0]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 1: vc4hdmi1 [vc4-hdmi-1], device 0: MAI PCM vc4-hdmi-hifi-0 [MAI PCM vc4-hdmi-hifi-0]
Subdevices: 1/1
Subdevice #0: subdevice #0
card 2: ALSA [bcm2835 ALSA], device 0: bcm2835 ALSA [bcm2835 ALSA]
Subdevices: 6/7
Subdevice #0: subdevice #0
Subdevice #1: subdevice #1
Subdevice #2: subdevice #2
Subdevice #3: subdevice #3
Subdevice #4: subdevice #4
Subdevice #5: subdevice #5
Subdevice #6: subdevice #6
card 2: ALSA [bcm2835 ALSA], device 1: bcm2835 IEC958/HDMI [bcm2835 IEC958/HDMI]
Subdevices: 1/1
Subdevice #0: subdevice #0
Card state can be printed with:
amixer -c 2 cget numid=1
amixer -c 2 cget numid=2
amixer -c 2 cget numid=3
amixer -c 2 cget numid=4
amixer -c 2 cget numid=5
In my case, the output of the last commands is:
numid=1,iface=MIXER,name='PCM Playback Volume'
; type=INTEGER,access=rw---R--,values=1,min=-10239,max=400,step=0
: values=-1158
| dBscale-min=-102.39dB,step=0.01dB,mute=1
numid=2,iface=MIXER,name='PCM Playback Switch'
; type=BOOLEAN,access=rw------,values=1
: values=on
numid=3,iface=MIXER,name='PCM Playback Route'
; type=INTEGER,access=rw------,values=1,min=0,max=2,step=0
: values=1
numid=4,iface=PCM,name='IEC958 Playback Default'
; type=IEC958,access=rw------,values=1
: values=[AES0=0x00 AES1=0x00 AES2=0x00 AES3=0x00]
numid=5,iface=PCM,name='IEC958 Playback Con Mask'
; type=IEC958,access=r-------,values=1
: values=[AES0=0x02 AES1=0x00 AES2=0x00 AES3=0x00]
The configuration can be tested with:
speaker-test -D sysdefault:CARD=2 -c 2 -l 1
aplay -D sysdefault:CARD=2 -r 48000 -f S16_LE -c 2 /dev/urandom
or simply:
speaker-test -c 2 -l 1
aplay -r 48000 -f S16_LE -c 2 /dev/urandom
The configuration is preserved across reboots, thanks to alsa-restore
and /var/lib/alsa/asound.state
.
It can be reset to 'default' state doing:
# service alsa-restore stop
# rm /var/lib/alsa/asound.state
# shutdown -h now
(and power off)
If this configuration works for others it might be useful to put it in the wiki or integrate it into the images.
After testing this configuration, I got distorted sound. I solved it by looking for the card number again:
user@rpi4-20210726:~$ cat /proc/asound/cards
0 [vc4hdmi0 ]: vc4-hdmi - vc4-hdmi-0
vc4-hdmi-0
1 [vc4hdmi1 ]: vc4-hdmi - vc4-hdmi-1
vc4-hdmi-1
2 [ALSA ]: bcm2835_alsa - bcm2835 ALSA
bcm2835 ALSA
and creating the file /etc/asound.conf
with this text:
pcm.!default {
type hw
card 2
device 0
}
ctl.!default {
type hw
card 2
device 0
}