Optimize automation-event process splitting
[ardour.git] / libs / ardour / ardour / interpolation.h
1 /*
2     Copyright (C) 1999-2010 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 <math.h>
21 #include <samplerate.h>
22
23 #include "ardour/libardour_visibility.h"
24 #include "ardour/types.h"
25
26 #ifndef __interpolation_h__
27 #define __interpolation_h__
28
29 namespace ARDOUR {
30
31 class LIBARDOUR_API Interpolation {
32 protected:
33         double _speed;
34         double _target_speed;
35
36         // the idea is that when the speed is not 1.0, we have to
37         // interpolate between samples and then we have to store where we thought we were.
38         // rather than being at sample N or N+1, we were at N+0.8792922
39         std::vector<double> phase;
40
41 public:
42         Interpolation ()  { _speed = 1.0; _target_speed = 1.0; }
43         virtual ~Interpolation() {}
44
45         void set_speed (double new_speed)          { _speed = new_speed; _target_speed = new_speed; }
46         void set_target_speed (double new_speed)   { _target_speed = new_speed; }
47
48         double target_speed()          const { return _target_speed; }
49         double speed()                 const { return _speed; }
50
51         void add_channel ()    { phase.push_back (0.0); }
52         void remove_channel () { phase.pop_back (); }
53
54         virtual void reset () {
55                 for (size_t i = 0; i < phase.size(); i++) {
56                         phase[i] = 0.0;
57                 }
58         }
59 };
60
61 class LIBARDOUR_API CubicInterpolation : public Interpolation {
62   public:
63         CubicInterpolation ();
64         samplecnt_t interpolate (int channel, samplecnt_t input_samples, Sample* input, samplecnt_t & output_samples, Sample* output);
65         samplecnt_t distance (samplecnt_t nframes);
66         void reset ();
67
68   private:
69         Sample z[4];
70         char   valid_z_bits;
71
72         bool is_valid (int n) const { return valid_z_bits & (1<<n); }
73         bool invalid (int n) const  { return !is_valid (n); }
74         void validate (int n)       { valid_z_bits |= (1<<n); }
75         void invalidate (int n)     { valid_z_bits &= (1<<n); }
76 };
77
78 } // namespace ARDOUR
79
80 #endif