Strip trailing whitespace and fix other whitespace errors (e.g. space/tab mixing...
[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
9 nframes_t
10 LinearInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
11 {
12         // index in the input buffers
13         nframes_t   i = 0;
14
15         double acceleration;
16         double distance = 0.0;
17
18         if (_speed != _target_speed) {
19                 acceleration = _target_speed - _speed;
20         } else {
21                 acceleration = 0.0;
22         }
23
24         distance = phase[channel];
25         for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
26                 i = floor(distance);
27                 Sample fractional_phase_part = distance - i;
28                 if (fractional_phase_part >= 1.0) {
29                         fractional_phase_part -= 1.0;
30                         i++;
31                 }
32
33                 if (input && output) {
34                 // Linearly interpolate into the output buffer
35                         output[outsample] =
36                                 input[i] * (1.0f - fractional_phase_part) +
37                                 input[i+1] * fractional_phase_part;
38                 }
39                 distance += _speed + acceleration;
40         }
41
42         i = floor(distance);
43         phase[channel] = distance - floor(distance);
44
45         return i;
46 }
47
48 nframes_t
49 CubicInterpolation::interpolate (int channel, nframes_t nframes, Sample *input, Sample *output)
50 {
51     // index in the input buffers
52     nframes_t   i = 0;
53
54     double acceleration;
55     double distance = 0.0;
56
57     if (_speed != _target_speed) {
58         acceleration = _target_speed - _speed;
59     } else {
60         acceleration = 0.0;
61     }
62
63     distance = phase[channel];
64     for (nframes_t outsample = 0; outsample < nframes; ++outsample) {
65         i = floor(distance);
66         Sample fractional_phase_part = distance - i;
67         if (fractional_phase_part >= 1.0) {
68             fractional_phase_part -= 1.0;
69             i++;
70         }
71
72         if (input && output) {
73             // Cubically interpolate into the output buffer
74             output[outsample] = cube_interp(fractional_phase_part, input[i-1], input[i], input[i+1], input[i+2]);
75         }
76         distance += _speed + acceleration;
77     }
78
79     i = floor(distance);
80     phase[channel] = distance - floor(distance);
81
82     return i;
83 }