【全志T113-S3_100ask】9-音频输入与输出
- 背景
- (一)amixer 的使用
- (二)amixer 设置音频输出参数
- (三)使用 aplay 播放音乐
- (四)使用 arecord 录音
背景
之前查看系统命令,发现有个aplay命令,然后尝试使用 aplay -Dhw:audiocodec -vvv gz.wav播放音频,然后报错 aplay: pcm_write:2127: write error: Input/output error
# aplay -Dhw:audiocodec gz.wav
Playing WAVE 'gz.wav' : [ 46.786665] [SNDCODEC][sunxi_card_hw_params][620]:stream_flag: 0
Signed 16 bit Little Endian, Rate 16000 Hz, Mono
aplay: pcm_write:2127: write error: Input/output error
然后就不会了。
8月2日100ask出了简单的教程,是需要先使用tinymix 设置参数的,但是。-bash: tinymix: command not found
我又不想再去编译安装 tinymix ,发现有个 amixer 命令,查阅资料成功配置,播放了音乐。
(一)amixer 的使用
1、查看用法
# 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 controlAvailable advanced commands:sevents show the mixer events for simple controlsevents show the mixer events for simple controls
有点复杂看不懂。。。查阅资料。
2、常用命令
本节参考 https://blog.csdn.net/meirixuexi/article/details/71513007
amixer controls 用于查看音频系统提供的操作接口
amixer contents 用于查看接口配置参数
amixer cget + 接口函数
amixer cset + 接口函数 + 设置值
解释:比如想要设置主音量,第一次使用的话先得查询一下接口函数的配置参数,但是又不知道接口叫什么,先查询所有接口
# amixer controls
numid=17,iface=MIXER,name='Headphone volume'
numid=30,iface=MIXER,name='Headphone Switch'
numid=12,iface=MIXER,name='FMINL gain volume'
numid=13,iface=MIXER,name='FMINR gain volume'
numid=2,iface=MIXER,name='ADC1 ADC2 swap'
numid=24,iface=MIXER,name='ADC1 Input FMINL Switch'
numid=25,iface=MIXER,name='ADC1 Input LINEINL Switch'
numid=23,iface=MIXER,name='ADC1 Input MIC1 Boost Switch'
numid=6,iface=MIXER,name='ADC1 volume'
...省略...
找到了Headphone volume,意思是耳机音量,因为我们是使用3.5mm接口的,所以这一项和我们想要的很像。
使用 amixer cget + 接口函数查询现在的参数,如:
# amixer cget numid=17,iface=MIXER,name='Headphone volume'numid=17,iface=MIXER,name='Headphone volume'; type=INTEGER,access=rw---R--,values=1,min=0,max=7,step=0: values=4| dBscale-min=-42.00dB,step=6.00dB,mute=0
value = 4,最大值为7
修改音量值为6:
# amixer cset numid=17,iface=MIXER,name='Headphone volume' 6numid=17,iface=MIXER,name='Headphone volume'; type=INTEGER,access=rw---R--,values=1,min=0,max=7,step=0: values=6| dBscale-min=-42.00dB,step=6.00dB,mute=0
(二)amixer 设置音频输出参数
经过查询,之所以出现 aplay: pcm_write:2127: write error: Input/output error 错误是因为没有输出设备。
# amixer cget numid=30,iface=MIXER,name='Headphone Switch'numid=30,iface=MIXER,name='Headphone Switch'; type=BOOLEAN,access=rw------,values=1: values=off
耳机开关的输出是 off !!!
将其开启:
# amixer cset numid=30,iface=MIXER,name='Headphone Switch' onnumid=30,iface=MIXER,name='Headphone Switch'; type=BOOLEAN,access=rw------,values=1: values=on
使用 aplay 可以从耳机输出音乐了!!
(三)使用 aplay 播放音乐
1、配置音频输出渠道:
# amixer cset numid=30,iface=MIXER,name='Headphone Switch' onnumid=30,iface=MIXER,name='Headphone Switch'; type=BOOLEAN,access=rw------,values=1: values=on
2、设置音量1:
# amixer cset numid=17,iface=MIXER,name='Headphone volume' 6numid=17,iface=MIXER,name='Headphone volume'; type=INTEGER,access=rw---R--,values=1,min=0,max=7,step=0: values=6| dBscale-min=-42.00dB,step=6.00dB,mute=0
设置音量2:
# amixer cset numid=5,iface=MIXER,name="PCM Volume" 170numid=5,iface=MIXER,name='DAC volume'; type=INTEGER,access=rw---R--,values=2,min=0,max=255,step=0: values=170,170| dBscale-min=-119.25dB,step=0.75dB,mute=0
3、播放音乐:
# aplay -Dhw:audiocodec -vvv piao.wavPlaying WAVE 'piao.wav' : [ 228.205229] [SNDCODEC][sunxi_card_hw_params][620]:stream_flag: 0
Signed 16 bit Little Endian, Rate 44100 Hz, Stereo
Hardware PCM card 0 'audiocodec' device 0 subdevice 0
Its setup is:stream : PLAYBACKaccess : RW_INTERLEAVEDformat : S16_LEsubformat : STD
...省略...
实测:
(四)使用 arecord 录音
配置音频输入渠道:
amixer cset numid=29,iface=MIXER,name='ADC3 Input MIC3 Boost Switch' on
amixer cset numid=31,iface=MIXER,name='HpSpeaker Switch' on
录音
arecord -Dhw:audiocodec -f S24_LE -r 16000 -c 1 -d 10 -vv ./in.wav
解释:使用 设备audiocodec
录制 格式为S24_LE
输入比特率为16000
通道1
时间为10s
保存在 ./in.wav
更多内容\查看帮助 arecord -h
实测: