LTC generator - reinit on xrun or graph-reorder
[ardour.git] / libs / ardour / interpolation.cc
index 0a9bbc3a7d731a629433bd3ba97ac2f57ec46d09..ccaaca7e76322f13ff5097e6d1eeaaa0b203ecc4 100644 (file)
@@ -10,21 +10,18 @@ framecnt_t
 LinearInterpolation::interpolate (int channel, framecnt_t nframes, Sample *input, Sample *output)
 {
        // index in the input buffers
-       framecnt_t   i = 0;
+       framecnt_t i = 0;
 
-       double acceleration;
-       double distance = 0.0;
+       double acceleration = 0;
 
        if (_speed != _target_speed) {
                acceleration = _target_speed - _speed;
-       } else {
-               acceleration = 0.0;
        }
 
-       distance = phase[channel];
        for (framecnt_t outsample = 0; outsample < nframes; ++outsample) {
-               i = floor(distance);
-               Sample fractional_phase_part = distance - i;
+               double const d = phase[channel] + outsample * (_speed + acceleration);
+               i = floor(d);
+               Sample fractional_phase_part = d - i;
                if (fractional_phase_part >= 1.0) {
                        fractional_phase_part -= 1.0;
                        i++;
@@ -36,12 +33,11 @@ LinearInterpolation::interpolate (int channel, framecnt_t nframes, Sample *input
                                input[i] * (1.0f - fractional_phase_part) +
                                input[i+1] * fractional_phase_part;
                }
-               distance += _speed + acceleration;
        }
 
+       double const distance = phase[channel] + nframes * (_speed + acceleration);
        i = floor(distance);
-       phase[channel] = distance - floor(distance);
-
+       phase[channel] = distance - i;
        return i;
 }
 
@@ -57,7 +53,7 @@ CubicInterpolation::interpolate (int channel, framecnt_t nframes, Sample *input,
     if (_speed != _target_speed) {
         acceleration = _target_speed - _speed;
     } else {
-        acceleration = 0.0;
+           acceleration = 0.0;
     }
 
     distance = phase[channel];
@@ -73,13 +69,13 @@ CubicInterpolation::interpolate (int channel, framecnt_t nframes, Sample *input,
     }
 
     /* keep this condition out of the inner loop */
-    
+
     if (input && output) {
 
            Sample inm1;
-           
+
            if (floor (distance) == 0.0) {
-                   /* best guess for the fake point we have to add to be able to interpolate at i == 0: 
+                   /* best guess for the fake point we have to add to be able to interpolate at i == 0:
                       .... maintain slope of first actual segment ...
                    */
                    inm1 = input[i] - (input[i+1] - input[i]);
@@ -106,7 +102,7 @@ CubicInterpolation::interpolate (int channel, framecnt_t nframes, Sample *input,
                    if (fractional_phase_part >= 1.0) {
                            fractional_phase_part -= 1.0;
                            ++i;
-                   } 
+                   }
 
                    // Cubically interpolate into the output buffer: keep this inlined for speed and rely on compiler
                    // optimization to take care of the rest