SIMPLE RATE CONVERTER
Pieter Suurmond, 2009
Open source 32->64 bit transparent audio samplerate converter in portable C.
FEATURES
- Only integer (multiply-accumulate) calculations during DSP (the problem with
floating point lies merely in the additions, not in the multiplications).
- Floating point is only used during the calculation of filter coefficients
(for easier goniometric- and Bessel-functions).
- A functional design (which is easier to maintain and read, though much less
efficient at runtime).
- Clarity of code.
- Transparence in sound.
- Monophonic as well as (interleaved) multichannel processing.
- The input/output ratio is represented by a rational number: an integer
numerator divided by an integer denominator. Furthermore, samplerates must
also be integers. (As a consequence, irrational conversions (like for
example 'pitch shifting' a sound by a tempered augmented fourth (sqrt(2)) is
not possible and must be approximated by a close ratio.
OPERATION
Audio is first upsampled (to maximally 20 times the least common multiple
of both the input and the requested samplerate) by inserting zero-samples.
Then, the upsampled audio is lowpass-filtered with a zero-phase FIR filter
which filtercoefficients (i.e. impulse-response) are formed by a Kaiser-
windowed sinc pulse. Then, the output is generated by decimation.
The Simple Rate Converter is completely transparant: audio data is never
rounded or clipped:
32 bit in --> SRC --> 64 bit out.
How you scale and round the output, and whether you dither it or not, is left
up to you. How many bits are added to your data by the SRC object can be
controlled by a gain parameter (8 is good 8-bit input; 16 is adviced for 16-
bit input; etc., thus doubling the number of bits).
DOWNLOAD
src-0.15.tar.gz Latest version.
On unix, one can unpack with 'tar zxf src-0.15.tar.gz' on the commandline.
COMPILATION
Move to the unpacked directory, where the Makefile resides, and simply type
'make all' on the commandline. This compiles the SRC example application
'aiffsrc' and selftest 'testsrc'.
Users with IDE-compilers can put the following 4 sourcefiles and 3 header-
files in a new project to build the AIFF-resampler demo application:
src.h demo/rdaiff.h demo/wraiff.h
src.c demo/rdaiff.c demo/wraiff.c demo/aiffsrc.c
And the following 2 sourcefiles and single headerfile to build the selftest:
src.h
src.c test/testsrc.c
DEMO
Example program aiffsrc converts AIFF files. Commandline syntax is as follows:
aiffsrc <SOUNDFILE> <RATE> [e]
Where <SOUNDFILE> is the pathname of an existing AIFF file and <RATE> is the
desired new samplerate: an integer number of sampleframes per second.
Optionally, the command may be ended with 'e' to force an exact lowpass filter
cutoff frequency. (Normally, without this 'e', a slightly lowered cutoff fre-
quency will be used to guarantee a safe transition band.) Examples:
aiffsrc recording1992.aif 22050 (to 22050 Hz with safe cutoff)
aiffsrc rec2001.aif 192000 e (to 192 kHz with critical cutoff)
Names of the output files are made by appending the requested samplerate and
options. Thus for example:
recording1992-22050.aif
rec2001-192000-e.aif
TODO
Note that the aiffsrc demo DOES throw away the extra bits (generated by multi-
plication and addition): 8 bit in gives 8 bit out, 16 bit in gives 16 bit out,
etcetera. But in principle, an 8 bit file could yield a 16-bit file,.. and so
on. Adding only 2 or 3 lines of code to aiffsrc.c will accomplish this, and
perhaps I'll add this as optional feature, in the future...
TEST
Type 'make selftest' to test the demo and run the selftest (with the lowest
verbosity level). When something goes wrong during the test, you can restart
it with increased verbosity level, for example:
test/testsrc v (dumps internal SRC parameters)
test/testsrc vv (also dumps filtercoefficients)
test/testsrc vvv (also dumps Fourier transform of Kaiser-sinc)
test/testsrc vvv > testreport (dumps all to a textfile)
API USAGE
For the programmer, it roughly works like this:
#include src.h
SRCp src = NULL; /* Create new converter object: */
new_SRC(&src, 96000, 44100, 2, 65536); /* stereo, from 96 to 44.1 kHz, */
int n; /* lowpass filter gain = 16 bits */
for (...all input frames...) /* (for 16 bit in --> 32 bit out).*/
{
n = do_SRC(src, input, &output); /* Body. */
...// store n outputframe(s).
}
for (...the remaining output frames...)
{
n = do_SRC(src, zero, &output); /* Tail. */
...// store n outputframe(s).
}
delete_SRC(&src); /* Release SRC object from memory. */
Function do_SRC() processes only 1 inputframe a time, and it may take a while
before it produces one or more (n) outputframes. This is caused by the group-
delay of the lowpass filter. For the same reason, one has to feed the con-
vertor some zero input samples after the last input sample, to get the tail
of the sound.
More precise (and actually working) example code can be found in
demo/aiffsrc.c. Headerfile src.h explains SRC usage in more detail.
SWEEP COMPARISON
Compared 3 open source sample rate converters with a frequency sweep up to
Nyquist (48 kHz) with a 24 bit, 96 kHz AIFF file converted to 44.1 kHz.
File sweep96k.aiff is the input file (generated by sweepgen.c), with a sine
sweep up to Nyquist (48 kHz) at -1.16 dB.
The following images were made with baudline (http://www.baudline.com/)
FFT size = 1024, Blackman window.
SECRET RABBIT CODE (libsamplerate version 0.1.2):
sndfile-resample -to 44100 -c 0 sweep96k.aiff sweep-rabbit_code.aiff
SOX (version 14.0.0):
sox sweep96k.aiff -r 44100 sweep-sox.aiff
SIMPLE RATE CONVERTER:
./aiffsrc sweep96k.aiff 44100
Below Nyquist, Simple Rate Converter exhibits less frequency mirrors than
Secret Rabbit Code and SOX.
Just below Nyquist, Simple Rate Converter's output is higher than the others.
But (just) above Nyquist, Simple Rate Converter seems to produce a stronger
mirror than Secret Rabbit Code and SOX.
One thing for sure: the Suurmond Converter is the slowest of all.
CREDITS
Thanks to Marc Groenewegen, Niels Bogaards and Maarten de Boer.
Website http://src.infinitewave.ca/ was also very helpful.
LICENSE
Simple Rate Converter version 0.15, april 18, 2009.
Latest version available at: http://kmt.hku.nl/~pieter/EDU/c/src/
Copyright (c) 2009 - Pieter Suurmond
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
Any person wishing to distribute modifications to the Software is
requested to send the modifications to the original developer so that
they can be incorporated into the canonical version.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.