前几日淘了块创新的Audigy2 ZS Notebook声卡,声卡的驱动还算不错,只要内核中开启pcmcia声卡支持为加上emu10k1的模块就好了.但之后设置的问题也来了,目标非常简单,希望声卡插进、拔出的时候,alsa的设置能相应自动变化.
我们可以利用udev来帮我们实现这个目标.
我们需要两个脚本和两个内容略为不同的asound.conf.
先看看两个声卡是啥名字:
代码:
ls -l /proc/asound/
我的机子上是:
代码:
lrwxrwxrwx 1 root root 5 2009-03-16 17:27 Audigy2 -> card2
dr-xr-xr-x 9 root root 0 2009-03-16 17:27 card0
dr-xr-xr-x 5 root root 0 2009-03-16 17:27 card1
dr-xr-xr-x 8 root root 0 2009-03-16 17:27 card2
-r--r--r-- 1 root root 0 2009-03-16 17:27 cards
-r--r--r-- 1 root root 0 2009-03-16 17:27 devices
-r--r--r-- 1 root root 0 2009-03-16 17:27 hwdep
lrwxrwxrwx 1 root root 5 2009-03-16 17:27 I82801DBICH4 -> card0
lrwxrwxrwx 1 root root 5 2009-03-16 17:27 Modem -> card1
dr-xr-xr-x 2 root root 0 2009-03-16 17:27 oss
-r--r--r-- 1 root root 0 2009-03-16 17:27 pcm
dr-xr-xr-x 2 root root 0 2009-03-16 17:27 seq
-r--r--r-- 1 root root 0 2009-03-16 17:27 timers
-r--r--r-- 1 root root 0 2009-03-16 17:27 version
其中板载声卡是 I82801DBICH4,外置的pcmcia声卡是Audigy2.要把名字记下,待会我们需要它们.
接下来要写两个asound.conf来指定外置声卡插拔情况下的alsa设置.先
代码:
mkdir /etc/alsa
cp /etc/asound.conf /etc/alsa/asound.ac97
mv /etc/asound.conf /etc/alsa/asound.audigy2
,asound.ac97是没有插外置声卡时用的,asound.audigy2就是外置声卡插上时用的.
然后在asound.ac97中加入:
代码:
# onboard
pcm.ac97 {
type plug
slave.pcm "dmix"
}
ctl.ac97 {
type hw
card I82801DBICH4
}
pcm.!default pcm.ac97
ctl.!default ctl.ac97
在asound.audigy2中加入
代码:
# pcmcia
pcm.audigy2hw {
type hw
card Audigy2
}
pcm.audigy2 {
type plug
slave.pcm "audigy2hw"
}
ctl.audigy2 {
type hw
card Audigy2
}
# onboard
pcm.ac97 {
type plug
slave.pcm "dmix"
}
ctl.ac97 {
type hw
card I82801DBICH4
}
pcm.!default pcm.audigy2
ctl.!default ctl.audigy2
要注意的是,xxx.ac97中声卡名是我板载声卡的名字,xxx.audigy2则是我外置声卡的名字,你需要替换成你自己的声卡.
这样在不同情况下,我们只要 ln -sf /etc/alsa/asound.xxx /etc/asound.conf就OK了.
接下来我们需要一个脚本在适当的时候改变上面的链接,脚本是从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 它.
,我们需要让udev在插入和拔出声卡的时候调用switch_default_sound,就大功告成了.,我们要写一个udev的rule为我们服务.
先用lspci找到声卡的PCI_ID
代码:
lspci -nn
比如我的是
代码:
02:00.0 Multimedia audio controller [0401]: Creative Labs SB0400 Audigy2 Value [1102:0008]
,其中1102:0008就是了.
接着,我们需要找出相应的udev信息.先把udev的信息导到文件udev.txt中
代码:
udevadm info --export-db > udev.txt
然后找出1102:0008
代码:
grep -C10 1102:0008 udev.txt
在我的电脑上结果是:
代码:
E: PCI_SLOT_NAME=0000:01:0a.0
E: MODALIAS=pci:v00001180d00000476sv000010CFsd00001254bc06sc07i00
P: /devices/pci0000:00/0000:00:1e.0/0000:01:0a.0/0000:02:00.0
E: UDEV_LOG=3
E: DEVPATH=/devices/pci0000:00/0000:00:1e.0/0000:01:0a.0/0000:02:00.0
E: DRIVER=EMU10K1_Audigy
E: PHYSDEVBUS=pci
E: PHYSDEVDRIVER=EMU10K1_Audigy
E: PCI_CLASS=40100
E: PCI_ID=1102:0008
E: PCI_SUBSYS_ID=1102:2001
E: PCI_SLOT_NAME=0000:02:00.0
E: MODALIAS=pci:v00001102d00000008sv00001102sd00002001bc04sc01i00
P: /devices/pci0000:00/0000:00:1e.0/0000:01:0a.1
E: UDEV_LOG=3
E: DEVPATH=/devices/pci0000:00/0000:00:1e.0/0000:01:0a.1
E: DRIVER=yenta_cardbus
E: PHYSDEVBUS=pci
E: PHYSDEVDRIVER=yenta_cardbus
从中可以看到声卡的PCI_SUBSYS_ID是1102:2001.现在我们就有了足够的信息来写udev的rules了.
在/etc/udev/rules.d下建立一个文件75-alsa_conf.rules
代码:
# udev rules file for my Audigy2 ZS Notebook devices (for udev 0.98 version)
# call /usr/local/sbin/switch_default_sound to change the link
# of /etc/asound.conf
#
#
ENV{PCI_ID}=="1102:0008", ENV{PCI_SUBSYS_ID}=="1102:2001",ACTION=="add", RUN ="/usr/local/sbin/switch_default_sound audigy2"
ENV{PCI_ID}=="1102:0008", ENV{PCI_SUBSYS_ID}=="1102:2001",ACTION=="remove", RUN ="/usr/local/sbin/switch_default_sound ac97"
在这个rules中,我们要求udev在PCI_ID为1102:0008且PCI_SUBSYS_ID为1102:2001的设备(就是处置的那块声卡)要被插入时,即ACTION为add时,调用"/usr/local/sbin/switch_default_sound audigy2",在声卡被移除时,即ACTION为remove时调用"/usr/local/sbin/switch_default_sound ac97".
但为了能有效地restore声音,你需要在第一次插入声卡并设置好音量后手动运行一下alsactl store Audigy2.
另外,插拔声卡的时候要确认你没有在放音乐哦
PS:
1.PCI_SUBSYS_ID查不到也不要紧,只要把75-alsa_conf.rules改为:
....................
ENV{PCI_ID}=="1102:0008",ACTION=="add", RUN ="/usr/local/sbin/switch_default_sound audigy2"
ENV{PCI_ID}=="1102:0008",ACTION=="remove", RUN ="/usr/local/sbin/switch_default_sound ac97"
.....................
2.Ubuntu 系统 -》首选项-》音效-》把设备页默认的设备全部改为外置声卡
3./etc/asound.conf不存在,自己创建一个即可.