A NodeJS interface to SoX audio utilities
This Node.js module abstracts the command-line usage of SoX so you can create complex SoX commands and run them with ease. You must have SoX installed in order to use this module.
You must have sox installed in order to use this module, and you must add it to your PATH. Visit the sox website to download it.
You can install sox-audio through npm:
npm install sox-audio
There are many usage examples in the examples folder, including how to concatenate and trim files, and how to transcode a raw audio stream into a wav audio stream.
The sox-audio module returns a constructor that you can use to instantiate Sox commands. You can instantiate a SoxCommand with or without the
var SoxCommand = ;var command = ;
You may pass an input file name or readable stream, and/or an options object, to the constructor.
var command = ;var command = ;var command = ;
SoxCommands accept one or any number of inputs. There are 4 different acceptable types of inputs:
fs.createReadStream('examples/assets/utterance_0.wav'), however only one input stream may be used per command
'-p'for piping the result of this subcommand as input into the main SoxCommand, and it must provide an outputFileType. You may use more than one input of this type in a command.
'|sox examples/assets/utterance_0.wav -t wav -p trim 5'. For this string, follow the format specified in the SoX documentation. You may use more than one input of this type in a command.
// Passing an input to the constructor is the same as calling .input()var command1 =input'examples/assets/utterance_1.wav'inputfs;// A string for a subcommand may be passed as input, following the format '|program [options]'.// The program in the subcommand does not have to be sox, it could be any program whose stdout// you want to use as an input file.var command2 =input'|sox examples/assets/utterance_0.wav -t wav -p trim 5 35';// We can implement the same behavior as command2 using another SoxCommand as a subcommandvar trimSubcommand =input'examples/assets/utterance_0.wav';var command3 =;
These methods set input-related options on the input that was most recently added, so you must add an input before calling these.
inputSampleRate(sampleRate)Set the sample rate in Hz (or kHz if appended with a 'k')
inputBits(bitRate)Set the number of bits in each encoded sample
inputEncoding(encoding)Set the audio encoding type (sometimes needed with file-types that support more than one encoding, like raw or wav). The main available encoding types are:
inputChannels(numChannels)Set the number of audio channels in the audio file
inputFileType(fileType)Set the type of the audio file
var command = ;commandinputinputStream;
SoxCommands accept one or any number of outputs. There are 3 different acceptable types of outputs:
fs.createWriteStream('examples/outputs/utterance_0.wav'), however only one output stream may be used per command
'--sox-pipe', this can be used in place of an output filename to specify that the Sox command should be used as an input pipe into another Sox command. You may refer to the sox documentation for -p
These methods set output-related options on the output that was most recently added, so you must add an output before calling these.
outputSampleRate(sampleRate)Set the sample rate in Hz (or kHz if appended with a 'k')
outputBits(bitRate)Set the number of bits in each encoded sample
outputEncoding(encoding)Set the audio encoding type (sometimes needed with file-types that support more than one encoding, like raw or wav). The main available encoding types are:
outputChannels(numChannels)Set the number of audio channels in the audio file
outputFileType(fileType)Set the type of the audio file to output, particularly important when the output is a stream
var command = ;commandinputinputStream;command;
SoX can be used to invoke a number of audio 'effects', which should be provided at the end of the command. Multiple effects may be applied by specifying them one after another. You can learn about all of the effects available to SoX here. SoxCommand currently providers helpers for some popular effects, and a catch-all method to apply any of the other effects.
Cuts portions out of the audio. Any number of positions may be given, either individually or as a list, and each position can be a number or formatted string. Once the first position is reached, the effect alternates between copying and discarding the audio at each position. Using 0 for the first position allows copying from the beginning of the audio. The format for a position can be:
5.2a number indicating 5.2 seconds from the previous position or the start of the audio if there was no previous
'15:25.30'a string indicating 15 minutes, 25 seconds, and 30 milliseconds from the previous position or the start of the audio file
'=2:05'a string indicating 2 minutes and 5 seconds into the audio file, relative to the start of the audio
'-3:30'a string indicating 3 minutes and 30 seconds before the end of the audio file
'=1250s'a string indicate 1250 samples into the audio file
Select the input file combining method, which can be one of the following strings:
A shorthand for applying the concatenate combining method. The audio from each input will be concatenated in the order added to the command to form the output file. The input files must have the same number of channels.
A catch-all method allowing you to apply any SoX effect. Apply the SoX effect with
effectName using the command line options provided in
effectOptionsList. Please refer to the SoX documentation on all available effects and their usages.
Starting the SoxCommand is as easy as
Of course, you need to fully specify the command first, with inputs, outputs, and optional effects. You can also set listeners on your command for
var command =input...;command;command;command;command;command;command;
The following example concatenates three audio files while trimming off the first 5 seconds of the first file, and the last 10 seconds of the last file through subcommands.
var SoxCommand = ;var TimeFormat = SoxCommandTimeFormat;var command = ;var startTimeFormatted = TimeFormat;var endTimeFormatted = TimeFormat;var trimFirstFileSubCommand =input'./assets/utterance_0.wav';var trimLastFileSubCommand =input'./assets/utterance_2.wav';commandinput'./assets/utterance_1.wav';;command;
In this example, a 44.1 kHz raw audio stream is converted on the fly to a 16 kHz wav audio stream.
var command = ;commandinputinputStream;command;
Check out this repo, make your changes, commit the code and then publish a new release on npm:
This code is under the MIT License.