Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I use two sound devices, normal analogue output and sound over HDMI. When I'm not using the TV I leave it turned off and switch to the speakers. As I do this regularly I would like a script to do it for me.

I did have a script which switched between the two, but only for the current application producing sound. I have lost it and can't seem to find the internet source.

Can anyone help me recreate this script?

share|improve this question
    
This seems to change the active profile for a specific card, doesn't help me switch between two cards. –  D4nC00per Jan 8 '13 at 19:59
    
I think what I want to do is move a sink to a different card? –  D4nC00per Jan 8 '13 at 20:01

1 Answer 1

up vote 1 down vote accepted

I found the script finally. This script will move all input sinks (i.e. your music, video sound etc.) to your other sound device. If you run the script again it will move all sinks back to your original sound device. (I have only tested this on two sound devices).

Instruction on how to setup:

  1. Paste the following code into a file using gedit.
  2. Save the file as speakers.sh (or some other file name) in your home directory.
  3. Change the file permissions to allow execution.

    • Right click the file and choose properties.
    • Select the permissions tab.
    • Tick the "Allow executing file as program" tick box

    or

    • chmod +x ./speakers.sh
  4. Double click the file and click run. It will change the output sound device.

I then added an option on the settings icon in the unity launcher for ease of access using Ubuntu Tweak. Enjoy!

Code:

#!/bin/bash

declare -i sinks=(`pacmd list-sinks | sed -n -e 's/\**[[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p'`)
declare -i sinks_count=${#sinks[*]}
declare -i active_sink_index=`pacmd list-sinks | sed -n -e 's/\*[[:space:]]index:[[:space:]]\([[:digit:]]\)/\1/p'`
declare -i next_sink_index=${sinks[0]}

#find the next sink (not always the next index number)
declare -i ord=0
while [ $ord -lt $sinks_count ];
do
echo ${sinks[$ord]}
if [ ${sinks[$ord]} -gt $active_sink_index ] ; then
    next_sink_index=${sinks[$ord]}
    break
fi
let ord++
done

#change the default sink
pacmd "set-default-sink ${next_sink_index}"

#move all inputs to the new sink
for app in $(pacmd list-sink-inputs | sed -n -e 's/index:[[:space:]]\([[:digit:]]\)/\1/p');
do
pacmd "move-sink-input $app $next_sink_index"
done

#display notification
declare -i ndx=0
pacmd list-sinks | sed -n -e 's/device.description[[:space:]]=[[:space:]]"\(.*\)"/\1/p' | while read line;
do
if [ $(( $ord % $sinks_count )) -eq $ndx ] ; then
    notify-send -i notification-audio-volume-high --hint=string:x-canonical-private-    synchronous: "Sound output switched to" "$line"
    exit
fi
let ndx++
done;

All credit goes to the guys here, I just re-found the answer to my question.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.