Skip to content

Instantly share code, notes, and snippets.

@un-def
Created January 12, 2022 11:30
Show Gist options
  • Save un-def/f8f00d532ee72464629c16feda297165 to your computer and use it in GitHub Desktop.
Save un-def/f8f00d532ee72464629c16feda297165 to your computer and use it in GitHub Desktop.
Switch between two PulseAudio sinks
#!/bin/sh
INTEGRATED='pci-0000_0c_00.4'
VIDEOCARD='pci-0000_0a_00.1'
TRAY_APP='volumeicon'
NOTIFICATION_SUMMARY='Audio Output'
die() {
notify-send "${NOTIFICATION_SUMMARY}" "${1}" -i audio-volume-muted -t 3000
exit 1
}
integrated=''
videocard=''
for sink in $(pacmd list-sinks | grep -oP '(?<=name: <)[^>]+'); do
case ${sink} in
*"${INTEGRATED}"*) integrated=${sink};;
*"${VIDEOCARD}"*) videocard=${sink};;
esac
done
if [ -z "${integrated}" ] || [ -z "${videocard}" ]; then
die 'Failed to inspect'
fi
if [ "$(pactl get-default-sink)" = "${integrated}" ]; then
sink=${videocard}
msg='Display'
else
sink=${integrated}
msg='Speakers'
fi
if ! pactl set-default-sink "${sink}"; then
die 'Failed to switch'
fi
notify-send "${NOTIFICATION_SUMMARY}" "${msg}" -i audio-volume-high -t 2000
killall -e ${TRAY_APP} 2> /dev/null
${TRAY_APP} &
@un-def
Copy link
Author

un-def commented Jan 12, 2022

The reason why we have to restart volumeicon: Maato/volumeicon#49

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