Script to convert FLAC files using Shibatch Sample Rate Converter (SSRC) while preserving meta data

When using your PC as a high end audiophile transport device for delevering digital audio to an external DA-converter as explained in How to turn Music Player Daemon (mpd) into an audiophile music player, one has to make sure that the audio files are in a suitable audio format for the DAC. Consult the Background section below to determine which formats are suitable.

The flac-src-shibatch script will take a directory containing digital audio files, and convert it to a chosen bitdepth and sample rate using the not-too-bad Shibatch sample rate converter in “twopass non-fast mode” while preserving the flac metadata stored in the original files. It will put the original files in a tar ball for future usage.

Preparation

  1. Install SSRC: See Shibatch SSRC Packages for Debian and Ubuntu
  2. Install flac and sox*:
  3. Download and unpack the source for the script:
     

Ad *: Although sox can be used as an audio backend and sample rate and format converter, in this scenario we only install it for obtaining the convenient soxi file analyzer.

Usage

The script resamples and replaces the original files in the directory and makes a tar ball containing the original files:

Where $sourcedir is a directory containing high resolution tagged flac files.

Background

The designer of my excellent Pink Faun 3.24 USB DAC, fitted its DAC with a Tenor USB chip and limited the maximum rate to that of USB Audio Class 1 (24bit / 96KHz) to make sure alle available operating systems can use the DAC as an audio device without having to install additional drivers.

However, when buying audio at HD Tracks  or Channel Classics, I want to get the best format available; currently that would be 24bit/176KHz or 24bit/192KHz. After buying and downloading the files I use Musicbrainz Picard to tag and rename the original files. If necessary I create the Musicbrainz entries myself. I then convert (downsample and convert) the files using my script to 24bit/96KHz when they have a sample rate which is higher than 96KHz. Files with lower sample rates are to be left alone.

To determine which formats are natively accepted by your USB DAC, and how it actually behaves when feeding it a certain format, have a look at What digital audio format (bit depth and sample rate) does your alsa sound card support and what does it actually use?

The resulting quality

The quality of the resulting file depends on a number of factors. The most important factor is the ratio between the original and the desired samplerate.

For instance, when the source file is in a multitude of fs = 48KHz, eg. (fs*4 =) 192KHz or (fs*8 =) 384KHz, the conversion in my case is a matter of simple calculation; leave out each nnd sample, where n is the multiplier devided by two (fs*2 = 96KHz). This effectively makes the audio signal more course because the time between samples will grow with n. Furthermore, the avaliable frequency band will be narrowed (divied by n), following the Nyquist-Shannon theorem (fs/2 – width needed for the lowpass filter). For example a 192KHz sample signal leaves room for a (theoretical) frequency band of <96KHz while the downsampled 96Khz sample will be limited to <48KHz. However, there will be no further interpretation of the audio like in the cases below.

For formats based on fs=44.1KHz,  like (fs * 2 = ) 88.2KHz and (fs * 4 = ) 176.4KHz, I need to let shibatch interpret the source and calculate a new file based on its internal algorithms, a Finit impluse response filter (FIR) applied through Fast Fourier Transforms (FFT). Furthermore, shibatch can be told to apply dithering when desired. This of course means that your milage may vary, depending on the source material. It can be rewarding to try different parameters while listening to the resulting file(s).

The scripts is available for downloading, sharing and modification at https://github.com/ronalde/flac-src-shibatch.

Related articles