Hi guys, I promised it a lot of time ago, but finally I had some time in the last two days to check these resampling algorithms I wrote and do some measurements. I haven't adapted them to the Resampler API because I still haven't managed to get some sound out of Haiku, so I hope someone else will test them and do the necessary adjustments (as said, I haven't got much time). Then some optimizations are possible, like preventing calculations on denormals (Intel P4) or check whether the # of output samples is a multiple of the # of input samples (or viceversa), etc., but however these are a good starting point, I guess. The first one is a linear interpolator: it's incredibly fast and I get circa 72-74 dB SNR. The second one is a cubic spline interpolator (4 points at a time) wihch is still fast and the quality is very good too: around 90-92 dB SNR. The third one is meant to be based on the Shannon interpolation formula (sinc-based interpolation) which theoretically should give even better results, but I think I've done something wrong and still haven't got a clue of what isn't working. I put it here in case someone knows how to fix it. Here's the code, MIT licensed: --- Copyright (c) 2007 Stefano D'Angelo 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. 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. #include <math.h> #define PI 3.14159265358979323846264338327950288419716939937510 void linear(float in_buf[], int n_in_samples, float out_buf[], int n_out_samples) { /* Position relative to the input buffer */ float x = (float) n_in_samples / (float) n_out_samples; int i = x; /* Input buffer index */ int j = 1; /* Output buffer index */ /* The first sample is always the same. */ out_buf[0] = in_buf[0]; /* Interpolation cycle. */ while (i < (n_in_samples - 1)) { out_buf[j] = in_buf[i] + (in_buf[i + 1] - in_buf[i]) * (x - (float) i); j++; /* Calculating the position each time gives much better results * than adding a fixed 'step' value. */ x = j * (float) n_in_samples / (float) n_out_samples; i = x; } /* Using the last slope for output samples beyond the last input * sample. */ while (j < n_out_samples) { out_buf[j] = in_buf[n_in_samples - 1] + (in_buf[n_in_samples - 1] - in_buf[n_in_samples - 2]) * (x - (float) i); j++; x = j * (float) n_in_samples / (float) n_out_samples; i = x; } } void cubic_spline(float in_buf[], int n_in_samples, float out_buf[], int n_out_samples) { /* Position relative to the input buffer */ float x = (float) n_in_samples / (float) n_out_samples; int i = x; /* Input buffer index */ int j = 1; /* Output buffer index */ float a, b, c; float rel_x, rel_x2, rel_x3; out_buf[0] = in_buf[0]; /* Interpolation beetween the first and second input sample. */ while (!i) { a = + 1.666667e-1 * in_buf[i + 3] - 0.5 * in_buf[i + 2] + 0.5 * in_buf[i + 1] - 1.666667e-1 * in_buf[i]; b = - 0.5 * in_buf[i + 3] + 2.0 * in_buf[i + 2] - 2.5 * in_buf[i + 1] + in_buf[i]; c = + 3.333333e-1 * in_buf[i + 3] - 1.5 * in_buf[i + 2] + 3.0 * in_buf[i + 1] - 1.833333 * in_buf[i]; rel_x = x - (float) i; rel_x2 = rel_x * rel_x; rel_x3 = rel_x2 * rel_x; out_buf[j] = a * rel_x3 + b * rel_x2 + c * rel_x + in_buf[i]; j++; x = j * (float) n_in_samples / (float) n_out_samples; i = x; } /* Cubic spline interpolation. */ while (i < (n_in_samples - 2)) { a = + 1.666667e-1 * in_buf[i + 2] - 0.5 * in_buf[i + 1] + 0.5 * in_buf[i] - 1.666667e-1 * in_buf[i - 1]; b = - 0.5 * in_buf[i + 2] + 2.0 * in_buf[i + 1] - 2.5 * in_buf[i] + in_buf[i - 1]; c = + 3.333333e-1 * in_buf[i + 2] - 1.5 * in_buf[i + 1] + 3.0 * in_buf[i] - 1.833333 * in_buf[i - 1]; rel_x = 1.0 + x - (float) i; rel_x2 = rel_x * rel_x; rel_x3 = rel_x2 * rel_x; out_buf[j] = a * rel_x3 + b * rel_x2 + c * rel_x + in_buf[i - 1]; j++; x = j * (float) n_in_samples / (float) n_out_samples; i = x; } /* Interpolation beetween the last two input samples. */ while (i < (n_in_samples - 1)) { a = + 1.666667e-1 * in_buf[i + 1] - 0.5 * in_buf[i] + 0.5 * in_buf[i - 1] - 1.666667e-1 * in_buf[i - 2]; b = - 0.5 * in_buf[i + 1] + 2.0 * in_buf[i] - 2.5 * in_buf[i - 1] + in_buf[i - 2]; c = + 3.333333e-1 * in_buf[i + 1] - 1.5 * in_buf[i] + 3.0 * in_buf[i - 1] - 1.833333 * in_buf[i - 2]; rel_x = 2.0 + x - (float) i; rel_x2 = rel_x * rel_x; rel_x3 = rel_x2 * rel_x; out_buf[j] = a * rel_x3 + b * rel_x2 + c * rel_x + in_buf[i - 2]; j++; x = j * (float) n_in_samples / (float) n_out_samples; i = x; } /* Beyond the last sample. */ while (j < n_out_samples) { rel_x = 3.0 + (x - (float) (n_in_samples - 1)); rel_x2 = rel_x * rel_x; rel_x3 = rel_x2 * rel_x; out_buf[j] = a * rel_x3 + b * rel_x2 + c * rel_x + in_buf[n_in_samples - 4]; j++; x = j * (float) n_in_samples / (float) n_out_samples; i = x; } } /* FIXME: fix it. */ /* TODO: use splines and not linear interpolation where it is not possible to * use sinc. */ void sinc(float in_buf[], int n_in_samples, float out_buf[], int n_out_samples) { /* Position relative to the input buffer */ float x = (float) n_in_samples / (float) n_out_samples; int i = x; /* Input buffer index */ int j = 1; /* Output buffer index */ float rel_x; out_buf[0] = in_buf[0]; /* Sinc interpolation starts after the third sample. */ while (i < 2) { /* Linear interpolation */ out_buf[j] = in_buf[0] + (in_buf[1] - in_buf[0]) * x; j++; x = j * (float) n_in_samples / (float) n_out_samples; i = x; } /* Sinc interpolation */ while (i < (n_in_samples - 2)) { rel_x = x - (float) i; if (rel_x < 1e-10) out_buf[j] = in_buf[i]; else out_buf[j] = in_buf[i + 2] * sin(PI * (rel_x - 2.0)) / (PI * (rel_x - 2.0)) + in_buf[i + 1] * sin(PI * (rel_x - 1.0)) / (PI * (rel_x - 1.0)) + in_buf[i] * sin(PI * rel_x) / (PI * rel_x) + in_buf[i - 1] * sin(PI * (rel_x + 1.0)) / (PI * (rel_x + 1.0)) + in_buf[i - 2] * sin(PI * (rel_x + 2.0)) / (PI * (rel_x + 2.0)); j++; x = j * (float) n_in_samples / (float) n_out_samples; i = x; } /* Linear interoplation again here. */ while (i < (n_in_samples - 1)) { out_buf[j] = in_buf[i] + (in_buf[i + 1] - in_buf[i]) * (x - (float) i); j++; x = j * (float) n_in_samples / (float) n_out_samples; i = x; } /* Using the last slope for samples beyond the last input sample. */ while (j < n_out_samples) { out_buf[j] = in_buf[n_in_samples - 1] + (in_buf[n_in_samples - 1] - in_buf[n_in_samples - 2]) * (x - (float) i); j++; x = j * (float) n_in_samples / (float) n_out_samples; i = x; } } ---- Hope you find it useful. For anything, I'm always here. Stefano