sox效果器

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调用方式与命令行调用方式结果对比如下
在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sox是一个名为“音效”的音频处理工具,它能够读取、写入和修改音频文件的格式。Python是一种脚本语言,常用于处理文本、制作网络,以及构建机学习和数据挖掘应用程序。在Python中,要安装Sox,需要按照以下步骤操作: 1. 安装Sox 在命令行下输入以下命令以安装Sox: sudo apt-get install sox 2. 安装PySox PySox是一个可用于Python的Sox封装,允许使用Python处理音频文件。在命令行下输入以下命令以安装PySox: pip install sox 安装成功后,可以使用以下命令来测试PySox: python -c "import sox; print(sox.__file__)" 该命令将输出PySox安装的路径,表明安装成功。 3. 使用PySox处理音频文件 在PySox内部,所有处理都是通过构造一个Sox装置来完成的。这个装置由一系列的效果操作组成,以及一些其他的参数。例如,下面的代码使用PySox将一个WAV文件重新压缩为MP3: import sox tfm = sox.Transformer() tfm.convert(samplerate=8000, n_channels=1, bitdepth=8) tfm.build('example.wav', 'example.mp3') 在上面的代码中,tfm是一个Sox转换,它执行了一个samplerate、n_channels和bitdepth的转换,然后将输入文件example.wav转换为输出文件example.mp3。 总之,PySox提供了一个简单而强大的方式来在Python中处理音频文件。要使用PySox,只需要安装Sox和PySox,然后就可以构造自己的Sox装置并对音频文件进行处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值