You are not logged in.

#1 2013-03-04 22:15:21

csolisr
Member
From: Costa Rica
Registered: 2012-06-10
Posts: 23
Website

How to switch quickly between the native soundcard and USB speakers?

My computer has two sound devices: one, the native sound card (which I use for my headphones), and an external pair of USB speakers. However, switching between them is a complicated task, mainly because I find almost no programs to do it:

* GNOME Control Center, Sound section: What currently works for me, but can only be used within GNOME Shell or Cinnamon. It can be used from other sessions, though (for example Spectrwm), but it's very heavyweight to have the whole GNOME desktop solely to switch between sound cards.
* KMix: Works only within KDE and nowhere else. What's more, to actually do the switch one must open the program, go to the options menu and select the soundcard (unlike GNOME, which is two clicks away from the status bar).
* Pavucontrol: Kind of works, but only in a per-application basis. That is, I can't tell the system to start all current and future sound streams in X device. What's worse, I must wait until the application is open to actually do the switch.
* XFCE4 Mixer: Doesn't work from outside XFCE. Even there, switching cards is not possible without literally editing a configuration file each time.
* Alsamixer and GNOME Alsamixer: Can't switch sound cards at all.

So, I'm still on the search of a lightweight program that can allow me to do something as simple as switching between my USB speakers and native headphones without much hassle. Is there anything else besides GNOME that can do the trick?

Offline

#2 2013-03-04 22:23:50

nagaseiori
Member
Registered: 2012-09-22
Posts: 72

Re: How to switch quickly between the native soundcard and USB speakers?

Could you write a script to edit /etc/asound.conf?

Offline

#3 2013-03-04 22:26:29

csolisr
Member
From: Costa Rica
Registered: 2012-06-10
Posts: 23
Website

Re: How to switch quickly between the native soundcard and USB speakers?

If I only knew what should I write in it. In fact, I didn't know you could switch soundcards with a command for ALSA.

Offline

#4 2013-03-05 01:40:43

csolisr
Member
From: Costa Rica
Registered: 2012-06-10
Posts: 23
Website

Re: How to switch quickly between the native soundcard and USB speakers?

Oh, and something else. It seems like my soundcard is being controlled by PulseAudio. No, I can't uninstall PA without losing GNOME and Cinnamon.

Offline

#5 2013-03-05 02:10:55

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,607
Website

Re: How to switch quickly between the native soundcard and USB speakers?

I know nothing about pulseaudio, but if you can still use asoundrc, that's the way I'd go.  You can learn all about it on the alsa page on arch's wiki, or on the alsaproject wiki.

Basically a single command along the lines of the following could switch it once asoundrc was created:

sed -i 's/card 0/card 1/' ~/.asoundrc

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2013-03-05 02:21:49

csolisr
Member
From: Costa Rica
Registered: 2012-06-10
Posts: 23
Website

Re: How to switch quickly between the native soundcard and USB speakers?

As I said, ALSA is not controlling my soundcard, and I can't force it to control it either. Asoundconf's commands do nothing to switch the soundcard on-the-fly, and I doubt that .asoundrc will do either.

Offline

#7 2013-03-05 02:29:07

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,356

Re: How to switch quickly between the native soundcard and USB speakers?

The script I use with pulse.

#/bin/bash
# paswitch 2011-02-02 by Ng Oon-Ee <ngoonee.talk@gmail.com>
# I can't remember where I found this script, can't locate the original author.
# Please inform me if you know, so that I can give proper attribution.
# CHANGES: Added auto-move all inputs to new default sound card.
# WAS: Pulse Audio Sound Card Switcher v1.0 2010-01-13
#   Switches between soundcards when run. All streams are moved to the new default sound-card.

# $totalsc: Number of sound cards available
totalsc=$(pacmd "list-sinks" | grep index: | wc -l) # total of sound cards: $totalsc
if [ $totalsc -le 1 ]; then # Check whether there are actually multiple cards available
  notify-send -u critical -t 5000 "Nothing to switch, system only has one sound card."
  exit
fi
# $scindex: The Pulseaudio index of the current default sound card
scindex=$(pacmd list-sinks | awk '$1 == "*" && $2 == "index:" {print $3}')
# $cards: A list of card Pulseaudio indexes
cards=$(pacmd list-sinks | sed 's|*||' | awk '$1 == "index:" {print $2}')
PICKNEXTCARD=1 # Is true when the previous card is default
count=0 # count of number of iterations
for CARD in $cards; do
  if [ $PICKNEXTCARD == 1 ]; then
# $nextsc: The pulseaudio index of the next sound card (to be switched to)
    nextsc=$CARD
    PICKNEXTCARD=0
# $nextind: The numerical index (1 to totalsc) of the next card
    nextind=$count
  fi
  if [ $CARD == $scindex ]; then # Choose the next card as default
    PICKNEXTCARD=1
  fi
  count=$((count+1))
done
pacmd "set-default-sink $nextsc" # switch default sound card to next 

# $inputs: A list of currently playing inputs
inputs=$(pacmd list-sink-inputs | awk '$1 == "index:" {print $2}')
for INPUT in $inputs; do # Move all current inputs to the new default sound card
  pacmd move-sink-input $INPUT $nextsc
done
# $nextscdec: The device.description of the new default sound card
# NOTE: This is the most likely thing to break in future, the awk lines may need playing around with
nextscdesc=$(pacmd list-sinks | awk '$1 == "device.description" {print substr($0,5+length($1 $2))}' \
                         | sed 's|"||g' | awk -F"," 'NR==v1{print$0}' v1=$((nextind+1))) 
#notify-send "Default sound-card changed to $nextscdesc"
dbus-send --session --type=method_call --reply-timeout=10000 \
  --dest=org.freedesktop.Notifications /org/freedesktop/Notifications \
  org.freedesktop.Notifications.Notify  string:"paswitch"  uint32:1 \
  string:$icon_name  string:"Default sound card is now"  string:"$nextscdesc" \
  array:string:"" dict:string:string:"",""  int32:3000
exit
# Below text was from original author and remains unaltered
# CC BY - creative commons
# Thanks God for help :) and guys lhunath, geirha, Tramp and others from irc #bash on freenode.net

Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

#8 2013-03-05 03:02:56

csolisr
Member
From: Costa Rica
Registered: 2012-06-10
Posts: 23
Website

Re: How to switch quickly between the native soundcard and USB speakers?

For some strange reason, your/his/her script only detects one sink, as does "pacmd list-sinks". But even more strangely, the script detects all of the soundcards. So, it seems like I have to switch the sink from a card to another, somehow.

Offline

#9 2013-03-05 09:10:33

mich41
Member
Registered: 2012-06-22
Posts: 796

Re: How to switch quickly between the native soundcard and USB speakers?

Trilby wrote:

Basically a single command along the lines of the following could switch it once asoundrc was created:

sed -i 's/card 0/card 1/' ~/.asoundrc

And then restart all applications which have already parsed the old .asoundrc.

Switching audio output on the fly requires something like jack or pulsaudio.

Offline

#10 2013-03-05 11:21:25

koz
Member
Registered: 2012-01-09
Posts: 37

Re: How to switch quickly between the native soundcard and USB speakers?

Can't you turn off the device(s) you aren't using in pavucontrol?
That would direct all output to the device isn't turned off?

Or am I missing something here?

Last edited by koz (2013-03-05 11:22:06)

Offline

#11 2013-03-05 11:30:23

65kid
Member
From: Germany
Registered: 2011-01-26
Posts: 663

Re: How to switch quickly between the native soundcard and USB speakers?

ponymix might help.

Offline

#12 2013-03-05 23:44:36

ngoonee
Forum Fellow
From: Between Thailand and Singapore
Registered: 2009-03-17
Posts: 7,356

Re: How to switch quickly between the native soundcard and USB speakers?

csolisr wrote:

For some strange reason, your/his/her script only detects one sink, as does "pacmd list-sinks". But even more strangely, the script detects all of the soundcards. So, it seems like I have to switch the sink from a card to another, somehow.

If you see it in pavucontrol you'll have it using my script. USB sound cards are separate sinks. Sorry, not possible to debug what's happening on your system without more details.


Allan-Volunteer on the (topic being discussed) mailn lists. You never get the people who matters attention on the forums.
jasonwryan-Installing Arch is a measure of your literacy. Maintaining Arch is a measure of your diligence. Contributing to Arch is a measure of your competence.
Griemak-Bleeding edge, not bleeding flat. Edge denotes falls will occur from time to time. Bring your own parachute.

Offline

Board footer

Powered by FluxBB