Re-integrate export-optimization branch.
[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 namespace AudioGrapher
9 {
10
11 class Routines
12 {
13   public:
14         typedef float (*compute_peak_t)          (float const *, nframes_t, float);
15         typedef void  (*apply_gain_to_buffer_t)  (float *, nframes_t, float);
16         
17         static void override_compute_peak         (compute_peak_t func)         { _compute_peak = func; }
18         static void override_apply_gain_to_buffer (apply_gain_to_buffer_t func) { _apply_gain_to_buffer = func; }
19         
20         static inline float compute_peak (float const * data, nframes_t frames, float current_peak)
21         {
22                 return (*_compute_peak) (data, frames, current_peak);
23         }
24
25         static inline void apply_gain_to_buffer (float * data, nframes_t frames, float gain)
26         {
27                 (*_apply_gain_to_buffer) (data, frames, gain);
28         }
29
30   private:
31         static inline float default_compute_peak (float const * data, nframes_t frames, float current_peak)
32         {
33                 for (nframes_t i = 0; i < frames; ++i) {
34                         float abs = std::fabs(data[i]);
35                         if (abs > current_peak) { current_peak = abs; }
36                 }
37                 return current_peak;
38         }
39
40         static inline void default_apply_gain_to_buffer (float * data, nframes_t frames, float gain)
41         {
42                 for (nframes_t i = 0; i < frames; ++i) {
43                         data[i] *= gain;
44                 }
45         }
46         
47         static compute_peak_t          _compute_peak;
48         static apply_gain_to_buffer_t  _apply_gain_to_buffer;
49 };
50
51 } // namespace
52
53 #endif // AUDIOGRAPHER_ROUTINES_H