分享

ALSA --- amixer控制声卡驱动实现Line-in功能

 arm_embed 2012-11-23
 

ALSA --- amixer控制声卡驱动实现Line-in功能

分类: ARM 491人阅读 评论(1) 收藏 举报

开发环境:Ubuntu12.04    开发板:OK6410,Linux3.0

alsamixer是Linux 音频架构ALSA工具的其中一个,用于配置音频的各个参数。

alsamixer是基于文本下的图形界面的,可以通过键盘的上下键,左右键等,很方便地设置需要的音量,开关某个switch(开关)等等操作,下图是在Ubuntu12.04系统上图形配置界面,操作非常的简单,这里使用的是Ubuntu12.04的Line-in功能(实现立体声功能):

声音播放输出通道设置:

声音输入设备通道设置:


笔者测试Ubuntu12.04的Line-in功能,所以选择音频输入通道为 Line。

amixer,是alsamixer的文本模式,即命令行模式,需要用amixer命令的形式去配置你的声卡的各个选项,可以这么说,你也许会直接修改Linux内核音频驱动源码来满足您的需求,比如选择音频输入通道是Mic输入,还是Line 输入,需要修改WM9714的寄存器来决定,而amixer可以从应用层来修改音频芯片的寄存器值,决定采用Mic输入或者Line输入。这样就大大简化了代码修改的难度,毕竟比直接修改Linux Kernel ALSA会简单些。

对于amixer的使用,你首先需要搞懂你要设置的参数是哪些,然后才可能去了解,如何去配置对应的值,整体来说,相对alsamixer来说,是有点繁琐,下面简要介绍其具体用法:

1.先看看amixer支持哪些命令,大概有哪些功能

amixer --help
Usage: amixer <options> [command]

Available options:
-h,--help       this help
-c,--card N     select the card
-D,--device N   select the device, default 'default'
-d,--debug      debug mode
-n,--nocheck    do not perform range checking
-v,--version    print version of this program
-q,--quiet      be quiet
-i,--inactive   show also inactive controls
-a,--abstract L select abstraction level (none or basic)
-s,--stdin      Read and execute commands from stdin sequentially

Available commands:
scontrols       show all mixer simple controls
scontents       show contents of all mixer simple controls (default command)
sset sID P      set contents for one mixer simple control
sget sID        get contents for one mixer simple control
controls        show all controls for given card
contents        show contents of all controls for given card
cset cID P      set control contents for one control
cget cID        get control contents for one control

2.再看看当前你的音频系统(不同的音频驱动对应不同的内容和操作接口)提供了那些供你使用的接口去操作

关于驱动里面已经提供了多少接口可以去操作,可以用命令:

amixer contents

查看,比如:

[root@FORLINX6410]# ./amixer  controls
numid=49,iface=MIXER,name='Headphone Mixer Aux Playback Volume'
numid=43,iface=MIXER,name='Headphone Mixer Beep Playback Volume'
numid=32,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'

××××××××××××××××××××××××××××××××××××××××××××××××

而对于所有的配置的值,可以通过 amixer contents打印:

[root@FORLINX6410]# ./amixer  contents
numid=49,iface=MIXER,name='Headphone Mixer Aux Playback Volume'
  ; type=INTEGER,access=rw---R--,values=1,min=0,max=7,step=0
  : values=5
  | dBscale-min=-15.00dB,step=3.00dB,mute=0
numid=43,iface=MIXER,name='Headphone Mixer Beep Playback Volume'
  ; type=INTEGER,access=rw---R--,values=1,min=0,max=7,step=0
  : values=5
  | dBscale-min=-15.00dB,step=3.00dB,mute=0
numid=32,iface=MIXER,name='Headphone Playback ZC Switch'
  ; type=BOOLEAN,access=rw------,values=2
  : values=off,off
。。。

3.搞懂如何去设置某个参数

总结起来就是,先要用get系列命令去看懂有哪些接口,然后再去用set系列的命令,去设置对应你所要设置的值。

想要针对某项设置,比如想要设置上面的Line-in输入的音量,‘Line In Volume',即controls中显示的:

numid=5,iface=MIXER,name='Line In Volume'
那么,可以先看看当前的值:

[root@FORLINX6410]# ./amixer cget  numid=5,iface=MIXER,name='Line In Volume'
numid=5,iface=MIXER,name='Line In Volume'
  ; type=INTEGER,access=rw---R--,values=2,min=0,max=31,step=0
  : values=23,23
  | dBscale-min=-34.50dB,step=1.50dB,mute=0
显示最大音量为31,假设想要设置为25,那么就用cset去设置:

amixer cset  numid=5,iface=MIXER,name='Line In Volume'  25
[root@FORLINX6410]# ../amixer cset  numid=5,iface=MIXER,name='Line In Volume'  25
numid=5,iface=MIXER,name='Line In Volume'
  ; type=INTEGER,access=rw---R--,values=2,min=0,max=31,step=0
  : values=25,25
  | dBscale-min=-34.50dB,step=1.50dB,mute=0

[提示]:

同上面介绍的的cget/cset系列命令:

controls        show all controls for given card
contents        show contents of all controls for given card
cset cID P      set control contents for one control
cget cID        get control contents for one control

类似的,还有另外一套sget/sset系列的命令,实现简单的参数设置,一般情况下使用 scontrols ,scontents即可满足您的需求。

scontrols       show all mixer simple controls
scontents       show contents of all mixer simple controls (default command)
sset sID P      set contents for one mixer simple control
sget sID        get contents for one mixer simple control

也是同样做法,比如:

[root@FORLINX6410]# ./amixer  scontrols
Simple mixer control 'Headphone',0
Simple mixer control 'Headphone Mixer Aux',0
Simple mixer control 'Headphone Mixer Beep',0
Simple mixer control 'Headphone Playback ZC',0
Simple mixer control 'Tone',0
Simple mixer control 'Tone Cut-off',0
Simple mixer control 'Bass',0
Simple mixer control 'Bass Control',0
Simple mixer control 'Bass Cut-off',0
Simple mixer control 'PCM',0
Simple mixer control 'Sidetone Mux',0
Simple mixer control 'Line In',0-----------(这里是设置Line-in的音量的参数,同以上设置Line-in音量功能相同)
Simple mixer control 'Mic 1',0
Simple mixer control 'Mic 1 Preamp',0
Simple mixer control 'Mic 2',0
Simple mixer control 'Mic 2 Preamp',0
Simple mixer control 'Mic A Source',0
Simple mixer control 'Mic B Source',0

。。。

Simple mixer control 'Left Capture Source',0 (这项很重要,左声道音频源输入选择)

Simple mixer control 'Right Capture Source',0 (右声道音频源输入选择)
。。。

同理,amixer scontents,可以查看当前所有的值,具体就不在这列举了。

另外,去查看或者配置用sget,比如:

[root@FORLINX6410]# ./amixer  sget  'Left Capture Source',0
Simple mixer control 'Left Capture Source',0
  Capabilities: enum
  Items: 'Mic 1' 'Mic 2' 'Line' 'Mono In' 'Headphone' 'Speaker' 'Mono Out' 'Zh'
  Item0: 'Mic 1'
如果想要修改对应设置,用amixer sset ,具体用法是:

amixer sset  sID(控制字符串)  P(支持的某个值)

其中sID,就是上面的Simple mixer control后面的那个字符串,比如 'Left Capture Source' 而对其设置就是,

[root@FORLINX6410]# ./amixer  sset 'Left Capture Source',0 Line
Simple mixer control 'Left Capture Source',0
  Capabilities: enum
  Items: 'Mic 1' 'Mic 2' 'Line' 'Mono In' 'Headphone' 'Speaker' 'Mono Out' 'Zh'
  Item0: 'Line'

然后再把右声道输入源设置为Line-in:

[root@FORLINX6410]# ./amixer  sset 'Right Capture Source',0 Line
Simple mixer control 'Right  Capture Source',0
  Capabilities: enum
  Items: 'Mic 1' 'Mic 2' 'Line' 'Mono In' 'Headphone' 'Speaker' 'Mono Out' 'Zh'
  Item0: 'Line'

这样您的设备就可以使用Line-in功能录音了。

执行命令:

[root@FORLINX6410]#./arecord -f cd -c 1 -t wav my-file.wav
Recording WAVE 'my-file.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Mono

开始Line-in录音 ,-c 1代表使用单声道,2 代表使用立体声.

播放刚才录制的声音文件:

.[root@FORLINX6410]#./aplay my-file.wav
Playing WAVE 'my-file.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Mono

OK,声音非常的清晰。

怎么样,使用ALSA提供的工具是不是很容易的控制声卡的参数呢,另外ALSA可以模拟OSS音频架构,目前OK6410开发板 Linux系统Qtopia声音的播放和录音还是使用的OSS架构,所以开发板的内核配置里面勾选上OSS配置。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多