1 #ifndef AUDIOGRAPHER_ROUTINES_H
2 #define AUDIOGRAPHER_ROUTINES_H
11 /// Allows overriding some routines with more efficient ones.
15 typedef uint32_t uint_type;
17 typedef float (*compute_peak_t) (float const *, uint_type, float);
18 typedef void (*apply_gain_to_buffer_t) (float *, uint_type, float);
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; }
23 /** Computes peak in float buffer
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
30 static inline float compute_peak (float const * data, uint_type frames, float current_peak)
32 return (*_compute_peak) (data, frames, current_peak);
35 /** Applies constant gain to buffer
37 * \param data data to which the gain is applied
38 * \param frames length of data
39 * \param gain gain that is applied
41 static inline void apply_gain_to_buffer (float * data, uint_type frames, float gain)
43 (*_apply_gain_to_buffer) (data, frames, gain);
47 static inline float default_compute_peak (float const * data, uint_type frames, float current_peak)
49 for (uint_type i = 0; i < frames; ++i) {
50 float abs = std::fabs(data[i]);
51 if (abs > current_peak) { current_peak = abs; }
56 static inline void default_apply_gain_to_buffer (float * data, uint_type frames, float gain)
58 for (uint_type i = 0; i < frames; ++i) {
63 static compute_peak_t _compute_peak;
64 static apply_gain_to_buffer_t _apply_gain_to_buffer;
69 #endif // AUDIOGRAPHER_ROUTINES_H