-nframes_t
-SplineInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
-{
- // How many input samples we need
- nframes_t n = ceil (double(nframes) * _speed + phase[channel]);
-
- // hans - we run on 64bit systems too .... no casting pointer to a sized integer, please
- printf("======== n: %u nframes: %u input: %p, output: %p\n", n, nframes, input, output);
-
- if (n <= 3) {
- return 0;
- }
-
- double M[n], t[n-2];
-
- // natural spline: boundary conditions
- M[0] = 0.0;
- M[n - 1] = 0.0;
-
- if (input) {
- // solve L * t = d
- t[0] = 6.0 * (input[0] - 2*input[1] + input[2]);
- for (nframes_t i = 1; i <= n - 3; i++) {
- t[i] = 6.0 * (input[i] - 2*input[i+1] + input[i+2])
- - l(i-1) * t[i-1];
- }
-
- // solve U * M = t
- M[n-2] = t[n-3] / m(n-3);
- //printf(" M[%d] = %lf \n", n-1 ,M[n-1]);
- //printf(" M[%d] = %lf \n", n-2 ,M[n-2]);
- for (nframes_t i = n-4;; i--) {
- M[i+1] = (t[i]-M[i+2])/m(i);
- //printf(" M[%d] = %lf\n", i+1 ,M[i+1]);
- if ( i == 0 ) break;
- }
- M[1] = 0.0;
- M[n - 2] = 0.0;
- //printf(" M[%d] = %lf \n", 0 ,M[0]);
- }
-
- assert (M[0] == 0.0 && M[n-1] == 0.0);
-
- // now interpolate
- // index in the input buffers
- nframes_t i = 0;
-
- double acceleration;
- double distance = 0.0;
-
- if (_speed != _target_speed) {
- acceleration = _target_speed - _speed;
- } else {
- acceleration = 0.0;
- }
-
- distance = phase[channel];
- assert(distance >= 0.0 && distance < 1.0);
-
- for (nframes_t outsample = 0; outsample < nframes; outsample++) {
- i = floor(distance);
-
- double x = double(distance) - double(i);
-
- // if distance is something like 0.999999999999
- // it will get rounded to 1 in the conversion to float above
- while (x >= 1.0) {
- x -= 1.0;
- i++;
- }
-
- assert(x >= 0.0 && x < 1.0);
-
- if (input && output) {
- assert (i <= n-1);
- double a3 = (M[i+1] - M[i]) / 6.0;
- double a2 = M[i] / 2.0;
- double a1 = input[i+1] - input[i] - (M[i+1] + 2.0*M[i])/6.0;
- double a0 = input[i];
- // interpolate into the output buffer
- output[outsample] = ((a3*x + a2)*x + a1)*x + a0;
- //std::cout << "input[" << i << "/" << i+1 << "] = " << input[i] << "/" << input[i+1] << " distance: " << distance << " output[" << outsample << "] = " << output[outsample] << std::endl;
- }
- distance += _speed + acceleration;
- }
-
- i = floor(distance);
- phase[channel] = distance - floor(distance);
- assert (phase[channel] >= 0.0 && phase[channel] < 1.0);
- printf("Moved input frames: %u ", i);
-
- return i;
-}