Toggle HDMI/analog sound output (sink) with a simple script.

Mmolodzk
  7 years ago
  1

Hi,

I was annoyed by the neccessity of changing sound card output to HDMI or build-in laptop speaker via sound manager. I wrote the below listed bash script to automate this by simply running it.

#!/bin/bash

# zmiana aktywnego wyjścia dźwięku na HDMI lub na monitor

# ustawienie parametrów skryptu:
hdmi_default_volume=50000
analog_default_volume=25000

current_output=`pacmd list-sinks | grep name: | sed -e 's/\tname: <//g' -e 's/>//g' -e 's/[a-z0-9_-]\+//' -e 's/[a-z0-9_-]\+//' -e 's/[.0-9]//g'`

echo "Current output is: " $current_output

if [[ $current_output = "hdmi-stereo" ]]; then
    echo "New output is: analog-stereo"
    pacmd set-card-profile 0 output:analog-stereo+input:analog-stereo
    sink_index=`pacmd list-sinks | grep index | sed -e 's/ \+//g' -e 's/[a-z*:]//g'`
    pacmd set-sink-volume $sink_index $hdmi_default_volume
fi

if [[ $current_output = "analog-stereo" ]]; then
    echo "New output is: hdmi-stereo"
    pacmd set-card-profile 0 output:hdmi-stereo
    sink_index=`pacmd list-sinks | grep index | sed -e 's/ \+//g' -e 's/[a-z*:]//g'`
    pacmd set-sink-volume $sink_index $analog_default_volume
fi

 

I have placed laucher of this script in desktop, so I can toggle between HDMI and analog sound by double-clicking it. If you have any other outputs you can easily modify the above codecto include them.