How can I switch between different audio output hardware using the shell?
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
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.
shell ubuntu audio pulseaudio
add a comment |
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.
shell ubuntu audio pulseaudio
add a comment |
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.
shell ubuntu audio pulseaudio
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.
shell ubuntu audio pulseaudio
shell ubuntu audio pulseaudio
edited Jan 28 '13 at 14:09
Luigi Massa Gallerano
asked Jan 28 '13 at 10:12
Luigi Massa GalleranoLuigi Massa Gallerano
5011510
5011510
add a comment |
add a comment |
8 Answers
8
active
oldest
votes
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
Hm. My pacmd doesn't have 'list-cards' option...
– ka3ak
Feb 27 '17 at 17:51
add a comment |
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
add a comment |
You can use pactl
, read its man page for more information.
add a comment |
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.
add a comment |
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
3
Please include at least a few words in your answer explaining what the script is and why it's handy.
– derobert
Aug 22 '14 at 11:16
add a comment |
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
add a comment |
As I told here (probably a duplicate), an alternative to Sound Switcher Indicator (which requieres adding a PPA):
In one line
In my case was hdmi-stereo-extra1+input
profile, so in one line would be: [[ $(pacmd list-cards | grep "active profile" | cut -d " " -f 3-) = "<output:hdmi-stereo-extra1+input:analog-stereo>" ]] && pacmd set-card-profile 0 "output:analog-stereo+input:analog-stereo" || pacmd set-card-profile 0 "output:hdmi-stereo-extra1+input:analog-stereo"
.
You can use a custom shortcut to execute it with bash -c
(will warn you if there is any conflict with other shortcut):
Also you can add an alias
to your .bashrc
.
In a script
I made some changes based on @user829996 (and here @user56655) answer:
#!/bin/bash
set -euo pipefail # strict mode
activeProfile() grep "active profile"
CURRENT_PROFILE="$(eval activeProfile)"
# If it doesn't work run pacmd list-cards and try the other outputs from profile section
ANALOG_PROFILE="output:analog-stereo+input:analog-stereo"
HDMI_PROFILE="output:hdmi-stereo-extra1+input:analog-stereo"
if [ "$CURRENT_PROFILE" = "<output:hdmi-stereo-extra1+input:analog-stereo>" ] ; then
pacmd set-card-profile 0 "$ANALOG_PROFILE"
else
pacmd set-card-profile 0 "$HDMI_PROFILE"
fi
activeProfile
add a comment |
I created the following python script that does the following:
- Toggle the default device to the next device on the list (with wrap around) regardless of the id's
- Moves all running applications to this device.
- Sends a notifications to the GUI with the device name.
#!/usr/bin/env python3
import subprocess
# Toggle default device to the next device (wrap around the list)
cards_info = subprocess.run(['pacmd','list-sinks'], stdout=subprocess.PIPE)
card_indexes = subprocess.run(['grep', 'index'], stdout=subprocess.PIPE, input=cards_info.stdout)
indexes_list = card_indexes.stdout.decode().splitlines()
card_descriptions = subprocess.run(['grep', 'device.description'], stdout=subprocess.PIPE, input=cards_info.stdout)
indices = [i for i, s in enumerate(indexes_list) if '*' in s]
if (len(indices) != 1):
print("Error finding default device")
exit(1)
default_index = indices[0]
next_default = 0
if (default_index != (len(indexes_list) - 1)):
next_default = default_index + 1
next_default_index = (indexes_list[next_default].split("index: ",1)[1])
subprocess.run(['pacmd','set-default-sink %s' %(next_default_index)], stdout=subprocess.PIPE)
# Move all existing applications to the new default device
inputs_info = subprocess.run(['pacmd','list-sink-inputs'], stdout=subprocess.PIPE)
inputs_indexes = subprocess.run(['grep', 'index'], stdout=subprocess.PIPE, input=inputs_info.stdout)
inputs_indexes_list = inputs_indexes.stdout.decode().splitlines()
for line in inputs_indexes_list:
input_index = (line.split("index: ",1)[1])
subprocess.run(['pacmd','move-sink-input %s %s' %(input_index, next_default_index)], stdout=subprocess.PIPE)
# Send notification to the GUI
descriptions_list = card_descriptions.stdout.decode().splitlines()
if (len(descriptions_list) == len(indexes_list)):
description = (descriptions_list[next_default].split("= ",1)[1])[1:-1]
args = ["notify-send", "Default audio card changed", 'Default audio card was set to %s' %(description)]
subprocess.run(args, stdout=subprocess.PIPE)
Assigned a keyboard shortcut to the script, and my life is happy now
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f62818%2fhow-can-i-switch-between-different-audio-output-hardware-using-the-shell%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
8 Answers
8
active
oldest
votes
8 Answers
8
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
Hm. My pacmd doesn't have 'list-cards' option...
– ka3ak
Feb 27 '17 at 17:51
add a comment |
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
Hm. My pacmd doesn't have 'list-cards' option...
– ka3ak
Feb 27 '17 at 17:51
add a comment |
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
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
edited Jan 29 '13 at 9:24
answered Jan 28 '13 at 14:24
Luigi Massa GalleranoLuigi Massa Gallerano
5011510
5011510
Hm. My pacmd doesn't have 'list-cards' option...
– ka3ak
Feb 27 '17 at 17:51
add a comment |
Hm. My pacmd doesn't have 'list-cards' option...
– ka3ak
Feb 27 '17 at 17:51
Hm. My pacmd doesn't have 'list-cards' option...
– ka3ak
Feb 27 '17 at 17:51
Hm. My pacmd doesn't have 'list-cards' option...
– ka3ak
Feb 27 '17 at 17:51
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Dec 21 '13 at 15:02
answered Dec 21 '13 at 12:15
LukasLukas
5112
5112
add a comment |
add a comment |
You can use pactl
, read its man page for more information.
add a comment |
You can use pactl
, read its man page for more information.
add a comment |
You can use pactl
, read its man page for more information.
You can use pactl
, read its man page for more information.
answered Jan 28 '13 at 10:25
favadifavadi
1,003612
1,003612
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Jan 13 '14 at 19:38
user56655user56655
211
211
add a comment |
add a comment |
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
3
Please include at least a few words in your answer explaining what the script is and why it's handy.
– derobert
Aug 22 '14 at 11:16
add a comment |
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
3
Please include at least a few words in your answer explaining what the script is and why it's handy.
– derobert
Aug 22 '14 at 11:16
add a comment |
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
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
edited Aug 24 '14 at 15:17
answered Aug 22 '14 at 11:06
P.WindridgeP.Windridge
1213
1213
3
Please include at least a few words in your answer explaining what the script is and why it's handy.
– derobert
Aug 22 '14 at 11:16
add a comment |
3
Please include at least a few words in your answer explaining what the script is and why it's handy.
– derobert
Aug 22 '14 at 11:16
3
3
Please include at least a few words in your answer explaining what the script is and why it's handy.
– derobert
Aug 22 '14 at 11:16
Please include at least a few words in your answer explaining what the script is and why it's handy.
– derobert
Aug 22 '14 at 11:16
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Jun 19 '13 at 3:41
Stanislav German-EvtushenkoStanislav German-Evtushenko
1113
1113
add a comment |
add a comment |
As I told here (probably a duplicate), an alternative to Sound Switcher Indicator (which requieres adding a PPA):
In one line
In my case was hdmi-stereo-extra1+input
profile, so in one line would be: [[ $(pacmd list-cards | grep "active profile" | cut -d " " -f 3-) = "<output:hdmi-stereo-extra1+input:analog-stereo>" ]] && pacmd set-card-profile 0 "output:analog-stereo+input:analog-stereo" || pacmd set-card-profile 0 "output:hdmi-stereo-extra1+input:analog-stereo"
.
You can use a custom shortcut to execute it with bash -c
(will warn you if there is any conflict with other shortcut):
Also you can add an alias
to your .bashrc
.
In a script
I made some changes based on @user829996 (and here @user56655) answer:
#!/bin/bash
set -euo pipefail # strict mode
activeProfile() grep "active profile"
CURRENT_PROFILE="$(eval activeProfile)"
# If it doesn't work run pacmd list-cards and try the other outputs from profile section
ANALOG_PROFILE="output:analog-stereo+input:analog-stereo"
HDMI_PROFILE="output:hdmi-stereo-extra1+input:analog-stereo"
if [ "$CURRENT_PROFILE" = "<output:hdmi-stereo-extra1+input:analog-stereo>" ] ; then
pacmd set-card-profile 0 "$ANALOG_PROFILE"
else
pacmd set-card-profile 0 "$HDMI_PROFILE"
fi
activeProfile
add a comment |
As I told here (probably a duplicate), an alternative to Sound Switcher Indicator (which requieres adding a PPA):
In one line
In my case was hdmi-stereo-extra1+input
profile, so in one line would be: [[ $(pacmd list-cards | grep "active profile" | cut -d " " -f 3-) = "<output:hdmi-stereo-extra1+input:analog-stereo>" ]] && pacmd set-card-profile 0 "output:analog-stereo+input:analog-stereo" || pacmd set-card-profile 0 "output:hdmi-stereo-extra1+input:analog-stereo"
.
You can use a custom shortcut to execute it with bash -c
(will warn you if there is any conflict with other shortcut):
Also you can add an alias
to your .bashrc
.
In a script
I made some changes based on @user829996 (and here @user56655) answer:
#!/bin/bash
set -euo pipefail # strict mode
activeProfile() grep "active profile"
CURRENT_PROFILE="$(eval activeProfile)"
# If it doesn't work run pacmd list-cards and try the other outputs from profile section
ANALOG_PROFILE="output:analog-stereo+input:analog-stereo"
HDMI_PROFILE="output:hdmi-stereo-extra1+input:analog-stereo"
if [ "$CURRENT_PROFILE" = "<output:hdmi-stereo-extra1+input:analog-stereo>" ] ; then
pacmd set-card-profile 0 "$ANALOG_PROFILE"
else
pacmd set-card-profile 0 "$HDMI_PROFILE"
fi
activeProfile
add a comment |
As I told here (probably a duplicate), an alternative to Sound Switcher Indicator (which requieres adding a PPA):
In one line
In my case was hdmi-stereo-extra1+input
profile, so in one line would be: [[ $(pacmd list-cards | grep "active profile" | cut -d " " -f 3-) = "<output:hdmi-stereo-extra1+input:analog-stereo>" ]] && pacmd set-card-profile 0 "output:analog-stereo+input:analog-stereo" || pacmd set-card-profile 0 "output:hdmi-stereo-extra1+input:analog-stereo"
.
You can use a custom shortcut to execute it with bash -c
(will warn you if there is any conflict with other shortcut):
Also you can add an alias
to your .bashrc
.
In a script
I made some changes based on @user829996 (and here @user56655) answer:
#!/bin/bash
set -euo pipefail # strict mode
activeProfile() grep "active profile"
CURRENT_PROFILE="$(eval activeProfile)"
# If it doesn't work run pacmd list-cards and try the other outputs from profile section
ANALOG_PROFILE="output:analog-stereo+input:analog-stereo"
HDMI_PROFILE="output:hdmi-stereo-extra1+input:analog-stereo"
if [ "$CURRENT_PROFILE" = "<output:hdmi-stereo-extra1+input:analog-stereo>" ] ; then
pacmd set-card-profile 0 "$ANALOG_PROFILE"
else
pacmd set-card-profile 0 "$HDMI_PROFILE"
fi
activeProfile
As I told here (probably a duplicate), an alternative to Sound Switcher Indicator (which requieres adding a PPA):
In one line
In my case was hdmi-stereo-extra1+input
profile, so in one line would be: [[ $(pacmd list-cards | grep "active profile" | cut -d " " -f 3-) = "<output:hdmi-stereo-extra1+input:analog-stereo>" ]] && pacmd set-card-profile 0 "output:analog-stereo+input:analog-stereo" || pacmd set-card-profile 0 "output:hdmi-stereo-extra1+input:analog-stereo"
.
You can use a custom shortcut to execute it with bash -c
(will warn you if there is any conflict with other shortcut):
Also you can add an alias
to your .bashrc
.
In a script
I made some changes based on @user829996 (and here @user56655) answer:
#!/bin/bash
set -euo pipefail # strict mode
activeProfile() grep "active profile"
CURRENT_PROFILE="$(eval activeProfile)"
# If it doesn't work run pacmd list-cards and try the other outputs from profile section
ANALOG_PROFILE="output:analog-stereo+input:analog-stereo"
HDMI_PROFILE="output:hdmi-stereo-extra1+input:analog-stereo"
if [ "$CURRENT_PROFILE" = "<output:hdmi-stereo-extra1+input:analog-stereo>" ] ; then
pacmd set-card-profile 0 "$ANALOG_PROFILE"
else
pacmd set-card-profile 0 "$HDMI_PROFILE"
fi
activeProfile
edited Apr 13 '17 at 12:22
Community♦
1
1
answered Mar 30 '17 at 22:35
Pablo BianchiPablo Bianchi
531612
531612
add a comment |
add a comment |
I created the following python script that does the following:
- Toggle the default device to the next device on the list (with wrap around) regardless of the id's
- Moves all running applications to this device.
- Sends a notifications to the GUI with the device name.
#!/usr/bin/env python3
import subprocess
# Toggle default device to the next device (wrap around the list)
cards_info = subprocess.run(['pacmd','list-sinks'], stdout=subprocess.PIPE)
card_indexes = subprocess.run(['grep', 'index'], stdout=subprocess.PIPE, input=cards_info.stdout)
indexes_list = card_indexes.stdout.decode().splitlines()
card_descriptions = subprocess.run(['grep', 'device.description'], stdout=subprocess.PIPE, input=cards_info.stdout)
indices = [i for i, s in enumerate(indexes_list) if '*' in s]
if (len(indices) != 1):
print("Error finding default device")
exit(1)
default_index = indices[0]
next_default = 0
if (default_index != (len(indexes_list) - 1)):
next_default = default_index + 1
next_default_index = (indexes_list[next_default].split("index: ",1)[1])
subprocess.run(['pacmd','set-default-sink %s' %(next_default_index)], stdout=subprocess.PIPE)
# Move all existing applications to the new default device
inputs_info = subprocess.run(['pacmd','list-sink-inputs'], stdout=subprocess.PIPE)
inputs_indexes = subprocess.run(['grep', 'index'], stdout=subprocess.PIPE, input=inputs_info.stdout)
inputs_indexes_list = inputs_indexes.stdout.decode().splitlines()
for line in inputs_indexes_list:
input_index = (line.split("index: ",1)[1])
subprocess.run(['pacmd','move-sink-input %s %s' %(input_index, next_default_index)], stdout=subprocess.PIPE)
# Send notification to the GUI
descriptions_list = card_descriptions.stdout.decode().splitlines()
if (len(descriptions_list) == len(indexes_list)):
description = (descriptions_list[next_default].split("= ",1)[1])[1:-1]
args = ["notify-send", "Default audio card changed", 'Default audio card was set to %s' %(description)]
subprocess.run(args, stdout=subprocess.PIPE)
Assigned a keyboard shortcut to the script, and my life is happy now
add a comment |
I created the following python script that does the following:
- Toggle the default device to the next device on the list (with wrap around) regardless of the id's
- Moves all running applications to this device.
- Sends a notifications to the GUI with the device name.
#!/usr/bin/env python3
import subprocess
# Toggle default device to the next device (wrap around the list)
cards_info = subprocess.run(['pacmd','list-sinks'], stdout=subprocess.PIPE)
card_indexes = subprocess.run(['grep', 'index'], stdout=subprocess.PIPE, input=cards_info.stdout)
indexes_list = card_indexes.stdout.decode().splitlines()
card_descriptions = subprocess.run(['grep', 'device.description'], stdout=subprocess.PIPE, input=cards_info.stdout)
indices = [i for i, s in enumerate(indexes_list) if '*' in s]
if (len(indices) != 1):
print("Error finding default device")
exit(1)
default_index = indices[0]
next_default = 0
if (default_index != (len(indexes_list) - 1)):
next_default = default_index + 1
next_default_index = (indexes_list[next_default].split("index: ",1)[1])
subprocess.run(['pacmd','set-default-sink %s' %(next_default_index)], stdout=subprocess.PIPE)
# Move all existing applications to the new default device
inputs_info = subprocess.run(['pacmd','list-sink-inputs'], stdout=subprocess.PIPE)
inputs_indexes = subprocess.run(['grep', 'index'], stdout=subprocess.PIPE, input=inputs_info.stdout)
inputs_indexes_list = inputs_indexes.stdout.decode().splitlines()
for line in inputs_indexes_list:
input_index = (line.split("index: ",1)[1])
subprocess.run(['pacmd','move-sink-input %s %s' %(input_index, next_default_index)], stdout=subprocess.PIPE)
# Send notification to the GUI
descriptions_list = card_descriptions.stdout.decode().splitlines()
if (len(descriptions_list) == len(indexes_list)):
description = (descriptions_list[next_default].split("= ",1)[1])[1:-1]
args = ["notify-send", "Default audio card changed", 'Default audio card was set to %s' %(description)]
subprocess.run(args, stdout=subprocess.PIPE)
Assigned a keyboard shortcut to the script, and my life is happy now
add a comment |
I created the following python script that does the following:
- Toggle the default device to the next device on the list (with wrap around) regardless of the id's
- Moves all running applications to this device.
- Sends a notifications to the GUI with the device name.
#!/usr/bin/env python3
import subprocess
# Toggle default device to the next device (wrap around the list)
cards_info = subprocess.run(['pacmd','list-sinks'], stdout=subprocess.PIPE)
card_indexes = subprocess.run(['grep', 'index'], stdout=subprocess.PIPE, input=cards_info.stdout)
indexes_list = card_indexes.stdout.decode().splitlines()
card_descriptions = subprocess.run(['grep', 'device.description'], stdout=subprocess.PIPE, input=cards_info.stdout)
indices = [i for i, s in enumerate(indexes_list) if '*' in s]
if (len(indices) != 1):
print("Error finding default device")
exit(1)
default_index = indices[0]
next_default = 0
if (default_index != (len(indexes_list) - 1)):
next_default = default_index + 1
next_default_index = (indexes_list[next_default].split("index: ",1)[1])
subprocess.run(['pacmd','set-default-sink %s' %(next_default_index)], stdout=subprocess.PIPE)
# Move all existing applications to the new default device
inputs_info = subprocess.run(['pacmd','list-sink-inputs'], stdout=subprocess.PIPE)
inputs_indexes = subprocess.run(['grep', 'index'], stdout=subprocess.PIPE, input=inputs_info.stdout)
inputs_indexes_list = inputs_indexes.stdout.decode().splitlines()
for line in inputs_indexes_list:
input_index = (line.split("index: ",1)[1])
subprocess.run(['pacmd','move-sink-input %s %s' %(input_index, next_default_index)], stdout=subprocess.PIPE)
# Send notification to the GUI
descriptions_list = card_descriptions.stdout.decode().splitlines()
if (len(descriptions_list) == len(indexes_list)):
description = (descriptions_list[next_default].split("= ",1)[1])[1:-1]
args = ["notify-send", "Default audio card changed", 'Default audio card was set to %s' %(description)]
subprocess.run(args, stdout=subprocess.PIPE)
Assigned a keyboard shortcut to the script, and my life is happy now
I created the following python script that does the following:
- Toggle the default device to the next device on the list (with wrap around) regardless of the id's
- Moves all running applications to this device.
- Sends a notifications to the GUI with the device name.
#!/usr/bin/env python3
import subprocess
# Toggle default device to the next device (wrap around the list)
cards_info = subprocess.run(['pacmd','list-sinks'], stdout=subprocess.PIPE)
card_indexes = subprocess.run(['grep', 'index'], stdout=subprocess.PIPE, input=cards_info.stdout)
indexes_list = card_indexes.stdout.decode().splitlines()
card_descriptions = subprocess.run(['grep', 'device.description'], stdout=subprocess.PIPE, input=cards_info.stdout)
indices = [i for i, s in enumerate(indexes_list) if '*' in s]
if (len(indices) != 1):
print("Error finding default device")
exit(1)
default_index = indices[0]
next_default = 0
if (default_index != (len(indexes_list) - 1)):
next_default = default_index + 1
next_default_index = (indexes_list[next_default].split("index: ",1)[1])
subprocess.run(['pacmd','set-default-sink %s' %(next_default_index)], stdout=subprocess.PIPE)
# Move all existing applications to the new default device
inputs_info = subprocess.run(['pacmd','list-sink-inputs'], stdout=subprocess.PIPE)
inputs_indexes = subprocess.run(['grep', 'index'], stdout=subprocess.PIPE, input=inputs_info.stdout)
inputs_indexes_list = inputs_indexes.stdout.decode().splitlines()
for line in inputs_indexes_list:
input_index = (line.split("index: ",1)[1])
subprocess.run(['pacmd','move-sink-input %s %s' %(input_index, next_default_index)], stdout=subprocess.PIPE)
# Send notification to the GUI
descriptions_list = card_descriptions.stdout.decode().splitlines()
if (len(descriptions_list) == len(indexes_list)):
description = (descriptions_list[next_default].split("= ",1)[1])[1:-1]
args = ["notify-send", "Default audio card changed", 'Default audio card was set to %s' %(description)]
subprocess.run(args, stdout=subprocess.PIPE)
Assigned a keyboard shortcut to the script, and my life is happy now
answered Mar 8 at 14:44
Nimrod.sNimrod.s
11
11
add a comment |
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f62818%2fhow-can-i-switch-between-different-audio-output-hardware-using-the-shell%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown