Guest User

Untitled

a guest
Jul 26th, 2024
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.04 KB | Software | 0 0
  1. #!/bin/bash
  2.  
  3. #
  4. # Movesoundoutput moves all sound outputs to a device, according to the given parameters.
  5. #
  6. # If the first parameter is '--list' , it only lists all known or filtered sound outputs.
  7. # Otherwise, the first parameter tells which sound output to switch to.
  8. # Only a partial name of the device or it's description is needed to select it.
  9. #
  10.  
  11. function RequiredPrograms () {
  12.   # Each program listed must exist, otherwise throw an error and exit.
  13.   for List in "$@"
  14.   do
  15.     if ! command -v "$List" > /dev/null ;then
  16.       echo "Error: Required command $List not found" >&2
  17.       exit 1
  18.     fi
  19.   done
  20. }
  21.  
  22. function ParsePacmd() {
  23.   # $1 = search term
  24.   list=$(pacmd list-sinks)
  25.   while true; do
  26.     nr=$(echo "$list" | grep -ni -m1 'index:' | cut -f1 -d':')
  27.     [ "$nr" == "" ] && break
  28.     PrintValidSink "$(head -n$nr <<< $list)" "$1"
  29.     nrinc=$((nr+1))
  30.     list=$(echo "$list" | tail -n+$nrinc)
  31.   done
  32.   PrintValidSink "$list" "$1"
  33. }
  34.  
  35. function PrintValidSink() {
  36.   # $1 = Sink data
  37.   # $2 = Search item
  38.   if grep 'device.master_device' <<< "$1" >/dev/null; then
  39.     return 0
  40.   fi
  41.   if [ -n "$2" ] && ! grep -i "$2" <<< "$1" >/dev/null; then
  42.     return 0
  43.   fi
  44.   GetSinkName "$1"
  45. }
  46.  
  47. function GetSinkName() {
  48.   # $1 = Sink Data
  49.   echo "$1" | grep '^\s*name:' | cut -f2 -d'<' | cut -f1 -d'>'
  50. }
  51.  
  52. function ErrorExit() {
  53.   echo Error: $* >&2
  54.   exit 1
  55. }
  56.  
  57. # These programs must exist.
  58. RequiredPrograms pacmd
  59.  
  60. # When --list parameter is applied, only list current sinks or filtered sinks.
  61. if [ "$1" == "--list" ]; then
  62.   shift
  63.   ParsePacmd "$1"
  64.   exit 0
  65. fi
  66.  
  67. if [ "$1" == "" ]; then
  68.   ErrorExit "No device string given"
  69. fi
  70.  
  71. # Find the correct sink.
  72. sink=$(ParsePacmd "$1")
  73. if [ $(wc -w <<< "$sink") -ne 1 ]; then
  74.   ErrorExit "Not exactly one sink filtered"
  75. fi
  76.  
  77. # Change the default audio sink.
  78. pacmd set-default-sink "$sink"
  79.  
  80. # Move all inputs to the new sink
  81. for app in $(pacmd list-sink-inputs | sed -n -e 's/index:[[:space:]]\([[:digit:]]\)/\1/p');
  82. do
  83.     pacmd move-sink-input $app "$sink"
  84. done
  85.  
  86. exit 0
  87.  
Advertisement
Add Comment
Please, Sign In to add comment