How to quickly switch sound output devices?

Please post suggestions for improvement of Cinnamon here
https://github.com/linuxmint/Cinnamon
Post Reply
DaveJones
Level 1
Level 1
Posts: 42
Joined: Tue Sep 20, 2016 7:37 pm

How to quickly switch sound output devices?

Post by DaveJones » Wed Dec 28, 2016 8:46 pm

Hi all!

I'm using LM18.1 with Cinnamon 3.2.7.

One of the common tasks that I do multiple times per day is to switch between audio outputs. I sometimes like to send the sound to my speakers and sometimes to my headphones. The way I've been doing it now is to select output in Sound part of System Settings. It's tedious and takes too many clicks. Is there an applet that would allow me to quickly switch outputs? Or maybe some way to assign this to a keyboard shortcut? Maybe command line?

Thanks for any tips!

Dave

User avatar
all41
Level 14
Level 14
Posts: 5021
Joined: Tue Dec 31, 2013 9:12 am
Location: Computer, Car, Cage

Re: How to quickly switch sound output devices?

Post by all41 » Wed Dec 28, 2016 11:49 pm

Here it happens automatically when plugging in the headphones.
Check out the alsa mixer settings for automute.
in a terminal:

Code: Select all

alsamixer
Proud to be a supporter and monthly contributor to Mint.

Cosmo.
Level 23
Level 23
Posts: 17823
Joined: Sat Dec 06, 2014 7:34 am

Re: How to quickly switch sound output devices?

Post by Cosmo. » Thu Dec 29, 2016 4:00 am

Take a look at pavucontrol.

Wandering
Level 1
Level 1
Posts: 16
Joined: Tue Jan 26, 2016 5:23 am

Re: How to quickly switch sound output devices?

Post by Wandering » Thu Dec 29, 2016 6:45 am

Have you considered a hardware solution? A couple of months ago I've bought a switch. It cost me 24 Australian dollars delivered. Now I can switch between four outputs with one light push of an index finger. I love it.

Also worth keeping in mind, by piggybacking one switch onto another you could switch between four inputs on one and four outputs on the other.

The switch I have is small, tucks in comfortably under my main monitor and looks like this:

Image

ugly
Level 3
Level 3
Posts: 170
Joined: Thu Nov 24, 2016 9:17 pm

Re: How to quickly switch sound output devices?

Post by ugly » Thu Dec 29, 2016 1:35 pm

Someone made a script to do this. I don't remember where I found this to give proper credit.

This script cycles through your sound devices:

Code: Select all

#!/bin/bash

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

if [ $active_sink_index -ne $major_sink_index ] ; then
	next_sink_index=active_sink_index+1
fi

#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 [ $next_sink_index -eq $ndx ] ; then
		notify-send -i notification-audio-volume-high "Sound output switched to" "$line"
		exit
	fi
	ndx+=1
done;
If you want to switch to a specific device, I used parts of that script to just pick a particular device:

Code: Select all

#!/bin/bash

#Device name variable
devicename="alsa_output.usb-iFi__by_AMR__iFi__by_AMR__HD_USB_Audio-00.analog-stereo"

#change the default sink
pacmd "set-default-sink "$devicename""

#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 "$devicename""
done
This is for my particular audio device. You'd have to replace the devicename="" with your own device.

To find your device name, open a terminal and enter:

Code: Select all

pacmd list-sinks
The devices will be listed as index: 0, index: 1, etc.
Underneath each 'index' there is an entry something like:
name: <alsa_output.usb-iFi__by_AMR__iFi__by_AMR__HD_USB_Audio-00.analog-stereo>
Grab the text in the angled brackets and paste it into devicename="".

You can save the script as an .sh file and run it any way you like.

To make switching quicker, you can assign the script to a shortcut key.

Menu --> System Settings --> Keyboard --> Shortcuts --> Custom Shortcuts --> Add custom shortcut --> select the script you just made

For me, I like to assign a few different audio outputs to CTRL+F9, CTRL+F10 to select between my DAC and my bluetooth headphones.

User avatar
phd21
Level 17
Level 17
Posts: 7534
Joined: Thu Jan 09, 2014 9:42 pm
Location: Florida

Re: How to quickly switch sound output devices?

Post by phd21 » Thu Dec 29, 2016 3:58 pm

Hi "DaveJones",

I just read your post and the good replies to it. Here are my thoughts on this as well.

FYI: There are some really nice graphical (GUI) programs for sound that start with "Qas", like the fantastic "Qasmixer", in the Software Manager or Synaptic Package Manager (SPM) that you can install and use. Sound "Mixers" can be a little confusing if you are not familiar with them. If you bring up "Qasmixer" and make it fullscreen, on the right side you will have mixer devices where you can control your system sound. If you do not see the mixer devices, hit "F6". The mixer item "sysdefault" will show all installed sound card devices, and you will have various options to adjust various volume controls using "sliders" which you can turn off and on using the "dot" below the volume slider. There are also various other check boxes below that you can use, and depending upon your sound card(s) that sometimes includes headphone checkboxes and or sliders as well, like "Headphone Jack Sense".

The applications included are:
- qasconfig - browser for the alsa configuration tree
- qashctl - mixer for alsa's high level control interface
- qasmixer - desktop mixer with features similar to alsamixer

Qasmixer is a desktop mixer application for alsa's "simple mixer interface" and offers features similar to alsamixer. it also features a sytem tray icon with basic mixer functionality.

Install "libasound2-plugin-equal" which is another system wide equalizer that's in the Software Manager or Synaptic Package Manager (SPM) that shows up in "QasMixer".

Hope this helps ...
Phd21: Mint KDE 18.3 & 19, 64-bit Awesome OS, Ancient Dell OptiPlex 780 Core2Duo E8400 3GHz,4gb Ram,256gb SDD, Video: Intel 4 Graphics, DVD Lightscribe. Why I use KDE?:https://opensource.com/life/15/4/9-reasons-to-use-kde

User avatar
aaLM
Level 1
Level 1
Posts: 3
Joined: Fri Mar 17, 2017 5:29 pm

Re: How to quickly switch sound output devices?

Post by aaLM » Fri Mar 17, 2017 6:10 pm

Thank you!

My laptop has an unused button and I can assign the script execution to this button. exactly what I've looked for. :) :) :)
ugly wrote:Someone made a script to do this. I don't remember where I found this to give proper credit.
Linux Mint 18.1 Cinnamon, Lenovo T430

ColdBoot
Level 3
Level 3
Posts: 196
Joined: Thu Feb 16, 2017 10:40 pm

Re: How to quickly switch sound output devices?

Post by ColdBoot » Sat Mar 18, 2017 4:04 am

ugly wrote:Someone made a script to do this. I don't remember where I found this to give proper credit.

This script cycles through your sound devices:

Code: Select all

#!/bin/bash

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

if [ $active_sink_index -ne $major_sink_index ] ; then
	next_sink_index=active_sink_index+1
fi

#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 [ $next_sink_index -eq $ndx ] ; then
		notify-send -i notification-audio-volume-high "Sound output switched to" "$line"
		exit
	fi
	ndx+=1
done;
If you want to switch to a specific device, I used parts of that script to just pick a particular device:

Code: Select all

#!/bin/bash

#Device name variable
devicename="alsa_output.usb-iFi__by_AMR__iFi__by_AMR__HD_USB_Audio-00.analog-stereo"

#change the default sink
pacmd "set-default-sink "$devicename""

#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 "$devicename""
done
This is for my particular audio device. You'd have to replace the devicename="" with your own device.

To find your device name, open a terminal and enter:

Code: Select all

pacmd list-sinks
The devices will be listed as index: 0, index: 1, etc.
Underneath each 'index' there is an entry something like:
name: <alsa_output.usb-iFi__by_AMR__iFi__by_AMR__HD_USB_Audio-00.analog-stereo>
Grab the text in the angled brackets and paste it into devicename="".

You can save the script as an .sh file and run it any way you like.

To make switching quicker, you can assign the script to a shortcut key.

Menu --> System Settings --> Keyboard --> Shortcuts --> Custom Shortcuts --> Add custom shortcut --> select the script you just made

For me, I like to assign a few different audio outputs to CTRL+F9, CTRL+F10 to select between my DAC and my bluetooth headphones.
Hi Ugly,

Can this switch between speakers and 3.5 fron headphone jack? For whatever reason this didn't work for me with the analog sound (and I have no use for HDMI just yet and it's disabled). Made a somewhat simpler toggler and attached it to a launcher:

Code: Select all

#! /bin/bash

set_slusalice() {
  pacmd set-sink-port 0 analog-output-headphones
  amixer set Front 0%
  pactl set-sink-volume 0 50%
}

set_zvucnici() {
  pacmd set-sink-port 0 analog-output-lineout
  pactl set-sink-volume 0 30%
 }

AKTIVAN=$(pacmd list | grep "<analog-output-lineout>" | cut -f3 -d ' ')

if [ "$AKTIVAN" == "<analog-output-lineout>" ]
 then
   set_slusalice
 else
   set_zvucnici
fi
P.S. My bad... I have somehow overlooked that those are USB headphones which is why they merit a separate device. My script is for a simpler case, for onboard Realtek sound, when auto-mute is not working and the headphones always appear unplugged.
Linux Mint Cinnamon 18.1
Intel G1820, DDR3 8GB, Nvidia GT720(2GB)

User avatar
aaLM
Level 1
Level 1
Posts: 3
Joined: Fri Mar 17, 2017 5:29 pm

Re: How to quickly switch sound output devices?

Post by aaLM » Sun Mar 19, 2017 10:20 am

Unfortunately, I've quickly bumped in a bug I couldn't live with in the script quoted by ugly. Every time i had removed my USB headset, the script stopped working and kept switching to the analog output from itself until the restart of my laptop.

I've found an another script that working almost without a bug. It also keeps circle-toggle the outputs. There is minor bug in (if it is a bug), but I can live with it. If remove the USB headset while the output is on the USB device then put it back again I have to click 2 times the button assigned to the script to switch to the analog output. It will work just fine thereafter again.

I rather prefer the new script for one more reason, because the previous one kept popping up notification with every action and having a new notification entry in the history too.

here is the new script:

Code: Select all

#!/bin/bash 

new_sink=$(pacmd list-sinks | grep index | tee /dev/stdout | grep -m1 -A1 "* index" | tail -1 | cut -c12-)

echo "Setting default sink to: $new_sink";
pacmd set-default-sink $new_sink
pacmd list-sink-inputs | grep index | while read line
do
echo "Moving input: ";
echo $line | cut -f2 -d' ';
echo "to sink: $new_sink";
pacmd move-sink-input `echo $line | cut -f2 -d' '` $new_sink

done 


credit goes for Gaco and user512557
original source: https://askubuntu.com/questions/71863/h ... g-playback
Linux Mint 18.1 Cinnamon, Lenovo T430

mayo83
Level 1
Level 1
Posts: 1
Joined: Fri May 05, 2017 2:11 am

Re: How to quickly switch sound output devices?

Post by mayo83 » Tue Aug 08, 2017 2:59 pm

i know the thread is old but i also one of the guys that switch from internal soundcard (with desktop speakers) to my usb headset (that has an own soundcard device). If u hit the right mousebutton on the speaker icon u can switch ur sound devices realy fast. not as fast as an keyboard shortcut but i can live with this functionality.

mttr
Level 1
Level 1
Posts: 12
Joined: Fri Mar 09, 2012 11:46 am

Re: How to quickly switch sound output devices?

Post by mttr » Wed Mar 07, 2018 4:12 pm

#!/bin/bash

# This toggles the line-out to headphones and vice versa
# Linux Mint Mate 18.3 Tested and Works
# Save this as a script file and make sure it is set to "Allow executing file as a program" in the permissions
# Create a launcher and put on panel to use it.
#
# There is a hidden char in front of __active port: <analog-output>"*" so you have to include it
# Find which output is the active one
test_output=$(pacmd info | grep -i " active port: <analog-output>"*)

if [[ "$test_output" = " active port: <analog-output-lineout>" ]] ; then
pacmd set-sink-port 1 "analog-output-headphones" ;

elif [[ "$test_output" = " active port: <analog-output-headphones>" ]] ; then
pacmd set-sink-port 1 "analog-output-lineout" ;

fi
Headphone.rtf
raw file of above to show text format
(1.33 KiB) Downloaded 166 times
# commands I have use to figure this out (don't use root)
# don't include the following in script
$ pacmd info | grep -i "active"
active port: <hdmi-output-0>
active port: <analog-output-headphones>
active profile: <output:hdmi-surround>
active profile: <output:analog-stereo>

me@mint-18 ~/Desktop $ ./headphone
active port: <analog-output-headphones> (notice the extra tab or space at the beginning of this output line)
$ pacmd info | grep -i "active port: <analog-output-"*
active port: <analog-output-lineout>
$ pacmd info | grep -i "active port: <analog-output-"*
active port: <analog-output-headphones>

mib91
Level 1
Level 1
Posts: 12
Joined: Fri Jul 10, 2015 4:00 am

Re: How to quickly switch sound output devices?

Post by mib91 » Sat Sep 15, 2018 6:09 am

mayo83 wrote:
Tue Aug 08, 2017 2:59 pm
i know the thread is old but i also one of the guys that switch from internal soundcard (with desktop speakers) to my usb headset (that has an own soundcard device). If u hit the right mousebutton on the speaker icon u can switch ur sound devices realy fast. not as fast as an keyboard shortcut but i can live with this functionality.
Big thanks to you! Would've been better, IMO, if the left click on Sound applet show those option by default but its better than to switch the output device from System Settings -> Sound -> Output tab.

Post Reply

Return to “Cinnamon”