sox是一个跨平台的命令行工具,可以进行音频格式转换,也可以应用各种音效到文件中,还支持在大多数平台进行播放与录制,堪称音频界的瑞士军刀。接下来,我们详细介绍sox库支持的音频格式、支持的音效、常用的API、常用的命令行。
目录
一、支持的音频格式
- Apple/SGI AIFF files
- SUN .au files(PCM, u-law, A-law)
- CDDA (光碟数字音频格式)
- AMR-WB & AMR-NB (需要ibamrwb & libamrnb库)
- MP2/MP3 (需要ibmp3lame库)
- MP4, AAC, AC3, WAVPACK, AMR-NB files (需要ffmpeg库)
- AVI, WMV, Ogg Theora, MPEG video files (需要ffmpeg库)
- Ogg Vorbis files (需要Ogg Vorbis库)
- FLAC files (需要libFLAC库)
- Microsoft .WAV file
二、支持的音效
1、音质效果
- 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
- 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、产生效果
- 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
- tremolo: Sinusoidal volume modulation
3、音量效果
- 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
- vol: Adjust audio volume
4、编辑效果
- 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
5、混音效果
- channels: Auto mix or duplicate to change number of channels
- divide+: Divide sample values by those in the 1st channel
- remix: Produce arbitrarily mixed output channels
- swap: Swap stereo channels
6、音调/音速效果
- 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
- tempo: Adjust tempo without changing pitch
7、控制效果
- dither: Add dither noise to increase quantisation SNR
- rate: Change audio sampling rate
8、专业混音
- 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
9、分析效果
- noiseprof: Produce a DFT profile of the audio
- spectrogram: graph signal level vs. frequency & time (需要libpng库)
- stat: Enumerate audio peak & RMS levels, freq
- stats: Multichannel aware `stat'
10、信号处理效果
- 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
1、sox_format_init
用于初始化关联文件格式的句柄。如果编译为动态库,它会探测并且初始化所有外部库,这个方法应该在其他操作之前调用。
2、sox_format_quit
用于反初始化关联文件格式的句柄。
3、sox_open_input
根据文件路径打开可读(输入)文件。
4、sox_open_output
根据文件路径打开可写(输出)文件。
5、sox_read
使用格式句柄来读取指定len的数据到buf(数据必须转换为32位有符号数)。
6、sox_write
使用格式句柄向buf写入指定len的数据(数据必须转换为32位有符号数)。
7、sox_close
关闭文件句柄。
8、sox_seek
对文件流进行seek操作。
9、sox_find_effect
根据音效名字来查找effect。
10、sox_create_effect
创建并实例化effect。
11、sox_effect_options
用于控制effect的选择参数。
12、sox_create_effects_chain
创建effect链。
13、sox_delete_effects_chain
删除effect链。
14、sox_add_effect
向effect链添加effect。
四、常用的命令行
sox支持音频播放、格式转换、音频录制、添加音效。下面分别进行介绍,精彩依然在继续。
1、播放音频
最简单的播放:play tiger.mp3
连续播放多个:play tiger.mp3 hello.mp3
控制播放音量:play -v 0.5 tiger.mp3
控制播放速度:play tiger.mp3 tempo 0.5
2、格式转换
使用-c指定声道数,-r指定采样率,-b指定采样位数。
mp3转为ogg:sox tiger.mp3 -c 2 -r 44100 hello.ogg
2个音频混音:sox -m tiger.mp3 hello.mp3 mix.mp3
2个音频拼接:sox tiger.mp3 hello.mp3 concat.mp3
3、音频录制
录制一段10s的音频:rec -c 2 -r 16000 test.mp3 trim 0 00:10
4、添加音效
1)混响reverb:play tiger.mp3 reverb
2)空灵echo:play tiger.mp3 echo 0.8 0.9 1000 0.6 (gain-in gain-out <delay decay>)
3)增益gain:play tiger.mp3 gain -l 6
4)转换声道remix:sox tiger.mp3 mono.mp3 remix 1
5)音调pitch
萝莉:play tiger.mp3 pitch -q 500
大叔:play tiger.mp3 pitch -q -1000
6)搞怪tempo:play tiger.mp3 tempo 2
7)颤抖tremolo:play tiger.mp3 tremolo 3.5 60
8)均衡器equalizer:play tiger.mp3 equalizer 4585 10.17q +9.4 equalizer 5356 4.33q +8.4 equalizer 6627 3.58q +7.5 equalizer 11297 2.62q +6.2
可以到GitHub一起学习音视频:GitHub - xufuji456/FFmpegAndroid: android端基于FFmpeg实现音频剪切、拼接、转码、编解码;视频剪切、水印、截图、转码、编解码、转Gif动图;音视频合成与分离,配音;音视频解码、同步与播放;FFmpeg本地推流、H264与RTMP实时推流直播;FFmpeg滤镜:素描、色彩平衡、hue、lut、模糊、九宫格等;歌词解析与显示