1 #ifndef AUDIOGRAPHER_ROUTINES_H
2 #define AUDIOGRAPHER_ROUTINES_H
8 #include "audiographer/visibility.h"
10 namespace AudioGrapher
13 /// Allows overriding some routines with more efficient ones.
14 class LIBAUDIOGRAPHER_API Routines
17 typedef uint32_t uint_type;
19 typedef float (*compute_peak_t) (float const *, uint_type, float);
20 typedef void (*apply_gain_to_buffer_t) (float *, uint_type, float);
22 static void override_compute_peak (compute_peak_t func) { _compute_peak = func; }
23 static void override_apply_gain_to_buffer (apply_gain_to_buffer_t func) { _apply_gain_to_buffer = func; }
25 /** Computes peak in float buffer
27 * \param data buffer from which the peak is computed
28 * \param frames length of the portion of \a buffer that is checked
29 * \param current_peak current peak of buffer, if calculated in several passes
30 * \return maximum of values in [\a data, \a data + \a frames) and \a current_peak
32 static inline float compute_peak (float const * data, uint_type frames, float current_peak)
34 return (*_compute_peak) (data, frames, current_peak);
37 /** Applies constant gain to buffer
39 * \param data data to which the gain is applied
40 * \param frames length of data
41 * \param gain gain that is applied
43 static inline void apply_gain_to_buffer (float * data, uint_type frames, float gain)
45 (*_apply_gain_to_buffer) (data, frames, gain);
49 static inline float default_compute_peak (float const * data, uint_type frames, float current_peak)
51 for (uint_type i = 0; i < frames; ++i) {
52 float abs = std::fabs(data[i]);
53 if (abs > current_peak) { current_peak = abs; }
58 static inline void default_apply_gain_to_buffer (float * data, uint_type frames, float gain)
60 for (uint_type i = 0; i < frames; ++i) {
65 static compute_peak_t _compute_peak;
66 static apply_gain_to_buffer_t _apply_gain_to_buffer;
71 #endif // AUDIOGRAPHER_ROUTINES_H