fd077e1b3f261b4db0cd6d006ce2d1a33afcf9d6
[ardour.git] / libs / audiographer / audiographer / routines.h
1 #ifndef AUDIOGRAPHER_ROUTINES_H
2 #define AUDIOGRAPHER_ROUTINES_H
3
4 #include "types.h"
5
6 #include <cmath>
7
8 #define routines_nframes_t uint32_t
9
10 namespace AudioGrapher
11 {
12
13 class Routines
14 {
15   public:
16         typedef float (*compute_peak_t)          (float const *, routines_nframes_t, float);
17         typedef void  (*apply_gain_to_buffer_t)  (float *, routines_nframes_t, float);
18         
19         static void override_compute_peak         (compute_peak_t func)         { _compute_peak = func; }
20         static void override_apply_gain_to_buffer (apply_gain_to_buffer_t func) { _apply_gain_to_buffer = func; }
21         
22         static inline float compute_peak (float const * data, routines_nframes_t frames, float current_peak)
23         {
24                 return (*_compute_peak) (data, frames, current_peak);
25         }
26
27         static inline void apply_gain_to_buffer (float * data, routines_nframes_t frames, float gain)
28         {
29                 (*_apply_gain_to_buffer) (data, frames, gain);
30         }
31
32   private:
33         static inline float default_compute_peak (float const * data, routines_nframes_t frames, float current_peak)
34         {
35                 for (routines_nframes_t i = 0; i < frames; ++i) {
36                         float abs = std::fabs(data[i]);
37                         if (abs > current_peak) { current_peak = abs; }
38                 }
39                 return current_peak;
40         }
41
42         static inline void default_apply_gain_to_buffer (float * data, routines_nframes_t frames, float gain)
43         {
44                 for (routines_nframes_t i = 0; i < frames; ++i) {
45                         data[i] *= gain;
46                 }
47         }
48         
49         static compute_peak_t          _compute_peak;
50         static apply_gain_to_buffer_t  _apply_gain_to_buffer;
51 };
52
53 } // namespace
54
55 #undef routines_nframes_t
56
57 #endif // AUDIOGRAPHER_ROUTINES_H