[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.

Notes:

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.

Thanks.

Joe


-------------- next part --------------
#!/bin/bash
## 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}" ]
  then
    SCRIPT_NAME="${NAME}"
  else
    SCRIPT_NAME="$(basename $0)"
  fi
}

## Get active audio port type
function get_active_audio_port {
  ## first very rough cut
  local ACTIVE_AUDIO_PORT_NAME
  RC=0 ## Success
  ACTIVE_AUDIO_PORT_NAME="$(pactl list | grep 'Active Port:' | awk '{print $3}')"
  case "${ACTIVE_AUDIO_PORT_NAME}" in
    "analog-output-speaker")
      ACTIVE_AUDIO_PORT="Speakers"
      ;;
     "analog-output-headphones")
      ACTIVE_AUDIO_PORT="Headphones"
      ;;
    "hdmi-output-0")
      ACTIVE_AUDIO_PORT="HDMI"
    ;;
    *)
      ACTIVE_AUDIO_PORT="Unknown"
      RC=1 ## Failure
    ;;
  esac
 
  return $RC
 }

##source "$HOME/bin/bash_trace" ## debug
script_name
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))
then
  yad ${YOPT} --info --button=gtk-ok:0 --text="Failed to detect current audio source" --width 250 --timeout=${DSPTIME}
  exit 1
fi

if [ $HDMI_STATUS = "connected" ]
then
  if [ "${ACTIVE_AUDIO_PORT}" == "HDMI" ]
  then
    yad ${YOPT} --info --button=gtk-ok:0 --text="Audio already set to HDMI" --width 250 --timeout=${DSPTIME}
    exit 1
  fi
  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[*]}
  do
    pacmd move-sink-input $i alsa_output.pci-0000_00_1b.0.hdmi-stereo  &> /dev/null
  done
  
  yad ${YOPT} --info --button=gtk-ok:0 --text="Audio directed to HDMI" --width 250 --timeout=${DSPTIME}
else
  if [ "${ACTIVE_AUDIO_PORT}" == "Speakers" ] || [ "${ACTIVE_AUDIO_PORT}" == "Headphones" ]
  then
    yad ${YOPT} --info --button=gtk-ok:0 --text="Audio already set to Analog Output" --width 250 --timeout=${DSPTIME}
    exit 1
  fi
  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[*]}
  do
    pacmd move-sink-input $i alsa_output.pci-0000_00_1b.0.analog-stereo  &> /dev/null
  done
  
  yad ${YOPT} --info --button=gtk-ok:0 --text="Audio directed to Analog Output" --width 250 --timeout=${DSPTIME}
fi


More information about the pulseaudio-discuss mailing list