You are not logged in.

#1 2006-09-04 22:05:51

toxic
Member
Registered: 2006-06-05
Posts: 117

Bash: Switch default soundcard (i.e. on usb-plugged in/out)

I've written a small bash script which changes your default ALSA card.

It doesn't use multiple files, or any symlinking between files. The configuration is ready into memory and processed in RAM and rewritten back to the configuration file.

The program can be run in mainly two ways. One, which forces default soundcard to a specific value, and the second which toggles between soundcards.
Running alsaswitch without parameters will toggle between your default soundcards (hw:0,0 and hw:1,0), but other values may be specified.
As a third option, a new .asoundrc (alsa user configuration file) may be generated.

I've included a usb-audio.rules file for udev. I've set it to always select the usb-soundcard whenever it's plugged in, or otherwise select my system soundcard. This can of course be configuered in any way, applicable by udev.


A word of warning goes to everyone who have stuffed their $HOME/.asoundrc with lots of stuff (why did you do that in the first case ....), to rethink if this script might edit any other variables. For instance, if card is forced for 1 to 2, every field matching "card 1" will be replaced by "card 2".  However, as far as warning goes, the script always takes a backup of your original configuration file before editing it.

:arrow:  The actual bash script can be found [url=http://www20.brinkster.com/warranty/alsaswitch.txt]here
[/url]

And the udev rules file is listed below:

#/etc/udev/rules.d/usb_audio.rules
#
# USB Audio device
#         This script tells udev to run alsaswitch on 
#         the input of device /dev/dsp1. 
#         When /dev/dsp1 is removed (unplugged) alsaswitch 
#         is run again.
#
#         The script tells alsa to set the usb-speaker to 
#         default when it's plugged in to the computer.
# NOTE 
#      alsaswitch works only with alsa. You may need to 
#       edit this script manually to fit your system needs. 
#         Run udevmonitor to list DEVPATH items to match against.
# RUN     /path/alsaswitch -c 1 will make card 1 default. 
#      alsaswitch -l to list available cards. Note that a 'real'
#         soundcard has an irq assigned. 
#

ACTION=="add", DEVPATH=="/class/sound/dsp1",  RUN+="/home/toxic/bin/alsaswitch -c 1"
ACTION=="remove", DEVPATH=="/class/sound/dsp1", RUN+="/home/toxic/bin/alsaswitch -c 0"


# Pseudo code translation of the above code 
#    If [ ACTION == "add"] && [ "Device added" == "/../dsp1/" ]; then
#           run /path/file.sh
#    fi

Offline

#2 2006-09-04 23:16:13

stonedz
Member
From: Firenze, Italy
Registered: 2006-08-29
Posts: 30
Website

Re: Bash: Switch default soundcard (i.e. on usb-plugged in/out)

Thanks for the script, It might be very useful to me.


"Software is like sex, it's better when it's free." L.T.

Offline

#3 2010-11-29 12:05:56

the_isz
Member
Registered: 2009-04-14
Posts: 280

Re: Bash: Switch default soundcard (i.e. on usb-plugged in/out)

@toxic:

Would you mind posting the bash script again? It seems it's not available under
the given link anymore and I'd really like to check it out.

Thank you in advance!

Offline

#4 2012-03-24 19:45:38

exitus_
Member
Registered: 2012-02-26
Posts: 3

Re: Bash: Switch default soundcard (i.e. on usb-plugged in/out)

I solved that a bit easier: I stored my configuration files with alsactl to specific files (with "alsactl -f ", e.g. ~/.asoundrc.usb for my usb soundcard)
I edit these new configuration files and especially add these lines to set the default soundcard, you can get the numbers for your soundcards with "cat /proc/asound/cards"

defaults.ctl.card 1
defaults.pcm.card 1
defaults.pcm.device 0

So if I want to use my usb soundcard I simply copy this file to the current .asoundrc like this:

cp -a ~/.asoundrc.usb ~/.asoundrc

Then you only have to restart your sound application (like your audio player) to run it with the new default soundcard.
To make this a bit more comfortable I added a menu entry in my Window Manager.
I wrote this because I thought maybe someone could also be interested in that issue.

Offline

Board footer

Powered by FluxBB