sox效果器的定义
sox效果器的定义可以查看effects.h文件,源码如下。
EFFECT(allpass)
EFFECT(band)
EFFECT(bandpass)
EFFECT(bandreject)
EFFECT(bass)
EFFECT(bend)
EFFECT(biquad)
EFFECT(chorus)
EFFECT(channels)
EFFECT(compand)
EFFECT(contrast)
EFFECT(dcshift)
EFFECT(deemph)
EFFECT(delay)
EFFECT(dft_filter) /* abstract */
EFFECT(dither)
EFFECT(divide)
EFFECT(downsample)
EFFECT(earwax)
EFFECT(echo)
EFFECT(echos)
EFFECT(equalizer)
EFFECT(fade)
EFFECT(fir)
EFFECT(firfit)
EFFECT(flanger)
EFFECT(gain)
EFFECT(highpass)
EFFECT(hilbert)
EFFECT(input)
#ifdef HAVE_LADSPA_H
EFFECT(ladspa)
#endif
EFFECT(loudness)
EFFECT(lowpass)
EFFECT(mcompand)
EFFECT(noiseprof)
EFFECT(noisered)
EFFECT(norm)
EFFECT(oops)
EFFECT(output)
EFFECT(overdrive)
EFFECT(pad)
EFFECT(phaser)
EFFECT(pitch)
EFFECT(rate)
EFFECT(remix)
EFFECT(repeat)
EFFECT(reverb)
EFFECT(reverse)
EFFECT(riaa)
EFFECT(silence)
EFFECT(sinc)
#ifdef HAVE_PNG
EFFECT(spectrogram)
#endif
EFFECT(speed)
#ifdef HAVE_SPEEXDSP
EFFECT(speexdsp)
#endif
EFFECT(splice)
EFFECT(stat)
EFFECT(stats)
EFFECT(stretch)
EFFECT(swap)
EFFECT(synth)
EFFECT(tempo)
EFFECT(treble)
EFFECT(tremolo)
EFFECT(trim)
EFFECT(upsample)
EFFECT(vad)
EFFECT(vol)
效果器的使用
效果器的使用方法有两种方式,一种是命令行方式,一种是通过API调用。
命令行方式
以norm为例,归一化音频文件到0dB或者其他值,该值应该是小于0的数值,大于0可能会出现失真警告。以4K正弦音频信号为例,分别进行比较。
命令如下
sox sin_4k.mp3 sin_4k-0.mp3 --norm=0
当norm=10时,出现警告
sox WARN gain: gain clipped 1043520 samples; decrease volume?
由频谱对比图可以看出,当norm=0时,处理的结果最为理想,既放大了音频音量也没有失真警告;当norm=+10时,出现失真警告;当norm=-10时,音量降低。
由时域波形可以看出,当norm=+10时,幅值超过了1.0,出现失真警告。
API调用方式
API调用流程
效果器结构如下
源码如下
int main(int argc, char * argv[])
{
static sox_format_t * in, * out; /* input and output files */
sox_effects_chain_t * chain;
sox_effect_t * e;
char * args[32];
assert(argc == 4);
/* All libSoX applications must start by initialising the SoX library */
assert(sox_init() == SOX_SUCCESS);
/* Open the input file (with default parameters) */
assert(in = sox_open_read(argv[1], NULL, NULL, NULL));
/* Open the output file; we must specify the output signal characteristics.
* Since we are using only simple effects, they are the same as the input
* file characteristics */
assert(out = sox_open_write(argv[2], &in->signal, NULL, NULL, NULL, NULL));
/* Create an effects chain; some effects need to know about the input
* or output file encoding so we provide that information here */
chain = sox_create_effects_chain(&in->encoding, &out->encoding);
/* The first effect in the effect chain must be something that can source
* samples; in this case, we use the built-in handler that inputs
* data from an audio file */
e = sox_create_effect(sox_find_effect("input"));
args[0] = (char *)in, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
/* This becomes the first `effect' in the chain */
assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
free(e);
/* Create the `norm' effect, and initialise it with the desired parameters: */
e = sox_create_effect(sox_find_effect("norm"));
char normStr[8] = {};
snprintf(normStr, sizeof(normStr), "%s", argv[3]);
//printf("normStr: %s\n", normStr);
args[0] = normStr, assert(sox_effect_options(e, 1, args) == SOX_SUCCESS);
/* Add the effect to the end of the effects processing chain: */
assert(sox_add_effect(chain, e, &in->signal, &in->signal) == SOX_SUCCESS);
free(e);
/* The last effect in the effect chain must be something that only consumes
* samples; in this case, we use the built-in handler that outputs
* 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);
/* Flow samples through the effects processing chain until EOF is reached */
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;
}
API调用方式与命令行调用方式结果对比如下