09ae2424906c3b923f66df563caa15fce356ee1b
[ardour.git] / libs / ardour / ardour / interpolation.h
1 #include <math.h>
2 //#include "ardour/types.h"
3
4 typedef float Sample;
5 #define nframes_t uint32_t
6
7 // 40.24 fixpoint math
8 #define FIXPOINT_ONE 0x1000000
9
10 class Interpolation {
11     protected:
12     /// speed in fixed point math
13     uint64_t      phi;
14     
15     /// target speed in fixed point math
16     uint64_t      target_phi;
17     
18     uint64_t      last_phase;
19
20         // Fixed point is just an integer with an implied scaling factor. 
21         // In 40.24 the scaling factor is 2^24 = 16777216,  
22         // so a value of 10*2^24 (in integer space) is equivalent to 10.0. 
23         //
24         // The advantage is that addition and modulus [like x = (x + y) % 2^40]  
25         // have no rounding errors and no drift, and just require a single integer add.
26         // (swh)
27         
28         static const int64_t fractional_part_mask  = 0xFFFFFF;
29         static const Sample  binary_scaling_factor = 16777216.0f;
30     
31     public:
32         Interpolation () : phi (FIXPOINT_ONE), target_phi (FIXPOINT_ONE), last_phase (0) {}
33     
34         void set_speed (double new_speed) {
35             target_phi = (uint64_t) (FIXPOINT_ONE * fabs(new_speed));
36             phi = target_phi;
37         }
38         
39         uint64_t get_phi () const { return phi; }
40         uint64_t get_target_phi () const { return target_phi; }
41         uint64_t get_last_phase () const { return last_phase; }
42  
43         virtual nframes_t interpolate (nframes_t nframes, Sample* input, Sample* output) = 0;
44 };
45
46 class LinearInterpolation : public Interpolation {
47     public:
48         nframes_t interpolate (nframes_t nframes, Sample* input, Sample* output);
49 };