Awesome script! This really should be installed as a default hotkey (and happen automatically when you plug in usb or hdmi cables)
Awesome script! This really should be installed as a default hotkey (and happen automatically when you plug in usb or hdmi cables)
Nice script, but is there a way to modify it so that I can switch not only the output of current application, but the output device -so to say- system-wide(that is the option in "output" menu of gnome-volume-control)?
Fantastic. I am ver grateful. I have been looking for this for quite a while,
This is awesome, but when I use it in GNOME-Shell the notifications just build up. I commented out the line with "notify-send", but I kinda liked the notification, does anyone know how to fix the build up?
If you are using Gnome Shell instead of Unity you can install the extension below. You'll then be able to switch from the volume short-cut in the top panel. https://extensions.gnome.org/extensi...n-volume-menu/
I have a script to change audio devices automatically if any one is interested.
Copy the following into the text editor:Code:mkdir ~/bin cd bin gedit audio_switch.sh
To get the settings name of your card profile you will have to runCode:#!/bin/bash status="$(cat /sys/class/drm/card0-HDMI-A-1/status)" if [ "${status}" = disconnected ] then pactl set-card-profile 0 output:analog-stereo+input:analog-stereo notify-send -i notification-audio-volume-high "Sound output switched to Laptop Speakers" exit elif [ "${status}" = connected ] then pactl set-card-profile 0 output:hdmi-stereo+input:analog-stereo notify-send -i notification-audio-volume-high "Sound output switched to HDMI" exit fi
If you scroll up to where there are a lot of 'output:'. These are the profile names. The zero is the card number this should be whatever it says for alsa.card.Code:pacmd list-cards
Save and close
Open up terminal and type:
In the text editor copy:Code:sudo gedit /etc/udev/rules.d/100-hdmi.rules
Save and closeCode:USER="$(who | grep :0\) | cut -f 1 -d ' ')" KERNEL=="card0", ACTION=="change", RUN+="/home/$USER/bin/audio_switch.sh"
Finally in terminal type:
sudo chmod +x ~/bin/audio_switch.sh
With a bit more tweaking you can get this to change setting for your HDMI. i.e. resolution, position, primary display etc.
In my script I use it to also change the resolution of my HDTV to its maximum when it is plugged in, as it is not correctly detected.
Enjoy!
Last edited by dar270785; February 1st, 2012 at 02:07 AM.
@dar270785
Great script. I used it to switch between my USB audio device and laptop speakers when I plug/unplug the laptop from its dock station. Very convenient when using a softphone like http://www.sflphone.org.
For anyone on Ubuntu 12.04 and wishing to add a shortcut key to switch between two audio cards/profiles, I hope this helps. I give credit to the first and last code post as these were my starting point. I only post this as the original post (from 2010) does not appear to work on Ubuntu 12.04. That said, I'm no expert and I might have mis-interpretted the results of my testing or just overlooked something. Either way, the following does work for me.
This script uses the "pacmd list-cards" command to first get a listing of the available sound cards/profile present in my system. It again uses the "pacmd set-card-profile" command to set the desired profile.
The pacmd output that you see below represents a piece of the output from "pacmd list-cards". Since there are so many, and since the names do not exactly match to the names seen in the GUI "Sound" tool available in Ubuntu, I ran the above command twice, while manually changing from my "HDMI/ DisplayPort 2" to "Analog Output" via the "Sound" application.
The key line below is the "active profile:" line where you see "<output:analog-stereo+input:analog-stereo>".
Using this technique, and instead of writing a more complex script, I simply use these two as logic flags in the script below. Change the script to use your profiles and you should be good to go, at least if you are toggling between two profiles.
# profiles:
output:analog-stereo: Analog Stereo Output (priority 6000)
output:analog-stereo+input:analog-stereo: Analog Stereo Duplex (priority 6060)
output:analog-surround-40: Analog Surround 4.0 Output (priority 700)
output:analog-surround-40+input:analog-stereo: Analog Surround 4.0 Output + Analog Stereo Input (priority 760)
output:analog-surround-41: Analog Surround 4.1 Output (priority 800)
output:analog-surround-41+input:analog-stereo: Analog Surround 4.1 Output + Analog Stereo Input (priority 860)
output:analog-surround-50: Analog Surround 5.0 Output (priority 700)
output:analog-surround-50+input:analog-stereo: Analog Surround 5.0 Output + Analog Stereo Input (priority 760)
output:analog-surround-51: Analog Surround 5.1 Output (priority 800)
output:analog-surround-51+input:analog-stereo: Analog Surround 5.1 Output + Analog Stereo Input (priority 860)
output:analog-surround-71: Analog Surround 7.1 Output (priority 700)
output:analog-surround-71+input:analog-stereo: Analog Surround 7.1 Output + Analog Stereo Input (priority 760)
output:iec958-stereo: Digital Stereo (IEC958) Output (priority 5500)
output:iec958-stereo+input:analog-stereo: Digital Stereo (IEC958) Output + Analog Stereo Input (priority 5560)
output:hdmi-stereo: Digital Stereo (HDMI) Output (priority 5400)
output:hdmi-stereo+input:analog-stereo: Digital Stereo (HDMI) Output + Analog Stereo Input (priority 5460)
output:hdmi-surround: Digital Surround 5.1 (HDMI) Output (priority 300)
output:hdmi-surround+input:analog-stereo: Digital Surround 5.1 (HDMI) Output + Analog Stereo Input (priority 360)
output:hdmi-stereo-extra1: Digital Stereo (HDMI) Output (priority 5200)
output:hdmi-stereo-extra1+input:analog-stereo: Digital Stereo (HDMI) Output + Analog Stereo Input (priority 5260)
input:analog-stereo: Analog Stereo Input (priority 60)
off: Off (priority 0)
active profile: <output:analog-stereo+input:analog-stereo>
active profile: <output:hdmi-stereo-extra1+input:analog-stereo> (I added this one to this listing, but it is from the second
(run of pacmd when the HDMI card was active.)
sinks:
alsa_output.pci-0000_00_1b.0.analog-stereo/#10: Built-in Audio Analog Stereo
Of the available "cards" / "profiles" listed, I only use two, the HDMI and Anolog cards. So all I want to do is toggle from one to the other.
Save the script whereever you save them (hopefully somewhere in your PATH, chmod +x, add a shortcut key via the "Keyboard" application and it should be working.Code:#!/bin/bash # My debug "echo" statements are still embedded, but feel fee to remove. hdmiprof="<output:hdmi-stereo-extra1+input:analog-stereo>" # to ease my parsing below, I keep the < and > on the string here. analogprof="<output:analog-stereo+input:analog-stereo>" # to ease my parsing below, I keep the < and > on the string here. currentprof=$(pacmd list-cards | grep "active profile: " | cut -d" " -f3) # Query pacmd to see what profile is active echo "$currentprof" if [ "$currentprof" = "$analogprof" ] ; then echo about to switch to HDMI Audio pacmd set-card-profile 0 output:hdmi-stereo-extra1+input:analog-stereo else echo about to swith to Analog Audio pacmd set-card-profile 0 output:analog-stereo+input:analog-stereo fi
Don't forget to give thanks and mark your thread as solved
Bookmarks