3 #include "ardour/interpolation.h"
5 using namespace ARDOUR;
8 FixedPointLinearInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
10 // the idea behind phase is that when the speed is not 1.0, we have to
11 // interpolate between samples and then we have to store where we thought we were.
12 // rather than being at sample N or N+1, we were at N+0.8792922
13 // so the "phase" element, if you want to think about this way,
14 // varies from 0 to 1, representing the "offset" between samples
15 uint64_t phase = last_phase[channel];
20 // phi = fixed point speed
21 if (phi != target_phi) {
22 phi_delta = ((int64_t)(target_phi - phi)) / nframes;
27 // index in the input buffers
30 for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
32 Sample fractional_phase_part = (phase & fractional_part_mask) / binary_scaling_factor;
34 if (input && output) {
35 // Linearly interpolate into the output buffer
36 // using fixed point math
38 input[i] * (1.0f - fractional_phase_part) +
39 input[i+1] * fractional_phase_part;
42 phase += phi + phi_delta;
45 last_phase[channel] = (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 printf("phase before: %lf\n", phase[channel]);
88 distance = phase[channel];
89 for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
91 Sample fractional_phase_part = distance - i;
92 if (fractional_phase_part >= 1.0) {
93 fractional_phase_part -= 1.0;
96 //printf("I: %u, distance: %lf, fractional_phase_part: %lf\n", i, distance, fractional_phase_part);
98 if (input && output) {
99 // Linearly interpolate into the output buffer
101 input[i] * (1.0f - fractional_phase_part) +
102 input[i+1] * fractional_phase_part;
104 //printf("distance before: %lf\n", distance);
105 distance += _speed + acceleration;
106 //printf("distance after: %lf, _speed: %lf\n", distance, _speed);
109 printf("before assignment: i: %d, distance: %lf\n", i, distance);
111 printf("after assignment: i: %d, distance: %16lf\n", i, distance);
112 phase[channel] = distance - floor(distance);
113 printf("speed: %16lf, i after: %d, distance after: %16lf, phase after: %16lf\n", _speed, i, distance, phase[channel]);
119 LinearInterpolation::add_channel_to (int input_buffer_size, int output_buffer_size)
121 phase.push_back (0.0);
125 LinearInterpolation::remove_channel_from ()
132 LinearInterpolation::reset()
134 for (size_t i = 0; i <= phase.size(); i++) {
139 LibSamplerateInterpolation::LibSamplerateInterpolation() : state (0)
144 LibSamplerateInterpolation::~LibSamplerateInterpolation()
146 for (size_t i = 0; i < state.size(); i++) {
147 state[i] = src_delete (state[i]);
152 LibSamplerateInterpolation::set_speed (double new_speed)
155 for (size_t i = 0; i < state.size(); i++) {
156 src_set_ratio (state[i], 1.0/_speed);
161 LibSamplerateInterpolation::reset_state ()
163 printf("INTERPOLATION: reset_state()\n");
164 for (size_t i = 0; i < state.size(); i++) {
166 src_reset (state[i]);
168 state[i] = src_new (SRC_SINC_FASTEST, 1, &error);
174 LibSamplerateInterpolation::add_channel_to (int input_buffer_size, int output_buffer_size)
176 SRC_DATA* newdata = new SRC_DATA;
178 /* Set up sample rate converter info. */
179 newdata->end_of_input = 0 ;
181 newdata->input_frames = input_buffer_size;
182 newdata->output_frames = output_buffer_size;
184 newdata->input_frames_used = 0 ;
185 newdata->output_frames_gen = 0 ;
187 newdata->src_ratio = 1.0/_speed;
189 data.push_back (newdata);
196 LibSamplerateInterpolation::remove_channel_from ()
198 SRC_DATA* d = data.back ();
202 src_delete (state.back ());
209 LibSamplerateInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
212 printf ("ERROR: trying to interpolate with no channels\n");
216 data[channel]->data_in = input;
217 data[channel]->data_out = output;
219 data[channel]->input_frames = nframes * _speed;
220 data[channel]->output_frames = nframes;
221 data[channel]->src_ratio = 1.0/_speed;
223 if ((error = src_process (state[channel], data[channel]))) {
224 printf ("\nError : %s\n\n", src_strerror (error));
228 //printf("INTERPOLATION: channel %d input_frames_used: %d\n", channel, data[channel]->input_frames_used);
230 return data[channel]->input_frames_used;