LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Switch outputs pulseaudio in Arch (https://www.linuxquestions.org/questions/linux-newbie-8/switch-outputs-pulseaudio-in-arch-4175526836/)

Tadaen 11-29-2014 03:19 PM

Switch outputs pulseaudio in Arch
 
I wrote this trying to automate switching to my bluetooth headset when I activate it. It works one way, changing to the onboard analog sound sink when no bluetooth is connected. Doesn't change inputs to the bluetooth sinks though. I'm at a loss as to what I'm doing wrong. I had this working fine a couple weeks ago. What is wrong with this script.

Code:

#!/bin/bash
#Tadaen Sylvermane
#mybluetooth

##### variables #####

## sink names
# onboard analog sink name
ANALOG=alsa_output.pci-0000_00_1b.0.analog-stereo
# miisport bt headset sink name
HEADSET=bluez_sink.50_19_09_00_C9_3D
# jbl charge bt speaker sink name
SPEAKER=bluez_sink.00_1D_DF_EE_00_81
## sink indexes
# active sink index
ACTIVESINK=$(pacmd list-sinks | grep index | grep "*" | awk '{print $3}')
# analog sink index
if pacmd list-sinks | grep -B 1 -i "$ANALOG" | grep index | grep "*" ; then
  ANALOGOB=$(pacmd list-sinks | grep -B 1 -i "$ANALOG" | grep index | awk '{print $3}')
else
  ANALOGOB=$(pacmd list-sinks | grep -B 1 -i "$ANALOG" | grep index | awk '{print $2}')
fi
# bt headset sink index
if pacmd list-sinks | grep -B 1 -i "$HEADSET" | grep index | grep "*" ; then
  HEADSETSINK=$(pacmd list-sinks | grep -B 1 -i "$HEADSET" | grep index | awk '{print $3}')
else
  HEADSETSINK=$(pacmd list-sinks | grep -B 1 -i "$HEADSET" | grep index | awk '{print $2}')
fi
# bt speaker sink index
if pacmd list-sinks | grep -B 1 -i "$SPEAKER" | grep index | grep "*" ; then
  SPEAKERSINK=$(pacmd list-sinks | grep -B 1 -i "$SPEAKER" | grep index | awk '{print $3}')
else
  SPEAKERSINK=$(pacmd list-sinks | grep -B 1 -i "$SPEAKER" | grep index | awk '{print $2}')
fi
## current sink inputs
SINKINPUTS=$(pacmd list-sink-inputs | grep index | awk '{print $2}')

##### begin script #####

btpowerup() { # enable bluetooth on login - set to run on startup
        echo -e "power on\nexit\n" | bluetoothctl
}

setsink() {
        chsinks() {
                pacmd set-default-sink "$1"
                for input in $SINKINPUTS
                do
                        pacmd move-sink-input "$input" "$1"
                done
        }
        if [[ "$HEADSETSINK" ]] ; then
                if [[ "$ACTIVESINK" != "$HEADSETSINK" ]] ; then
                          chsinks "$HEADSETSINK"
                fi
        elif [[ "$SPEAKERSINK" ]] ; then
                if [[ "$ACTIVESINK" != "$SPEAKERSINK" ]] ; then
                          chsinks "$SPEAKERSINK"
                fi
        else
                chsinks "$ANALOGOB"
        fi
}

case "$1" in
        poweron) # enable bluetooth power
                btpowerup
                ;;
        sinkswap) # swap sinks
                setsink
                ;;
        *)
                echo "usage $0 {poweron | sinkswap}"
                ;;
esac

##### end script #####



All times are GMT -5. The time now is 09:58 PM.