2 Copyright (C) 2006 Paul Davis
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)
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
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.
22 #include "pbd/compose.h"
24 #include "ardour/audio_buffer.h"
25 #include "ardour/buffer_set.h"
26 #include "ardour/dB.h"
27 #include "ardour/meter.h"
28 #include "ardour/midi_buffer.h"
29 #include "ardour/rc_configuration.h"
30 #include "ardour/runtime_functions.h"
34 using namespace ARDOUR;
36 PBD::Signal0<void> Metering::Meter;
38 PeakMeter::PeakMeter (Session& s, const std::string& name)
39 : Processor (s, string_compose ("meter-%1", name))
44 /** Get peaks from @a bufs
45 * Input acceptance is lenient - the first n buffers from @a bufs will
46 * be metered, where n was set by the last call to setup(), excess meters will
50 PeakMeter::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool)
52 if (!_active && !_pending_active) {
56 // cerr << "meter " << name() << " runs with " << bufs.available() << " inputs\n";
58 const uint32_t n_audio = min (current_meters.n_audio(), bufs.count().n_audio());
59 const uint32_t n_midi = min (current_meters.n_midi(), bufs.count().n_midi());
63 // Meter MIDI in to the first n_midi peaks
64 for (uint32_t i = 0; i < n_midi; ++i, ++n) {
66 MidiBuffer& buf (bufs.get_midi(i));
68 for (MidiBuffer::iterator e = buf.begin(); e != buf.end(); ++e) {
69 const Evoral::MIDIEvent<framepos_t> ev(*e, false);
70 if (ev.is_note_on()) {
71 const float this_vel = log(ev.buffer()[2] / 127.0 * (M_E*M_E-M_E) + M_E) - 1.0;
76 val += 1.0 / bufs.get_midi(n).capacity();
82 _peak_power[n] = max (val, _peak_power[n]);
85 // Meter audio in to the rest of the peaks
86 for (uint32_t i = 0; i < n_audio; ++i, ++n) {
87 _peak_power[n] = compute_peak (bufs.get_audio(i).data(), nframes, _peak_power[n]);
90 // Zero any excess peaks
91 for (uint32_t i = n; i < _peak_power.size(); ++i) {
92 _peak_power[i] = 0.0f;
95 _active = _pending_active;
101 for (size_t i = 0; i < _peak_power.size(); ++i) {
102 _peak_power[i] = 0.0f;
107 PeakMeter::reset_max ()
109 for (size_t i = 0; i < _max_peak_power.size(); ++i) {
110 _max_peak_power[i] = -INFINITY;
115 PeakMeter::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
122 PeakMeter::configure_io (ChanCount in, ChanCount out)
124 if (out != in) { // always 1:1
130 reset_max_channels (in);
132 return Processor::configure_io (in, out);
136 PeakMeter::reflect_inputs (const ChanCount& in)
140 const size_t limit = min (_peak_power.size(), (size_t) current_meters.n_total ());
141 const size_t n_midi = min (_peak_power.size(), (size_t) current_meters.n_midi());
143 for (size_t n = 0; n < limit; ++n) {
145 _visible_peak_power[n] = 0;
147 _visible_peak_power[n] = -INFINITY;
152 ConfigurationChanged (in, in); /* EMIT SIGNAL */
156 PeakMeter::reset_max_channels (const ChanCount& chn)
158 uint32_t const limit = chn.n_total();
160 while (_peak_power.size() > limit) {
161 _peak_power.pop_back();
162 _visible_peak_power.pop_back();
163 _max_peak_power.pop_back();
166 while (_peak_power.size() < limit) {
167 _peak_power.push_back(0);
168 _visible_peak_power.push_back(minus_infinity());
169 _max_peak_power.push_back(minus_infinity());
172 assert(_peak_power.size() == limit);
173 assert(_visible_peak_power.size() == limit);
174 assert(_max_peak_power.size() == limit);
177 /** To be driven by the Meter signal from IO.
178 * Caller MUST hold its own processor_lock to prevent reconfiguration
179 * of meter size during this call.
189 assert(_visible_peak_power.size() == _peak_power.size());
191 const size_t limit = min (_peak_power.size(), (size_t) current_meters.n_total ());
193 for (size_t n = 0; n < limit; ++n) {
195 /* grab peak since last read */
197 float new_peak = _peak_power[n]; /* XXX we should use atomic exchange from here ... */
198 _peak_power[n] = 0; /* ... to here */
200 /* compute new visible value using falloff */
202 if (new_peak > 0.0) {
203 new_peak = fast_coefficient_to_dB (new_peak);
205 new_peak = minus_infinity();
208 /* update max peak */
210 _max_peak_power[n] = std::max (new_peak, _max_peak_power[n]);
212 if (Config->get_meter_falloff() == 0.0f || new_peak > _visible_peak_power[n]) {
213 _visible_peak_power[n] = new_peak;
216 new_peak = _visible_peak_power[n] - (Config->get_meter_falloff() * 0.01f);
217 _visible_peak_power[n] = std::max (new_peak, -INFINITY);
223 PeakMeter::state (bool full_state)
225 XMLNode& node (Processor::state (full_state));
226 node.add_property("type", "meter");