Science and technology

Convert audio information with this versatile Linux command

I work with media, and if you work with any type of media, you be taught fairly shortly that standardization is a priceless device. Just as you would not attempt to add a fraction to a decimal with out changing one or the opposite, I’ve realized that it is not preferrred to mix media of differing codecs. Most hobbyist-level functions make the conversion course of invisible to the consumer as a comfort. Flexible software program geared toward customers needing management over the high quality particulars of their property, nevertheless, typically go away it as much as you to transform your media to your required format upfront. I’ve a couple of favourite instruments for conversion, and a kind of is the so-called Swiss military knife of sound, SoX.

Installing

On Linux or BSD, you possibly can set up the sox command (and a few useful symlinks) out of your software program repository or ports tree.

You can even set up SoX from its residence on Sourceforge.net. It would not launch typically, however its codebase tends to be steady, so if you would like the most recent options (akin to Opus assist), it is easy and protected to construct.

SoX offers primarily the sox command, however set up additionally creates a couple of helpful symlinks: play, rec, and soxi.

Getting details about information with SoX

SoX reads and rewrites audio information. Whether it shops the rewritten audio information is as much as you. There are use circumstances wherein you need not retailer the transformed information, as an illustration, if you’re sending the output on to your audio system for playback. Before doing any conversion, nevertheless, it is often a good suggestion to find out precisely what you are coping with within the first place.

To collect details about an audio file, use the soxi command. This is a symlink to sox –info.

$ soxi countdown.mp3
Input File     : '/residence/tux/countdown.mp3'
Channels       : 1
Sample Rate    : 44100
Precision      : 16-bit
Duration       : 00:00:11.21 = 494185 samples...
File Size      : 179okay
Bit Rate       : 128okay
Sample Encoding: MPEG audio (layer I, II or III)

This output offers you a good suggestion of what codec the audio file is encoded in, the file size, file measurement, pattern price, and the variety of channels. Some of those you would possibly assume you already know, however I by no means belief assumptions when media is delivered to me by a consumer. Verify media attributes with soxi.

Converting information

In this instance, the audio of a recreation present countdown has been delivered as an MP3 file. While almost all enhancing functions settle for compressed audio, none of them truly edit the compressed information. Conversion is occurring someplace, whether or not it is a secret background process or a immediate so that you can save a duplicate. I usually favor to do the conversion myself, upfront. This method, I can management what format I am utilizing. I can do a lot of media in batches in a single day as an alternative of losing priceless manufacturing time ready for an enhancing utility to churn via them on demand.

The sox command is supposed for changing audio information. There are a couple of phases within the sox pipeline:

  • enter
  • mix
  • results
  • output

In command syntax, the results step is, confusingly, written final. That means the pipeline consists this manner:

enter → mix → output → results

Encoding

The easiest conversion command entails solely an enter file and an output file. Here’s the command to transform an MP3 file to a lossless FLAC file:

$ sox countdown.mp3 output.flac
$ soxi output.flac

Input File     : 'output.flac'
Channels       : 1
Sample Rate    : 44100
Precision      : 16-bit
Duration       : 00:00:11.18 = 493056 samples...
File Size      : 545okay
Bit Rate       : 390okay
Sample Encoding: 16-bit FLAC
Comment        : 'Comment=Processed by SoX'

Effects

The results chain is specified on the finish of a command. It can alter audio previous to sending the info to its ultimate vacation spot. For occasion, generally audio that is too loud may cause issues throughout conversion:

$ sox unhealthy.wav unhealthy.ogg
sox WARN sox: `unhealthy.ogg' output clipped 126 samples; lower quantity?

Applying a acquire impact can typically clear up this downside:

$ sox unhealthy.wav unhealthy.ogg acquire -1

Fade

Another helpful impact is fade. This impact helps you to outline the form of a fade-in or fade-out, together with what number of seconds you need the fade to span.

Here’s an instance of a six-second fade-in utilizing an inverted parabola:

$ sox intro.ogg intro.flac fade p 6

This applies a three-second fade-in to the top of the audio and a fade-out beginning on the eight-second mark (the intro music is just 11 seconds, so the fade-out can also be three-seconds on this case):

$ sox intro.ogg intro.flac fade p three eight

The totally different sorts of fades (sine, linear, inverted parabola, and so forth), in addition to the choices fade provides (fade-in, fade-out), are listed within the sox man web page.

Effect syntax

Each impact plugin has its personal syntax, so discuss with the person web page for particulars on the best way to invoke each.

Effects could be daisy-chained in a single command, a minimum of to the extent that you just need to mix them. In different phrases, there isn’t any syntax to use a flanger impact solely throughout a six-second fade-out. For one thing that exact, you want a graphical sound wave editor or a digital audio workstation akin to LMMS or Rosegarden. However, in the event you simply have results that you just need to apply as soon as, you possibly can listing them collectively in the identical command.

This command applies a -1 acquire impact, a tempo stretch of 1.35, and a fade-out:

$ sox intro.ogg output.flac acquire -1 stretch 1.35 fade p zero 6
$ soxi output.flac

Input File     : 'output.flac'
Channels       : 1
Sample Rate    : 44100
Precision      : 16-bit
Duration       : 00:00:15.10 = 665808 samples...
File Size      : 712okay
Bit Rate       : 377okay
Sample Encoding: 16-bit FLAC
Comment        : 'Comment=Processed by SoX'

Combining audio

SoX can even mix audio information, both by concatenating them or by mixing them.

To be part of (or concatenate) information into one, present a couple of enter file in your command:

$ sox countdown.mp3 intro.ogg output.flac

In this instance, output.flac now incorporates countdown audio, adopted instantly by intro music.

If you need the 2 tracks to play over each other on the similar time, although, you should use the –combine combine choice:

$ sox --combine combine countdown.mp3 intro.ogg output.flac

Imagine, nevertheless, that the 2 enter information differed in additional than simply their codecs. It’s not unusual for vocal tracks to be recorded in mono (one channel), however for music to be recorded in a minimum of stereo (two channels). SoX will not default to an answer, so you need to standardize the format of the 2 information your self first.

Altering audio information

Options associated to the file title listed after it. For occasion, the –channels choice on this command applies solely to enter.wav and NOT to instance.ogg or output.flac:

$ sox --channels 2 enter.wav instance.ogg output.flac

This signifies that the place of an choice could be very vital in SoX. Should you specify an choice initially of your command, you are primarily solely overriding what SoX gleans from the enter information by itself. Options positioned instantly earlier than the output file, nevertheless, decide how SoX writes the audio information.

To clear up the earlier downside of incompatible channels, you possibly can first standardize your inputs, after which combine:

$ sox countdown.mp3 --channels 2 countdown-stereo.flac acquire -1
$ soxi countdown-stereo.flac

Input File     : 'countdown-stereo.flac'
Channels       : 2
Sample Rate    : 44100
Precision      : 16-bit
Duration       : 00:00:11.18 = 493056 samples...
File Size      : 545okay
Bit Rate       : 390okay
Sample Encoding: 16-bit FLAC
Comment        : 'Comment=Processed by SoX'

$ sox --combine combine
countdown-stereo.flac
intro.ogg
output.flac

SoX completely requires a number of instructions for complicated actions, so it is regular to create a number of momentary and intermediate information as wanted.

Multichannel audio

Not all audio is constrained to 1 or two channels, in fact. If you need to mix a number of audio channels into one file, you are able to do that with SoX and the –combine merge choice:

$ sox --combine merge countdown.mp3 intro.ogg output.flac
$ soxi output.flac

Input File     : 'output.flac'
Channels       : three
[...]

Easy audio manipulation

It might sound unusual to work with audio utilizing no visible interface, and for some duties, SoX undoubtedly is not the most effective device. However, for a lot of duties, SoX offers a simple and light-weight toolkit. SoX is an easy command with highly effective potential. With it, you possibly can convert audio, manipulate channels and waveforms, and even generate your personal sounds. This article has solely supplied a short overview of its capabilities, so go learn its man web page or online documentation after which see what you possibly can create.

Most Popular

To Top