4 #include "ardour/interpolation.h"
6 using namespace ARDOUR
;
10 LinearInterpolation::interpolate (int channel
, nframes_t nframes
, Sample
*input
, Sample
*output
)
12 // index in the input buffers
16 double distance
= 0.0;
18 if (_speed
!= _target_speed
) {
19 acceleration
= _target_speed
- _speed
;
24 distance
= phase
[channel
];
25 for (nframes_t outsample
= 0; outsample
< nframes
; ++outsample
) {
27 Sample fractional_phase_part
= distance
- i
;
28 if (fractional_phase_part
>= 1.0) {
29 fractional_phase_part
-= 1.0;
33 if (input
&& output
) {
34 // Linearly interpolate into the output buffer
36 input
[i
] * (1.0f
- fractional_phase_part
) +
37 input
[i
+1] * fractional_phase_part
;
39 distance
+= _speed
+ acceleration
;
43 phase
[channel
] = distance
- floor(distance
);
49 CubicInterpolation::interpolate (int channel
, nframes_t nframes
, Sample
*input
, Sample
*output
)
51 // index in the input buffers
55 double distance
= 0.0;
57 if (_speed
!= _target_speed
) {
58 acceleration
= _target_speed
- _speed
;
63 distance
= phase
[channel
];
66 /* no interpolation possible */
68 for (i
= 0; i
< nframes
; ++i
) {
75 /* keep this condition out of the inner loop */
77 if (input
&& output
) {
81 if (floor (distance
) == 0.0) {
82 /* best guess for the fake point we have to add to be able to interpolate at i == 0:
83 .... maintain slope of first actual segment ...
85 inm1
= input
[i
] - (input
[i
+1] - input
[i
]);
90 for (nframes_t outsample
= 0; outsample
< nframes
; ++outsample
) {
92 float f
= floor (distance
);
93 float fractional_phase_part
= distance
- f
;
95 /* get the index into the input we should start with */
99 /* fractional_phase_part only reaches 1.0 thanks to float imprecision. In theory
100 it should always be < 1.0. If it ever >= 1.0, then bump the index we use
101 and back it off. This is the point where we "skip" an entire sample in the
102 input, because the phase part has accumulated so much error that we should
103 really be closer to the next sample. or something like that ...
106 if (fractional_phase_part
>= 1.0) {
107 fractional_phase_part
-= 1.0;
111 // Cubically interpolate into the output buffer: keep this inlined for speed and rely on compiler
112 // optimization to take care of the rest
113 // shamelessly ripped from Steve Harris' swh-plugins (ladspa-util.h)
115 output
[outsample
] = input
[i
] + 0.5f
* fractional_phase_part
* (input
[i
+1] - inm1
+
116 fractional_phase_part
* (4.0f
* input
[i
+1] + 2.0f
* inm1
- 5.0f
* input
[i
] - input
[i
+2] +
117 fractional_phase_part
* (3.0f
* (input
[i
] - input
[i
+1]) - inm1
+ input
[i
+2])));
119 distance
+= _speed
+ acceleration
;
125 /* not sure that this is ever utilized - it implies that one of the input/output buffers is missing */
127 for (nframes_t outsample
= 0; outsample
< nframes
; ++outsample
) {
128 distance
+= _speed
+ acceleration
;
133 phase
[channel
] = distance
- floor(distance
);