Advertisement
theroot

switchaudio.sh

Feb 8th, 2013
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.92 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # switches the default audio output device for pulse audio, and any applications currently using it to the next available one.
  4.  
  5. tmpfile=/tmp/pasink.tmp
  6.  
  7. function getNextSink {
  8.  
  9.         sinks=`pacmd list-sinks |sed 's/*//' |awk '/[*]|index:/{print $2}' >${tmpfile}`
  10.         sink_array=( `cat "${tmpfile}" `)
  11.  
  12.         tot_sinks=${#sinks_array[@]}
  13.         active_sink=`pacmd list-sinks |awk '/* index:/{print $3}'`
  14.  
  15.         array_index=${!sink_array[@]}
  16.  
  17.         for each in ${array_index}
  18.         do
  19.                 if [ ${sink_array[$each]} -eq ${active_sink} ]
  20.                 then
  21.                         active_sink_pos=${each}
  22.                 fi
  23.  
  24.                 last_sink_pos=${each}
  25.         done
  26.  
  27.         count=0
  28.  
  29.         if [ $active_sink_pos -eq $last_sink_pos ]
  30.         then
  31.                 next_sink_pos=0
  32.         else
  33.                 let next_sink_pos=${active_sink_pos}+1
  34.         fi
  35.  
  36.         next_sink=${sink_array[${next_sink_pos}]}
  37.  
  38. }
  39.  
  40. function getSinkInputs {
  41.  
  42.         inputs=`pacmd list-sink-inputs |sed 's/*//' |awk '/[*]|index:/{print $2}' >${tmpfile}`
  43.         input_array=`cat $tmpfile`
  44.  
  45. }
  46.  
  47. function setDefaultSink {
  48.  
  49.         pacmd set-default-sink ${next_sink} 2>&1 >/dev/null
  50.  
  51. }
  52.  
  53. function moveSinkInput {
  54.  
  55. for each in ${input_array}
  56. do
  57.         pacmd move-sink-input ${each} ${next_sink} 2>&1 >/dev/null
  58. done
  59.  
  60. }
  61.  
  62.  
  63. case "$*" in
  64.         --get-sink-inputs)
  65.                         getSinkInputs
  66.         ;;
  67.         --move-sink-inputs)
  68.                         getNextSink
  69.                         getSinkInputs
  70.                         moveSinkInput
  71.         ;;
  72.         --set-default-sink)
  73.                         getNextSink
  74.                         setDefaultSink
  75.         ;;
  76.         --next)
  77.                         getNextSink
  78.                         setDefaultSink
  79.                         getSinkInputs
  80.                         moveSinkInput
  81.         ;;
  82. esac
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement