Hi-Res FLAC files are one of the pinnacles of the Hi-Fi audio experience. However, if you rip your CD into FLAC files, you might find that they are often hard to play outside the equipment they are made for. This can be an issue for users who own high-resolution audio tracks but do not have the tools to play them properly.
One way to fix this is to use sox to resample your Hi-Res FLAC files. This tutorial teaches the basics of sox and how to use it to resample FLAC files in Linux.
Tips: if you are confused over the different audio formats, check out this guide to understand the differences between MP3, AAC, FLAC and many other formats.
What Is Sox and How Does Resampling Work?
At its core, sox is a command-line audio processing program. Unlike Audacity, it allows you to manipulate audio files straight from your terminal. This makes sox useful for those who want to make simple edits or automate a complex audio task.
Resampling, on the other hand, is the process of using complex algorithms to transform a high fidelity signal to a lower one. This approach retains the quality of an audio track while reducing the amount of data that you store in your computer.
Aside from that, resampling also allows you to play your Hi-Res FLAC files on lower-end devices. A Rockbox iPod Video, for example, will lag and stutter while playing a 24-bit audio file.
Alternative: other than sox, you can also use flac2all to transcode FLAC files to other modern audio formats.
Resampling a Hi-Res FLAC File Using sox
Note: the steps below are for Ubuntu, but usage remains the same regardless of the Linux distribution you are using.
The first step in resampling FLAC files is to install both the sox utility and its dependencies. Run the following command in Debian and Ubuntu:
sudo apt install sox flac ffmpeg
Check whether it is properly installed: sox --version
.
Find both the Bit Depth and the Sample Rate of your FLAC file by using the built-in file
program to print these details in the terminal:
file sample.flac
In my case, my FLAC file has a bit depth of 24-bit and a sample rate of 96 kHz.
Finding the Right Resample Rate for sox
Find the right resample rate for your FLAC file. The general rule of thumb in resampling is that the target resample rate should be divisible to the original sample rate by 2 or 4.
You should only resample a 192 kHz FLAC file to either 96 or 48 kHz to ensure the resampling algorithm can transform the original signal with as little distortion as possible.
Optimizing the Resampling in Your Hi-Res FLAC File
Once you know the right resample rate to use, you can run sox to resample your FLAC file. In my case, I am resampling a 96 kHz file, so I am running the following command:
sox -S sample.flac -b 16 resample.flac rate -v -L 48000 dither
- The
-b
flag tells sox to set a new bit depth for the output file. For example, a value of 16 will set the new bit depth at 16-bit. - On the other hand, the
rate
option tells sox to set a new sample rate for the output file. - The
-v
flag forces sox to use the “Very High Quality” resampling algorithm. - The
-L
flag also forces sox to use a “Linear Phase” response during resampling. This is a filter that attempts to neutralize any “signal echo” in the FLAC file. - Lastly, the
dither
option generates a small amount of noise across the whole track. This serves as a mask that will hide any imperfections in the resampling process.
Automating the Resampling Process
While it is doable to use sox on a per-file basis, it is also possible to automate the entire process using the shell. (Check out the beginner’s guide to shell scripting.) This is useful if you intend to resample an entire album of Hi-Res FLAC tracks.
One of the quickest ways to automate the resampling process is to use a Bash for loop. For example, you can run the following lines of code to resample an entire folder of FLAC files:
mkdir -p ./flac-dir/resample && cd ./flac-dir for flac in *.flac; do sox -S "${flac}" -b 16 resample/"${flac}" rate -v -L 48000 dither; done
The first command will create the output folder for the new FLAC files as well as change the current working directory. The second command will loop through every file in the current folder and resample it using sox.
Frequently Asked Questions
Is is possible to resample an MP3 file using sox?
The rate command for sox does not work with lossy formats, so it can’t be used to resample an MP3 file.
I am getting a "no handler for detected file type" error. Is sox broken?
No! This happens whenever sox is unable to read and decode your High Resolution FLAC file. The most common causes for this problem are a missing system library and missing compiler flag.
To fix the missing system library, run the following command: sudo apt install libsox-fmt-all
. Fixing the missing compiler flag will require you to uninstall the current sox package and compile it from source.
Sox is reporting that my Hi-Res FLAC file is clipping. Did my resampling fail?
It is normal for a High Resolution FLAC file to clip during a resample, as the resampling process will omit most of a FLAC file’s higher and inaudible frequency data.
Despite this, it is still possible to tell sox to compensate for possible clipping during resampling. To do this, add the -G
flag to your sox command: sox -S sample.flac -G -b 16 resample.flac rate -v -L 48000 dither
.
Image credit: Unsplash. All alterations and screenshots by Ramces Red.
Our latest tutorials delivered straight to your inbox