Only show user-presets in favorite sidebar
[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 float 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         return enabled ? gain : 1.0;
50 }
51
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() .
55   * \n Not RT safe
56   */
57 void Normalizer::alloc_buffer(samplecnt_t samples)
58 {
59         delete [] buffer;
60         buffer = new float[samples];
61         buffer_size = samples;
62 }
63
64 /// Process a const ProcessContext \see alloc_buffer() \n RT safe
65 void Normalizer::process (ProcessContext<float> const & c)
66 {
67         if (throw_level (ThrowProcess) && c.samples() > buffer_size) {
68                 throw Exception (*this, "Too many samples given to process()");
69         }
70
71         if (enabled) {
72                 memcpy (buffer, c.data(), c.samples() * sizeof(float));
73                 Routines::apply_gain_to_buffer (buffer, c.samples(), gain);
74         }
75
76         ProcessContext<float> c_out (c, buffer);
77         ListedSource<float>::output (c_out);
78 }
79
80 /// Process a non-const ProcsesContext in-place \n RT safe
81 void Normalizer::process (ProcessContext<float> & c)
82 {
83         if (enabled) {
84                 Routines::apply_gain_to_buffer (c.data(), c.samples(), gain);
85         }
86         ListedSource<float>::output(c);
87 }
88
89 } // namespace