2 Copyright (C) 2012 Paul Davis
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "audiographer/general/normalizer.h"
23 namespace AudioGrapher
26 Normalizer::Normalizer (float target_dB)
31 target = pow (10.0f, target_dB * 0.05f);
34 Normalizer::~Normalizer()
39 /// Sets the peak found in the material to be normalized \see PeakReader \n RT safe
40 float Normalizer::set_peak (float peak)
42 if (peak == 0.0f || peak == target) {
49 return enabled ? gain : 1.0;
52 /** Allocates a buffer for using with const ProcessContexts
53 * This function does not need to be called if
54 * non-const ProcessContexts are given to \a process() .
57 void Normalizer::alloc_buffer(samplecnt_t samples)
60 buffer = new float[samples];
61 buffer_size = samples;
64 /// Process a const ProcessContext \see alloc_buffer() \n RT safe
65 void Normalizer::process (ProcessContext<float> const & c)
67 if (throw_level (ThrowProcess) && c.samples() > buffer_size) {
68 throw Exception (*this, "Too many samples given to process()");
72 memcpy (buffer, c.data(), c.samples() * sizeof(float));
73 Routines::apply_gain_to_buffer (buffer, c.samples(), gain);
76 ProcessContext<float> c_out (c, buffer);
77 ListedSource<float>::output (c_out);
80 /// Process a non-const ProcsesContext in-place \n RT safe
81 void Normalizer::process (ProcessContext<float> & c)
84 Routines::apply_gain_to_buffer (c.data(), c.samples(), gain);
86 ListedSource<float>::output(c);