fix merge errors with master
[ardour.git] / libs / audiographer / audiographer / general / normalizer.h
1 #ifndef AUDIOGRAPHER_NORMALIZER_H
2 #define AUDIOGRAPHER_NORMALIZER_H
3
4 #include "audiographer/visibility.h"
5 #include "audiographer/sink.h"
6 #include "audiographer/routines.h"
7 #include "audiographer/utils/listed_source.h"
8
9 #include <cstring>
10
11 namespace AudioGrapher
12 {
13
14 /// A class for normalizing to a specified target in dB
15 class LIBAUDIOGRAPHER_API Normalizer
16   : public ListedSource<float>
17   , public Sink<float>
18   , public Throwing<>
19 {
20   public:
21         /// Constructs a normalizer with a specific target in dB \n RT safe
22         Normalizer (float target_dB)
23           : enabled (false)
24           , buffer (0)
25           , buffer_size (0)
26         {
27                 target = pow (10.0f, target_dB * 0.05f);
28         }
29         
30         ~Normalizer()
31         {
32                 delete [] buffer;
33         }
34
35         /// Sets the peak found in the material to be normalized \see PeakReader \n RT safe
36         void set_peak (float peak)
37         {
38                 if (peak == 0.0f || peak == target) {
39                         /* don't even try */
40                         enabled = false;
41                 } else {
42                         enabled = true;
43                         gain = target / peak;
44                 }
45         }
46
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() .
50           * \n Not RT safe
51           */
52         void alloc_buffer(framecnt_t frames)
53         {
54                 delete [] buffer;
55                 buffer = new float[frames];
56                 buffer_size = frames;
57         }
58
59         /// Process a const ProcessContext \see alloc_buffer() \n RT safe
60         void process (ProcessContext<float> const & c)
61         {
62                 if (throw_level (ThrowProcess) && c.frames() > buffer_size) {
63                         throw Exception (*this, "Too many frames given to process()");
64                 }
65                 
66                 if (enabled) {
67                         memcpy (buffer, c.data(), c.frames() * sizeof(float));
68                         Routines::apply_gain_to_buffer (buffer, c.frames(), gain);
69                 }
70                 
71                 ProcessContext<float> c_out (c, buffer);
72                 ListedSource<float>::output (c_out);
73         }
74         
75         /// Process a non-const ProcsesContext in-place \n RT safe
76         void process (ProcessContext<float> & c)
77         {
78                 if (enabled) {
79                         Routines::apply_gain_to_buffer (c.data(), c.frames(), gain);
80                 }
81                 ListedSource<float>::output(c);
82         }
83         
84   private:
85         bool      enabled;
86         float     target;
87         float     gain;
88         
89         float *   buffer;
90         framecnt_t buffer_size;
91 };
92
93
94 } // namespace
95
96 #endif // AUDIOGRAPHER_NORMALIZER_H