The GNU of Life, the Universe and Everything

April 14, 2010

How to change sound from two sound cards

Filed under: bash, console, gentoo, sound — Tags: , , , , , , , — Pedro Carvalho @ 5:29 pm

I have two sound cards. One is the inboard and the other is a USB card.

I’m toggling often between them so i created this simple script: (see the end for link to files)

if [[ "$1" = "-l" ]] || [[ ! -n "$1" ]]; then
cat /proc/asound/cards
card=`grep "^ $1" /proc/asound/cards`
echo "Changing to sound card $card"
ln -fs $dir.asoundrc_"$1"0 $dir.asoundrc

The dir variable has to be changed to the user’s name.
This script moves around the .asoundrc files. there’s one for each card available or for each card used:

Example of different Alsa setups:

i have 4 entries in /proc/asound/cards:

0 [VirMIDI ]: VirMIDI - VirMIDI
Virtual MIDI Card 1
1 [NVidia ]: HDA-Intel - HDA NVidia
HDA NVidia at 0xfbf78000 irq 21
2 [hercdjrmx0 ]: hdj_mod - Hercules DJ Console RMX
Hercules Hercules DJ Console RMX at usb-0000:00:04.0-1, full speed
3 [RMX ]: USB-Audio - Hercules DJ Console RMX
Hercules Hercules DJ Console RMX at usb-0000:00:04.0-1, full speed

so i have .asoundrc_00 , .asoundrc_10 , .asoundrc_20 and .asoundrc_30

this is the basic model: (for sound card 1)

# File: ~/.asoundrc (nvidia nforce setup)
pcm.!default {
type plug
slave.pcm "dmixer"
pcm.dsp0 {
type plug
slave.pcm "dmixer"
pcm.dmixer {
type dmix
ipc_key 1024
slave {
pcm "hw:1,0"
period_time 0
period_size 1024
buffer_size 8192
rate 48000 #many new cards are 48000 only
bindings {
0 0
1 1
ctl.dmixer {
type hw
card NVidia
# card RMX

# for 5.1 speakers
pcm.ch51dup {
slave.pcm surround51
slave.channels 6
type route
ttable.0.0 1
ttable.1.1 1
ttable.0.2 1
ttable.1.3 1
ttable.0.4 0.5
ttable.1.4 0.5
ttable.0.5 0.5
ttable.1.5 0.5

Then i just have to create the other 3 files changing the line in red to pcm “hw:0,0 (for sound card 0),  pcm “hw:2,0″ (for sound card 2) and pcm “hw:3,0″ (for sound card 3)

The lines in blue can be changed too, but i’ve found that they are ignored. After a reboot, the ordering of the cards might change so its useful to have a file for each slot.

Installing the script in your home

The .asoundrc* files are placed in the user’s home directory
The script goes to /usr/local/bin.

files needed

The Shocking Blue Green Theme Blog at


Get every new post delivered to your Inbox.