Skip to content

Instantly share code, notes, and snippets.

@matmunn
Last active June 10, 2016 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matmunn/7161dc49e3c4b6323aa28536b1662f49 to your computer and use it in GitHub Desktop.
Save matmunn/7161dc49e3c4b6323aa28536b1662f49 to your computer and use it in GitHub Desktop.
Script to toggle sound output devices
#!/usr/bin/bash
OUTPUTDEV=`cat ~/.bin/sound_device`
INPUTS=($(pacmd list-sink-inputs | grep index | awk '{print $2}'))
if [ $OUTPUTDEV = "Speakers" ]
then
# Persist the changes in our file
OUTPUTDEV="Headset"
echo $OUTPUTDEV > ~/.bin/sound_device
# Set the default mode for the card we're switching to
pactl set-card-profile 1 output:analog-stereo
# Set the default sink for all new streams to our headset
pactl set-default-sink alsa_output.usb-Plantronics_Plantronics_HD1-00.analog-stereo
# Move all current streams to the headset
for i in ${INPUTS[*]}; do pacmd move-sink-input $i alsa_output.usb-Plantronics_Plantronics_HD1-00.analog-stereo &> /dev/null; done
else
# We're changing back to line out speakers now
OUTPUTDEV="Speakers"
echo $OUTPUTDEV > ~/.bin/sound_device
# Mark sure the card is in surround sound mode
pactl set-card-profile 2 output:analog-surround-51
# All future streams are to come through the line out
pactl set-default-sink alsa_output.pci-0000_00_1b.0.analog-surround-51
# Move all current streams to the speakers
for i in ${INPUTS[*]}; do pacmd move-sink-input $i alsa_output.pci-0000_00_1b.0.analog-surround-51 &> /dev/null; done
fi
# Show a notification on GNOME so I know what happened
notify-send 'Audio Changed' "Audio has been changed to $OUTPUTDEV" --icon=dialog-information
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment