Maschine2 UI: basic widgets
[ardour.git] / libs / ardour / interpolation.cc
1 /*
2     Copyright (C) 2012 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <stdint.h>
21 #include <cstdio>
22
23 #include "ardour/interpolation.h"
24 #include "ardour/midi_buffer.h"
25
26 using namespace ARDOUR;
27
28
29 framecnt_t
30 LinearInterpolation::interpolate (int channel, framecnt_t nframes, Sample *input, Sample *output)
31 {
32         // index in the input buffers
33         framecnt_t i = 0;
34
35         double acceleration = 0;
36
37         if (_speed != _target_speed) {
38                 acceleration = _target_speed - _speed;
39         }
40
41         for (framecnt_t outsample = 0; outsample < nframes; ++outsample) {
42                 double const d = phase[channel] + outsample * (_speed + acceleration);
43                 i = floor(d);
44                 Sample fractional_phase_part = d - i;
45                 if (fractional_phase_part >= 1.0) {
46                         fractional_phase_part -= 1.0;
47                         i++;
48                 }
49
50                 if (input && output) {
51                         // Linearly interpolate into the output buffer
52                         output[outsample] =
53                                 input[i] * (1.0f - fractional_phase_part) +
54                                 input[i+1] * fractional_phase_part;
55                 }
56         }
57
58         double const distance = phase[channel] + nframes * (_speed + acceleration);
59         i = floor(distance);
60         phase[channel] = distance - i;
61         return i;
62 }
63
64 framecnt_t
65 CubicInterpolation::interpolate (int channel, framecnt_t nframes, Sample *input, Sample *output)
66 {
67         // index in the input buffers
68         framecnt_t i = 0;
69
70         double acceleration;
71         double distance = phase[channel];
72
73         if (_speed != _target_speed) {
74                 acceleration = _target_speed - _speed;
75         } else {
76                 acceleration = 0.0;
77         }
78
79         if (nframes < 3) {
80                 /* no interpolation possible */
81
82                 if (input && output) {
83                         for (i = 0; i < nframes; ++i) {
84                                 output[i] = input[i];
85                         }
86                 }
87
88                 phase[channel] = 0;
89                 return nframes;
90         }
91
92         /* keep this condition out of the inner loop */
93
94         if (input && output) {
95                 /* best guess for the fake point we have to add to be able to interpolate at i == 0:
96                  * .... maintain slope of first actual segment ...
97                  */
98                 Sample inm1 = input[i] - (input[i+1] - input[i]);
99
100                 for (framecnt_t outsample = 0; outsample < nframes; ++outsample) {
101                         /* get the index into the input we should start with */
102                         i = floor (distance);
103                         float fractional_phase_part = fmod (distance, 1.0);
104
105                         // Cubically interpolate into the output buffer: keep this inlined for speed and rely on compiler
106                         // optimization to take care of the rest
107                         // shamelessly ripped from Steve Harris' swh-plugins (ladspa-util.h)
108
109                         output[outsample] = input[i] + 0.5f * fractional_phase_part * (input[i+1] - inm1 +
110                                         fractional_phase_part * (4.0f * input[i+1] + 2.0f * inm1 - 5.0f * input[i] - input[i+2] +
111                                                 fractional_phase_part * (3.0f * (input[i] - input[i+1]) - inm1 + input[i+2])));
112
113                         distance += _speed + acceleration;
114                         inm1 = input[i];
115                 }
116
117                 i = floor (distance);
118                 phase[channel] = fmod (distance, 1.0);
119
120         } else {
121                 /* used to calculate play-distance with acceleration (silent roll)
122                  * (use same algorithm as real playback for identical rounding/floor'ing)
123                  */
124                 for (framecnt_t outsample = 0; outsample < nframes; ++outsample) {
125                         distance += _speed + acceleration;
126                 }
127                 i = floor (distance);
128                 phase[channel] = fmod (distance, 1.0);
129         }
130
131         return i;
132 }
133
134 /* CubicMidiInterpolation::distance is identical to
135  * return CubicInterpolation::interpolate (0, nframes, NULL, NULL);
136  */
137 framecnt_t
138 CubicMidiInterpolation::distance (framecnt_t nframes, bool /*roll*/)
139 {
140         assert (phase.size () == 1);
141
142         framecnt_t i = 0;
143
144         double acceleration;
145         double distance = phase[0];
146
147         if (nframes < 3) {
148                 /* no interpolation possible */
149                 phase[0] = 0;
150                 return nframes;
151         }
152
153         if (_speed != _target_speed) {
154                 acceleration = _target_speed - _speed;
155         } else {
156                 acceleration = 0.0;
157         }
158
159         for (framecnt_t outsample = 0; outsample < nframes; ++outsample) {
160                 distance += _speed + acceleration;
161         }
162
163         i = floor (distance);
164         phase[0] = fmod (distance, 1.0);
165
166         return i;
167 }