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 for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
90 Sample fractional_phase_part = distance - i;
91 if (fractional_phase_part >= 1.0) {
92 fractional_phase_part -= 1.0;
96 if (input && output) {
97 // Linearly interpolate into the output buffer
99 input[i] * (1.0f - fractional_phase_part) +
100 input[i+1] * fractional_phase_part;
102 distance += _speed + acceleration;
106 phase[channel] = distance - floor(distance);
112 CubicInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
114 // index in the input buffers
118 double distance = 0.0;
120 if (_speed != _target_speed) {
121 acceleration = _target_speed - _speed;
126 distance = phase[channel];
127 for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
129 Sample fractional_phase_part = distance - i;
130 if (fractional_phase_part >= 1.0) {
131 fractional_phase_part -= 1.0;
135 if (input && output) {
136 // Cubically interpolate into the output buffer
137 output[outsample] = cube_interp(fractional_phase_part, input[i-1], input[i], input[i+1], input[i+2]);
139 distance += _speed + acceleration;
143 phase[channel] = distance - floor(distance);
148 SplineInterpolation::SplineInterpolation()
150 // precompute LU-factorization of matrix A
151 // see "Teubner Taschenbuch der Mathematik", p. 1105
152 // We only need to calculate up to 20, because they
153 // won't change any more above that
155 for (int i = 0; i <= 20 - 2; i++) {
157 _m[i+1] = 4.0 - _l[i];
162 SplineInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
164 // How many input samples we need
165 nframes_t n = ceil (double(nframes) * _speed + phase[channel]);
167 // hans - we run on 64bit systems too .... no casting pointer to a sized integer, please
168 printf("======== n: %u nframes: %u input: %p, output: %p\n", n, nframes, input, output);
176 // natural spline: boundary conditions
182 t[0] = 6.0 * (input[0] - 2*input[1] + input[2]);
183 for (nframes_t i = 1; i <= n - 3; i++) {
184 t[i] = 6.0 * (input[i] - 2*input[i+1] + input[i+2])
189 M[n-2] = t[n-3] / m(n-3);
190 //printf(" M[%d] = %lf \n", n-1 ,M[n-1]);
191 //printf(" M[%d] = %lf \n", n-2 ,M[n-2]);
192 for (nframes_t i = n-4;; i--) {
193 M[i+1] = (t[i]-M[i+2])/m(i);
194 //printf(" M[%d] = %lf\n", i+1 ,M[i+1]);
199 //printf(" M[%d] = %lf \n", 0 ,M[0]);
202 assert (M[0] == 0.0 && M[n-1] == 0.0);
205 // index in the input buffers
209 double distance = 0.0;
211 if (_speed != _target_speed) {
212 acceleration = _target_speed - _speed;
217 distance = phase[channel];
218 assert(distance >= 0.0 && distance < 1.0);
220 for (nframes_t outsample = 0; outsample < nframes; outsample++) {
223 double x = double(distance) - double(i);
225 // if distance is something like 0.999999999999
226 // it will get rounded to 1 in the conversion to float above
232 assert(x >= 0.0 && x < 1.0);
234 if (input && output) {
236 double a3 = (M[i+1] - M[i]) / 6.0;
237 double a2 = M[i] / 2.0;
238 double a1 = input[i+1] - input[i] - (M[i+1] + 2.0*M[i])/6.0;
239 double a0 = input[i];
240 // interpolate into the output buffer
241 output[outsample] = ((a3*x + a2)*x + a1)*x + a0;
242 //std::cout << "input[" << i << "/" << i+1 << "] = " << input[i] << "/" << input[i+1] << " distance: " << distance << " output[" << outsample << "] = " << output[outsample] << std::endl;
244 distance += _speed + acceleration;
248 phase[channel] = distance - floor(distance);
249 assert (phase[channel] >= 0.0 && phase[channel] < 1.0);
250 printf("Moved input frames: %u ", i);
255 LibSamplerateInterpolation::LibSamplerateInterpolation() : state (0)
260 LibSamplerateInterpolation::~LibSamplerateInterpolation()
262 for (size_t i = 0; i < state.size(); i++) {
263 state[i] = src_delete (state[i]);
268 LibSamplerateInterpolation::set_speed (double new_speed)
271 for (size_t i = 0; i < state.size(); i++) {
272 src_set_ratio (state[i], 1.0/_speed);
277 LibSamplerateInterpolation::reset_state ()
279 printf("INTERPOLATION: reset_state()\n");
280 for (size_t i = 0; i < state.size(); i++) {
282 src_reset (state[i]);
284 state[i] = src_new (SRC_SINC_FASTEST, 1, &error);
290 LibSamplerateInterpolation::add_channel_to (int input_buffer_size, int output_buffer_size)
292 SRC_DATA* newdata = new SRC_DATA;
294 /* Set up sample rate converter info. */
295 newdata->end_of_input = 0 ;
297 newdata->input_frames = input_buffer_size;
298 newdata->output_frames = output_buffer_size;
300 newdata->input_frames_used = 0 ;
301 newdata->output_frames_gen = 0 ;
303 newdata->src_ratio = 1.0/_speed;
305 data.push_back (newdata);
312 LibSamplerateInterpolation::remove_channel_from ()
314 SRC_DATA* d = data.back ();
318 src_delete (state.back ());
325 LibSamplerateInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
328 printf ("ERROR: trying to interpolate with no channels\n");
332 data[channel]->data_in = input;
333 data[channel]->data_out = output;
335 data[channel]->input_frames = nframes * _speed;
336 data[channel]->output_frames = nframes;
337 data[channel]->src_ratio = 1.0/_speed;
339 if ((error = src_process (state[channel], data[channel]))) {
340 printf ("\nError : %s\n\n", src_strerror (error));
344 //printf("INTERPOLATION: channel %d input_frames_used: %d\n", channel, data[channel]->input_frames_used);
346 return data[channel]->input_frames_used;