while using SDL for enabling audio in my application,
I see the opportunity to enhance the quality of the
audio converter (SDL_AudioCVT).
Currently the sample rate conversion is done by using
doubling every sample for up-conversion, or using
every second sample for down-conversion. This, while
working really fast, has the drawback of not taking
aliasing into account.
Here is an example: consider a sinus signal of 3.9 kHz
with a sample rate of 8kHz. If we change the sample
rate to 16 kHz with the algorithm mentioned above, we
got two signals, one with 3.9 kHz which is desired and
one with 4.1 kHz which is an aliasing artefact and
should be rejected.
In the enhanced audio converter this rejection is done
with an appropiate filter with an cut off frequence at
4 kHz, a quarter of the new sampling frequence.
For down-conversion, the signal is filtered first, and
the every second sample is taken.
If the ratio between source and destination rate is
larger then 2, the process is broken into steps to get
a high cutoff slope and achieve low computational load.
The conversation between abbitrary samplerates is done,
by approximate the ratio of src and dst rate by a
fraction n/d where an n,d < 17. This results in a
perfect match fo sample rate conversation 32kHz<->48kHz,
and and minor error of 0.3% for e.g. 44.1kHz<->48kHz
or 44.1kHz<->32kHz. The fraction has to be small, because
we need a representation of each filter precalculated.
So, if we use the 44.1->48 example above, which is
approximated by 12/11, we need 12 coefficient sets,
in the other direction we need 11 coefficient sets.
To avoid the implementation of the filter for every
sample-format, the structure of of the AudioCVT is
modified. In a first step the sample format is converted
to signed short, the second step changes the sample
rate, and the final step converts to the desired
destination format. The remaining targets for the filter
a mono and stereo format, which are taken care by a
template-like implementation, for maximum speed.
Finally the filter can work in two modes, one is called
Looped while the other is straight. Filter corrolate
samples before and after the actual one. Therefore, is
a looped signal is considered, the samples after the
end are the beginning ones, while in the straigt case
they are simply zeroes.
I the following mail the SDL_audio_converter.c, and to the
third and fourth mail the header files filter_templates.h
and SDL_audio_converter.h. While these files compile, no
test is perfomed, so it is unlikely that they are working
May I ask you to comment on the coding style and on
compatibilty issues? Is it likely that this code is included
in the SDL?