1 #ifndef AUDIOGRAPHER_NORMALIZER_H
2 #define AUDIOGRAPHER_NORMALIZER_H
4 #include "audiographer/visibility.h"
5 #include "audiographer/sink.h"
6 #include "audiographer/routines.h"
7 #include "audiographer/utils/listed_source.h"
11 namespace AudioGrapher
14 /// A class for normalizing to a specified target in dB
15 class LIBAUDIOGRAPHER_API Normalizer
16 : public ListedSource<float>
21 /// Constructs a normalizer with a specific target in dB \n RT safe
22 Normalizer (float target_dB)
27 target = pow (10.0f, target_dB * 0.05f);
35 /// Sets the peak found in the material to be normalized \see PeakReader \n RT safe
36 void set_peak (float peak)
38 if (peak == 0.0f || peak == target) {
47 /** Allocates a buffer for using with const ProcessContexts
48 * This function does not need to be called if
49 * non-const ProcessContexts are given to \a process() .
52 void alloc_buffer(framecnt_t frames)
55 buffer = new float[frames];
59 /// Process a const ProcessContext \see alloc_buffer() \n RT safe
60 void process (ProcessContext<float> const & c)
62 if (throw_level (ThrowProcess) && c.frames() > buffer_size) {
63 throw Exception (*this, "Too many frames given to process()");
67 memcpy (buffer, c.data(), c.frames() * sizeof(float));
68 Routines::apply_gain_to_buffer (buffer, c.frames(), gain);
71 ProcessContext<float> c_out (c, buffer);
72 ListedSource<float>::output (c_out);
75 /// Process a non-const ProcsesContext in-place \n RT safe
76 void process (ProcessContext<float> & c)
79 Routines::apply_gain_to_buffer (c.data(), c.frames(), gain);
81 ListedSource<float>::output(c);
90 framecnt_t buffer_size;
96 #endif // AUDIOGRAPHER_NORMALIZER_H