-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]) + 1;
- //printf("n = %d\n", n);
-
- 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);
- for (nframes_t i = n-4;; i--) {
- M[i+1] = (t[i]-M[i+2])/m(i);
- if ( i == 0 ) break;
- }
- }
-
- 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];
- for (nframes_t outsample = 0; outsample < nframes; outsample++) {
- i = floor(distance);
-
- Sample x = double(distance) - double(i);
-
- // if distance is something like 0.999999999999
- // it will get rounded to 1 in the conversion to float above
- if (x >= 1.0) {
- x = 0.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;
- }
- distance += _speed + acceleration;
- }
-
- i = floor(distance);
- phase[channel] = distance - floor(distance);
- assert (phase[channel] >= 0.0 && phase[channel] < 1.0);
-
- return i;
-}