歡迎您光臨本站 註冊首頁

Ubuntu下創新筆記本外置pcmcia音效卡自動切換

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

前幾日淘了塊創新的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不存在,自己創建一個即可.


[火星人 ] Ubuntu下創新筆記本外置pcmcia音效卡自動切換已經有740次圍觀

http://coctec.com/docs/linux/show-post-57989.html