接下来我们需要一个脚本在适当的时候改变上面的链接,脚本是从ALSA Wiki上处理usb外接声卡转换的脚本改写而来的,脚本switch_default_sound:
代码:
#!/bin/bash
# Name: switch_default_sound
# Last modified: 2009.03.16
# use to switch the asound.conf file
#
# because we hope to restore the sound card levels as well, but udev
# will hang when waiting for this script to finish,
# so we call restore_alsa_sound as daemon, which wait and try to restore
# sound level after udev finish, and alsa make the card ready.
usage(){
echo usage: `basename $0` [`ls /etc/alsa/ -1 | sed -e 's:/etc/alsa/::g' -e 's/asound.//g'`]
}
if [ -n $1 ] && [ -e /etc/alsa/asound."$1" ];
then
ln -sf /etc/alsa/asound."$1" /etc/asound.conf
# we need to call restore_alsa_sound as daemon, so that, we can
# return to udev, and leave rest to restore_alsa_sound
restore_alsa_sound "$1" &
else
usage
fi
这个脚本除了完成asound.conf的链接工作外,还将调用restore_alsa_sound完成音量重置的工作(我还用它重启mpd)。因为 udev在调用switch_default_sound的时候是停在那里等它返回的,而在udev完成设备添加前alsa是得不到声卡信息的,音量恢复就无法完成,而我们在switch_default_sound中用while来等,这样udev就死等在那里了。为了避免这样的问题,我们需要用 daemon的形式调用restore_alsa_sound,把剩下的工作交给它。
代码:
#!/usr/bin/env Python
# Name: restore_alsa_sound
# Last Modified: 2009.03.16
# use to retore the alsa sound level
from sys import argv
from os import system as os_system
from time import sleep
if argv[1] == "audigy2":
retry = 0
while ( retry <= 10):
if os_system ("alsactl restore Audigy2") == 0:
break
sleep(0.5)
os_system ("/etc/init.d/mpd restart")
注意, alsactl restore
Audigy2中的Audigy2是我外置声卡的名字,你得换成你自己的。另外,如果你没有用mpd的话,就把最后一行去掉。
接着,把switch_default_sound和restore_alsa_sound放到 /usr/local/sbin下,并chmod 755 它。