globally remove all trailing whitespace from ardour code base.
[ardour.git] / libs / audiographer / src / general / normalizer.cc
1 /*
2     Copyright (C) 2012 Paul Davis
3     Author: Sakari Bergen
4
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.
9
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.
14
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.
18
19 */
20
21 #include "audiographer/general/normalizer.h"
22
23 namespace AudioGrapher
24 {
25
26 Normalizer::Normalizer (float target_dB)
27           : enabled (false)
28           , buffer (0)
29           , buffer_size (0)
30 {
31         target = pow (10.0f, target_dB * 0.05f);
32 }
33         
34 Normalizer::~Normalizer()
35 {
36         delete [] buffer;
37 }
38
39 /// Sets the peak found in the material to be normalized \see PeakReader \n RT safe
40 void Normalizer::set_peak (float peak)
41 {
42         if (peak == 0.0f || peak == target) {
43                 /* don't even try */
44                 enabled = false;
45         } else {
46                 enabled = true;
47                 gain = target / peak;
48         }
49 }
50
51 /** Allocates a buffer for using with const ProcessContexts
52   * This function does not need to be called if
53   * non-const ProcessContexts are given to \a process() .
54   * \n Not RT safe
55   */
56 void Normalizer::alloc_buffer(framecnt_t frames)
57 {
58         delete [] buffer;
59         buffer = new float[frames];
60         buffer_size = frames;
61 }
62
63 /// Process a const ProcessContext \see alloc_buffer() \n RT safe
64 void Normalizer::process (ProcessContext<float> const & c)
65 {
66         if (throw_level (ThrowProcess) && c.frames() > buffer_size) {
67                 throw Exception (*this, "Too many frames given to process()");
68         }
69         
70         if (enabled) {
71                 memcpy (buffer, c.data(), c.frames() * sizeof(float));
72                 Routines::apply_gain_to_buffer (buffer, c.frames(), gain);
73         }
74         
75         ProcessContext<float> c_out (c, buffer);
76         ListedSource<float>::output (c_out);
77 }
78
79 /// Process a non-const ProcsesContext in-place \n RT safe
80 void Normalizer::process (ProcessContext<float> & c)
81 {
82         if (enabled) {
83                 Routines::apply_gain_to_buffer (c.data(), c.frames(), gain);
84         }
85         ListedSource<float>::output(c);
86 }
87
88 } // namespace