Xinterpolation.*: Add old fixed point and double linear interpolation as alternatives
[ardour.git] / libs / ardour / interpolation.cc
1 #include <stdint.h>
2
3 #include "ardour/interpolation.h"
4
5 using namespace ARDOUR;
6
7 nframes_t
8 FixedPointLinearInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
9 {
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];
16         
17         // acceleration
18         int64_t  phi_delta;
19
20         // phi = fixed point speed
21         if (phi != target_phi) {
22                 phi_delta = ((int64_t)(target_phi - phi)) / nframes;
23         } else {
24                 phi_delta = 0;
25         }
26         
27         // index in the input buffers
28         nframes_t   i = 0;
29
30         for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
31                 i = phase >> 24;
32                 Sample fractional_phase_part = (phase & fractional_part_mask) / binary_scaling_factor;
33                 
34                 if (input && output) {
35                         // Linearly interpolate into the output buffer
36                         // using fixed point math
37                         output[outsample] = 
38                                 input[i] * (1.0f - fractional_phase_part) +
39                                 input[i+1] * fractional_phase_part;
40                 }
41                 
42                 phase += phi + phi_delta;
43         }
44
45         last_phase[channel] = (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(int 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         printf("phase before: %lf\n", phase[channel]);
88         distance = phase[channel];
89         for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
90                 i = distance;
91                 Sample fractional_phase_part = distance - i;
92                 if (fractional_phase_part >= 1.0) {
93                         fractional_phase_part -= 1.0;
94                         i++;
95                 }
96                 //printf("I: %u, distance: %lf, fractional_phase_part: %lf\n", i, distance, fractional_phase_part);
97                 
98                 if (input && output) {
99                 // Linearly interpolate into the output buffer
100                         output[outsample] = 
101                                 input[i] * (1.0f - fractional_phase_part) +
102                                 input[i+1] * fractional_phase_part;
103                 }
104                 //printf("distance before: %lf\n", distance);
105                 distance += _speed + acceleration;
106                 //printf("distance after: %lf, _speed: %lf\n", distance, _speed);
107         }
108         
109         printf("before assignment: i: %d, distance: %lf\n", i, distance);
110         i = floor(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]);
114         
115         return i;
116 }
117
118 void 
119 LinearInterpolation::add_channel_to (int input_buffer_size, int output_buffer_size)
120 {
121         phase.push_back (0.0);
122 }
123
124 void 
125 LinearInterpolation::remove_channel_from ()
126 {
127         phase.pop_back ();
128 }
129
130
131 void
132 LinearInterpolation::reset() 
133 {
134         for(int i = 0; i <= phase.size(); i++) {
135                 phase[i] = 0.0;
136         }
137 }
138
139 LibSamplerateInterpolation::LibSamplerateInterpolation() : state (0)
140 {
141         _speed = 1.0;
142 }
143
144 LibSamplerateInterpolation::~LibSamplerateInterpolation() 
145 {
146         for (int i = 0; i < state.size(); i++) {
147                 state[i] = src_delete (state[i]);
148         }
149 }
150
151 void
152 LibSamplerateInterpolation::set_speed (double new_speed)
153
154         _speed = new_speed; 
155         for (int i = 0; i < state.size(); i++) {
156                 src_set_ratio (state[i], 1.0/_speed);
157         }
158 }
159
160 void
161 LibSamplerateInterpolation::reset_state ()
162 {
163         printf("INTERPOLATION: reset_state()\n");
164         for (int i = 0; i < state.size(); i++) {
165                 if (state[i]) {
166                         src_reset (state[i]);
167                 } else {
168                         state[i] = src_new (SRC_SINC_FASTEST, 1, &error);
169                 }
170         }
171 }
172
173 void
174 LibSamplerateInterpolation::add_channel_to (int input_buffer_size, int output_buffer_size) 
175 {
176         SRC_DATA* newdata = new SRC_DATA;
177         
178         /* Set up sample rate converter info. */
179         newdata->end_of_input = 0 ; 
180
181         newdata->input_frames  = input_buffer_size;
182         newdata->output_frames = output_buffer_size;
183
184         newdata->input_frames_used = 0 ;
185         newdata->output_frames_gen = 0 ;
186
187         newdata->src_ratio = 1.0/_speed;
188         
189         data.push_back (newdata);
190         state.push_back (0);
191         
192         reset_state ();
193 }
194
195 void
196 LibSamplerateInterpolation::remove_channel_from () 
197 {
198         delete data.back ();
199         data.pop_back ();
200         delete state.back ();
201         state.pop_back ();
202         reset_state ();
203 }
204
205 nframes_t
206 LibSamplerateInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
207 {       
208         if (!data.size ()) {
209                 printf ("ERROR: trying to interpolate with no channels\n");
210                 return 0;
211         }
212         
213         data[channel]->data_in     = input;
214         data[channel]->data_out   = output;
215         
216         data[channel]->input_frames  = nframes * _speed;
217         data[channel]->output_frames = nframes;
218         data[channel]->src_ratio         = 1.0/_speed; 
219
220         if ((error = src_process (state[channel], data[channel]))) {    
221                 printf ("\nError : %s\n\n", src_strerror (error));
222                 exit (1);
223         }
224         
225         //printf("INTERPOLATION: channel %d input_frames_used: %d\n", channel, data[channel]->input_frames_used);
226         
227         return data[channel]->input_frames_used;
228 }