This script allows you to quickly switch between available audio devices on Ubuntu using PulseAudio.
- Ubuntu (or another Debian-based Linux distribution)
- PulseAudio (which is already installed by default on Ubuntu and flavors)
- Clone this repository or download the
audio-switcher.shscript. - Open a terminal and navigate to the directory where you cloned the repository.
- Run the following command to make the script executable:
chmod +x audio-switcher.sh. - Run the script using the following command:
./audio-switcher.sh. - The script will switch to the next available audio output device and move all inputs to it.
- You can assign a keyboard shortcut to run the script using the instructions below.
- Open
Settings->Keyboard->View and Customize Shortcuts - Click
Customize Shortcutsthen click the plus sign to add a new shortcut - Enter "Switch Audio Output" or any other name you prefer in the Name field
- In the Command/Script field, enter the full path to the script. For example,
/path/to/audio-switcher.sh. - Click Add and then choose a suitable key combination for your shortcut.
There are two versions of the script provided in this repository. The only difference between the two is that one version skips output devices with a specific device.description, such as "GP107GL High Definition Audio Controller Digital Stereo (HDMI)". This is useful if you want to exclude certain output devices from being cycled through by the script.
The version that skips specific output devices is named audio-switcher-exclude.sh. The other version is named audio-switcher.sh.
The script works by using the pacmd command to interact with the PulseAudio server. It first gets a list of all available audio sinks (output devices) and their indexes using the pacmd list-sinks command. It then creates an array of all available sinks, and finds the index of the active sink.
If there are no other available sinks, the script will keep on using current active sink. Otherwise, it will switch to the next sink in the array and move all inputs to it using the pacmd set-default-sink and pacmd move-sink-input commands.
- This script only switches audio output devices, not input devices or recording sources.
- If you encounter the error "No PulseAudio daemon running, or not running as session daemon" when running the script with
sudo, try running it withoutsudo. Alternatively, you can run the script as a non-root user by adding your user to thepulse-accessgroup using the following command:sudo usermod -aG pulse-access your_username. - The script
audio-switcher-exclude.shskips over sinks with a specific device description. If you have a different sink that you want to exclude, you can modifyexclude_devon line 3 of the script accordingly. You can also use the following command:pacmd list-sinksto find the device description of the sink you want to exclude. - This script was tested on Ubuntu 22.04.2 LTS.