Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Automatically switch all PulseAudio streams to a USB headset
# Adapted from Erik Johnson's script at
# Updated by Edward Faulkner <> to move existing
# streams and eliminate the extra fork script.
# You'll need to change these to point at your headset device.
if [ "$1" == "--fork" ]; then
# When run from udev, we fork to give pulseaudio a chance to
# detect the new device
bash $0 &
elif [ "$UID" == "0" ]; then
# If we're running as root, we just forked from udev. Now sleep so
# pulseaudio can run.
sleep 1
# Check process table for users running PulseAudio
for user in `ps axc -o user,command | grep pulseaudio | cut -f1 -d' ' | sort | uniq`;
# And relaunch ourselves for each of those users.
su $user -c "bash $0"
# Running as non-root, so adjust our PulseAudio directly.
pacmd set-default-sink $OUTPUT >/dev/null 2>&1
pacmd set-default-source $INPUT >/dev/null 2>&1
for playing in $(pacmd list-sink-inputs | awk '$1 == "index:" {print $2}')
pacmd move-sink-input $playing $OUTPUT >/dev/null 2>&1
for recording in $(pacmd list-source-outputs | awk '$1 == "index:" {print $2}')
pacmd move-source-output $recording $INPUT >/dev/null 2>&1
# Invoke from udev by creating a file like:
# /etc/udev/rules.d/85-usb-headset.rules
# Containing a rule like this (will need to be customized for your device):
#KERNEL=="card?", SUBSYSTEM=="sound", ATTR{id}=="Everyman", ACTION=="add", RUN+="/path/to/this/script/usb-headset --fork"

Nevermind, I see. Using /var/log/syslog I was able to determine that my udev path is:


That allowed me to do:

udevadm info -a -p /devices/pci0000:00/0000:00:1d.0/usb8/8-1/8-1.2/8-1.2:1.3/0003:1038:1211.0008/input/input29 | grep "KERNEL\|SUBSYSTEM\|ATTRS{id"

And this gives me the criteria needed for my udev rule.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment