Fix up some problems with Region::_master_sources
[ardour.git] / libs / ardour / interpolation.cc
1 #include <stdint.h>
2 #include "ardour/interpolation.h"
3
4 using namespace ARDOUR;
5
6 nframes_t
7 LinearInterpolation::interpolate (nframes_t nframes, Sample *input, Sample *output)
8 {
9         // the idea is that when the speed is not 1.0, we have to 
10         // interpolate between samples and then we have to store where we thought we were. 
11         // rather than being at sample N or N+1, we were at N+0.8792922
12         
13         // index in the input buffers
14         nframes_t   i = 0;
15         
16         double acceleration;
17         double distance = 0.0;
18         
19         if (_speed != _target_speed) {
20                 acceleration = _target_speed - _speed;
21         } else {
22                 acceleration = 0.0;
23         }
24
25         for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
26                 i = distance;
27                 Sample fractional_phase_part = distance - i;
28                 
29                 if (input && output) {
30                 // Linearly interpolate into the output buffer
31                         output[outsample] = 
32                                 input[i] * (1.0f - fractional_phase_part) +
33                                 input[i+1] * fractional_phase_part;
34                 }
35                 distance   += _speed + acceleration;
36         }
37         
38         i = (distance + 0.5L);
39         // playback distance
40         return i;
41 }