Sox in phonetic research

From Phonlab
Revision as of 14:51, 8 October 2013 by Ronald (talk | contribs) (Created page with "# Using `sox` in phonetic research `sox` is a great command line tool for working with audio files, and this page illustrates some of the ways phoneticians can make use of it in…")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search
  1. Using `sox` in phonetic research

`sox` is a great command line tool for working with audio files, and this page illustrates some of the ways phoneticians can make use of it in their research. Once you have learned its syntax you can perform a wide variety of audio processing tasks that can easily be incorporated into a batch processing script to handle hundreds or thousands audio files.

In all of these examples, input and output files are assumed to be single-channel, unless specified otherwise. The Bash shell is also assumed, though most of the examples will work in other shells.

These are not the only possible usages, and you should consult the [`sox` documentation](http://sox.sourceforge.net/Docs/Documentation) for additional effects and options.

    1. `sox` command line basics

The basic syntax of a `sox` command is:

   > sox input1 [input2..n] output [effect]

An English translation of this is: 'Exchange this file [these files] into that file [by means of some effect].' (The name `sox` derives from 'Sound eXchange'.) Portions of the command surrounded by `[]` are optional.

It's important to note that the `sox` command always looks for at least one input file and an output file, even when it's not obvious what those files should be. For example, when you want `sox` to generate one second of white noise and don't need to copy sample rate, etc. from some other file, then specifying an input file may not make sense. In such an event, you can substitute the special filename `-n` where the input file normally goes in the command line to tell `sox` not to look for a real input file.

Other times you might not want to create a real output file, for example, if you want the output of one `sox` command to be the input for a second `sox` command without creating an intermediate file. In that case you can use the special filename `-p` in place of the output filename to tell `sox` that the output will be piped to another command's input.

    1. Converting audio file types, formats, and channels
      1. Convert file type

This converts an .au file to .wav:

   > sox input.au output.wav
      1. Change sample rate

This resamples `input.wav` and creates a new file with a sample rate of 22050Hz:

   > sox input.wav -r 22050 output.wav
      1. Change sample rate and data size

This creates a new file with the specified sample rate and specifies that sample values will be 8 bits/sample.

   > sox input.wav -r 22050 -b 8 output.wav
      1. Change sample rate and file type:

This converts .wav to .aiff and resamples the signal to 12000Hz:

   > sox input.wav -r 12000 output.aiff
      1. Override a sample rate

Occasionally an audio file's header misidentifies the sample rate of the signal because of an error when the file was created or processed (or if the file is a raw audio file there is no header). You can override the header sample rate value by specifying the real sample rate as an input file option:

   > sox -r 22050 input.wav output.wav