e49b69574e3e8bfbcc19e5deba9f235df4bda71a
[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 using namespace ARDOUR;
33
34 PBD::Signal0<void> Metering::Meter;
35
36 PeakMeter::PeakMeter (Session& s, const XMLNode& node)
37         : Processor (s, node)
38 {
39         
40 }
41
42 /** Get peaks from @a bufs
43  * Input acceptance is lenient - the first n buffers from @a bufs will
44  * be metered, where n was set by the last call to setup(), excess meters will
45  * be set to 0.
46  */
47 void
48 PeakMeter::run (BufferSet& bufs, sframes_t /*start_frame*/, sframes_t /*end_frame*/, nframes_t nframes, bool)
49 {
50         if (!_active && !_pending_active) {
51                 return;
52         }
53
54         const uint32_t n_audio = min(_configured_input.n_audio(), bufs.count().n_audio());
55         const uint32_t n_midi  = min(_configured_input.n_midi(), bufs.count().n_midi());
56
57         uint32_t n = 0;
58
59         // Meter MIDI in to the first n_midi peaks
60         for (uint32_t i = 0; i < n_midi; ++i, ++n) {
61                 float val = 0.0f;
62                 for (MidiBuffer::iterator e = bufs.get_midi(i).begin(); e != bufs.get_midi(i).end(); ++e) {
63                         const Evoral::MIDIEvent<nframes_t> ev(*e, false);
64                         if (ev.is_note_on()) {
65                                 const float this_vel = log(ev.buffer()[2] / 127.0 * (M_E*M_E-M_E) + M_E) - 1.0;
66                                 if (this_vel > val) {
67                                         val = this_vel;
68                                 }
69                         } else {
70                                 val += 1.0 / bufs.get_midi(n).capacity();
71                                 if (val > 1.0) {
72                                         val = 1.0;
73                                 }
74                         }
75                 }
76                 _peak_power[n] = val;
77         }
78
79         // Meter audio in to the rest of the peaks
80         for (uint32_t i = 0; i < n_audio; ++i, ++n) {
81                 _peak_power[n] = compute_peak (bufs.get_audio(i).data(), nframes, _peak_power[n]);
82         }
83
84         // Zero any excess peaks
85         for (uint32_t i = n; i < _peak_power.size(); ++i) {
86                 _peak_power[i] = 0.0f;
87         }
88
89         _active = _pending_active;
90 }
91
92 void
93 PeakMeter::reset ()
94 {
95         for (size_t i = 0; i < _peak_power.size(); ++i) {
96                 _peak_power[i] = 0.0f;
97         }
98 }
99
100 void
101 PeakMeter::reset_max ()
102 {
103         for (size_t i = 0; i < _max_peak_power.size(); ++i) {
104                 _max_peak_power[i] = -INFINITY;
105         }
106 }
107
108 bool
109 PeakMeter::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
110 {
111         out = in;
112         return true;
113 }
114
115 bool
116 PeakMeter::configure_io (ChanCount in, ChanCount out)
117 {
118         if (out != in) { // always 1:1
119                 return false;
120         }
121
122         current_meters = in;
123
124         return Processor::configure_io (in, out);
125 }
126
127 void
128 PeakMeter::reflect_inputs (const ChanCount& in)
129 {
130         current_meters = in;
131 }
132
133 void
134 PeakMeter::reset_max_channels (const ChanCount& chn)
135 {
136         uint32_t limit = chn.n_total();
137
138         while (_peak_power.size() > limit) {
139                 _peak_power.pop_back();
140                 _visible_peak_power.pop_back();
141                 _max_peak_power.pop_back();
142         }
143
144         while (_peak_power.size() < limit) {
145                 _peak_power.push_back(0);
146                 _visible_peak_power.push_back(minus_infinity());
147                 _max_peak_power.push_back(minus_infinity());
148         }
149
150         assert(_peak_power.size() == limit);
151         assert(_visible_peak_power.size() == limit);
152         assert(_max_peak_power.size() == limit);
153 }
154
155 /** To be driven by the Meter signal from IO.
156  * Caller MUST hold its own processor_lock to prevent reconfiguration
157  * of meter size during this call.
158  */
159
160 void
161 PeakMeter::meter ()
162 {
163         if (!_active) {
164                 return;
165         }
166
167         assert(_visible_peak_power.size() == _peak_power.size());
168
169         const size_t limit = min (_peak_power.size(), (size_t) current_meters.n_total ());
170
171         for (size_t n = 0; n < limit; ++n) {
172
173                 /* grab peak since last read */
174
175                 float new_peak = _peak_power[n]; /* XXX we should use atomic exchange from here ... */
176                 _peak_power[n] = 0;              /* ... to here */
177
178                 /* compute new visible value using falloff */
179
180                 if (new_peak > 0.0) {
181                         new_peak = fast_coefficient_to_dB (new_peak);
182                 } else {
183                         new_peak = minus_infinity();
184                 }
185
186                 /* update max peak */
187
188                 _max_peak_power[n] = std::max (new_peak, _max_peak_power[n]);
189
190                 if (Config->get_meter_falloff() == 0.0f || new_peak > _visible_peak_power[n]) {
191                         _visible_peak_power[n] = new_peak;
192                 } else {
193                         // do falloff
194                         new_peak = _visible_peak_power[n] - (Config->get_meter_falloff() * 0.01f);
195                         _visible_peak_power[n] = std::max (new_peak, -INFINITY);
196                 }
197         }
198 }
199
200 XMLNode&
201 PeakMeter::state (bool full_state)
202 {
203         XMLNode& node (Processor::state (full_state));
204         node.add_property("type", "meter");
205         return node;
206 }
207
208