Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Change default audio input on Devuan with Pulseaudio

I switched to pulseaudio recently, which means some of my workflows have changed.

I learned how to use pulseaudio command lines to set my default input. Apparently the default gets reset when I unplug my headset.

files/2024/listings/use-mic.sh (Source)

1
2
3
4
#!/bin/sh
# Startdate: 2024-02-25-1 20:36
# https://askubuntu.com/questions/14077/how-can-i-change-the-default-audio-device-from-command-line
pacmd set-default-source alsa_input.pci-0000_00_1f.3.analog-stereo

Pacmd worked well with autocomplete, and I already knew my correct info name was "analog stereo."

And now, so monitor when my headphones get plugged in, I had to install acpid for this daemon which I've added to my ~/.fluxbox/startup script.

files/2024/listings/daemon-use-mic.sh (Source)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#!/bin/sh
# startdate: 2024-02-26-2 08:07
# Reference:
#    https://askubuntu.com/questions/1267949/how-do-i-automatically-switch-pulseaudio-input-to-headset-upon-connection/1284966#1284966
# Dependencies:
#    acpid, pulseaudio

# just in case we drop use-mic.sh
index="$( pacmd list-sources | grep -E 'index|ports|analog-input-headset-mic' | grep -E '\*\sindex:\s+[0-9]' | awk '{print $NF}' )"

acpi_listen | while IFS= read -r line ;
do
   echo "${line}" | grep -qE "HEADPHONE plug" 1>/dev/null 2>&1 && /usr/local/bin/use-mic.sh ;
   # or we could run "pacmd set-source-port ${index} analog-input-headset-mic
done

References

Weblinks

  1. sound - How can I change the default audio device from command line? - Ask Ubuntu
  2. 18.04 - How do I automatically switch PulseAudio input to headset upon connection? - Ask Ubuntu

Comments