spline interpolation: fix crash bugs on negative speed and NULL inputs
[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         //printf("processing channel: %d\n", channel);
89         //printf("phase before: %lf\n", phase[channel]);
90         for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
91                 i = floor(distance);
92                 Sample fractional_phase_part = distance - i;
93                 if (fractional_phase_part >= 1.0) {
94                         fractional_phase_part -= 1.0;
95                         i++;
96                 }
97                 //printf("I: %u, distance: %lf, fractional_phase_part: %lf\n", i, distance, fractional_phase_part);
98                 
99                 if (input && output) {
100                 // Linearly interpolate into the output buffer
101                         output[outsample] = 
102                                 input[i] * (1.0f - fractional_phase_part) +
103                                 input[i+1] * fractional_phase_part;
104                 }
105                 //printf("distance before: %lf\n", distance);
106                 distance += _speed + acceleration;
107                 //printf("distance after: %lf, _speed: %lf\n", distance, _speed);
108         }
109         
110         //printf("before assignment: i: %d, distance: %lf\n", i, distance);
111         i = floor(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]);
115         
116         return i;
117 }
118
119 SplineInterpolation::SplineInterpolation()
120 {
121     // precompute LU-factorization of matrix A
122     // see "Teubner Taschenbuch der Mathematik", p. 1105
123     // We only need to calculate up to 20, because they
124     // won't change any more above that
125     _m[0] = 4.0;
126     for (int i = 0; i <= 20 - 2; i++) {
127         _l[i] = 1.0 / _m[i];
128         _m[i+1] = 4.0 - _l[i];
129     }
130 }
131
132 nframes_t
133 SplineInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
134 {
135     // How many input samples we need
136     nframes_t n = ceil (double(nframes) * _speed + phase[channel]) + 1;
137     //printf("n = %d\n", n);
138
139     if (n <= 3) {
140         return 0;
141     }
142     
143     double M[n], t[n-2];
144     
145     // natural spline: boundary conditions
146     M[0]     = 0.0;
147     M[n - 1] = 0.0;
148     
149     if (input) {
150         // solve L * t = d
151         t[0] = 6.0 * (input[0] - 2*input[1] + input[2]); 
152         for (nframes_t i = 1; i <= n - 3; i++) {
153             t[i] = 6.0 * (input[i] - 2*input[i+1] + input[i+2])
154                    - l(i-1) * t[i-1];
155         }
156         
157         // solve U * M = t
158         M[n-2] = t[n-3] / m(n-3);
159         for (nframes_t i = n-4;; i--) {
160             M[i+1] = (t[i]-M[i+2])/m(i);
161             if ( i == 0 ) break;
162         }
163     }
164     
165     assert (M[0] == 0.0 && M[n-1] == 0.0);
166     
167     // now interpolate
168     // index in the input buffers
169     nframes_t   i = 0;
170     
171     double acceleration;
172     double distance = 0.0;
173     
174     if (_speed != _target_speed) {
175         acceleration = _target_speed - _speed;
176     } else {
177         acceleration = 0.0;
178     }
179     
180     distance = phase[channel];
181     for (nframes_t outsample = 0; outsample < nframes; outsample++) {
182         i = floor(distance);
183         
184         Sample x = double(distance) - double(i);
185         
186         // if distance is something like 0.999999999999
187         // it will get rounded to 1 in the conversion to float above
188         if (x >= 1.0) {
189             x = 0.0;
190             i++;
191         } 
192         
193         assert(x >= 0.0 && x < 1.0);
194         
195         if (input && output) {
196             assert (i <= n-1);
197             double a3 = (M[i+1] - M[i]) / 6.0;
198             double a2 = M[i] / 2.0;
199             double a1 = input[i+1] - input[i] - (M[i+1] + 2.0*M[i])/6.0;
200             double a0 = input[i];
201             // interpolate into the output buffer
202             output[outsample] = ((a3*x + a2)*x + a1)*x + a0;
203         }
204         distance += _speed + acceleration;
205     }
206     
207     i = floor(distance);
208     phase[channel] = distance - floor(distance);
209     assert (phase[channel] >= 0.0 && phase[channel] < 1.0);
210     
211     return i;
212 }
213
214 LibSamplerateInterpolation::LibSamplerateInterpolation() : state (0)
215 {
216         _speed = 1.0;
217 }
218
219 LibSamplerateInterpolation::~LibSamplerateInterpolation() 
220 {
221         for (size_t i = 0; i < state.size(); i++) {
222                 state[i] = src_delete (state[i]);
223         }
224 }
225
226 void
227 LibSamplerateInterpolation::set_speed (double new_speed)
228
229         _speed = new_speed; 
230         for (size_t i = 0; i < state.size(); i++) {
231                 src_set_ratio (state[i], 1.0/_speed);
232         }
233 }
234
235 void
236 LibSamplerateInterpolation::reset_state ()
237 {
238         printf("INTERPOLATION: reset_state()\n");
239         for (size_t i = 0; i < state.size(); i++) {
240                 if (state[i]) {
241                         src_reset (state[i]);
242                 } else {
243                         state[i] = src_new (SRC_SINC_FASTEST, 1, &error);
244                 }
245         }
246 }
247
248 void
249 LibSamplerateInterpolation::add_channel_to (int input_buffer_size, int output_buffer_size) 
250 {
251         SRC_DATA* newdata = new SRC_DATA;
252         
253         /* Set up sample rate converter info. */
254         newdata->end_of_input = 0 ; 
255
256         newdata->input_frames  = input_buffer_size;
257         newdata->output_frames = output_buffer_size;
258
259         newdata->input_frames_used = 0 ;
260         newdata->output_frames_gen = 0 ;
261
262         newdata->src_ratio = 1.0/_speed;
263         
264         data.push_back (newdata);
265         state.push_back (0);
266         
267         reset_state ();
268 }
269
270 void
271 LibSamplerateInterpolation::remove_channel_from () 
272 {
273         SRC_DATA* d = data.back ();
274         delete d;
275         data.pop_back ();
276         if (state.back ()) {
277                 src_delete (state.back ());
278         }
279         state.pop_back ();
280         reset_state ();
281 }
282
283 nframes_t
284 LibSamplerateInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
285 {       
286         if (!data.size ()) {
287                 printf ("ERROR: trying to interpolate with no channels\n");
288                 return 0;
289         }
290         
291         data[channel]->data_in     = input;
292         data[channel]->data_out   = output;
293         
294         data[channel]->input_frames  = nframes * _speed;
295         data[channel]->output_frames = nframes;
296         data[channel]->src_ratio         = 1.0/_speed; 
297
298         if ((error = src_process (state[channel], data[channel]))) {    
299                 printf ("\nError : %s\n\n", src_strerror (error));
300                 exit (1);
301         }
302         
303         //printf("INTERPOLATION: channel %d input_frames_used: %d\n", channel, data[channel]->input_frames_used);
304         
305         return data[channel]->input_frames_used;
306 }