Linux音频(3):alsa-lib以及alsa-utils
alsa-lib是为了简化、便利用户空间对ALSA驱动框架声卡编程的开源库。
alsa-utils是alsa的一个工具包,里面包含有声卡测试和音频编辑的工具.
1 alsa-lib
alsa-lib在Buildroot中配置如下,包含一系列库插件:
Target packages ->Libraries ->Audio/Sound ->alsa-lib ->(/dev/snd) directory with ALSA device files ->(all) built PCM plugins ->(all) built control plugins ->aload ->mixer ->pcm ->rawmidi ->hwdep ->seq ->ucm ->alisp ->old-symbols ->alsa-plugins
2 alsa-utils
alsa-utils是基于alsa-lib以及Kernel ALSA的一系列工具包:
Target packages Audio and video applications alsa-utils alsaconf aconnect alsactl alsaloop alsamixer alsaucm alsatplg amidi amixer aplay/arecord aplaymidi arecordmidi aseqdump aseqnet bat iecset speaker-test
2.1 aplay
aplay命令用于播放音频文件,可以使用它来播放 WAV、MP3 等格式的音频文件。aplay支持不同的音频设备,并提供一些选项以调整播放参数。
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--支持Playback的硬件设备列表。 -D, --device=NAME select PCM by name--选择特定的PCM硬件作为播放设备。 -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 recorded for 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 S20_LE S20_BE U20_LE U20_BE 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)
aplay -l显示当前Playback设备:
**** List of PLAYBACK Hardware Devices **** card 0: rockchipes8388 [rockchip-es8388], device 0: dailink-multicodecs ES8323.7-0011-0 [dailink-multicodecs ES8323.7-0011-0] Subdevices: 1/1 Subdevice #0: subdevice #0
选择Card 0 Device 0硬件,立体声方式来播放wav格式音频,对应的设备节点为/dev/snd/pcmC0D0p:
aplay -D hw:0,0 -t wav -v -V stereo topeet_test/05_headphone/1.wav
播放显示如下:
Playing WAVE 'topeet_test/05_headphone/1.wav' : Signed 16 bit Little Endian, Rate 44100 Hz, Stereo--音频格式为16bit 44.1KHz Stereo音源。 Hardware PCM card 0 'rockchip-es8388' device 0 subdevice 0 Its setup is: stream : PLAYBACK access : RW_INTERLEAVED format : S16_LE subformat : STD channels : 2 rate : 44100 exact rate : 44100 (44100/1) msbits : 16 buffer_size : 22052 period_size : 5513 period_time : 125011 tstamp_mode : NONE tstamp_type : MONOTONIC period_step : 1 avail_min : 5513 period_event : 0 start_threshold : 22052 stop_threshold : 22052 silence_threshold: 0 silence_size : 0 boundary : 6207086186423386112 appl_ptr : 0 hw_ptr : 0 +#########################76%|75%#########################+
2.2 arecord
arecord命令用于录制音频,您可以使用它从麦克风、线路输入等音频输入设备中录制声音,并将录制的音频保存为文件。arecord支持不同的音频设备,并提供一些选项以调整录制参数。
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--指定录音Channel数,默认mono,即1。 -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 recorded for 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 S20_LE S20_BE U20_LE U20_BE 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显示Capture硬件设备列表:
**** List of CAPTURE Hardware Devices **** card 0: rockchipes8388 [rockchip-es8388], device 0: dailink-multicodecs ES8323.7-0011-0 [dailink-multicodecs ES8323.7-0011-0] Subdevices: 1/1 Subdevice #0: subdevice #0 card 1: rockchiphdmiin [rockchip,hdmiin], device 0: fddf8000.i2s-dummy_codec hdmiin-dc-0 [fddf8000.i2s-dummy_codec hdmiin-dc-0] Subdevices: 1/1 Subdevice #0: subdevice #0
arecord录音:
arecord -f S16_LE -V stereo -r 48000 -d 10 stereo.wav -v
arecord -d 10 -f cd -r 44100 -c 2 -t wav test.wav
将arecord录音通过管道直接给aplay播放:
arecord -f S16_LE -V stereo -r 48000 | aplay
2.3 amixer
amixer命令用于管理音频设置,可以显示和调整音量、切换通道、静音等。您可以使用amixer来获取和设置音频设备的状态和参数。
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 volume 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--显示某一Card Control的序号、接口类型、名称。 contents show contents of all controls for given card--显示某一个Card Control的详细信息。 cset cID P set control contents for one control cget cID get control contents for one control Available advanced commands: sevents show the mixer events for simple controls events show the mixer events for simple controls
amixer contents查看Control详细信息:
numid=21,iface=MIXER,name='PCM Volume' ; type=INTEGER,access=rw---R--,values=2,min=0,max=192,step=0 : values=181,181 | dBscale-min=-96.00dB,step=0.50dB,mute=1
numid=21,iface=MIXER,name='PCM Volume' ; type=INTEGER,access=rw---R--,values=2,min=0,max=192,step=0 : values=192,192 | dBscale-min=-96.00dB,step=0.50dB,mute=1
amixer cset设置某一个cID的值:amixer cset numid=21,iface=MIXER,name='PCM Volume' 100。
numid=21,iface=MIXER,name='PCM Volume' ; type=INTEGER,access=rw---R--,values=2,min=0,max=192,step=0 : values=100,100 | dBscale-min=-96.00dB,step=0.50dB,mute=1
2.4 alsamixer
alsamixer是一个交互式的音频混音控制台程序,它提供了对音量、通道、增益等设置的直观界面。
Usage: alsamixer [options] Useful options: -h, --help this help -c, --card=NUMBER sound card number or id -D, --device=NAME mixer device name -m, --mouse enable mouse -M, --no-mouse disable mouse -f, --config=FILE configuration file -F, --no-config do not load configuration file -V, --view=MODE starting view mode: playback/capture/all Debugging options: -g, --no-color toggle using of colors -a, --abstraction=NAME mixer abstraction level: none/basic
alsamixer界面中的操作快捷键:
│Esc Exit │F1 ? H Help │F2 / System information │F3 Show playback controls │F4 Show capture controls │F5 Show all controls │Tab Toggle view mode (F3/F4/F5) │F6 S Select sound card │L Redraw screen │ │Left Move to the previous control--选择Control。 │Right Move to the next control │ │Up/Down Change volume--音量了解快捷键。 │+ - Change volume │Page Up/Dn Change volume in big steps │End Set volume to 0% │0-9 Set volume to 0%-90% │Q W E Increase left/both/right volumes │Z X C Decrease left/both/right volumes │B Balance left and right volumes │M Toggle mute--静音开关。 │< > Toggle left/right mute │ │Space Toggle capture │; ' Toggle left/right capture
alsamixer界面如下:
3 amixer/alsamixer和Control关联
从amixer或者alsamixer中看到的Control,可以在Machine/Platform/Codec的驱动中找到Control原型: