[pulseaudio-discuss] Upgrading a pa script from Ubuntu 12.04 to 16.10

Joe josephj at main.nc.us
Sat Oct 22 06:00:30 UTC 2016

Before I get into all the details of my script, etc., I would be very 
happy to use someone else's utility that does what I want, if it already 
exists. I want to click an icon and get results without having to 
navigate through a bunch of GUI menus every time.

When it worked, I used this script many times a week, so I'd really like 
to get it working again.

I cobbled together the attached script from posts on the Internet. It 
worked fine in 12.04, but is completely broken in 16.04 because the 
output of the commands it parses has changed, etc..

I do not understand any of the details of pulse audio.

If attachments are a problem for the list, I can put it on Dropbox. I 
just don't want it to get mangled by things that reformat email messages.

I have an icon in my KDE 5 panel which runs this script. When I click on 
it, I want it to detect if I have an HDMI device (TV or AV receiver) 
plugged in. If it is plugged in, I want it to switch my audio output 
(stereo) from my internal analog audio on my notebook (speakers or 
headphones) to HDMI audio. If the HDMI device is not detected, it should 
switch the audio output from HDMI to analog stereo on my notebook.


The script tells me if the audio is already directed correctly.

AFAIK, all my HDMI devices are the same. I just need to know if any one 
of them is attached, not which one or other details. I don't use 
anything other than stereo.

Sometimes, when I attach an HDMI device, I don't want to run this 
script, so I just want it to run manually when I click on the icon.

All of this worked on the old version before I upgraded my OS.

I currently have HDMI audio as my first choice in system settings with 
analog as the second choice. I think that affects arguments to pulse 
audio commands.

Any help would be appreciated.

I am running KDE 5.18.0, Kubuntu 16.04 separately on a couple Toshiba 
notebooks with 64-bit Intel processors (i3 and i7).

I have not modified pulse audio in any way.

I'm moderately competent in bash and I know how to work with the desktop 
icon. All I need is to get the pulse audio part working again.



-------------- next part --------------
## audio_select
## Copyleft 08/11/2014 - JPmicrosystems
## Last modified 05/14/2015
## If HDMI is connected, audio output is channelled to HDMI
## Otherwise it's channelled to internal audio

## From: https://askubuntu.com/questions/458194/switching-to-hdmi-audio-when-hdmi-is-plugged-into-a-laptop-14-04

## Make a script name in SCRIPT_NAME
function script_name {
  ## See if the calling script has a name string defined
  if [ -n "${NAME}" ]
    SCRIPT_NAME="$(basename $0)"

## Get active audio port type
function get_active_audio_port {
  ## first very rough cut
  RC=0 ## Success
  ACTIVE_AUDIO_PORT_NAME="$(pactl list | grep 'Active Port:' | awk '{print $3}')"
      RC=1 ## Failure
  return $RC

##source "$HOME/bin/bash_trace" ## debug
HDMI_STATUS=$(cat /sys/class/drm/card0/*HDMI*/status)
INPUTS=($(pacmd list-sink-inputs | grep index | awk '{print $2}'))
DSPTIME=10            # yad message persistence time
YOPT="--center --on-top --title ${SCRIPT_NAME}"  # yad common options

get_active_audio_port ## Find out where audio is going now
if ((RC))
  yad ${YOPT} --info --button=gtk-ok:0 --text="Failed to detect current audio source" --width 250 --timeout=${DSPTIME}
  exit 1

if [ $HDMI_STATUS = "connected" ]
  if [ "${ACTIVE_AUDIO_PORT}" == "HDMI" ]
    yad ${YOPT} --info --button=gtk-ok:0 --text="Audio already set to HDMI" --width 250 --timeout=${DSPTIME}
    exit 1
  pactl set-card-profile 0 output:hdmi-stereo
  pactl set-default-sink alsa_output.pci-0000_00_1b.0.hdmi-stereo
  for i in ${INPUTS[*]}
    pacmd move-sink-input $i alsa_output.pci-0000_00_1b.0.hdmi-stereo  &> /dev/null
  yad ${YOPT} --info --button=gtk-ok:0 --text="Audio directed to HDMI" --width 250 --timeout=${DSPTIME}
  if [ "${ACTIVE_AUDIO_PORT}" == "Speakers" ] || [ "${ACTIVE_AUDIO_PORT}" == "Headphones" ]
    yad ${YOPT} --info --button=gtk-ok:0 --text="Audio already set to Analog Output" --width 250 --timeout=${DSPTIME}
    exit 1
  pactl set-card-profile 0 output:analog-stereo
  pactl set-default-sink alsa_output.pci-0000_00_1b.0.analog-stereo
  for i in ${INPUTS[*]}
    pacmd move-sink-input $i alsa_output.pci-0000_00_1b.0.analog-stereo  &> /dev/null
  yad ${YOPT} --info --button=gtk-ok:0 --text="Audio directed to Analog Output" --width 250 --timeout=${DSPTIME}

More information about the pulseaudio-discuss mailing list