-nframes_t
-SplineInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
-{
-
- // now interpolate
- // index in the input buffers
- nframes_t i = 0, delta_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) {
- // if i changed, recalculate coefficients
- if (delta_i == 1) {
- // if i changed, rotate the M's
- M[0] = M[1];
- M[1] = M[2];
- M[2] = 6.0 * (input[i] - 2.0*input[i+1] + input[i+2]) - 4.0*M[1] - M[0];
- printf("\ny[%d] = %lf\n", i, input[i]);
- printf("y[%d] = %lf\n", i+1, input[i+1]);
- printf("y[%d] = %lf\n\n", i+2, input[i+2]);
- printf("M[2] = %lf M[1] = %lf M[0] = %lf y-term: %lf M-term: %lf\n",
- M[2], M[1], M[0], 6.0 * (input[i] - 2.0*input[i+1] + input[i+2]),
- - 4.0*M[1] - M[0]);
- }
- double a3 = (M[1] - M[0]) / 6.0;
- double a2 = M[0] / 2.0;
- double a1 = input[i+1] - input[i] - (M[1] + 2.0*M[0]) / 6.0;
- double a0 = input[i];
- // interpolate into the output buffer
- output[outsample] = ((a3*x + a2)*x + a1)*x + a0;
- //printf( "input[%d/%d] = %lf/%lf distance: %lf output[%d] = %lf\n", i, i+1, input[i], input[i+1], distance, outsample, output[outsample]);
-
- }
- distance += _speed + acceleration;
-
- delta_i = floor(distance) - i;
- }
-
- i = floor(distance);
- phase[channel] = distance - floor(distance);
- assert (phase[channel] >= 0.0 && phase[channel] < 1.0);
-
- return i;
-}