Home ALSA 和 SoX
Post
Cancel

ALSA 和 SoX

ALSA

Advanced Linux Sound Architecture (ALSA)

The Advanced Linux Sound Architecture (ALSA) provides audio and MIDI functionality to the Linux operating system. ALSA has the following significant features:

  • Efficient support for all types of audio interfaces, from consumer sound cards to professional multichannel audio interfaces.
  • Fully modularized sound drivers.
  • SMP and thread-safe design (PLEASE READ THIS).
  • User space library (alsa-lib) to simplify application programming and provide higher level functionality.
  • Support for the older Open Sound System (OSS) API, providing binary compatibility for most OSS programs.

ALSA 的主要特性包括:高效支持所有类型的音频接口,从消费级声卡到专业的多通道音频接口;完全模块化的设计;支持对称多处理(SMP)和线程安全;对 OSS 的向后兼容,以及提供了用户空间的 alsa-lib 库来简化应用程序的开发。

安装

1
$ sudo apt-get install alsa-base alsa-utils

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

其中,aplay/arecord 是播放/录制工具;alsamixer/amixer 是图形化/命令行音频参数配置工具;speaker-test 是扬声器测试工具。

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 列出声卡设备
$ aplay -l
**** List of PLAYBACK Hardware Devices ****
card 0: Black [TI BeagleBone Black], device 0: davinci-mcasp.0-i2s-hifi i2s-hifi-0 [davinci-mcasp.0-i2s-hifi i2s-hifi-0]
  Subdevices: 1/1
  Subdevice #0: subdevice #0
card 1: II [Jabra EVOLVE 30 II], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

# 列出录音设备
$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: II [Jabra EVOLVE 30 II], device 0: USB Audio [USB Audio]
  Subdevices: 1/1
  Subdevice #0: subdevice #0

# 查看 card1 信息
$ ll /proc/asound/card1
$ cat /proc/asound/card1/stream0
GN Audio A/S Jabra EVOLVE 30 II at usb-musb-hdrc.1-1, full speed : USB Audio

Playback:
  Status: Stop
  Interface 2
    Altset 1
    Format: S16_LE
    Channels: 2
    Endpoint: 4 OUT (SYNC)
    Rates: 8000, 16000, 32000, 44100, 48000
    Bits: 16
    Channel map: FL FR

Capture:
  Status: Stop
  Interface 1
    Altset 1
    Format: S16_LE
    Channels: 1
    Endpoint: 3 IN (SYNC)
    Rates: 8000, 16000, 44100, 48000
    Bits: 16
    Channel map: MONO
# Channels 表示为声道数,Rates 表示支持的采样率。
1
2
3
4
# 录制
$ arecord -D "plughw:1,0" -f S16_LE -r 48000 -d 5 -t wav file.wav
# 播放
$ aplay -D "plughw:1,0" -f S16_LE -r 48000 file.wav
指令参数说明
-Dplughw:1,0选择设备,1 代表 card1,0 代表 subdevice
-r48000采样率
-fS16_LE录音格式,16bit 位宽
-d5录制 5 秒
-twav输出音频格式为 wav
 file.wav保存的音频名称
1
2
# 测试
$ speaker-test -D plughw:1,0 -c 2 -t wav

SoX

SoX - Sound eXchange

SoX is a cross-platform (Windows, Linux, MacOS X, etc.) command line utility that can convert various formats of computer audio files in to other formats. It can also apply various effects to these sound files, and, as an added bonus, SoX can play and record audio files on most platforms.

SoX 是一个跨平台(Windows,Linux,MacOS 等)的命令行实用程序,可以将各种格式的音频文件转换为需要的其他格式。 SoX 还可以对输入的音频文件应用各种效果,也支持在大多数平台上播放录制音频文件。

文档

  • sox - the main SoX manual (also covers rec & play): html pdf
  • soxi: html pdf
  • SoX Formats & Device Drivers: html pdf
  • libsox - SoX file handler and effects engine: html pdf

安装

1
2
$ sudo apt-get install sox
$ sudo apt-get install libsox-fmt-all

使用

SoX — 音频处理工具里的瑞士军刀

SoX 工具的所有功能都可以通过一个简单的 sox 命令及相应的选项实现。但它同时提供了 play 命令用于播放音频文件,rec 命令用于录制音频,以及 soxi 命令用于获取音频的文件头中包含的信息。

1
2
3
4
5
6
7
8
9
10
11
12
13
SYNOPSIS
       sox [global-options] [format-options] infile1
            [[format-options] infile2] ... [format-options] outfile
            [effect [effect-options]] ...

       play [global-options] [format-options] infile1
            [[format-options] infile2] ... [format-options]
            [effect [effect-options]] ...

       rec [global-options] [format-options] outfile
            [effect [effect-options]] ...
       
       soxi [-V[level]] [-T] [-t|-r|-c|-s|-d|-D|-b|-B|-p|-e|-a] infile1 ...

soxisox --i 获取元数据。

1
2
3
4
5
6
7
8
9
$ soxi 50/01.我们的田野.mp3
Input File     : '50/01.我们的田野.mp3'
Channels       : 2
Sample Rate    : 44100
Precision      : 16-bit
Duration       : 00:00:34.15 = 1505795 samples = 2560.88 CDDA sectors
File Size      : 820k
Bit Rate       : 192k
Sample Encoding: MPEG audio (layer I, II or III)

recplay 基本的录制和播放。

1
2
3
4
5
6
# rec <==> sox -d test.mp3
$ rec test.mp3
# play <==> sox test.mp3 -d
$ play test.mp3

# -d 表示默认音频设备,sox 命令的基本格式为:sox <input> <output>

sox

1
2
3
4
5
6
# 录制半小时音频
$ sox -t alsa plughw:1,0 record.mp3 trim 0 00:30:00
# 录制长时间音频,每隔一小时保存一次
$ sox -t alsa plughw:1,0 record.mp3 trim 0 01:00:00 : newfile : restart
# 指定音频设备播放音频
$ sox song.mp3 -t alsa plughw:1,0
This post is licensed under CC BY 4.0 by the author.