Page 3 of 4 FirstFirst 1234 LastLast
Results 21 to 30 of 34

Thread: Audio device switch

  1. #21
    Join Date
    Apr 2007
    Location
    WV
    Beans
    60
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Audio device switch

    Awesome script! This really should be installed as a default hotkey (and happen automatically when you plug in usb or hdmi cables)

  2. #22
    Join Date
    Jan 2010
    Beans
    3

    Re: Audio device switch

    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)?

  3. #23
    Join Date
    Oct 2009
    Beans
    4

    Re: Audio device switch

    Fantastic. I am ver grateful. I have been looking for this for quite a while,

  4. #24
    Join Date
    Jun 2011
    Location
    Utah
    Beans
    98
    Distro
    Ubuntu Development Release

    Re: Audio device switch

    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?

  5. #25
    Join Date
    Jul 2007
    Location
    The Netherlands
    Beans
    36
    Distro
    Ubuntu 12.10 Quantal Quetzal

    Re: Audio device switch

    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/

  6. #26
    Join Date
    Sep 2011
    Beans
    2

    Re: Audio device switch

    I have a script to change audio devices automatically if any one is interested.
    Code:
    mkdir ~/bin
    cd bin
    gedit audio_switch.sh
    Copy the following into the text editor:
    Code:
    #!/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
    To get the settings name of your card profile you will have to run
    Code:
    pacmd list-cards
    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.

    Save and close

    Open up terminal and type:
    Code:
    sudo gedit /etc/udev/rules.d/100-hdmi.rules
    In the text editor copy:
    Code:
    USER="$(who | grep :0\) | cut -f 1 -d ' ')"
    KERNEL=="card0", ACTION=="change", RUN+="/home/$USER/bin/audio_switch.sh"
    Save and close

    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.

  7. #27
    Join Date
    Jan 2006
    Beans
    1

    Re: Audio device switch

    Quote Originally Posted by dar270785 View Post
    I have a script to change audio devices automatically if any one is interested.
    Great job!


  8. #28
    Join Date
    Aug 2009
    Beans
    2

    Re: Audio device switch

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

  9. #29
    Join Date
    Dec 2008
    Beans
    3

    Re: Audio device switch

    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.

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

  10. #30
    Join Date
    Jan 2007
    Beans
    1,634
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Audio device switch

    Quote Originally Posted by psmbfuer View Post
    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.

    -- snip --

    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
    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.
    Do you have any idea how to modify this for not only setting the different profiles, but setting them on different cards? I only have one sound card, but
    Code:
    pactl list cards
    lists HDMI as card 0 and the regular laptop speakers as card 1.
    Don't forget to give thanks and mark your thread as solved

Page 3 of 4 FirstFirst 1234 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •