How do I automatically switch PulseAudio input to headset upon connection?
Solution 1:
I almost fix it on my laptop running Debian 10 this way:
- Unplug your headset.
- Edit
/etc/pulse/daemon.conf
— setlog-level = debug
- Restart pulseaudio:
pulseaudio -k
- Run
journalctl --follow
in a separate terminal/tmux window - Plug in your headset and watch what happens.
module-alsa-card.c: Jack 'Headphone Mic Jack' is now plugged in
says which jack is detected - Edit
/usr/share/pulseaudio/alsa-mixer/paths/analog-input-headset-mic.conf
, find the[Jack Headphone Mic]
section and changestate.plugged
fromunknown
toyes
I said "almost", because
- Mic gain did not restore
- It does not distinguish external loudspeakers (TRS jack) and headset (TRRS jack) and switch input source anyway, so I get static noise instead of internal mic audio.
The blog post guided me this direction.
Solution 2:
The solution sent above works for some PCs, but not for all. I tested solutions for a long time that I could find in the documentation or on forums and this was the only one that worked.
So here is a script that I created that you can add when starting a session (unfortunately not for the entire computer because PulseAudio is a service that runs independently for each user).
#!/bin/bash
index=$(pacmd list-sources | egrep 'index|ports|analog-input-headset-mic' | egrep '\*\sindex:\s+[0-9]' | cut -d':' -f2);
acpi_listen | while IFS= read -r line;
do
if [ "$line" = "jack/headphone HEADPHONE plug" ]
then
pacmd set-source-port $index analog-input-headset-mic;
elif [ "$line" = "jack/headphone HEADPHONE unplug" ]
then
pacmd set-source-port $index analog-input-internal-mic;
fi
done
This topic is part of my sources and here is another link that helped me in my research and to develop my script:
- What code is executed when headphones are disconnected?