The great audio processing overhaul.
[ardour.git] / libs / ardour / meter.cc
1 /*
2     Copyright (C) 2006 Paul Davis 
3     
4     This program is free software; you can redistribute it and/or modify it
5     under the terms of the GNU General Public License as published by the Free
6     Software Foundation; either version 2 of the License, or (at your option)
7     any later version.
8     
9     This program is distributed in the hope that it will be useful, but WITHOUT
10     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12     for more details.
13     
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include "ardour/meter.h"
20 #include <algorithm>
21 #include <cmath>
22 #include "ardour/buffer_set.h"
23 #include "ardour/peak.h"
24 #include "ardour/dB.h"
25 #include "ardour/session.h"
26 #include "ardour/midi_buffer.h"
27 #include "ardour/audio_buffer.h"
28 #include "ardour/runtime_functions.h"
29
30 using namespace std;
31
32 namespace ARDOUR {
33
34
35 /** Get peaks from @a bufs
36  * Input acceptance is lenient - the first n buffers from @a bufs will
37  * be metered, where n was set by the last call to setup(), excess meters will
38  * be set to 0.
39  */
40 void
41 PeakMeter::run_in_place (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes)
42 {
43         const uint32_t n_audio = min(_configured_input.n_audio(), bufs.count().n_audio());
44         const uint32_t n_midi  = min(_configured_input.n_midi(), bufs.count().n_midi());
45         
46         uint32_t n = 0;
47         
48         // Meter MIDI in to the first n_midi peaks
49         for (uint32_t i = 0; i < n_midi; ++i, ++n) {
50                 float val = 0.0f;
51                 for (MidiBuffer::iterator e = bufs.get_midi(i).begin(); e != bufs.get_midi(i).end(); ++e) {
52                         const Evoral::MIDIEvent<nframes_t> ev(*e, false);
53                         if (ev.is_note_on()) {
54                                 const float this_vel = log(ev.buffer()[2] / 127.0 * (M_E*M_E-M_E) + M_E) - 1.0;
55                                 if (this_vel > val) {
56                                         val = this_vel;
57                                 }
58                         } else {
59                                 val += 1.0 / bufs.get_midi(n).capacity();
60                                 if (val > 1.0) {
61                                         val = 1.0;
62                                 }
63                         }
64                 }
65                 _peak_power[n] = val;
66         }
67
68         // Meter audio in to the rest of the peaks
69         for (uint32_t i = 0; i < n_audio; ++i, ++n) {
70                 _peak_power[n] = compute_peak (bufs.get_audio(i).data(), nframes, _peak_power[n]); 
71         }
72
73         // Zero any excess peaks
74         for (uint32_t i = n; i < _peak_power.size(); ++i) {
75                 _peak_power[i] = 0.0f;
76         }
77 }
78
79 void
80 PeakMeter::reset ()
81 {
82         for (size_t i = 0; i < _peak_power.size(); ++i) {
83                 _peak_power[i] = 0.0f;
84         }
85 }
86
87 void
88 PeakMeter::reset_max ()
89 {
90         for (size_t i = 0; i < _max_peak_power.size(); ++i) {
91                 _max_peak_power[i] = -INFINITY;
92         }
93 }
94
95 bool
96 PeakMeter::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
97 {
98         out = in;
99         return true;
100 }
101
102 bool
103 PeakMeter::configure_io (ChanCount in, ChanCount out)
104 {
105         if (out != in) { // always 1:1
106                 return false;
107         }
108         
109         uint32_t limit = in.n_total();
110         
111         while (_peak_power.size() > limit) {
112                 _peak_power.pop_back();
113                 _visible_peak_power.pop_back();
114                 _max_peak_power.pop_back();
115         }
116
117         while (_peak_power.size() < limit) {
118                 _peak_power.push_back(0);
119                 _visible_peak_power.push_back(minus_infinity());
120                 _max_peak_power.push_back(minus_infinity());
121         }
122
123         assert(_peak_power.size() == limit);
124         assert(_visible_peak_power.size() == limit);
125         assert(_max_peak_power.size() == limit);
126
127         return Processor::configure_io (in, out);
128 }
129
130 /** To be driven by the Meter signal from IO.
131  * Caller MUST hold io_lock!
132  */
133 void
134 PeakMeter::meter ()
135 {
136         assert(_visible_peak_power.size() == _peak_power.size());
137
138         const size_t limit = _peak_power.size();
139
140         for (size_t n = 0; n < limit; ++n) {
141
142                 /* XXX we should use atomic exchange here */
143
144                 /* grab peak since last read */
145
146                 float new_peak = _peak_power[n];
147                 _peak_power[n] = 0;
148                 
149                 /* compute new visible value using falloff */
150
151                 if (new_peak > 0.0) {
152                         new_peak = coefficient_to_dB (new_peak);
153                 } else {
154                         new_peak = minus_infinity();
155                 }
156                 
157                 /* update max peak */
158                 
159                 _max_peak_power[n] = std::max (new_peak, _max_peak_power[n]);
160                 
161                 if (Config->get_meter_falloff() == 0.0f || new_peak > _visible_peak_power[n]) {
162                         _visible_peak_power[n] = new_peak;
163                 } else {
164                         // do falloff
165                         new_peak = _visible_peak_power[n] - (Config->get_meter_falloff() * 0.01f);
166                         _visible_peak_power[n] = std::max (new_peak, -INFINITY);
167                 }
168         }
169 }
170
171 XMLNode&
172 PeakMeter::state (bool full_state)
173 {
174         return get_state();
175 }
176
177 XMLNode&
178 PeakMeter::get_state()
179 {
180         XMLNode* node = new XMLNode(state_node_name);
181         node->add_property("type", "meter");
182         return *node;
183 }
184
185 } // namespace ARDOUR