TIP: Switching Soundcards In Ubuntu
Blog | Tech Blog | Secure Coding | Twitter | RSS Feed | Get Email Updates
If you have multiple soundcards in your system and have to switch between them regularly (e.g. laptop owners like me, who use an external soundcard while being docked to groove to the highest and the lowest of frequencies through those 7.1 channels and use the in-built sound card while on the move) you would definitely be under-whelmed by the less-than-stellar performance of ubuntu in switching between the sound cards. For me at least, ubuntu (fiesty 7.04 amd64) never seems to recognize that I’ve connected the speakers to my Audigy 2 ZS Notebook PCMCIA card, and merrily continues to huff-puff through the tinny laptop speakers until I manually go into the sound preferences, switch over to multichannel playback and vice versa. Well, I still haven’t found an automated solution for the switching, but here is a little cli-magic to do this in a click.
If you have alsa on ur system, just use the command “asoundconf list” first to list out all the soundcards connected to your system. e.g. on my system, the output is
$asoundconf list
IXP
AUDIGY2
Here, IXP is my internal sound card and AUDIGY2 is my external (PCMCIA) sound card. Now, all you need to is use “asoundconf set-default-card <cardname>” to switch over to any card desired. So, what I did was make 2 bash scripts, and insert this command in them with each having one of my soundcards’ names.
script 1 – setixp.sh
#!/bin/bash
asoundconf set-default-card IXP
Similarly, script 2 – setaudigy.sh
#!/bin/bash
asoundconf set-default-card AUDIGY2
Now, all that remains is save the two scripts (maybe in ur home directory or ur desktop), and make them executable (use “chmod +x <scriptname>”). Now, whenever you want to switch to a card, just double-click on the appropriate script, and watch the sound getting redirected. oops, make that “hear” the sound
.
© Shantanu Goel | TIP: Switching Soundcards In Ubuntu
|
Liked this post? Get FREE Updates Subscribe to RSS feed |






This post has 8 comments
February 18th, 2008
An alternative way to do the same in a single script (without prior knowledge of installed cards) would be
#!/bin/bash
oldIFS=${IFS}
IFS=$’\n’
cardList=($(asoundconf list | grep -v “Names of available sound cards”))
IFS=${oldIFS}
if [ ${#cardList[@]} -eq 1 ]
then
echo “The system detedted only 1 audio card”
echo “Press enter to exit”
read
exit 0
fi
echo “Select the AUDIO card to be selected as default”
select card in “${cardList[@]}”
do
if [ -n "${card}" ]
then
echo “Changing default-card to: ${card}”
asoundconf set-default-card ${card}
echo “Press enter to exit”
read
exit $?
else
echo “Please select a valid entry”
fi
done
February 18th, 2008
Yeah, that’d be a good and generic approach. But it’d take a click and some typing
. It can be used as it is by someone who just doesn’t want to make his own script.
But, a bit of extra effort (pre-determining the soundcards available) would make it much easier and faster later on (single click switching).
September 17th, 2008
For a simpler (albeit less impressive) solution, you could also add a small script to your System –> Preferences –> Sessions –> Startup Programs as follows:
#!/bin/bash
AUD=`asoundconf list | grep ‘AUDIGY2′`
if [ "$AUD" = "AUDIGY2" ]; then asoundconf set-default-card AUDIGY2;
else asoundconf set-default-card IXP; fi
There’s probably a way to call this from Udev whenever the device is hot plugged, but I don’t know how. As-is, you could either manually run the script or log off and back on to activate the appropriate sound device.
September 17th, 2008
Thanks Rojo, but actually the problem here is that with this script the sound card will always be set to Audigy2 because the sound card itself is always plugged into my PCMCIA slot, the speakers might or might not be, so need to switch the sound cards manually..I’m trying to find out if there is some indication when the speakers cables are plugged into the sound card, etc..maybe I can look at the driver source code and log something which can be used to trigger the script..
December 4th, 2008
Thanks – should be useful in other distros too
Shantanu, to solve your hardware issue, see if you can make or get made a ‘y’ connection that connects both soundcards to the speakers.
December 5th, 2008
An auxiliary note for anyone who just wants to set one card as default *all the time*, on a non-PulseAudio system: you can /etc/modprobe.conf and use the index= option, which all ALSA drivers respect.
If you have one card handled by snd-hda-intel and one handled by snd-ice1724 , and you wanted the snd-ice1724 card to be default, you’d add these lines:
options snd-ice1724 index=0
options snd-hda-intel index=1
and that sorts it.
If you’re using any modern distribution with PulseAudio (and you don’t have any problems with it…), you can both set a default device and move playing streams on-the-fly from one device to another in ‘pavucontrol’.
December 5th, 2008
No, you cannot detect when the speakers are plugged in. It has nothing to do with the operating system, the sound card simply does not detect what is plugged in, so there is no way for the operating system to know. Every operating system requires you to tell it what sound card you want to use for output.
December 5th, 2008
@Little Birdie: Actually the sound card that I have (Creative Audigy 2 ZS Notebook) can detect whether the speakers are connected or not. Creative gives a utility for windows that automatically switches to using the internal soundcard/speakers of my laptop if I don’t connect the speakers to the creative sound card. So, I’m guessing hw wise it is possible but would need some support in the drivers.