4 #include "ardour/interpolation.h"
6 using namespace ARDOUR;
10 LinearInterpolation::interpolate (int channel, framecnt_t nframes, Sample *input, Sample *output)
12 // index in the input buffers
15 double acceleration = 0;
17 if (_speed != _target_speed) {
18 acceleration = _target_speed - _speed;
21 for (framecnt_t outsample = 0; outsample < nframes; ++outsample) {
22 double const d = phase[channel] + outsample * (_speed + acceleration);
24 Sample fractional_phase_part = d - i;
25 if (fractional_phase_part >= 1.0) {
26 fractional_phase_part -= 1.0;
30 if (input && output) {
31 // Linearly interpolate into the output buffer
33 input[i] * (1.0f - fractional_phase_part) +
34 input[i+1] * fractional_phase_part;
38 double const distance = phase[channel] + nframes * (_speed + acceleration);
40 phase[channel] = distance - i;
45 CubicInterpolation::interpolate (int channel, framecnt_t nframes, Sample *input, Sample *output)
47 // index in the input buffers
51 double distance = 0.0;
53 if (_speed != _target_speed) {
54 acceleration = _target_speed - _speed;
59 distance = phase[channel];
62 /* no interpolation possible */
64 for (i = 0; i < nframes; ++i) {
71 /* keep this condition out of the inner loop */
73 if (input && output) {
77 if (floor (distance) == 0.0) {
78 /* best guess for the fake point we have to add to be able to interpolate at i == 0:
79 .... maintain slope of first actual segment ...
81 inm1 = input[i] - (input[i+1] - input[i]);
86 for (framecnt_t outsample = 0; outsample < nframes; ++outsample) {
88 float f = floor (distance);
89 float fractional_phase_part = distance - f;
91 /* get the index into the input we should start with */
95 /* fractional_phase_part only reaches 1.0 thanks to float imprecision. In theory
96 it should always be < 1.0. If it ever >= 1.0, then bump the index we use
97 and back it off. This is the point where we "skip" an entire sample in the
98 input, because the phase part has accumulated so much error that we should
99 really be closer to the next sample. or something like that ...
102 if (fractional_phase_part >= 1.0) {
103 fractional_phase_part -= 1.0;
107 // Cubically interpolate into the output buffer: keep this inlined for speed and rely on compiler
108 // optimization to take care of the rest
109 // shamelessly ripped from Steve Harris' swh-plugins (ladspa-util.h)
111 output[outsample] = input[i] + 0.5f * fractional_phase_part * (input[i+1] - inm1 +
112 fractional_phase_part * (4.0f * input[i+1] + 2.0f * inm1 - 5.0f * input[i] - input[i+2] +
113 fractional_phase_part * (3.0f * (input[i] - input[i+1]) - inm1 + input[i+2])));
115 distance += _speed + acceleration;
121 /* not sure that this is ever utilized - it implies that one of the input/output buffers is missing */
123 for (framecnt_t outsample = 0; outsample < nframes; ++outsample) {
124 distance += _speed + acceleration;
129 phase[channel] = distance - floor(distance);