How to quickly switch sound output devices?

Please post suggestions for improvement of Cinnamon on:
https://github.com/linuxmint/Cinnamon
Forum rules
Before you post read how to get help. Topics in this forum are automatically closed 6 months after creation.
Locked
DaveJones
Level 2
Level 2
Posts: 56
Joined: Tue Sep 20, 2016 7:37 pm

How to quickly switch sound output devices?

Post by DaveJones »

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
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 1 time in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
all41
Level 19
Level 19
Posts: 9520
Joined: Tue Dec 31, 2013 9:12 am
Location: Computer, Car, Cage

Re: How to quickly switch sound output devices?

Post by all41 »

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

Code: Select all

alsamixer
Everything in life was difficult before it became easy.
Cosmo.
Level 24
Level 24
Posts: 22968
Joined: Sat Dec 06, 2014 7:34 am

Re: How to quickly switch sound output devices?

Post by Cosmo. »

Take a look at pavucontrol.
Wandering

Re: How to quickly switch sound output devices?

Post by Wandering »

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
User avatar
ugly
Level 5
Level 5
Posts: 592
Joined: Thu Nov 24, 2016 9:17 pm

Re: How to quickly switch sound output devices?

Post by ugly »

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.
phd21
Level 20
Level 20
Posts: 10104
Joined: Thu Jan 09, 2014 9:42 pm
Location: Florida

Re: How to quickly switch sound output devices?

Post by phd21 »

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 20 Cinnamon & KDE Neon 64-bit Awesome OS's, Dell Inspiron I5 7000 (7573, quad core i5-8250U ) 2 in 1 touch screen
aaLM

Re: How to quickly switch sound output devices?

Post by aaLM »

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

Re: How to quickly switch sound output devices?

Post by ColdBoot »

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

Re: How to quickly switch sound output devices?

Post by aaLM »

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
mayo83

Re: How to quickly switch sound output devices?

Post by mayo83 »

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: 45
Joined: Fri Mar 09, 2012 11:46 am

Re: How to quickly switch sound output devices?

Post by mttr »

#!/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
# 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 »

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

Re: How to quickly switch sound output devices?

Post by justXUX »

Guys I found a better solution to this Install so called Indicator Sound Switcher, it works like an applet and is very easy,customizable. I'm using it in Linux Mint 19.1 (works great). Here links:
https://www.omgubuntu.co.uk/2016/09/ind ... buntu-snap
https://github.com/yktoo/indicator-sound-switcher
Locked

Return to “Cinnamon”