Android中使用ALSA声卡

 

Android中使用ALSA声卡

首先,cd到Android源码树根目录下:

cd /home/figo/android/Android-2.0

从Android主页下载ALSA声卡的相关源码:

git clone git://android.git.kernel.org/platform/external/alsa-lib.git

git clone git://android.git.kernel.org/platform/external/alsa-utils.git

git clone git://android.git.kernel.org/platform/hardware/alsa_sound.git

这样下载完之后的各个包将会自动放到合适的地方。

 第二步,修改自己板子的BoardConfig.mk,使板子使用ALSA声卡,如下:
#HAVE_HTC_AUDIO_DRIVER := true
#BOARD_USES_GENERIC_AUDIO := true
BOARD_USES_ALSA_AUDIO := true
BUILD_WITH_ALSA_UTILS := true

如果是使用我们提供的Android-2.0那么这个BoardConfig.mk应该位于Android-2.0/build/target/board/generic

第三步,重新编译Android

第四步,在制作出来的根文件系统中添加/system/etc/asound.conf脚本配置声卡的工作参数,内容如下:
# #
# # Mixer devices
# #
ctl.AndroidPlayback {
        type hw
        card 0 # Can replace with drivers name from /proc/asound/cards
}
ctl.AndroidRecord {
        type hw
        card 0
}

# #
# # Playback devices
# #
pcm.AndroidPlayback {
                type hw
                card 0
                device 0
}

pcm.AndroidPlayback_Speaker {
                type hw
                card 0
                device 0
}

pcm.AndroidPlayback_Speaker_normal {
                type hw
                card 0
                device 0
}

pcm.AndroidPlayback_Speaker_ringtone {
                type hw
                card 0
                device 0
}

pcm.AndroidPlayback_Speaker_incall {
                type hw
                card 0
                device 0
}

pcm.AndroidPlayback_Earpiece {
                type hw
                card 0
                device 0
}
               
pcm.AndroidPlayback_Earpiece_normal {
                type hw
                card 0
                device 0
}

pcm.AndroidPlayback_Earpiece_ringtone {
                type hw
                card 0
                device 0
}

pcm.AndroidPlayback_Earpiece_incall {
                type hw
                card 0
                device 0
}

pcm.AndroidPlayback_Bluetooth {
                type hw
                card 0
                device 0
}
               
pcm.AndroidPlayback_Bluetooth_normal {
                type hw
                card 0
                device 0
}

pcm.AndroidPlayback_Bluetooth_ringtone {
                type hw
                card 0
                device 0
}

pcm.AndroidPlayback_Bluetooth_incall {
                type hw
                card 0
                device 0
}

pcm.AndroidPlayback_Headset {
                type hw
                card 0
                device 0
}
               
pcm.AndroidPlayback_Headset_normal {
                type hw
                card 0
                device 0
}

pcm.AndroidPlayback_Headset_ringtone {
                type hw
                card 0
                device 0
}

pcm.AndroidPlayback_Headset_incall {
                type hw
                card 0
                device 0
}

pcm.AndroidPlayback_Bluetooth-A2DP {
                type hw
                card 0
                device 0
}
               
pcm.AndroidPlayback_Bluetooth-A2DP_normal {
                type hw
                card 0
                device 0
}

pcm.AndroidPlayback_Bluetooth-A2DP_ringtone {
                type hw
                card 0
                device 0
}

pcm.AndroidPlayback_Bluetooth-A2DP_incall {
                type hw
                card 0
                device 0
}

pcm.AndroidRecord {
                type hw
                card 0
                device 0
}

pcm.AndroidRecord_Microphone {
                type hw
                card 0
                device 0
}

 

 

 

 

 

alsa 音频路径的问题:
在sound/soc/codecs目录中有很多音频codec的codec驱动,我使用的是wm9713,AP是s3c6410;这里个驱动文件中定义了很多widget和control,alsa在playback或record的时候,sound/soc/soc-dapm.c中的dapm_power_widgets函数会根据“配置情况”打开相应的widget,搭建一个完整的音频路径,只要该路径搭建成功,就可以正常工作;
sound/soc/codecs/wm9713.c中的audio_map[]就是一个wm9713的路由表,根据wm9713手册中的Audio Paths Overview可以选择自己需要的音频路径,在audio_map[]中测试一下,看audio_map中是否支持这种路径。
alsa 的配置
alsa音频的调试最主要的是alsa的配置。alsa使用amixer命令打开audio_map[]中的开关(control/switch)和其它一些controls,这些control设置后,使用aplay/arecord的时候即可搭建正确的路径,实现播放和录音。
比如我在调试的时候,在不用amixer控制时(默认状态),arecord可以正确录音,使用sound/soc/soc-dapm.c中的dump_dapm函数dump出的路径是正确的;而aplay的时候,dump_dapm出来的路径是错误的,原因是默认设置里没有打开playback的开关(switch),运行如下命令即可正确playback:
alsa_amixer cset numid=4,iface=MIXER,name='Headphone Playback Switch' 1
alsa_amixer cset numid=93,iface=MIXER,name='Left Headphone Out Mux' 2
alsa_amixer cset numid=34,iface=MIXER,name='Out3 Playback Switch'1
alsa_amixer cset numid=95,iface=MIXER,name='Left Speaker Out Mux' 4
alsa_amixer cset numid=94,iface=MIXER,name='Right Speaker Out Mux' 2
alsa_amixer cset numid=91,iface=MIXER,name='Out 3 Mux' 2
alsa_amixer cset numid=81,iface=MIXER,name='Left HP Mixer PCM Playback Swit' 1
alsa_amixer cset numid=75,iface=MIXER,name='Right HP Mixer PCM Playback Swi' 1
alsa_amixer cset numid=3,iface=MIXER,name='Headphone Playback Volume' 26
alsa_amixer cset numid=36,iface=MIXER,name='Out3 Playback Volume' 48
其实就是打开playback路径需要的开关,dapm_power_widgets会自动把这些开关连接的widget连接起来,构成一个播放路径。
Android中alsa的配置:
Andriod中使用alsa-lib,也需要对配置音频路径。配置方法有两个:
1、在AudioHardwareALSA.cpp中的doRouting中使用system函数调用amixer进行配置
system("alsa_amixer cset numid=4,iface=MIXER,name='Headphone Playback Switch' 1");
2、编写asound.conf文件,AudioHardwareALSA.cpp中的ALSAMixer::ALSAMixer对象初始化的时候会通过alsa-lib的conf.c文件中的函数读取/etc/asound.conf文件,获取配置信息,对codec进行配置。

alsa的一些命令
alsa_amixer该命令配置主要配置音频codec的mixer开关、mux对路选择、volume值等;
alsa_amixer --help
alsa_amixer contents
alsa_amixer contents
numid=30,iface=MIXER,name='Headphone Playback ZC Switch'
; type=BOOLEAN,access=rw------,values=2
: values=off,off
numid=4,iface=MIXER,name='Headphone Playback Switch'
; type=BOOLEAN,access=rw------,values=2
: values=off,off
numid=3,iface=MIXER,name='Headphone Playback Volume'
; type=INTEGER,access=rw------,values=2,min=0,max=31,step=0
: values=31,31
numid=6,iface=MIXER,name='PCM Playback Volume'
; type=INTEGER,access=rw------,values=2,min=0,max=31,step=0
: values=23,23
numid=5,iface=MIXER,name='Line In Volume'
; type=INTEGER,access=rw------,values=2,min=0,max=31,step=0
: values=23,23
numid=7,iface=MIXER,name='Mic 1 Volume'
; type=INTEGER,access=rw------,values=1,min=0,max=31,step=0
: values=23
numid=8,iface=MIXER,name='Mic 2 Volume'
; type=INTEGER,access=rw------,values=1,min=0,max=31,step=0
: values=23
numid=85,iface=MIXER,name='Mic A Source'
; type=ENUMERATED,access=rw------,values=1,items=3
; Item #0 'Mic 1'
; Item #1 'Mic 2 A'
; Item #2 'Mic 2 B'
: values=0



alsa_amixer controls
numid=30,iface=MIXER,name='Headphone Playback ZC Switch'
numid=4,iface=MIXER,name='Headphone Playback Switch'
numid=3,iface=MIXER,name='Headphone Playback Volume'
numid=6,iface=MIXER,name='PCM Playback Volume'
numid=5,iface=MIXER,name='Line In Volume'
numid=7,iface=MIXER,name='Mic 1 Volume'
numid=8,iface=MIXER,name='Mic 2 Volume'
numid=85,iface=MIXER,name='Mic A Source'
numid=84,iface=MIXER,name='Mic B Source'
numid=9,iface=MIXER,name='Mic Boost (+20dB) Switch'
numid=10,iface=MIXER,name='Mic Headphone Mixer Volume'
numid=47,iface=MIXER,name='Aux Playback Headphone Volume'
numid=48,iface=MIXER,name='Aux Playback Master Volume'
numid=49,iface=MIXER,name='Aux Playback Mono Volume'
numid=67,iface=MIXER,name='Mono Mixer Aux Playback Switch'
numid=69,iface=MIXER,name='Mono Mixer Bypass Playback Swit'
numid=70,iface=MIXER,name='Mono Mixer Mic 1 Sidetone Switc'
numid=71,iface=MIXER,name='Mono Mixer Mic 2 Sidetone Switc'
numid=65,iface=MIXER,name='Mono Mixer PC Beep Playback Swi'
numid=68,iface=MIXER,name='Mono Mixer PCM Playback Switch'
numid=66,iface=MIXER,name='Mono Mixer Voice Playback Switc'
alsa_alsactl store:该命令生成/etc/asound.state文件,该文件显示当请codec的状态,可以根据该文件检查codec的状态是否正确。
# cat /etc/asound.state
state.SMDK6400 {
control.1 {
comment.access 'read write'
comment.type INTEGER
comment.count 2
comment.range '0 - 31'
iface MIXER
name 'Speaker Playback Volume'
value.0 31
value.1 31
}
control.2 {
comment.access 'read write'
comment.type BOOLEAN
comment.count 2
iface MIXER
name 'Speaker Playback Switch'
value.0 false
value.1 false
}
control.3 {
comment.access 'read write'
comment.type INTEGER
comment.count 2
comment.range '0 - 31'
iface MIXER
name 'Headphone Playback Volume'
value.0 26
value.1 26
}
control.4 {
comment.access 'read write'
comment.type BOOLEAN
comment.count 2
iface MIXER
name 'Headphone Playback Switch'
value.0 true
value.1 true
}
control.5 {
comment.access 'read write'
comment.type INTEGER
comment.count 2
comment.range '0 - 31'
iface MIXER
name 'Line In Volume'
value.0 23
value.1 23
}
使用amixer命令可以参照alsa_amixer contents的内容;编写asound.conf,可以参照alsa_alsactl生成的/etc/asound.state。
下面是我的asound.conf的一段,其它的pcm.AndroidPlayback_xxx可以写法一样,就是hook_argsp[]中的内容根据自己的情况设置。

##
## Mixer Devices
##
ctl.AndroidPlayback {
type hw
card 0 # Can replace with driver"s name from /proc/asound/cards
}
ctl.AndroidRecord {
type hw
card 0 # Can replace with driver"s name from /proc/asound/cards
}
##
## Playback Devices
##
pcm.AndroidPlayback {
type hooks
slave.pcm {
type hw
card 0
device 0 # Must be of type "digital audio playback"
}
hooks.0 {
type ctl_elems
hook_args [
{
name 'Master Playback Switch'
value true
}
{
name 'Master Playback Volume'
value.0 51
value.1 51
}
{
name 'Phone Playback Switch'
value false
}
{
name 'Phone Playback Volume'
value.0 0
value.1 0
}
{
name 'Mic Playback Switch'
value false
}
{
name 'Mic Playback Volume'
value.0 0
value.1 0
}
{
name 'Mic Boost (+20dB)'
value false
}
{
name 'Line Playback Switch'
value false
}
{
name 'Line Playback Volume'
value.0 0
value.1 0
}
{
name 'PCM Playback Switch'
value true
}
{
name 'PCM Playback Volume'
value.0 51
value.1 51
}
{
name 'Capture Source'
value.0 Mic
value.1 Mic
}
{
name 'Capture Switch'
value true
}
{
name 'Capture Volume'
value.0 0
value.1 0
}
]
}
}

 

Linux的声卡驱动中ALSA与OSS的区别和简单流程介

Advanced Linux Sound Architecture 的简称为 ALSA,译成中文的意思是 Linux 高级声音体系,ALSA不仅仅是包括对声卡的支持和驱动;

它的特征如下:

1、对所有音频接口的高效支持,从普通用户的声卡到专业级别多路音频设备;

2、声卡驱动完全模块化设计;

3、SMP and thread-safe design.

4、开发库(alsa-lib) 为程序设计提供了简单、方便,并且拥有有高级的效果和功能;

5、支持旧版本的OSS API 结口,能为大多数的OSS应用程序提供兼容;OSS是一个商业性的驱动,OSS有一个简装本的代码已经移入内核和ALSA,其中alsa-oss就是;OSS公司据说目前已经并不存在了;我们没有必要用OSS 公司提供的商业版本;用ALSA和OSS简装版足够;

如何查看硬件芯片;

  在Linux操作系统中,所有的硬件都是以芯片组来区分的,品牌并不是最重要的;硬件最重要的标识是芯片组;所以您在讨论区求助的时候,只说硬件品牌,而不提供芯片组,大家是帮助不了您的,切记;

  我们查看硬件的芯片组是的命令是 lspci -v 或者是dmesg,由于dmesg输出的信息不太多,不够直观;所以经常用的还是lspci -v ;也可以用lshal 获取;最方便的还是lspci -v;初学者还是用 lspci -v 更好一点;

  代码:

  [root@localhost beinan]# lspci -v

  我们运行lspci -v 后,如果查看声卡芯片组,发会现有类似下面的一段;

    [root@localhost beinan]# lspci -v

Multimedia audio controller: Intel Corporation 82801DB/DBL/DBM (ICH4/ICH4-L/ICH4-M)

AC'97 Audio Controller (rev 03)

Subsystem: Hewlett-Packard Company: Unknown device 3080

  2.6.11-1.1369_FC4 i686 i686 i386

  上面的表示的是系统的内核版本,处理器架构等;lsmod 查看已经加载的内核模块;

如果一个设备在内核的编译中是以模块方式加载的,主要是通过lsmod 来查看;

  [root@localhost beinan]# lsmod

  可能初学Linux的弟兄会问,内核的module存放在哪里,我们能不能自己来手工加载模块;内核的模块放在 /lib/modules/内核的版本/kernel/ ,比如在Fedora 4.0 中,我们可以看到如下的:

  [root@localhost beinan]# ls /lib/modules/2.6.11-1.1369_FC4/kernel/

  arch/  crypto/ drivers/ fs/   lib/   net/   sound/其中,sound 就是声卡模块存放目录,大多数的硬件驱动放在 drivers目录,fs 是文件系统模块的目录;net是与网络有关的存放目录,比如一些网络协议支持的模块、防火墙支持的模块等;arch 是cpu方面 ... ...

  如果我们想自己加载模块,就到这些目录中查看相应模块的信息,然后用 modprobe 来加载;

  [root@localhost beinan]# modinfo snd-intel8x0

  查看一个模块的信息,我们用 modinfo 来查看,所要查看的模块不要带 .ko 或者.o 之类的;比如查看到类似下面的信息;

  description: Intel 82801AA,82901AB,i810,i820,i830,i840,i845,MX440; SiS 7012; Ali 5455

  通过这个我们足可以知道这是Intel 集成声卡,通过lspci -v 得到的声卡信息,感觉他们很相近;所以就能尝试用这个模块来驱动;

  [root@localhost beinan]# modprobe snd-intel8x0

  用 modprobe 加载了模块,然后我们通过 lsmod 就能看到了;对于声卡模块是这么加载的,其它设备的驱动模块也是如此。

 

 

XRUN Debug

From AlsaProject

Jump to: navigation, search

This page describes the build-in ALSA driver tool to debug PCM ring buffer positions.

[edit] /proc/asound/card#/pcm0p/xrun_debug

Replace '#' with your card number (usually 0). This proc file can enable various debugging tools. The CONFIG_SND_PCM_XRUN_DEBUG, CONFIG_SND_VERBOSE_PROCFS, CONFIG_SND_DEBUG options must be enabled in your kernel (if xrun_debug proc file is present - this feature is enabled).

 1   Basic debugging - show xruns in ksyslog interface
 2   Dump stack - dump stack for basic debugging
 4   Jiffies check - compare the position with kernel jiffies (a sort of in-kernel monotonic clock),
     show what's changed when basic debugging is enabled
 8   Dump positions on each period update call
 16  Dump positions on each hardware pointer update call
 32  Enable logging of last 10 ring buffer positions
 64  Show the last 10 ring buffer position only once (when first error situation occured)

To enable more features just do sum values of above (for example 1+2=3).

Some good value combinations:

 # Enable basic debugging and dump stack
 # Usefull to just see, if PCM stream is stopped for a reason (usually wrong audio process timing from scheduler)
 echo 3 > /proc/asound/card0/pcm0p/xrun_debug
 # Enable basic debugging, do jiffies check and enable one shot dump of last 10 ring buffer positions
 # Usefull, when the position is broken only after some of time (to reduce ksyslog messages)
 echo 101 > /proc/asound/card0/pcm0p/xrun_debug
 # Enable basic debugging, do jiffies check and dump position on each period and hardware pointer update calls
 # Usefull when the lowlevel (specific) hardware driver is somehow broken
 echo 29 > /proc/asound/card0/pcm0p/xrun_debug

 

你可能感兴趣的:(Android开发)