admin管理员组

文章数量:1122847

【RP

文章目录

  • 音频基础
  • 前面
  • 一、常用命令/软件
    • 1.1 aplay 命令播放音频文件
    • 1.2 amixer 命令
      • alsamixer与amixer的区别
    • 1.3 arecord 命令
  • 二、开发板
    • 命令行录音测试
    • 修改配置文件
    • 使用rkmedia例程录音与播放
    • 配置技巧

要想使用以下这些命令需要安装alsa-lib和alsa-utils库。

音频基础

(1)采样频率(sample rate):录音设备在一秒钟内对声音信号的采样次数。
(2)通道数(channel):分为单声道mono;立体声stereo。两个声道,说明只有左右两边有声音传过来。
(3)比特率(bitrate):也叫码率。针对编码格式,表示压缩编码后每秒的音频数据量大小。计算公式:比特率 = 采样率 x 采样精度 x 声道数。单位kbps。
(4)量化位数:指每个采样点里传输的数字信号次数,一般有8位、16位、32位等。S3C2440只支持8位和16位。

前面

 在Linux开发板上使用cat /proc/asound/cards命令来查看设备上有什么声卡。
如下是荣品的PRO-RV1126开发板上的声卡,板子上有两个声卡,一个是硅麦的声卡,一个是 rk809 集成的声卡,rockchippdmmica 是硅麦的声卡,为默认声卡,只能录音不能播放,rockchiprk809co 为 rk809 集成的声卡,可以录音和播放,
录音仅限 LANE IN,Loopback 为本地回环声卡。

$ cat /proc/asound/cards0 [rockchippdmmica]: rockchip_pdm-mi - rockchip,pdm-mic-arrayrockchip,pdm-mic-array1 [rockchiprk809co]: rockchip_rk809- - rockchip,rk809-codecrockchip,rk809-codec7 [Loopback       ]: Loopback - LoopbackLoopback 1

使用 amixer 指令可以进行配置声卡。
查看声卡状态,需要指定声卡序号

$ amixer -c1 contents
numid=1,iface=CARD,name='Headphones Jack'; type=BOOLEAN,access=r-------,values=1: values=off
numid=9,iface=MIXER,name='Master Playback Volume'; type=INTEGER,access=rw---RW-,values=2,min=0,max=255,step=0: values=255,255| dBscale-min=-20.00dB,step=0.03dB,mute=0
numid=6,iface=MIXER,name='ADCL Capture Volume'; type=INTEGER,access=rw---R--,values=1,min=0,max=126,step=0: values=126| dBscale-min=-95.00dB,step=0.75dB,mute=0
numid=7,iface=MIXER,name='ADCR Capture Volume'; type=INTEGER,access=rw---R--,values=1,min=0,max=126,step=0: values=126| dBscale-min=-95.00dB,step=0.75dB,mute=0
numid=3,iface=MIXER,name='Capture MIC Path'; type=ENUMERATED,access=rw------,values=1,items=4; Item #0 'MIC OFF'; Item #1 'Main Mic'; Item #2 'Hands Free Mic'; Item #3 'BT Sco Mic': values=1
numid=2,iface=MIXER,name='Playback Path'; type=ENUMERATED,access=rw------,values=1,items=11; Item #0 'OFF'; Item #1 'RCV'; Item #2 'SPK'; Item #3 'HP'; Item #4 'HP_NO_MIC'; Item #5 'BT'; Item #6 'SPK_HP'; Item #7 'RING_SPK'; Item #8 'RING_HP'; Item #9 'RING_HP_NO_MIC'; Item #10 'RING_SPK_HP': values=6
numid=4,iface=MIXER,name='DACL Playback Volume'; type=INTEGER,access=rw---R--,values=1,min=0,max=126,step=0: values=50| dBscale-min=-95.00dB,step=0.75dB,mute=0
numid=5,iface=MIXER,name='DACR Playback Volume'; type=INTEGER,access=rw---R--,values=1,min=0,max=126,step=0: values=50| dBscale-min=-95.00dB,step=0.75dB,mute=0
numid=8,iface=MIXER,name='Digital Capture Volume'; type=INTEGER,access=rw---RW-,values=2,min=0,max=120,step=0: values=120,120| dBscale-min=-30.00dB,step=0.41dB,mute=0

一、常用命令/软件

aplay amixer arecord

1.1 aplay 命令播放音频文件

使用 aplay 可以播放音频文件

$ aplay -h
Usage: aplay [OPTION]... [FILE]...-h, --help              help--version           print current version
-l, --list-devices      list all soundcards and digital audio devices
-L, --list-pcms         list device names
-D, --device=NAME       select PCM by name
-q, --quiet             quiet mode
-t, --file-type TYPE    file type (voc, wav, raw or au)
-c, --channels=#        channels
-f, --format=FORMAT     sample format (case insensitive)
-r, --rate=#            sample rate
-d, --duration=#        interrupt after # seconds
-s, --samples=#         interrupt after # samples per channel
-M, --mmap              mmap stream
-N, --nonblock          nonblocking mode
-F, --period-time=#     distance between interrupts is # microseconds
-B, --buffer-time=#     buffer duration is # microseconds--period-size=#     distance between interrupts is # frames--buffer-size=#     buffer duration is # frames
-A, --avail-min=#       min available space for wakeup is # microseconds
-R, --start-delay=#     delay for automatic PCM start is # microseconds(relative to buffer size if <= 0)
-T, --stop-delay=#      delay for automatic PCM stop is # microseconds from xrun
-v, --verbose           show PCM structure and setup (accumulative)
-V, --vumeter=TYPE      enable VU meter (TYPE: mono or stereo)
-I, --separate-channels one file for each channel
-i, --interactive       allow interactive operation from stdin
-m, --chmap=ch1,ch2,..  Give the channel map to override or follow--disable-resample  disable automatic rate resample--disable-channels  disable automatic channel conversions--disable-format    disable automatic format conversions--disable-softvol   disable software volume control (softvol)--test-position     test ring buffer position--test-coef=#       test coefficient for ring buffer position (default 8)expression for validation is: coef * (buffer_size / 2)--test-nowait       do not wait for ring buffer - eats whole CPU--max-file-time=#   start another output file when the old file has recordedfor this many seconds--process-id-file   write the process ID here--use-strftime      apply the strftime facility to the output file name--dump-hw-params    dump hw_params of the device--fatal-errors      treat all errors as fatal
Recognized sample formats are: S8 U8 S16_LE S16_BE U16_LE U16_BE S24_LE S24_BE U24_LE U24_BE S32_LE S32_BE U32_LE U32_BE FLOAT_LE FLOAT_BE FLOAT64_LE FLOAT64_BE IEC958_SUBFRAME_LE IEC958_SUBFRAME_BE MU_LAW A_LAW IMA_ADPCM MPEG GSM SPECIAL S24_3LE S24_3BE U24_3LE U24_3BE S20_3LE S20_3BE U20_3LE U20_3BE S18_3LE S18_3BE U18_3LE U18_3BE G723_24 G723_24_1B G723_40 G723_40_1B DSD_U8 DSD_U16_LE DSD_U32_LE DSD_U16_BE DSD_U32_BE
Some of these may not be available on selected hardware
The available format shortcuts are:
-f cd (16 bit little endian, 44100, stereo)
-f cdr (16 bit big endian, 44100, stereo)
-f dat (16 bit little endian, 48000, stereo)

-D 参数用于指定音频设备 PCM,以 hwx,x 开头(前 x 表示声卡号,后 x 表示设备号)。

例子:aplay -Dhw:1,0 /tmp/record.wav

  • 声卡号1
  • 设备号0

aplay -l 可以查看所有可以播放音频的声卡。

$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 1: rockchiprk809co [rockchip,rk809-codec], device 0: ff800000.i2s-rk817-hifi rk817-hifi-0 [ff800000.i2s-rk817-hifi rk817-hifi-0]Subdevices: 1/1Subdevice #0: subdevice #0
card 7: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM]Subdevices: 8/8Subdevice #0: subdevice #0Subdevice #1: subdevice #1Subdevice #2: subdevice #2Subdevice #3: subdevice #3Subdevice #4: subdevice #4Subdevice #5: subdevice #5Subdevice #6: subdevice #6Subdevice #7: subdevice #7
card 7: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM]Subdevices: 8/8Subdevice #0: subdevice #0Subdevice #1: subdevice #1Subdevice #2: subdevice #2Subdevice #3: subdevice #3Subdevice #4: subdevice #4Subdevice #5: subdevice #5Subdevice #6: subdevice #6Subdevice #7: subdevice #7

可以看到,这里序号是从 card1 开始,因为 card0 是硅麦声卡,不支持播放录音,如果在没有配置默认声卡的情况下,直接使用 aplay 指令播放会默认选择 card0 ,然后会提示

$ aplay /tmp/record.wav
aplay: main:828: audio open error: No such file or directory

如果已经添加了 /etc/asound.conf 文件的情况下,就可以直接使用 asound.conf 指定的声卡进行播放,比如指定 card 1 device 0

cat /etc/asound.conf
pcm.!default{type hwcard 1device 0
}
ctl.!default{type hwcard 0
}

然后这样就能直接使用 aplay 播放了,相当于执行如下指令

$ aplay -Dhw:1,0 /tmp/record.wav

1.2 amixer 命令

alsamixer与amixer的区别

  • alsamixer是Linux音频框架ALSA工具之一,用于配置音频各个参数;
  • alsamixer是基于文本图形界面的,可以在终端中显示.通过键盘的上下键,左右键等实现音量设置,开关操作等。
  • amixer,是alsamixer的文本模式,即命令行模式,以命令行的形式去配置声卡的各个选项,比如选择音频输入通道是Mic输入,还是Line输入。

命令帮助:

# amixer -h
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-R,--raw-volume Use the raw value (default)-M,--mapped-volume Use the mapped volumeAvailable commands:scontrols       show all mixer simple controlsscontents       show contents of all mixer simple controls (default command)sset sID P      set contents for one mixer simple controlsget sID        get contents for one mixer simple controlcontrols        show all controls for given cardcontents        show contents of all controls for given cardcset cID P      set control contents for one controlcget cID        get control contents for one control

常用的命令:

  • amixer controls 用于查看音频系统提供的操作接口
  • amixer contents 用于查看接口配置参数
  • amixer cget + 接口函数
  • amixer cset + 接口函数 + 设置值

 通常是先用cget/sget命令去查看接口,然后再用cset/sset命令设置所要设置的值;sget/sset相当于简化版,实现简单的参数设置,一般情况下使用scontrols ,scontents即可满足需求;

# amixer -c1 contents
numid=1,iface=CARD,name='Headphones Jack'; type=BOOLEAN,access=r-------,values=1: values=on
numid=9,iface=MIXER,name='Master Playback Volume'; type=INTEGER,access=rw---RW-,values=2,min=0,max=255,step=0: values=255,255| dBscale-min=-20.00dB,step=0.03dB,mute=0
numid=6,iface=MIXER,name='ADCL Capture Volume'; type=INTEGER,access=rw---R--,values=1,min=0,max=126,step=0: values=126| dBscale-min=-95.00dB,step=0.75dB,mute=0
numid=7,iface=MIXER,name='ADCR Capture Volume'; type=INTEGER,access=rw---R--,values=1,min=0,max=126,step=0: values=126| dBscale-min=-95.00dB,step=0.75dB,mute=0
numid=3,iface=MIXER,name='Capture MIC Path'; type=ENUMERATED,access=rw------,values=1,items=4; Item #0 'MIC OFF'; Item #1 'Main Mic'; Item #2 'Hands Free Mic'; Item #3 'BT Sco Mic': values=1
numid=2,iface=MIXER,name='Playback Path'; type=ENUMERATED,access=rw------,values=1,items=11; Item #0 'OFF'; Item #1 'RCV'; Item #2 'SPK'; Item #3 'HP'; Item #4 'HP_NO_MIC'; Item #5 'BT'; Item #6 'SPK_HP'; Item #7 'RING_SPK'; Item #8 'RING_HP'; Item #9 'RING_HP_NO_MIC'; Item #10 'RING_SPK_HP': values=6
numid=4,iface=MIXER,name='DACL Playback Volume'; type=INTEGER,access=rw---R--,values=1,min=0,max=126,step=0: values=10| dBscale-min=-95.00dB,step=0.75dB,mute=0
numid=5,iface=MIXER,name='DACR Playback Volume'; type=INTEGER,access=rw---R--,values=1,min=0,max=126,step=0: values=10| dBscale-min=-95.00dB,step=0.75dB,mute=0
numid=8,iface=MIXER,name='Digital Capture Volume'; type=INTEGER,access=rw---RW-,values=2,min=0,max=120,step=0: values=120,120| dBscale-min=-30.00dB,step=0.41dB,mute=0
$ amixer -c1 controls
numid=1,iface=CARD,name='Headphones Jack'
numid=10,iface=MIXER,name='Master Playback Volume'
numid=6,iface=MIXER,name='ADCL Capture Volume'
numid=7,iface=MIXER,name='ADCR Capture Volume'
numid=3,iface=MIXER,name='Capture MIC Path'
numid=2,iface=MIXER,name='Playback Path'
numid=4,iface=MIXER,name='DACL Playback Volume'

1.3 arecord 命令

# arecord -h
Usage: arecord [OPTION]... [FILE]...-h, --help              help--version           print current version
-l, --list-devices      list all soundcards and digital audio devices
-L, --list-pcms         list device names
-D, --device=NAME       select PCM by name
-q, --quiet             quiet mode
-t, --file-type TYPE    file type (voc, wav, raw or au)
-c, --channels=#        channels
-f, --format=FORMAT     sample format (case insensitive)
-r, --rate=#            sample rate
-d, --duration=#        interrupt after # seconds
-s, --samples=#         interrupt after # samples per channel
-M, --mmap              mmap stream
-N, --nonblock          nonblocking mode
-F, --period-time=#     distance between interrupts is # microseconds
-B, --buffer-time=#     buffer duration is # microseconds--period-size=#     distance between interrupts is # frames--buffer-size=#     buffer duration is # frames
-A, --avail-min=#       min available space for wakeup is # microseconds
-R, --start-delay=#     delay for automatic PCM start is # microseconds(relative to buffer size if <= 0)
-T, --stop-delay=#      delay for automatic PCM stop is # microseconds from xrun
-v, --verbose           show PCM structure and setup (accumulative)
-V, --vumeter=TYPE      enable VU meter (TYPE: mono or stereo)
-I, --separate-channels one file for each channel
-i, --interactive       allow interactive operation from stdin
-m, --chmap=ch1,ch2,..  Give the channel map to override or follow--disable-resample  disable automatic rate resample--disable-channels  disable automatic channel conversions--disable-format    disable automatic format conversions--disable-softvol   disable software volume control (softvol)--test-position     test ring buffer position--test-coef=#       test coefficient for ring buffer position (default 8)expression for validation is: coef * (buffer_size / 2)--test-nowait       do not wait for ring buffer - eats whole CPU--max-file-time=#   start another output file when the old file has recordedfor this many seconds--process-id-file   write the process ID here--use-strftime      apply the strftime facility to the output file name--dump-hw-params    dump hw_params of the device--fatal-errors      treat all errors as fatal
Recognized sample formats are: S8 U8 S16_LE S16_BE U16_LE U16_BE S24_LE S24_BE U24_LE U24_BE S32_LE S32_BE U32_LE U32_BE FLOAT_LE FLOAT_BE FLOAT64_LE FLOAT64_BE IEC958_SUBFRAME_LE IEC958_SUBFRAME_BE MU_LAW A_LAW IMA_ADPCM MPEG GSM SPECIAL S24_3LE S24_3BE U24_3LE U24_3BE S20_3LE S20_3BE U20_3LE U20_3BE S18_3LE S18_3BE U18_3LE U18_3BE G723_24 G723_24_1B G723_40 G723_40_1B DSD_U8 DSD_U16_LE DSD_U32_LE DSD_U16_BE DSD_U32_BE
Some of these may not be available on selected hardware
The available format shortcuts are:
-f cd (16 bit little endian, 44100, stereo)
-f cdr (16 bit big endian, 44100, stereo)
-f dat (16 bit little endian, 48000, stereo)

查看当前设备上面可以支持录音的声卡可以使用命令:arecord -l

$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 0: rockchippdmmica [rockchip,pdm-mic-array], device 0: ff830000.pdm-dummy_codec dummy-codec-0 [ff830000.pdm-dummy_codec dummy-codec-0]Subdevices: 1/1Subdevice #0: subdevice #0
card 1: rockchiprk809co [rockchip,rk809-codec], device 0: ff800000.i2s-rk817-hifi rk817-hifi-0 [ff800000.i2s-rk817-hifi rk817-hifi-0]Subdevices: 1/1Subdevice #0: subdevice #0
card 7: Loopback [Loopback], device 0: Loopback PCM [Loopback PCM]Subdevices: 8/8Subdevice #0: subdevice #0Subdevice #1: subdevice #1Subdevice #2: subdevice #2Subdevice #3: subdevice #3Subdevice #4: subdevice #4Subdevice #5: subdevice #5Subdevice #6: subdevice #6Subdevice #7: subdevice #7
card 7: Loopback [Loopback], device 1: Loopback PCM [Loopback PCM]Subdevices: 8/8Subdevice #0: subdevice #0Subdevice #1: subdevice #1Subdevice #2: subdevice #2Subdevice #3: subdevice #3Subdevice #4: subdevice #4Subdevice #5: subdevice #5Subdevice #6: subdevice #6Subdevice #7: subdevice #7
[root@RV1126_RV1109:/mnt/nfs]#
arecord -c 2 -r 44100 -f S16_LE -d 10 record.wav

二、开发板

命令行录音测试

录音失败1,指定声卡1和设备0

arecord -Dhw:1,0 -c 2 -r 44100 -f S16_LE -d 10 /tmp/record.wav

录音失败2,根据开发板文档建议:使用硅麦录音,默认使用硅麦录音(这里是根据/etc/asound.conf文件里面的应该是Line in麦克风),不指定声卡。

arecord -c 2 -r 44100 -f S16_LE -d 10 /tmp/record.wav

录音成功:
指定声卡0,设备0。

arecord -Dhw:0,0 -c 2 -r 44100 -f S16_LE -d 10 /tmp/record.wav

修改配置文件

从上面的测试可以知道,-Dhw:1,0是无法录音的,而默认的声卡配置文件/etc/asound.conf里面配置的就是card 1, device 0,所以运行默认声卡录音指令arecord -c 2 -r 44100 -f S16_LE -d 10 /tmp/record.wav是不成功的!
文件/etc/asound.conf

pcm.!default{type hwcard 1device 0}
ctl.!default{type hwcard 0}

改成

pcm.!default{type hwcard 0device 0}
ctl.!default{type hwcard 0}

我再运行arecord -c 2 -r 44100 -f S16_LE -d 10 /tmp/record.wav就可以正常录音了!但是播放音频却又失败了

$ aplay /tmp/record.wav
aplay: main:828: audio open error: No such file or directory

原因是:

card0 是硅麦声卡,不支持播放录音,使用card0录音(板载了硅麦),播放音频需要指定card1来播放。
card1 是Line in,插上Line in话筒是可以录音的,现在没有插所以是无法录音。

$ aplay -Dhw:1,0 /tmp/record.wav
Playing WAVE '/tmp/record.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo

使用rkmedia例程录音与播放

音频录制,录制默认输出为 /tmp/ai.pcm ,默认使用pdm数字硅麦,录音直接执行

$ rkmedia_ai_test

指定使用 rk809 声卡执行录音

$ rkmedia_ai_test -d default:CARD=rockchiprk809co

指定使用硅麦声卡执行录音

$ rkmedia_ai_test -d sysdefault:CARD=rockchippdmmica

音频播放,需要指定声卡1才能播放,因为默认声卡为 pdm 数字硅麦声卡,没有音频输出功能。

$ rkmedia_ao_test -d sysdefault:CARD=rockchiprk809co -i /tmp/ai.pcm

配置技巧

根据控制信息设定声卡状态

$ amixer -c 1 controls
numid=1,iface=CARD,name='Headphones Jack'
numid=10,iface=MIXER,name='Master Playback Volume'
numid=6,iface=MIXER,name='ADCL Capture Volume'
numid=7,iface=MIXER,name='ADCR Capture Volume'
numid=3,iface=MIXER,name='Capture MIC Path'
numid=2,iface=MIXER,name='Playback Path'
numid=4,iface=MIXER,name='DACL Playback Volume'
numid=5,iface=MIXER,name='DACR Playback Volume'
numid=11,iface=MIXER,name='Digital Capture Volume'

使用命令amixer -c 1 cgetamixer -c 1 cset后面接拷贝上面的某一项就可以了。

a) 设定指定声卡可控属性信息

$ amixer -c 1 cget numid=1,iface=MIXER,name='Playback Path'
numid=1,iface=MIXER,name='Playback Path'
; type=ENUMERATED,access=rw------,values=1,items=11
; Item #0 'OFF'
; Item #1 'RCV'
; Item #2 'SPK'
; Item #3 'HP'
; Item #4 'HP_NO_MIC'
; Item #5 'BT'
; Item #6 'SPK_HP'
; Item #7 'RING_SPK'
; Item #8 'RING_HP'
; Item #9 'RING_HP_NO_MIC'
; Item #10 'RING_SPK_HP'
: values=6

b) 获取指定声卡可控属性信息

$ amixer -c 1 cset numid=1,iface=MIXER,name='Playback Path' 4
numid=1,iface=MIXER,name='Playback Path'
; type=ENUMERATED,access=rw------,values=1,items=11
; Item #0 'OFF'
; Item #1 'RCV'
; Item #2 'SPK'
; Item #3 'HP'
; Item #4 'HP_NO_MIC'
; Item #5 'BT'
; Item #6 'SPK_HP'
; Item #7 'RING_SPK'
; Item #8 'RING_HP'
; Item #9 'RING_HP_NO_MIC'
; Item #10 'RING_SPK_HP'
: values=4

本文标签: RP