2 Copyright (C) 2012 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "ardour/interpolation.h"
24 #include "ardour/midi_buffer.h"
26 using namespace ARDOUR;
30 LinearInterpolation::interpolate (int channel, framecnt_t nframes, Sample *input, Sample *output)
32 // index in the input buffers
35 double acceleration = 0;
37 if (_speed != _target_speed) {
38 acceleration = _target_speed - _speed;
41 for (framecnt_t outsample = 0; outsample < nframes; ++outsample) {
42 double const d = phase[channel] + outsample * (_speed + acceleration);
44 Sample fractional_phase_part = d - i;
45 if (fractional_phase_part >= 1.0) {
46 fractional_phase_part -= 1.0;
50 if (input && output) {
51 // Linearly interpolate into the output buffer
53 input[i] * (1.0f - fractional_phase_part) +
54 input[i+1] * fractional_phase_part;
58 double const distance = phase[channel] + nframes * (_speed + acceleration);
60 phase[channel] = distance - i;
65 CubicInterpolation::interpolate (int channel, framecnt_t nframes, Sample *input, Sample *output)
67 // index in the input buffers
71 double distance = phase[channel];
73 if (_speed != _target_speed) {
74 acceleration = _target_speed - _speed;
80 /* no interpolation possible */
82 if (input && output) {
83 for (i = 0; i < nframes; ++i) {
92 /* keep this condition out of the inner loop */
94 if (input && output) {
95 /* best guess for the fake point we have to add to be able to interpolate at i == 0:
96 * .... maintain slope of first actual segment ...
98 Sample inm1 = input[i] - (input[i+1] - input[i]);
100 for (framecnt_t outsample = 0; outsample < nframes; ++outsample) {
101 /* get the index into the input we should start with */
102 i = floor (distance);
103 float fractional_phase_part = fmod (distance, 1.0);
105 // Cubically interpolate into the output buffer: keep this inlined for speed and rely on compiler
106 // optimization to take care of the rest
107 // shamelessly ripped from Steve Harris' swh-plugins (ladspa-util.h)
109 output[outsample] = input[i] + 0.5f * fractional_phase_part * (input[i+1] - inm1 +
110 fractional_phase_part * (4.0f * input[i+1] + 2.0f * inm1 - 5.0f * input[i] - input[i+2] +
111 fractional_phase_part * (3.0f * (input[i] - input[i+1]) - inm1 + input[i+2])));
113 distance += _speed + acceleration;
117 i = floor (distance);
118 phase[channel] = fmod (distance, 1.0);
121 /* used to calculate play-distance with acceleration (silent roll)
122 * (use same algorithm as real playback for identical rounding/floor'ing)
124 for (framecnt_t outsample = 0; outsample < nframes; ++outsample) {
125 distance += _speed + acceleration;
127 i = floor (distance);
128 phase[channel] = fmod (distance, 1.0);
134 /* CubicMidiInterpolation::distance is identical to
135 * return CubicInterpolation::interpolate (0, nframes, NULL, NULL);
138 CubicMidiInterpolation::distance (framecnt_t nframes, bool /*roll*/)
140 assert (phase.size () == 1);
145 double distance = phase[0];
148 /* no interpolation possible */
153 if (_speed != _target_speed) {
154 acceleration = _target_speed - _speed;
159 for (framecnt_t outsample = 0; outsample < nframes; ++outsample) {
160 distance += _speed + acceleration;
163 i = floor (distance);
164 phase[0] = fmod (distance, 1.0);