Fix export threading timeouts when disk operations take long. Might fix other timeout...
[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 /// Allows overriding some routines with more efficient ones.
12 class Routines
13 {
14   public:
15         typedef uint32_t uint_type;
16         
17         typedef float (*compute_peak_t)          (float const *, uint_type, float);
18         typedef void  (*apply_gain_to_buffer_t)  (float *, uint_type, float);
19         
20         static void override_compute_peak         (compute_peak_t func)         { _compute_peak = func; }
21         static void override_apply_gain_to_buffer (apply_gain_to_buffer_t func) { _apply_gain_to_buffer = func; }
22         
23         /** Computes peak in float buffer
24           * \n RT safe
25           * \param data buffer from which the peak is computed
26           * \param frames length of the portion of \a buffer that is checked
27           * \param current_peak current peak of buffer, if calculated in several passes
28           * \return maximum of values in [\a data, \a data + \a frames) and \a current_peak
29           */
30         static inline float compute_peak (float const * data, uint_type frames, float current_peak)
31         {
32                 return (*_compute_peak) (data, frames, current_peak);
33         }
34
35         /** Applies constant gain to buffer
36          * \n RT safe
37          * \param data data to which the gain is applied
38          * \param frames length of data
39          * \param gain gain that is applied
40          */
41         static inline void apply_gain_to_buffer (float * data, uint_type frames, float gain)
42         {
43                 (*_apply_gain_to_buffer) (data, frames, gain);
44         }
45
46   private:
47         static inline float default_compute_peak (float const * data, uint_type frames, float current_peak)
48         {
49                 for (uint_type i = 0; i < frames; ++i) {
50                         float abs = std::fabs(data[i]);
51                         if (abs > current_peak) { current_peak = abs; }
52                 }
53                 return current_peak;
54         }
55
56         static inline void default_apply_gain_to_buffer (float * data, uint_type frames, float gain)
57         {
58                 for (uint_type i = 0; i < frames; ++i) {
59                         data[i] *= gain;
60                 }
61         }
62         
63         static compute_peak_t          _compute_peak;
64         static apply_gain_to_buffer_t  _apply_gain_to_buffer;
65 };
66
67 } // namespace
68
69 #endif // AUDIOGRAPHER_ROUTINES_H