-
-
Save yeroc/162a6676c62a52cb77e238dfe37e2181 to your computer and use it in GitHub Desktop.
Automatically switch all PulseAudio streams to a USB headset
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Adapted from Erik Johnson's script at | |
# http://terminalmage.net/2011/11/17/setting-a-usb-headset-as-the-default-pulseaudio-device/ | |
# | |
# Updated by Edward Faulkner <[email protected]> to move existing | |
# streams and eliminate the extra fork script. | |
# You'll need to change these to point at your headset device. Get the device names by running | |
# 'pacmd dump' | |
OUTPUT="alsa_output.usb-Generic_FREETALK_Everyman_0000000001-00-Everyman.analog-stereo" | |
INPUT="alsa_input.usb-Generic_FREETALK_Everyman_0000000001-00-Everyman.analog-stereo" | |
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`; | |
do | |
# And relaunch ourselves for each of those users. | |
su $user -c "bash $0" | |
done | |
else | |
# 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}') | |
do | |
pacmd move-sink-input $playing $OUTPUT >/dev/null 2>&1 | |
done | |
for recording in $(pacmd list-source-outputs | awk '$1 == "index:" {print $2}') | |
do | |
pacmd move-source-output $recording $INPUT >/dev/null 2>&1 | |
done | |
fi | |
# 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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment