【Linux】alsa-utils工具包alsactl、alsamixer、amixer、aplay、arecord等

1、alsamixer 查看、设置音量

alsamixer是Linux 音频架构ALSA中的Alsa工具的其中一个,用于配置音频的各个参数。alsamixer是基于文本下的图形界面的,可以通过键盘的上下键,左右键等,很方便地设置需要的音量,开关某个switch(开关)等等操作。
安装方法:sudo apt install alsa-utils
在这里插入图片描述

2、amixer 查看、设置音量

安装方法:sudo apt install alsa-utils

2.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 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
  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.2 使用示例

amixer cget numid=(amixer controls 显示的)查看具体的选项
amixer cset controls中所输出的某个参数 具体的值(比如,0,1,On,Off等)

amixer -M set PCM 100% //设置音量
amixer name='PCM Playback Volume' 128 //设置音量

amixer -D hw:CODEC -M set PCM 100% //指定声音设备,设置音量
amixer -D hw:CODEC cset name='PCM Playback Volume' 128 //指定声音设备,设置音量

amixer -c 1 sset Line,0 80%,40% unmute cap
//设置第2块声卡的"line"的左声道音量为80%,右声道为40%,取消静音,并设置它为声音源.

amixer -c 2 cset numid=34 40% //设置第34个声卡元素为40%.

amixer -c 0 set Headphone 30(97%) //将第一个声卡耳机音量设置为30或者97%

amixer -c 0 get Headphone //查看第一个声卡耳机音量设置

2.3 查看音频控件

$ amixer scontrols
Simple mixer control 'Master',0
Simple mixer control 'PCM',0
Simple mixer control 'Mic Boost',0
...
Simple mixer control 'Beep',0
Simple mixer control 'Capture',0
Simple mixer control 'Internal Mic Boost',0

$ amixer controls
numid=38,iface=CARD,name='HDMI/DP,pcm=10 Jack'
numid=14,iface=CARD,name='HDMI/DP,pcm=3 Jack'
numid=20,iface=CARD,name='HDMI/DP,pcm=7 Jack'
...
numid=49,iface=MIXER,name='PCM Playback Volume'
numid=6,iface=MIXER,name='Mic Boost Volume'
numid=48,iface=PCM,name='Playback Channel Map',device=10

2.4 查看音频控件内容

$ amixer scontents
Simple mixer control 'Master',0
  Capabilities: pvolume pswitch
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 74
  Mono:
  Front Left: Playback 72 [97%] [-2.00dB] [on]
  Front Right: Playback 72 [97%] [-2.00dB] [on]
Simple mixer control 'PCM',0
  Capabilities: pvolume
  Playback channels: Front Left - Front Right
  Limits: Playback 0 - 255
  Mono:
  Front Left: Playback 253 [99%] [-0.40dB]
  Front Right: Playback 253 [99%] [-0.40dB]
...
Simple mixer control 'Internal Mic Boost',0
  Capabilities: volume
  Playback channels: Front Left - Front Right
  Capture channels: Front Left - Front Right
  Limits: 0 - 3
  Front Left: 3 [100%] [36.00dB]
  Front Right: 3 [100%] [36.00dB]

$ amixer contents
numid=38,iface=CARD,name='HDMI/DP,pcm=10 Jack'
  ; type=BOOLEAN,access=r-------,values=1
  : values=off
numid=14,iface=CARD,name='HDMI/DP,pcm=3 Jack'
  ; type=BOOLEAN,access=r-------,values=1
  : values=off
...
numid=12,iface=PCM,name='Playback Channel Map'
  ; type=INTEGER,access=r----R--,values=2,min=0,max=36,step=0
  : values=3,4
  | container
    | chmap-fixed=FL,FR
numid=48,iface=PCM,name='Playback Channel Map',device=10
  ; type=INTEGER,access=rw---R--,values=8,min=0,max=36,step=0
  : values=0,0,0,0,0,0,0,0
  | container
    | chmap-variable=FL,FR

2.5 查看声卡信息

$ amixer info
Card default 'PCH'/'HDA Intel PCH at 0xf1520000 irq 140'
  Mixer name	: 'Conexant CX20753/4'
  Components	: 'HDA:14f15111,17aa5068,00100101 HDA:8086280b,80860101,00100000'
  Controls      : 49
  Simple ctrls  : 11

3、alsactl

alsactl -f alsa.cfg store //保存音量配置
alsactl -f alsa.cfg restore //恢复音量配置
// -f,--file 配置文件 (默认是 /var/lib/alsa/asound.state)
$ alsactl -h
Usage: alsactl <options> command

global options:
  -h,--help        this help
  -d,--debug       debug mode
  -v,--version     print version of this program

Available state options:
  -f,--file #      configuration file (default /var/lib/alsa/asound.state)
  -l,--lock        use file locking to serialize concurrent access
  -L,--no-lock     do not use file locking to serialize concurrent access
  -O,--lock-state-file #  state lock file path (default /var/lock/asound.state.lock)
  -F,--force       try to restore the matching controls as much as possible
                     (default mode)
  -g,--ignore      ignore 'No soundcards found' error
  -P,--pedantic    do not restore mismatching controls (old default)
  -I,--no-init-fallback  
                   don't initialize even if restore fails
  -r,--runstate #  save restore and init state to this file (only errors)
                     default settings is 'no file set'
  -R,--remove      remove runstate file at first, otherwise append errors
  -p,--period #    store period in seconds for the daemon command
  -e,--pid-file #  pathname for the process id (daemon mode)

Available init options:
  -E,--env #=#     set environment variable for init phase (NAME=VALUE)
  -i,--initfile #  main configuation file for init phase
                     (default /usr/share/alsa/init/00main)
  -b,--background  run daemon in background
  -s,--syslog      use syslog for messages
  -n,--nice #      set the process priority (see 'man nice')
  -c,--sched-idle  set the process scheduling policy to idle (SCHED_IDLE)

Available commands:
  store     <card>  save current driver setup for one or each soundcards
                      to configuration file
  restore   <card>  load current driver setup for one or each soundcards
                      from configuration file
  nrestore  <card>  like restore, but notify the daemon to rescan soundcards
  init      <card>  initialize driver to a default state
  daemon    <card>  store state periodically for one or each soundcards
  rdaemon   <card>  like daemon but do the state restore at first
  kill      <cmd>   notify daemon to quit, rescan or save_and_quit
  monitor   <card>  monitor control events

4、alsa-utils工具包

alsa-utils包含的工具有:
alsactl, aconnect, alsamixer, amidi, amixer, aplay, aplaymidi, arecord, arecordmidi,
aseqnet, iecset, speaker-test

4.1 alsactl

alsactl用来对alsa声卡驱动进行一些高级的设置.系统中装有多个声卡,它也可以支持.有时在音量控制面板无法调整的选项,可以使用alsactl来实现.
alsactl可以将指定声卡的驱动程序设置信息保存到配置文件.或从配置文件中恢复指定声卡的驱动程序的设置信息.

$ alsactl -h
Usage: alsactl <options> command

global options:
  -h,--help        this help
  -d,--debug       debug mode
  -v,--version     print version of this program

Available state options:
  -f,--file #      configuration file (default /var/lib/alsa/asound.state)
  -l,--lock        use file locking to serialize concurrent access
  -L,--no-lock     do not use file locking to serialize concurrent access
  -O,--lock-state-file #  state lock file path (default /var/lock/asound.state.lock)
  -F,--force       try to restore the matching controls as much as possible
                     (default mode)
  -g,--ignore      ignore 'No soundcards found' error
  -P,--pedantic    do not restore mismatching controls (old default)
  -I,--no-init-fallback  
                   don't initialize even if restore fails
  -r,--runstate #  save restore and init state to this file (only errors)
                     default settings is 'no file set'
  -R,--remove      remove runstate file at first, otherwise append errors
  -p,--period #    store period in seconds for the daemon command
  -e,--pid-file #  pathname for the process id (daemon mode)

Available init options:
  -E,--env #=#     set environment variable for init phase (NAME=VALUE)
  -i,--initfile #  main configuation file for init phase
                     (default /usr/share/alsa/init/00main)
  -b,--background  run daemon in background
  -s,--syslog      use syslog for messages
  -n,--nice #      set the process priority (see 'man nice')
  -c,--sched-idle  set the process scheduling policy to idle (SCHED_IDLE)

Available commands:
  store     <card>  save current driver setup for one or each soundcards
                      to configuration file
  restore   <card>  load current driver setup for one or each soundcards
                      from configuration file
  nrestore  <card>  like restore, but notify the daemon to rescan soundcards
  init      <card>  initialize driver to a default state
  daemon    <card>  store state periodically for one or each soundcards
  rdaemon   <card>  like daemon but do the state restore at first
  kill      <cmd>   notify daemon to quit, rescan or save_and_quit
  monitor   <card>  monitor control events

4.2 aconnect

aconnect是ALSA音序器的连接管理器.用来连接或断开ALSA音序器上的端口.端口是可以随意定义的.
如,使用aconnect可以连接到任何由aseqview建立的设备端口.

$ aconnect
aconnect - ALSA 音序器连接管理器
Copyright (C) 1999-2000 Takashi Iwai
用法:
 *两端口间的连接/未连接
   aconnect [-options] sender receiver
     sender, receiver = client:port pair
     -d,--disconnect     断开连接
     -e,--exclusive      唯一的连接
     -r,--real #         按队列转换 real-time-stamp
     -t,--tick #         按队列转换 tick-time-stamp
 列出连接的端口(没有订阅行为)
   aconnect -i|-o [-options]
     -i,--input          列出输入 (可读) ports
     -o,--output         列出输出 (可写) ports
     -l,--list           列出每个端口的当前连接
 * 移除所有导出的连接
     -x, --removeall

命令格式:

示例:
连接端口64:0到65:0:
% aconnect 64:0 65:0
这个连接是单向的,所有到发送端口64:0的数据,将被重定向到接收65:0端口.如果有另一个端口65:1,
也使用64:0作为发送端口,则数据会同时发送到2个接收端口.
端口连接时,使用:
% aconnect -d 64:0 65:0
地址也可以使用客户端的名字来代替:
% aconnect External:0 Emu8000:1
使用-i打印出输入端口信息.-o打印出输出端口信息.
% aconnect -i
client 0: ’System’ [type=kernel]
0 ’Timer ’
1 ’Announce ’
client 64: ’External MIDI-0’ [type=kernel]
0 ’MIDI 0-0 ’
可以使用-x选项来清除所有的连接.
% aconnect -x

4.3 amidi的使用

amidi的作用是对ALSA的RawMIDI端口进行读写.
amidi是一个命令行工具,允许你以独占模式向MIDI设备读/写数据.

命令格式:
amidi options
选项:
-h,-V,-l,-L 用于显示信息.
-s,-r,-S,-d 用于发送/接收数据.
-h, --help
打印帮助信息.
-V, --version
打印版本号.
-l, --list-devices
打印所有硬件MIDI端口的列表.
-L, --list-rawmidis
打印所有RawMIDI定义.
-p, --port=name
设置要使用的ALSA RawMIDI端口.若不指定,则使用声卡0的端口0.
-s, --send=filename
发送指定文件的内容到MIDI端口.文件中必须包含raw MIDI命令(.syx,.mid文件).
-r, --receive=filename
将MIDI端口接收的数据写入指定文件.
-S, --send-hex="..."
发送十六进制字节到MIDI端口.
-d, --dump
从MIDI端口接收数据,然后以十六进制形式打印出来.
-t, --timeout=秒
指定超时,当端口无数据输出达到超时时长时,将停止接收数据.
示例:
amidi -p hw:0 -s my_settings.syx
发送my_settings.syx终端MIDI命令到端口 hw:0.
amidi -S ’
发送XG复位到默认端口.
amidi -p virtual -d
建立一个虚拟RawMIDI端口,然后发送所有数据到这个端口.

4.4 arecord,aplay

arecord,aplay是命令行的ALSA声卡驱动的录音和播放工具.
arecord是命令行ALSA声卡驱动的录音程序.支持多种文件格式和多个声卡.
aplay是命令行播放工具,支持多种文件格式.
命令格式:

arecord [flags] [filename]
aplay [flags] [filename [filename]] ...
选项:
-h, --help
帮助.
--version
打印版本信息.
-l, --list-devices
列出全部声卡和数字音频设备.
-L, --list-pcms
列出全部PCM定义.
-D, --device=NAME
指定PCM设备名称.
-q --quiet
安静模式.
-t, --file-type TYPE
文件类型(voc,wav,raw或au).
-c, --channels=#
设置通道号.
-f --format=FORMAT
设置格式.格式包括: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
-r, --rate=#
设置频率.
-d, --duration=#
设置持续时间,单位为秒.
-s, --sleep-min=#
设置最小休眠时间.
-M, --mmap
mmap流.
-N, --nonblock
设置为非块模式.
-B, --buffer-time=#
缓冲持续时长.单位为微妙.
-v, --verbose
显示PCM结构和设置.
-I, --separate-channels
设置为每个通道一个单独文件.
示例:
aplay -c 1 -t raw -r 22050 -f mu_law foobar
播放raw文件foobar.以22050Hz,单声道,8位,mu_law格式.
arecord -d 10 -f cd -t wav -D copy foobar.wav
以CD质量录制foobar.wav文件10秒钟.使用PCM的"copy".
arecord -vv -t wav -f S16_LE -c 2 -d 10 -r 24000 ./record.wav
-vv 显示录音文件的信息
-t 录音文件的类型
-f 文件格式
-c 声道,2为双声道
-r 设置波特率
-d 录音时间

4.5 aplaymidi

aplaymidi用来播放标准的MIDI文件.
aplaymidi是一个命令行工具,可以在一个或多个ALSA端口上播放MIDI
文件.
命令格式:

aplaymidi -p client port[,...] [-d delay] midifile ...
选项:
-h, --help
输出帮助信息.
-V, --version
输出版本信息.
-l, --list
输出可以使用的输出端口列表.
-p, --port=client port,...
设置端口.
-d, --delay=seconds
设置MIDI文件结束后,等待时长.

4.6 arecordmidi

arecordmidi用于录制标准的MIDI文件.
arecordmidi可以从一个或多个ALSA端口上,录制一个标准MIDI文件.

命令格式:
arecordmidi -p client port[,...] [options] midifile
选项:
-h,--help
打印帮助信息.
-V,--version
打印版本号.
-l,--list
打印可以使用的输入端口.
-p,--port=clientort,...
设置端口.
-b,--bpm=beats
设置MIDI文件的速率,默认为120 BPM.
-f,--fps=frames
设置帧率.
-s,--split-channels
设置每个通道将录制成一个单独的MIDI文件.
-d,--dump
在标准输出上,以文本形式显示接受到的事件信息

4.7 aseqnet

aseqnet是ALSA调节器的网络连接工具.
aseqnet是ALSA调节器的客户端程序,可以从网络上发送和接收事件数据包.
网络上有主机A,主机B.A为服务器端,B为客户端.ALSA调节器系统必须同事运行
在两个服务器上.然后建立服务器端口:
hostA% aseqnet
sequencer opened: 128:0
在HostB上执行:
hostB% aseqnet hostA
sequencer opened: 132:0
现在所有发送到HostA:128:0的数据将被传送到HostB:132:0上,反之亦然.
命令格式:

aseqnet [remotehost]
选项:
-p port
指定TCP端口号或服务名.
-s addr
设置指定地址用于读操作.
-d addr
设置指定地址用于写操作.
-v
详细输出模式.

4.8 iecset的使用

设置或输出IEC958状态位.
iecset是个小工具,通过ALSA的API,设置或输出IEC958(或称S/PDIF)状态位信息.
直接运行iecset将输出当前IEC958的状态信息. 命令格式:

iecset [options] [cmd arg...]
选项:
-D device
设置需要打开的设备名.
-c card
设置需要打开的网卡名.
-x
输出AESx字节格式的状态信息.
-i
从标准输入读取命令信息,每行一个命令.
命令:
professional
专业模式(true)或用户模式(false).
audio
音频模式(true).
rate
采样频率,单位Hz.
emphasis
设置加强值.0 = none, 1 = 50/15us, 2 = CCITT.
lock
速率锁.
sbits
采样位:2 = 20bit, 4 = 24bit, 6 = undefined.
wordlength
设置字长:0 = No, 2 = 22-18 bit, 4 = 23-19 bit, 5 = 24-20
bit, 6 = 20-16 bit.
category
分类:值从0到0x7f.
copyright
设置是否包含版权.
original
原始标记:
示例:
输出当前IEC958信息.
$ iecset
Mode: consumer
Data: audio
Rate: 44100 Hz
Copyright: permitted
Emphasis: none
Category: general
Original: 1st generation
Clock: 1000 ppm
显示当前第1块声卡的IEC958状态位.
$ iecset -Dhw:0
Mode: consumer
Data: non-audio
Rate: 44100 Hz
Copyright: permitted
Emphasis: none
Category: general
Original: 1st generation
Clock: 1000 ppm
设置当前为用户模式,并打开"非音频"位.
$ iecset pro off audio off
Mode: consumer
Data: non-audio
Rate: 44100 Hz
Copyright: permitted
Emphasis: none
Category: general
Original: 1st generation
Clock: 1000 ppm

4.9 speaker-test的使用

speaker-test是一个针对 ALSA驱动的声音测试工具.
speaker-test可以分别对左右声道进行单独的测试.

命令格式:
speaker-test [-options]
选项:
-c | --channels NUM
设置通道数目.
-D | --device NAME
设置使用的PCM设备名.
-f | --frequency FREQ
设置声音频率.
--help
输出帮助信息.
-b | --buffer TIME
设置缓冲区时长.0为使用最大的缓冲区大小.
-p | --period TIME
设置节拍为多少微秒.
-r | --rate RATE
设置音频率.
-t | --test pink|sine|wav
-t pink 表示测试时使用噪声.
-t sine 表示测试时使用音频信号声.
-t wav 表示测试时使用WAV文件.
-l | --nloops COUNT
设置测试循环的次数.
-w | --wavfile
设置测试时播放的wav文件.
-W | --wavdir
设置一个包含wav文件的目录.默认为/usr/share/sounds/alsa.

示例:
在一个音频接口上进行立体声测试
speaker-test -Dplug:front -c2

在两个音频接口上进行4声道测试.
speaker-test -Dplug:surround40 -c4

在立体声接口上进行5.1声道测试.
speaker-test -Dplug:surround51 -c6

测试低音扬声器.
speaker-test -Dplug:surround51 -c6 -s1 -f75

おすすめ

転載: blog.csdn.net/u010168781/article/details/120726074