Ubuntu 15.04 Audio Output Keyboard Shortcut

My computer is located in my bedroom, which is awesomely convenient except when I want to play audio. Usually it’s not a problem to play it out loud, but in the evening it’s gotta be headphones or muted. Ubuntu makes it easy to change audio sources using the Sound Manager in the navbar, but change sources 3-4 times a day and it starts getting annoying quick.

audio_selector

I’m surprised that there is no easy way to switch sources, so I decided to set up a custom keyboard shortcut to run a script that toggles between sources.

First, you’ll need the script.

#!/bin/bash

declare -i active_sink=`pacmd list-sinks | sed -n 's/.*\*.*index: \([0-9]\)/\1/p'`
 declare -i next_sink_index=0
 declare -i available_sinks=(`pacmd list-sinks | sed -n 's/.*index: \([0-9]\)/\1/p'`)
 for index in ${!available_sinks[@]};
 do
 if [ ${available_sinks[$index]} -eq $active_sink ]; then
 next_sink_index=${index}+1
 fi
 done
 if [ $next_sink_index -ge ${#available_sinks[@]} ]; then
 next_sink_index=0
 fi
 new_sink=${available_sinks[$next_sink_index]}
 pacmd "set-default-sink ${new_sink}"
 declare -i apps_playing=`pacmd list-sink-inputs | sed -n 's/.*index: \([0-9]\)/\1/p'`

 for app in $apps_playing;
 do
 pacmd "move-sink-input $app $new_sink"
 done

Essentially, this script uses pacmd list-sinks and pacmd list-sinks-inputs to scrape out information about the currently available and playing audio. It builds a list of valid outputs, and then it will flip all playing sources through all available outputs.

Place the script wherever you’d like, make it executable and then set up the keyboard shortcut.

System Settings > Keyboard > Shortcuts > Custom Shortcuts > +

shortcut_define

Name the shortcut something memorable, and then set the command to point to your sound-switcher script. Mine is /home/midas/sound-switcher.sh

After clicking Apply, you just need to bind it to a hotkey or combination.

keyboard_shortcuts

I picked F9 because my keyboard has a little music note there, so it makes the most sense for me.

Hopefully this makes your audio-flipping life a little easier. Let me know if you come across any problems, and I’ll be happy to help.

Advertisements
Ubuntu 15.04 Audio Output Keyboard Shortcut

One thought on “Ubuntu 15.04 Audio Output Keyboard Shortcut

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s