1 #ifndef AUDIOGRAPHER_NORMALIZER_H
2 #define AUDIOGRAPHER_NORMALIZER_H
4 #include "audiographer/sink.h"
5 #include "audiographer/routines.h"
6 #include "audiographer/utils/listed_source.h"
10 namespace AudioGrapher
13 /// A class for normalizing to a specified target in dB
15 : public ListedSource<float>
20 /// Constructs a normalizer with a specific target in dB \n RT safe
21 Normalizer (float target_dB)
26 target = pow (10.0f, target_dB * 0.05f);
34 /// Sets the peak found in the material to be normalized \see PeakReader \n RT safe
35 void set_peak (float peak)
37 if (peak == 0.0f || peak == target) {
46 /** Allocates a buffer for using with const ProcessContexts
47 * This function does not need to be called if
48 * non-const ProcessContexts are given to \a process() .
51 void alloc_buffer(nframes_t frames)
54 buffer = new float[frames];
58 /// Process a const ProcessContext \see alloc_buffer() \n RT safe
59 void process (ProcessContext<float> const & c)
61 if (throw_level (ThrowProcess) && c.frames() > buffer_size) {
62 throw Exception (*this, "Too many frames given to process()");
66 memcpy (buffer, c.data(), c.frames() * sizeof(float));
67 Routines::apply_gain_to_buffer (buffer, c.frames(), gain);
70 ProcessContext<float> c_out (c, buffer);
71 ListedSource<float>::output (c_out);
74 /// Process a non-const ProcsesContext in-place \n RT safe
75 void process (ProcessContext<float> & c)
78 Routines::apply_gain_to_buffer (c.data(), c.frames(), gain);
80 ListedSource<float>::output(c);
89 nframes_t buffer_size;
95 #endif // AUDIOGRAPHER_NORMALIZER_H