2 #include <samplerate.h>
4 #include "ardour/types.h"
6 #ifndef __interpolation_h__
7 #define __interpolation_h__
13 double _speed, _target_speed;
15 // the idea is that when the speed is not 1.0, we have to
16 // interpolate between samples and then we have to store where we thought we were.
17 // rather than being at sample N or N+1, we were at N+0.8792922
18 std::vector<double> phase;
22 Interpolation () { _speed = 1.0; _target_speed = 1.0; }
23 ~Interpolation () { phase.clear(); }
25 void set_speed (double new_speed) { _speed = new_speed; _target_speed = new_speed; }
26 void set_target_speed (double new_speed) { _target_speed = new_speed; }
28 double target_speed() const { return _target_speed; }
29 double speed() const { return _speed; }
31 void add_channel_to (int input_buffer_size, int output_buffer_size) { phase.push_back (0.0); }
32 void remove_channel_from () { phase.pop_back (); }
35 for (size_t i = 0; i < phase.size(); i++) {
41 // 40.24 fixpoint math
42 #define FIXPOINT_ONE 0x1000000
44 class FixedPointLinearInterpolation : public Interpolation {
46 /// speed in fixed point math
49 /// target speed in fixed point math
52 std::vector<uint64_t> last_phase;
54 // Fixed point is just an integer with an implied scaling factor.
55 // In 40.24 the scaling factor is 2^24 = 16777216,
56 // so a value of 10*2^24 (in integer space) is equivalent to 10.0.
58 // The advantage is that addition and modulus [like x = (x + y) % 2^40]
59 // have no rounding errors and no drift, and just require a single integer add.
62 static const int64_t fractional_part_mask = 0xFFFFFF;
63 static const Sample binary_scaling_factor = 16777216.0f;
67 FixedPointLinearInterpolation () : phi (FIXPOINT_ONE), target_phi (FIXPOINT_ONE) {}
69 void set_speed (double new_speed) {
70 target_phi = (uint64_t) (FIXPOINT_ONE * fabs(new_speed));
74 uint64_t get_phi() { return phi; }
75 uint64_t get_target_phi() { return target_phi; }
76 uint64_t get_last_phase() { assert(last_phase.size()); return last_phase[0]; }
77 void set_last_phase(uint64_t phase) { assert(last_phase.size()); last_phase[0] = phase; }
79 void add_channel_to (int input_buffer_size, int output_buffer_size);
80 void remove_channel_from ();
82 nframes_t interpolate (int channel, nframes_t nframes, Sample* input, Sample* output);
86 class LinearInterpolation : public Interpolation {
90 nframes_t interpolate (int channel, nframes_t nframes, Sample* input, Sample* output);
93 class CubicInterpolation : public Interpolation {
95 // shamelessly ripped from Steve Harris' swh-plugins
96 static inline float cube_interp(const float fr, const float inm1, const float
97 in, const float inp1, const float inp2)
99 return in + 0.5f * fr * (inp1 - inm1 +
100 fr * (4.0f * inp1 + 2.0f * inm1 - 5.0f * in - inp2 +
101 fr * (3.0f * (in - inp1) - inm1 + inp2)));
105 nframes_t interpolate (int channel, nframes_t nframes, Sample* input, Sample* output);
110 * @class SplineInterpolation
112 * @brief interpolates using cubic spline interpolation over an input period
114 * Splines are piecewise cubic functions between each samples,
115 * where the cubic polynomials' values, first and second derivatives are equal
116 * on each sample point.
118 * The interpolation polynomial in the i-th interval then has the form
119 * p_i(x) = a3 (x - i)^3 + a2 (x - i)^2 + a1 (x - i) + a0
120 * = ((a3 * (x - i) + a2) * (x - i) + a1) * (x - i) + a0
122 * a3 = (M[i+1] - M[i]) / 6
124 * a1 = y[i+1] - y[i] - M[i+1]/6 - M[i]/3
127 * The M's are calculated recursively:
128 * M[i+2] = 6.0 * (y[i] - 2y[i+1] + y[i+2]) - 4M[i+1] - M[i]
131 class SplineInterpolation : public Interpolation {
137 SplineInterpolation();
138 nframes_t interpolate (int channel, nframes_t nframes, Sample* input, Sample* output);
141 class LibSamplerateInterpolation : public Interpolation {
143 std::vector<SRC_STATE*> state;
144 std::vector<SRC_DATA*> data;
151 LibSamplerateInterpolation ();
152 ~LibSamplerateInterpolation ();
154 void set_speed (double new_speed);
155 void set_target_speed (double new_speed) {}
156 double speed () const { return _speed; }
158 void add_channel_to (int input_buffer_size, int output_buffer_size);
159 void remove_channel_from ();
161 nframes_t interpolate (int channel, nframes_t nframes, Sample* input, Sample* output);
162 void reset() { reset_state (); }
165 } // namespace ARDOUR