I just knocked up a little bash script to switch audio devices because i use i3wm instead of Desktop like gnome
I have 3 audio devices, the built in speakers, headphones and a usb dac
So i created a script to easily switch audio outputs called switch-audio,
which lets you see the current audio device and switch between built in speakers, headphones and usb audio device
The set up.
1 - Create a bin directory in your home directory
2 - Edit your ~/.bashrc and add the home bin directory to your bash path
3 - Add the switch-audio script to your home bin directory
4 - Make the script executable
5 - Source your ~/.bashrc to pick up the switch-audio script
6 - Run the switch-audio script with sudo
This displays the following menu which lets you switch audio devices,
and see the currently used audio device or quit the script
1) speakers
2) headphones
3) usb
4) current
5) quit
select an audio device:
The script creates a prompt called 'select an audio device'
At the prompt just type the number next to the audio device you want to switch to,
or type 4 to view the current audio device or 5 to quit
I have 3 audio devices, the built in speakers, headphones and a usb dac
So i created a script to easily switch audio outputs called switch-audio,
which lets you see the current audio device and switch between built in speakers, headphones and usb audio device
The set up.
1 - Create a bin directory in your home directory
Bash:
mkdir -p ~/bin
2 - Edit your ~/.bashrc and add the home bin directory to your bash path
Bash:
# home bin
if [ -d "$HOME/bin" ]; then
PATH="$HOME/bin:$PATH"
fi
3 - Add the switch-audio script to your home bin directory
Bash:
#!/usr/local/bin/bash
# check to see if script was run as root
if [[ $UID -ne 0 ]]; then
printf "%s\n" "$(basename "$0") must be run as root using sudo"
exit 1
fi
# Create the prompt
PS3="select an audio device: "
options=("speakers" "headphones" "usb" "current" "quit")
OLD_IFS=${IFS}; #ifs space seperator
IFS=$'\t\n' #change ifs seperator from spaces to new line
# select and case statement
select opt in "${options[@]}"; do
case $opt in
speakers)
sysctl hw.snd.default_unit=0
break;;
headphones)
sysctl hw.snd.default_unit=1
break;;
usb)
sysctl hw.snd.default_unit=2
break;;
current)
cat /dev/sndstat
break;;
quit)
echo "Quitting"
IFS=${OLD_IFS}
break;;
*) printf "%s\n" "Usage: $(basename "$0") [ speakers | headphones | usb | current | quit ]";;
esac
done
4 - Make the script executable
Bash:
chmod u+x ~/bin/switch-audio
5 - Source your ~/.bashrc to pick up the switch-audio script
Bash:
source ~/.bashrc
6 - Run the switch-audio script with sudo
sudo switch-audio
This displays the following menu which lets you switch audio devices,
and see the currently used audio device or quit the script
1) speakers
2) headphones
3) usb
4) current
5) quit
select an audio device:
The script creates a prompt called 'select an audio device'
At the prompt just type the number next to the audio device you want to switch to,
or type 4 to view the current audio device or 5 to quit