Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I use my laptop with an external monitor which has speakers. When the monitor is attached through HDMI I can switch (using the GUI: Sound Setting --> Hardware) between the normal laptop audio output and the monitor output.

I repeat this procedure a lot of time and I started to wonder if I can automate it or, anyway, execute it in a faster way using the shell.

My distro is Ubuntu 12.04 with gnome 3.

EDIT:

I tried using pacmd, but list-sinks gives me only the device I'm currently using:

pacmd list-sinks | grep name:
name: <alsa_output.pci-0000_00_1b.0.hdmi-stereo>

After a switch from GUI:

pacmd list-sinks | grep name:
name: <alsa_output.pci-0000_00_1b.0.analog-stereo>

And if I try to change it I get:

pacmd set-default-sink alsa_output.pci-0000_00_1b.0.hdmi-stereo
Welcome to PulseAudio! Use "help" for usage information.
Sink alsa_output.pci-0000_00_1b.0.hdmi-stereo does not exist.
share|improve this question

6 Answers 6

up vote 7 down vote accepted

In this case the card is always the same. What is changing between a switch and another is the "card-profile".

So the solution which actually worked is:

pacmd set-card-profile <cardindex> <profilename>

In my case I found all the card profiles with:

pacmd list-cards

And after I can switch between monitor and laptop speakers with:

pacmd set-card-profile 0 output:hdmi-stereo

And:

pacmd set-card-profile 0 output:analog-stereo+input:analog-stereo

Where 0 is the index of the card:

pacmd list-cards
Welcome to PulseAudio! Use "help" for usage information.
>>> 1 card(s) available.
    index: 0
    name: <alsa_card.pci-0000_00_1b.0>

And finally, in order to make the switch faster, I set up two alias in my .bashrc file:

alias audio-hdmi='pacmd set-card-profile 0 output:hdmi-stereo+input:analog-stereo'
alias audio-laptop='pacmd set-card-profile 0 output:analog-stereo+input:analog-stereo'

This way I can switch between audio from the monitor or from the laptop (headphones) typing in the shell: audio-hdmi or audio-laptop

share|improve this answer

You can use pactl, read its man page for more information.

share|improve this answer

I wrote a small indicator applet that lets you switch the sound output. No shell script but maybe helpful to you or other readers.

https://github.com/lkettenb/sound-output-switcher

Screenshot

share|improve this answer

I created a very small script based on the previous ones, which not only switches the audio but also the video output. It uses the disper to switch between displays.

Here is the code:

#!/bin/bash

CURRENT_PROFILE=$(pacmd list-cards | grep "active profile" | cut -d ' ' -f 3-)

if [ "$CURRENT_PROFILE" = "<output:hdmi-stereo>" ]; then
        pacmd set-card-profile 0 "output:analog-stereo+input:analog-stereo"
        disper -s
else 
        pacmd set-card-profile 0 "output:hdmi-stereo"
        disper -S        
fi

For me it is especially useful since I don't like to clone the displays. I either use one or the other. You may need to adapt the audio profiles to your specific system.

share|improve this answer

You can try this script: https://github.com/giner/helplinux/tree/master/scripts/switch-sound

Tested on Ubuntu 10.04 - 13.04 and Arch Linux

share|improve this answer

Lukas's python script (https://github.com/lkettenb/sound-output-switcher, posted above) to implement a notifier applet works well. It needs the appindicator package. That can be installed with

sudo apt-get install python-appindicator
share|improve this answer
1  
Please include at least a few words in your answer explaining what the script is and why it's handy. –  derobert Aug 22 at 11:16

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.