ed27d05d2a23dab63258020a987f0a3e35247488
[ardour.git] / libs / ardour / interpolation.cc
1 #include <stdint.h>
2 #include <cstdio>
3
4 #include "ardour/interpolation.h"
5
6 using namespace ARDOUR;
7
8 nframes_t
9 FixedPointLinearInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
10 {
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];
17         
18         // acceleration
19         int64_t  phi_delta;
20
21         // phi = fixed point speed
22         if (phi != target_phi) {
23                 phi_delta = ((int64_t)(target_phi - phi)) / nframes;
24         } else {
25                 phi_delta = 0;
26         }
27         
28         // index in the input buffers
29         nframes_t   i = 0;
30
31         for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
32                 i = the_phase >> 24;
33                 Sample fractional_phase_part = (the_phase & fractional_part_mask) / binary_scaling_factor;
34                 
35                 if (input && output) {
36                         // Linearly interpolate into the output buffer
37                         output[outsample] = 
38                                 input[i] * (1.0f - fractional_phase_part) +
39                                 input[i+1] * fractional_phase_part;
40                 }
41                 
42                 the_phase += phi + phi_delta;
43         }
44
45         last_phase[channel] = (the_phase & fractional_part_mask);
46         
47         // playback distance
48         return i;
49 }
50
51 void 
52 FixedPointLinearInterpolation::add_channel_to (int /*input_buffer_size*/, int /*output_buffer_size*/)
53 {
54         last_phase.push_back (0);
55 }
56
57 void 
58 FixedPointLinearInterpolation::remove_channel_from ()
59 {
60         last_phase.pop_back ();
61 }
62
63 void
64 FixedPointLinearInterpolation::reset() 
65 {
66         for (size_t i = 0; i <= last_phase.size(); i++) {
67                 last_phase[i] = 0;
68         }
69 }
70
71
72 nframes_t
73 LinearInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
74 {
75         // index in the input buffers
76         nframes_t   i = 0;
77         
78         double acceleration;
79         double distance = 0.0;
80         
81         if (_speed != _target_speed) {
82                 acceleration = _target_speed - _speed;
83         } else {
84                 acceleration = 0.0;
85         }
86         
87         distance = phase[channel];
88         for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
89                 i = floor(distance);
90                 Sample fractional_phase_part = distance - i;
91                 if (fractional_phase_part >= 1.0) {
92                         fractional_phase_part -= 1.0;
93                         i++;
94                 }
95                 
96                 if (input && output) {
97                 // Linearly interpolate into the output buffer
98                         output[outsample] = 
99                                 input[i] * (1.0f - fractional_phase_part) +
100                                 input[i+1] * fractional_phase_part;
101                 }
102                 distance += _speed + acceleration;
103         }
104         
105         i = floor(distance);
106         phase[channel] = distance - floor(distance);
107         
108         return i;
109 }
110
111 nframes_t
112 CubicInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
113 {
114     // index in the input buffers
115     nframes_t   i = 0;
116     
117     double acceleration;
118     double distance = 0.0;
119     
120     if (_speed != _target_speed) {
121         acceleration = _target_speed - _speed;
122     } else {
123         acceleration = 0.0;
124     }
125     
126     distance = phase[channel];
127     for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
128         i = floor(distance);
129         Sample fractional_phase_part = distance - i;
130         if (fractional_phase_part >= 1.0) {
131             fractional_phase_part -= 1.0;
132             i++;
133         }
134         
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]);
138         }
139         distance += _speed + acceleration;
140     }
141     
142     i = floor(distance);
143     phase[channel] = distance - floor(distance);
144     
145     return i;
146 }
147
148 SplineInterpolation::SplineInterpolation()
149 {
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
154     _m[0] = 4.0;
155     for (int i = 0; i <= 20 - 2; i++) {
156         _l[i] = 1.0 / _m[i];
157         _m[i+1] = 4.0 - _l[i];
158     }
159 }
160
161 nframes_t
162 SplineInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
163 {
164     // How many input samples we need
165     nframes_t n = ceil (double(nframes) * _speed + phase[channel]);
166     
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);
169     
170     if (n <= 3) {
171         return 0;
172     }
173     
174     double M[n], t[n-2];
175     
176     // natural spline: boundary conditions
177     M[0]     = 0.0;
178     M[n - 1] = 0.0;
179     
180     if (input) {
181         // solve L * t = d
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])
185                    - l(i-1) * t[i-1];
186         }
187         
188         // solve U * M = t
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]);
195             if ( i == 0 ) break;
196         }
197         M[1]     = 0.0;
198         M[n - 2] = 0.0;
199         //printf(" M[%d] = %lf \n", 0 ,M[0]);
200     }
201     
202     assert (M[0] == 0.0 && M[n-1] == 0.0);
203     
204     // now interpolate
205     // index in the input buffers
206     nframes_t   i = 0;
207     
208     double acceleration;
209     double distance = 0.0;
210     
211     if (_speed != _target_speed) {
212         acceleration = _target_speed - _speed;
213     } else {
214         acceleration = 0.0;
215     }
216     
217     distance = phase[channel];
218     assert(distance >= 0.0 && distance < 1.0);
219     
220     for (nframes_t outsample = 0; outsample < nframes; outsample++) {
221         i = floor(distance);
222         
223         double x = double(distance) - double(i);
224         
225         // if distance is something like 0.999999999999
226         // it will get rounded to 1 in the conversion to float above
227         while (x >= 1.0) {
228             x -= 1.0;
229             i++;
230         } 
231         
232         assert(x >= 0.0 && x < 1.0);
233         
234         if (input && output) {
235             assert (i <= n-1);
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;
243         }
244         distance += _speed + acceleration;
245     }
246     
247     i = floor(distance);
248     phase[channel] = distance - floor(distance);
249     assert (phase[channel] >= 0.0 && phase[channel] < 1.0);
250     printf("Moved input frames: %u ", i);
251     
252     return i;
253 }
254
255 LibSamplerateInterpolation::LibSamplerateInterpolation() : state (0)
256 {
257         _speed = 1.0;
258 }
259
260 LibSamplerateInterpolation::~LibSamplerateInterpolation() 
261 {
262         for (size_t i = 0; i < state.size(); i++) {
263                 state[i] = src_delete (state[i]);
264         }
265 }
266
267 void
268 LibSamplerateInterpolation::set_speed (double new_speed)
269
270         _speed = new_speed; 
271         for (size_t i = 0; i < state.size(); i++) {
272                 src_set_ratio (state[i], 1.0/_speed);
273         }
274 }
275
276 void
277 LibSamplerateInterpolation::reset_state ()
278 {
279         printf("INTERPOLATION: reset_state()\n");
280         for (size_t i = 0; i < state.size(); i++) {
281                 if (state[i]) {
282                         src_reset (state[i]);
283                 } else {
284                         state[i] = src_new (SRC_SINC_FASTEST, 1, &error);
285                 }
286         }
287 }
288
289 void
290 LibSamplerateInterpolation::add_channel_to (int input_buffer_size, int output_buffer_size) 
291 {
292         SRC_DATA* newdata = new SRC_DATA;
293         
294         /* Set up sample rate converter info. */
295         newdata->end_of_input = 0 ; 
296
297         newdata->input_frames  = input_buffer_size;
298         newdata->output_frames = output_buffer_size;
299
300         newdata->input_frames_used = 0 ;
301         newdata->output_frames_gen = 0 ;
302
303         newdata->src_ratio = 1.0/_speed;
304         
305         data.push_back (newdata);
306         state.push_back (0);
307         
308         reset_state ();
309 }
310
311 void
312 LibSamplerateInterpolation::remove_channel_from () 
313 {
314         SRC_DATA* d = data.back ();
315         delete d;
316         data.pop_back ();
317         if (state.back ()) {
318                 src_delete (state.back ());
319         }
320         state.pop_back ();
321         reset_state ();
322 }
323
324 nframes_t
325 LibSamplerateInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
326 {       
327         if (!data.size ()) {
328                 printf ("ERROR: trying to interpolate with no channels\n");
329                 return 0;
330         }
331         
332         data[channel]->data_in     = input;
333         data[channel]->data_out   = output;
334         
335         data[channel]->input_frames  = nframes * _speed;
336         data[channel]->output_frames = nframes;
337         data[channel]->src_ratio         = 1.0/_speed; 
338
339         if ((error = src_process (state[channel], data[channel]))) {    
340                 printf ("\nError : %s\n\n", src_strerror (error));
341                 exit (1);
342         }
343         
344         //printf("INTERPOLATION: channel %d input_frames_used: %d\n", channel, data[channel]->input_frames_used);
345         
346         return data[channel]->input_frames_used;
347 }