|
之前买了个 Rapoo 无线耳机,snd_usb_audio 通用驱动即可使用;主板上有 intel 的集成声卡。然而在系统中(debian openbox)居然没有找到设置默认声卡的工具,故写之。单声卡同学请直接无视。
因查找资料时记得看到过 Ubuntu PPA 源里有 asound 的一个脚本工具可以做这个事(原理未知,没有看过内容),故我将之也命名为 asound 吧。初步完成,写得比较粗糙。诸位可随意更改,呵呵。
这个原理就很简单了:调整声卡驱动加载优先级。
用法就更简单了,不带参数直接运行获得声卡信息(id 先找到),然后再:需要支持 正则判断 的 bash 版本。其实是用来判断参数是否是单数字的,直接去除也行,自己用。
发现 bug 请直接跟帖,或 PM,或不屑。多谢。
- #!/bin/bash
- # 26/01/2011
- # - adjust priorities of modules to set the default sound card
- # - need root or sudo to run 'modprobe'
- CONFIG="/tmp/asound.conf"
- CARDS="/proc/asound/cards"
- MODS="/proc/asound/modules"
- function usage {
- echo " Usage: `basename $0` [sound_card_id]"
- exit 1
- }
- [ $# \> 1 ] && usage
- N_CARDS=`sed -ne '/^ \+[0-9]\+/p' $CARDS | wc -l`
- # drivers not ready
- [ $N_CARDS == 0 ] && echo " No sound cards found." && exit 0
- # info of sound cards
- if [ $# == 0 ]
- then
- # detect sound cards
- echo "$N_CARDS card(s) detected:"
- sed -ne '/^ \+[0-9]\+/s/\[.*\]//p' $CARDS
- echo "Current mixer:" `head -1 $CARDS | cut -d':' -f2`
- if [ $N_CARDS \> 1 ]
- then
- echo ""
- echo "To change default card:" "`basename $0` [sound_card_id]"
- fi
- exit 0
- fi
- # NAN
- [[ $# == 1 ]] && [[ ! ($1 =~ [0-9]) ]] && usage
- # id 0
- [ x"$1" == x"0" ] && echo " ID 0 is the default device, so nothing changed." && exit 0
- if [ $1 \> $((N_CARDS-1)) ] || [ $1 \< 0 ]
- then
- echo " device with ID $1 not found."
- exit 1
- fi
- # clean config file
- :>$CONFIG
- for ((i=0;i<$N_CARDS;i++))
- do
- # disabled by default
- inx=-2
- # request
- [ $i == $1 ] && inx=0
-
- awk '{if($1=="'$i'") printf("options %s index=%d\n", $2, "'$inx'")}' $MODS \
- >> $CONFIG
- done
- MODULES=(`awk '{print $2}' $MODS`)
- # if busy
- if grep "running" /proc/asound/timers &> /dev/null
- then
- echo "modules are busy, exit..."
- exit 1
- fi
- # unload modules
- sudo modprobe -q -r ${MODULES[@]}
- # wait
- sleep 1
- # isolate unload and reload stmts to keep the index as it was
- # reload modules
- for mod in ${MODULES[@]}
- do
- sudo modprobe -q $mod -C $CONFIG
- done
- echo " all done!"
- exit 0
复制代码 |
|