编程笔记编解码

程序员军刀之 sox

2017-04-09  本文已影响437人  老瓦在霸都

SoX 是一款强大音频文件工具箱,是音频操作方面的瑞士军刀, 转码, 播放, 录制,以及查看音频文件格式都很方便, 其中主要包含四个命令行小工具:

音频格式转换工具 sox

可以从 http://sox.sourceforge.net/ 下载安装
MacOS 上直接用 brew install sox 安装

用法如下

sox [global-options] [format-options] infile1 [[format-options] infile2] ... [format-options] outfile [effect [effect-options]] ...

输入输出参数选项 :

sox 的强大在于它能给音频文件添加各种音效和处理, 各种效果可以链式排列逐个对音频文件进行处理

例如 gain �效果, 可对于某些声道放大或减小音频信号
<pre>
gain [−e|−B|−b|−r] [−n] [−l|−h] [gain-dB]
</pre>

注意使用任一 −e, −B, −b, −r, 或 −n 参数都需要额外的磁盘空间, 对于流媒体不太适用

没有其他参数的 gain-dB 用来根据给定的分贝数值调整信号强度水平
正值为放大, 负值为减小

带有其他参数的 gain-dB 是在相应处理过程之后进行分贝的放大或减小

示例

音频格式查询工具 soxi

soxi -V 1000.wav

input File     : '1000.wav'
Channels       : 1
Sample Rate    : 8000
Precision      : 14-bit
Duration       : 00:00:02.14 = 17090 samples ~ 160.219 CDDA sectors
File Size      : 17.1k
Bit Rate       : 64.2k
Sample Encoding: 8-bit u-law

音频播放工具 play

play [global-options] [format-options] infile1 [[format-options] infile2] ... [format-options] [effect [effect-options]] ...
<pre>
play 1000.wav
</pre>

音频录制工具 rec

rec [global-options] [format-options] outfile [effect [effect-options]] ...

示例

libsox

libsox is a library of sound sample file format readers/writers and sound effects processors. It is mainly developed for use by SoX but is useful for any sound application.

libsox是一个对于声音采样文件格式的读写和音效处理器库。 它主要用于工具SoX,�对于其他声音应用程序都很有用。但是它的 license 是 GPL/LGPL , 在商业产品代码中使用要注意

例如:

#ifdef NDEBUG /* N.B. assert used with active statements so enable always. */
#undef NDEBUG /* Must undef above assert.h or other that might include it. */
#endif

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

/*
 * Reads input file, applies vol & flanger effects, stores in output file.
 * E.g. example1 monkey.au monkey.aiff
 */
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[10];

  assert(argc == 3);

  /* 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 `vol' effect, and initialise it with the desired parameters: */
  e = sox_create_effect(sox_find_effect("vol"));
  args[0] = "3dB", 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);

  /* Create the `flanger' effect, and initialise it with default parameters: */
  e = sox_create_effect(sox_find_effect("flanger"));
  assert(sox_effect_options(e, 0, NULL) == 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;
}

Reference

上一篇下一篇

猜你喜欢

热点阅读