Sox音频处理简介

1 篇文章 0 订阅

SoX 音频处理简介

SoX(即 Sound eXchange)是一个音频处理工具,支持跨平台(Windows,Linux,MacOS 等)命令行的实用程序,可以将各种格式的音频文件转换为需要的其他格式。SoX 还可以对输入的音频文件应用各种效果,也支持在大多数平台上播放和录制音频文件。此外Sox支持自写API插件使用。

简介

SoX 支持读取和写入常见格式的音频文件,并在此过程中选择性的加入一些声音效果。
它可以组合多个输入源及合成音效,在许多系统上也可以作为音频播放器或多轨录音机使用。
SoX 工具在大部分 Linux 系统上可以直接通过软件包管理器安装,命令如下:
sudo apt-get install sox
Mac 系统上则可以使用安装命令如下:
brew install sox
安装成功后,使用命令:

play [infile_path]
即可播放音频,但注意sox支持的音频格式有限。

soxi [infile_path]
即可查看音频文件信息。

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

sox可使用的场景包括但不限于:
1.更改声道
2.更改音量
3.截取音频
4.拼接音频
5.合成音频
6.静音效果
7.混合音频
8.改变播放速度

一.基本使用

1.1 获取音频信息

soxi 或 sox --i 命令可以通过分析音频文件的文件头,获取其元数据(如通道数、采样率、编码等)。
soxi C_120.wav
在这里插入图片描述
soxi可支持所有命令如下:

$ soxi
Usage: soxi [-V[level]] [-T] [-t|-r|-c|-s|-d|-D|-b|-B|-p|-e|-a] infile1 ...

-t  Show detected file-type
-r  Show sample-rate
-c  Show number of channels
-s  Show number of samples (0 if unavailable)
-d  Show duration in hours, minutes and seconds (0 if unavailable)
-D  Show duration in seconds (0 if unavailable)
-b  Show number of bits per sample (0 if not applicable)
-B  Show the bitrate averaged over the whole file (0 if unavailable)
-p  Show estimated sample precision in bits
-e  Show the name of the audio encoding
-a  Show file comments (annotations) if available

1.2 播放与录制

play 和 rec 命令提供了最基本的播放和录制功能。
播放:$ play xxx.wav
录制:$ rec xxx.wav
其中 -d 选项用于指定播放或录制时使用的音频设备,不指定时则表示使用默认设备。
使用play命令时注意sox支持的音频格式有限;

trim 效果可以从音频文件中裁剪提取指定的片段到输出文件。

$ play xxx.wav trim 10.0 5.0

代表截取10s~15s的音频数据。

二 . 音频效果器使用

2.1 使用方法

音频效果器渲染流文件实时播放(以phase效果器为例):

play xxx.wav phaser 0.8 0.74 3 0.4 0.5 -t

不同效果器参数详情参考官方文档。
音频效果器渲染后输出新的音频文件:

sox in_file.wav out_file.wav phaser 0.8 0.74 3 0.4 0.5 -t

sox支持不同音频效果器叠加;

2.2 滤波器类

sox支持滤波器类音频效果器如下:
allpass: RBJ all-pass biquad IIR filter
bandpass: RBJ band-pass biquad IIR filter
bandreject: RBJ band-reject biquad IIR filter
band: SPKit resonator band-pass IIR filter
bass: Tone control: RBJ shelving biquad IIR filter
equalizer: RBJ peaking equalisation biquad IIR filter
firfit+: FFT convolution FIR filter using given freq. response (W.I.P.)
highpass: High-pass filter: Single pole or RBJ biquad IIR
hilbert: Hilbert transform filter (90 degrees phase shift)
lowpass: Low-pass filter: single pole or RBJ biquad IIR
sinc: Sinc-windowed low/high-pass/band-pass/reject FIR
treble: Tone control: RBJ shelving biquad IIR filter

2.3 合成效果器

sox支持合成类音频音效如下:
chorus: Make a single instrument sound like many
delay: Delay one or more channels
echo: Add an echo
echos: Add a sequence of echos
flanger: Stereo flanger
overdrive: Non-linear distortion
phaser: Phase shifter
repeat: Loop the audio a number of times
reverb: Add reverberation
reverse: Reverse the audio (to search for Satanic messages 😉
tremolo: Sinusoidal volume modulation

2.4 音量效果器

sox支持音量类效果如下:
compand: Signal level compression/expansion/limiting
contrast: Phase contrast volume enhancement
dcshift: Apply or remove DC offset
fade: Apply a fade-in and/or fade-out to the audio
gain: Apply gain or attenuation; normalise/equalise/balance/headroom
loudness: Gain control with ISO 226 loudness compensation
mcompand: Multi-band compression/expansion/limiting
norm: Normalise to 0dB (or other)
vol: Adjust audio volume

2.5 剪辑效果器

sox提供音频编辑效果,主要是添加、删除一类的功能,如下:
pad: Pad (usually) the ends of the audio with silence
silence: Remove portions of silence from the audio
splice: Perform the equivalent of a cross-faded tape splice
trim: Cuts portions out of the audio
vad: Voice activity detector

2.6 混音效果器

sox提供混音类效果器如下:
channels: Auto mix or duplicate to change number of channels
divide+: Divide sample values by those in the 1st channel (W.I.P.)
remix: Produce arbitrarily mixed output channels
swap: Swap stereo channels

2.7 节奏和音高效果器

sox提供节奏和音高修正功能,如下:
bend: Bend pitch at given times without changing tempo
pitch: Adjust pitch (= key) without changing tempo
speed: Adjust pitch & tempo together
stretch: Adjust tempo without changing pitch (simple alg.)
tempo: Adjust tempo without changing pitch (WSOLA alg.)

2.8 主线路效果器

sox还支持信噪比提升,采样率更改。
dither: Add dither noise to increase quantisation SNR
rate: Change audio sampling rate

2.9 特殊效果器

sox提供了一些特殊效果器玩法:
deemph: ISO 908 CD de-emphasis (shelving) IIR filter
earwax: Process CD audio to best effect for headphone use
noisered: Filter out noise from the audio
oops: Out Of Phase Stereo (or Karaoke) effect
riaa: RIAA vinyl playback equalisation

2.10 其他

这里我关注比较多的是sox提供的降噪功能。
noiseprof: Produce a DFT profile of the audio (use with noisered)
spectrogram: graph signal level vs. frequency & time (needs libpng)
stat: Enumerate audio peak & RMS levels, approx. freq., etc.
stats: Multichannel aware stat

ladspa: Apply LADSPA plug-in effects e.g. CMT (Computer Music Toolkit)
synth: Synthesise/modulate audio tones or noise signals
newfile: Create a new output file when an effects chain ends.
restart: Restart 1st effects chain when multiple chains exist

biquad: 2nd-order IIR filter using externally provided coefficients
downsample: Reduce sample rate by discarding samples
fir: FFT convolution FIR filter using externally provided coefficients
upsample: Increase sample rate by zero stuffing

三.API的使用

sox支持自主创建API,使用流程如下:
首先编译连接:
gcc source.c -l sox -I/usr/local/include -L/usr/local/lib
Mac使用时候编译会报错,需要指定gcc编译器,执行
brew install gcc
然后查看gcc版本号,使用gcc编译时候mac会切换至clang编译器,这个地方有很多的坑,使用gcc编译器时候需要指定编译器版本号。
在使用SOX库之前,须初始化整个库的全局参数,如下:

sox_init();

退出SOX则需要调用如下代码:

sox_quit();

如果没有调用sox_quit方法,那么不可以再一次调用sox_init,否则会Crash。

第二步初始化输入文件,代码如下:

sox_format_t* in;
const char* in_path = "/your_file_path/input.wav";
in = sox_open_read(in_path,NULL,NULL,NULL);

初始化输出文件,代码如下:

sox_format_t* out;
const char* out_path = "/your_save_path/output.wav";
out = sox_open_write(out_path,&in->signal,NULL,NULL,NULL,NULL);

sox使用类似责任链设计模式的方式设计整个系统,所以使用时需要先构造一下效果器链,然后将使用的效果器一个一个加到这个效果链中。先来构造这个效果器链,代码如下:

sox_effects_chain_t* chain;
chain = sox_create_chain(&in->encoding , &out->encoding);

先来构建一下输入数据的效果器:

sox_effect_t* inputEffect;
inputEffect = sox_create_cffect(sox_find_effect("input"));

上述代码构造了一个输入数据的效果器,接下来吧参数添加进效果器,代码如下:

char* args[10];
args[0] = (char*)in;
sox_effect_options(inputEffect,1,args) //参数:效果器,参数个数,具体参数

目前已经给inputEffect效果器配置好了需要的参数,然后要将这个效果器增加到效果器链中,并将这个效果器释放掉,代码如下:

sox_add_effect(chain,inputEffect,&in->signal,&in->signal);
free(inputEffect);

接下来在加入一个增加音量的效果器,添加代码如下:

sox_effect_t*  volumeEffect;
volumeEffect = sox_create_cffect(sox_find_effect("vol"));
args[0] = "20db";
sox_effect_options(volumeEffect,1,args) ;
sox_add_effect(chain,volumeEffect,&in->signal,&in->signal);
free(volumeEffect);

接下来添加一个输出数据的效果器,代码如下:

sox_effect_t*  outputEffect;
outputEffect = sox_create_cffect(sox_find_effect("output"));
args[0] = (char*)out;
sox_effect_options(outputEffect,1,args) ;
sox_add_effect(chain,outputEffect,&in->signal,&in->signal);
free(outputEffect);

完整效果器链:
input.wav—>inputEffect---->volumeEffect----->outputEffect---->output.wav
接下来运行起效果器:

sox_flow_effects(chain,NULL,NULL);

处理完后就要销毁这个效果器链了:

sox_delete_effects_chain(chain);
sox_close(out);
sox_close(in);
sox_quit();

至此分析完所有的代码,官方给出的API使用完整代码如下:

#include "sox.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

int main(int argc, char * argv[])
{
  static sox_format_t * in, * out; 
  sox_effects_chain_t * chain;
  sox_effect_t * e;
  char * args[10];
  
  assert(argc == 3);
  assert(sox_init() == SOX_SUCCESS);
  assert((in = sox_open_read(argv[1], NULL, NULL, NULL)));
  
   * file characteristics */
  assert((out = sox_open_write(argv[2], &in->signal, NULL, NULL, NULL, NULL)));

  /* Create an effects chain */
  chain = sox_create_effects_chain(&in->encoding, &out->encoding);
  e = sox_create_effect(sox_find_effect("input"));
  args[0] = (char *)in, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
  assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
  free(e);

  /* Create the `vol' effect */
  e = sox_create_effect(sox_find_effect("vol"));
  args[0] = "3dB", assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
  assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
  free(e);

  /* Create the `flanger' effect */
  e = sox_create_effect(sox_find_effect("flanger"));
  assert(sox_effect_options(e, 0, NULL) == SOX_SUCCESS);
  assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
  free(e);

  /* data to an audio file */
  e = sox_create_effect(sox_find_effect("output"));
  args[0] = (char *)out, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
  assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
  free(e);

  sox_flow_effects(chain, NULL, NULL);

  /* All done; tidy up: */
  sox_delete_effects_chain(chain);
  sox_close(out);
  sox_close(in);
  sox_quit();
  return 0;
}

自用时候可以去掉一些assert函数。

总结

sox整体给我感觉侧重音频效果器实现,因此在使用过程中经常和ffmpeg一起配合使用。sox实现音频效果器快速。sox在bash栏中可快速处理音频效果,并且支持了自己设计API使用。
更多的信息参考sox官网

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值