2 #include <samplerate.h>
4 #include "ardour/types.h"
6 #ifndef __interpolation_h__
7 #define __interpolation_h__
13 double _speed, _target_speed;
16 Interpolation () { _speed = 1.0; }
18 void set_speed (double new_speed) { _speed = new_speed; }
19 void set_target_speed (double new_speed) { _target_speed = new_speed; }
21 double target_speed() const { return _target_speed; }
22 double speed() const { return _speed; }
24 void add_channel_to (int input_buffer_size, int output_buffer_size) {}
25 void remove_channel_from () {}
30 // 40.24 fixpoint math
31 #define FIXPOINT_ONE 0x1000000
33 class FixedPointLinearInterpolation : public Interpolation {
35 /// speed in fixed point math
38 /// target speed in fixed point math
41 std::vector<uint64_t> last_phase;
43 // Fixed point is just an integer with an implied scaling factor.
44 // In 40.24 the scaling factor is 2^24 = 16777216,
45 // so a value of 10*2^24 (in integer space) is equivalent to 10.0.
47 // The advantage is that addition and modulus [like x = (x + y) % 2^40]
48 // have no rounding errors and no drift, and just require a single integer add.
51 static const int64_t fractional_part_mask = 0xFFFFFF;
52 static const Sample binary_scaling_factor = 16777216.0f;
56 FixedPointLinearInterpolation () : phi (FIXPOINT_ONE), target_phi (FIXPOINT_ONE) {}
58 void set_speed (double new_speed) {
59 target_phi = (uint64_t) (FIXPOINT_ONE * fabs(new_speed));
63 uint64_t get_phi() { return phi; }
64 uint64_t get_target_phi() { return target_phi; }
65 uint64_t get_last_phase() { assert(last_phase.size()); return last_phase[0]; }
66 void set_last_phase(uint64_t phase) { assert(last_phase.size()); last_phase[0] = phase; }
68 void add_channel_to (int input_buffer_size, int output_buffer_size);
69 void remove_channel_from ();
71 nframes_t interpolate (int channel, nframes_t nframes, Sample* input, Sample* output);
75 class LinearInterpolation : public Interpolation {
77 // the idea is that when the speed is not 1.0, we have to
78 // interpolate between samples and then we have to store where we thought we were.
79 // rather than being at sample N or N+1, we were at N+0.8792922
80 std::vector<double> phase;
83 void add_channel_to (int input_buffer_size, int output_buffer_size);
84 void remove_channel_from ();
86 nframes_t interpolate (int channel, nframes_t nframes, Sample* input, Sample* output);
90 class LibSamplerateInterpolation : public Interpolation {
92 std::vector<SRC_STATE*> state;
93 std::vector<SRC_DATA*> data;
100 LibSamplerateInterpolation ();
101 ~LibSamplerateInterpolation ();
103 void set_speed (double new_speed);
104 void set_target_speed (double new_speed) {}
105 double speed () const { return _speed; }
107 void add_channel_to (int input_buffer_size, int output_buffer_size);
108 void remove_channel_from ();
110 nframes_t interpolate (int channel, nframes_t nframes, Sample* input, Sample* output);
111 void reset() { reset_state (); }
114 } // namespace ARDOUR