Advertisement
wja

switch-audio-out.sh

wja
Apr 20th, 2016
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.53 KB | None | 0 0
  1. #!/bin/bash
  2. # Toggles between two audio outputs.
  3. # Note: Only works with a maximum of two audio outputs.
  4. # Note: `pacmd` does not report its exit code to Bash, and does not print errors to stderr. I don't know why not, but that's how it is. It's better to use `pactl` for one-off commands.
  5.  
  6. # What to do if there is an error.
  7. switch_error(){
  8.     echo "Error: $error"
  9.     zenity --error \
  10.         --title="$(basename "$0")" \
  11.         --text="$error\n$(basename "$0") cancelled."
  12.     exit 1
  13. }
  14.  
  15. # The sink (output) that is not currently in use.
  16. next_sink="$(pacmd list-sinks | fgrep -v '*' | grep 'index' | grep -Eo '[0-9]{1,10}')"
  17.  
  18. # Input to the sinks.
  19. sink_inputs="$(pacmd list-sink-inputs | grep 'index' | grep -Eo '[0-9]{1,10}')"
  20.  
  21. # Preliminary checks.
  22. # Exits if less than two audio outputs are detected.
  23. if [[ -z "$next_sink" ]]; then
  24.     error="Fewer than two audio outputs detected."
  25.     switch_error
  26. # Confirms there are not more than two audio sinks, by looking for a space in the sink listing.
  27. elif echo "$next_sink" | grep -q ' '; then
  28.     error="More than two audio outputs detected."
  29.     switch_error
  30. fi
  31.  
  32. # Moves any currently playing audio streams to the new sink.
  33. for i in $sink_inputs; do
  34.     move_sink_input="$(pactl move-sink-input "$i" "$next_sink")"
  35.     if [[ -z "$move_sink_input" ]]; then
  36.         echo 'Switched sink input '"$i"' onto sink '"$next_sink"
  37.     else
  38.         >&2 echo "Error: pactl: $move_sink_input"
  39.         error="Could not move sink input."
  40.         switch_error
  41.     fi
  42. done
  43.  
  44. # Changes the default sink.
  45. set_default_sink="$(pactl set-default-sink "$next_sink")"
  46. if [[ -z "$set_default_sink" ]]; then
  47.     echo 'Switched default audio output to sink '"$next_sink"
  48. else    
  49.     >&2 echo "Error: pactl: $set_default_sink"
  50.     error="Could not switch default sink."
  51.     switch_error
  52. fi
  53.  
  54. # Finds info about the new sink.
  55. sink_info="$(pacmd list-sinks | grep -A 1 '* index')"
  56.  
  57. # Reports the name of the new sink.
  58. if [[ "$sink_info" == *"Logitech_Wireless_Headset"* ]]; then
  59.     sink_name="Logitech wireless headset"
  60. elif [[ "$sink_info" == *"pci-0000_00_1b.0.analog-stereo"* ]]; then
  61.     sink_name="built-in audio"
  62. else
  63.     sink_name="$(echo "$sink_info" | grep 'name' | cut -d'<' -f2 | cut -d'>' -f1)"
  64. fi
  65.  
  66. # Shows more info in terminal.
  67. echo 'Audio output now '"$sink_name"
  68.  
  69. # Shows a notification.
  70. if [[ -n "$sink_inputs" ]]; then
  71.     notify_sink_inputs="\nMoved currently playing audio streams."
  72. fi
  73. killall notify-osd
  74. notify-send "Audio output" 'Switched to '"$sink_name.$notify_sink_inputs"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement