4 #include "ardour/interpolation.h"
6 using namespace ARDOUR;
9 FixedPointLinearInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
11 // the idea behind phase is that when the speed is not 1.0, we have to
12 // interpolate between samples and then we have to store where we thought we were.
13 // rather than being at sample N or N+1, we were at N+0.8792922
14 // so the "phase" element, if you want to think about this way,
15 // varies from 0 to 1, representing the "offset" between samples
16 uint64_t the_phase = last_phase[channel];
21 // phi = fixed point speed
22 if (phi != target_phi) {
23 phi_delta = ((int64_t)(target_phi - phi)) / nframes;
28 // index in the input buffers
31 for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
33 Sample fractional_phase_part = (the_phase & fractional_part_mask) / binary_scaling_factor;
35 if (input && output) {
36 // Linearly interpolate into the output buffer
38 input[i] * (1.0f - fractional_phase_part) +
39 input[i+1] * fractional_phase_part;
42 the_phase += phi + phi_delta;
45 last_phase[channel] = (the_phase & fractional_part_mask);
52 FixedPointLinearInterpolation::add_channel_to (int /*input_buffer_size*/, int /*output_buffer_size*/)
54 last_phase.push_back (0);
58 FixedPointLinearInterpolation::remove_channel_from ()
60 last_phase.pop_back ();
64 FixedPointLinearInterpolation::reset()
66 for (size_t i = 0; i <= last_phase.size(); i++) {
73 LinearInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
75 // index in the input buffers
79 double distance = 0.0;
81 if (_speed != _target_speed) {
82 acceleration = _target_speed - _speed;
87 distance = phase[channel];
88 //printf("processing channel: %d\n", channel);
89 //printf("phase before: %lf\n", phase[channel]);
90 for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
92 Sample fractional_phase_part = distance - i;
93 if (fractional_phase_part >= 1.0) {
94 fractional_phase_part -= 1.0;
97 //printf("I: %u, distance: %lf, fractional_phase_part: %lf\n", i, distance, fractional_phase_part);
99 if (input && output) {
100 // Linearly interpolate into the output buffer
102 input[i] * (1.0f - fractional_phase_part) +
103 input[i+1] * fractional_phase_part;
105 //printf("distance before: %lf\n", distance);
106 distance += _speed + acceleration;
107 //printf("distance after: %lf, _speed: %lf\n", distance, _speed);
110 //printf("before assignment: i: %d, distance: %lf\n", i, distance);
112 //printf("after assignment: i: %d, distance: %16lf\n", i, distance);
113 phase[channel] = distance - floor(distance);
114 //printf("speed: %16lf, i after: %d, distance after: %16lf, phase after: %16lf\n", _speed, i, distance, phase[channel]);
119 SplineInterpolation::SplineInterpolation()
121 // precompute LU-factorization of matrix A
122 // see "Teubner Taschenbuch der Mathematik", p. 1105
124 for (int i = 0; i <= MAX_PERIOD_SIZE - 2; i++) {
131 SplineInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
133 // How many input samples we need
134 nframes_t n = ceil (double(nframes) * _speed) + 2;
135 // |------------------------------------------^
136 // this won't be here in the debugged version.
140 // natural spline: boundary conditions
145 // see "Teubner Taschenbuch der Mathematik", p. 1105
146 t[0] = 6.0 * (input[0] - 2*input[1] + input[2]);
147 for (nframes_t i = 1; i <= n - 3; i++) {
148 t[i] = 6.0 * (input[i] - 2*input[i+1] + input[i+2])
153 // see "Teubner Taschenbuch der Mathematik", p. 1105
154 M[n-2] = -t[n-3] / m[n-3];
155 for (nframes_t i = n-4;; i--) {
156 M[i+1] = -(t[i] + M[i+2]) / m[i];
161 // index in the input buffers
165 double distance = 0.0;
167 if (_speed != _target_speed) {
168 acceleration = _target_speed - _speed;
173 distance = phase[channel];
174 for (nframes_t outsample = 0; outsample < nframes; outsample++) {
177 Sample x = distance - i;
179 /* this would break the assertion below
186 if (input && output) {
188 double a0 = input[i];
189 double a1 = input[i+1] - input[i] - M[i+1]/6.0 - M[i]/3.0;
190 double a2 = M[i] / 2.0;
191 double a3 = (M[i+1] - M[i]) / 6.0;
192 // interpolate into the output buffer
193 output[outsample] = ((a3*x +a2)*x +a1)*x + a0;
195 distance += _speed + acceleration;
199 phase[channel] = distance - floor(distance);
204 LibSamplerateInterpolation::LibSamplerateInterpolation() : state (0)
209 LibSamplerateInterpolation::~LibSamplerateInterpolation()
211 for (size_t i = 0; i < state.size(); i++) {
212 state[i] = src_delete (state[i]);
217 LibSamplerateInterpolation::set_speed (double new_speed)
220 for (size_t i = 0; i < state.size(); i++) {
221 src_set_ratio (state[i], 1.0/_speed);
226 LibSamplerateInterpolation::reset_state ()
228 printf("INTERPOLATION: reset_state()\n");
229 for (size_t i = 0; i < state.size(); i++) {
231 src_reset (state[i]);
233 state[i] = src_new (SRC_SINC_FASTEST, 1, &error);
239 LibSamplerateInterpolation::add_channel_to (int input_buffer_size, int output_buffer_size)
241 SRC_DATA* newdata = new SRC_DATA;
243 /* Set up sample rate converter info. */
244 newdata->end_of_input = 0 ;
246 newdata->input_frames = input_buffer_size;
247 newdata->output_frames = output_buffer_size;
249 newdata->input_frames_used = 0 ;
250 newdata->output_frames_gen = 0 ;
252 newdata->src_ratio = 1.0/_speed;
254 data.push_back (newdata);
261 LibSamplerateInterpolation::remove_channel_from ()
263 SRC_DATA* d = data.back ();
267 src_delete (state.back ());
274 LibSamplerateInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
277 printf ("ERROR: trying to interpolate with no channels\n");
281 data[channel]->data_in = input;
282 data[channel]->data_out = output;
284 data[channel]->input_frames = nframes * _speed;
285 data[channel]->output_frames = nframes;
286 data[channel]->src_ratio = 1.0/_speed;
288 if ((error = src_process (state[channel], data[channel]))) {
289 printf ("\nError : %s\n\n", src_strerror (error));
293 //printf("INTERPOLATION: channel %d input_frames_used: %d\n", channel, data[channel]->input_frames_used);
295 return data[channel]->input_frames_used;