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.
19 #include "ardour/meter.h"
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"
32 using namespace ARDOUR;
34 sigc::signal<void> Metering::Meter;
35 Glib::StaticMutex Metering::m_meter_signal_lock;
38 Metering::connect (sigc::slot<void> the_slot)
40 // SignalProcessor::Meter is emitted from another thread so the
41 // Meter signal must be protected.
42 Glib::Mutex::Lock guard (m_meter_signal_lock);
43 return Meter.connect (the_slot);
47 Metering::disconnect (sigc::connection& c)
49 Glib::Mutex::Lock guard (m_meter_signal_lock);
56 The meter signal lock is taken to prevent modification of the
57 Meter signal while updating the meters, taking the meter signal
58 lock prior to taking the io_lock ensures that all IO will remain
62 Metering::update_meters()
64 Glib::Mutex::Lock guard (m_meter_signal_lock);
65 Meter(); /* EMIT SIGNAL */
68 /** Get peaks from @a bufs
69 * Input acceptance is lenient - the first n buffers from @a bufs will
70 * be metered, where n was set by the last call to setup(), excess meters will
74 PeakMeter::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
76 const uint32_t n_audio = min(_configured_input.n_audio(), bufs.count().n_audio());
77 const uint32_t n_midi = min(_configured_input.n_midi(), bufs.count().n_midi());
81 // Meter MIDI in to the first n_midi peaks
82 for (uint32_t i = 0; i < n_midi; ++i, ++n) {
84 for (MidiBuffer::iterator e = bufs.get_midi(i).begin(); e != bufs.get_midi(i).end(); ++e) {
85 const Evoral::MIDIEvent<nframes_t> ev(*e, false);
86 if (ev.is_note_on()) {
87 const float this_vel = log(ev.buffer()[2] / 127.0 * (M_E*M_E-M_E) + M_E) - 1.0;
92 val += 1.0 / bufs.get_midi(n).capacity();
101 // Meter audio in to the rest of the peaks
102 for (uint32_t i = 0; i < n_audio; ++i, ++n) {
103 _peak_power[n] = compute_peak (bufs.get_audio(i).data(), nframes, _peak_power[n]);
106 // Zero any excess peaks
107 for (uint32_t i = n; i < _peak_power.size(); ++i) {
108 _peak_power[i] = 0.0f;
112 PeakMeter::PeakMeter (Session& s, const XMLNode& node)
113 : Processor (s, node)
120 for (size_t i = 0; i < _peak_power.size(); ++i) {
121 _peak_power[i] = 0.0f;
126 PeakMeter::reset_max ()
128 for (size_t i = 0; i < _max_peak_power.size(); ++i) {
129 _max_peak_power[i] = -INFINITY;
134 PeakMeter::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
141 PeakMeter::configure_io (ChanCount in, ChanCount out)
143 if (out != in) { // always 1:1
147 uint32_t limit = in.n_total();
149 while (_peak_power.size() > limit) {
150 _peak_power.pop_back();
151 _visible_peak_power.pop_back();
152 _max_peak_power.pop_back();
155 while (_peak_power.size() < limit) {
156 _peak_power.push_back(0);
157 _visible_peak_power.push_back(minus_infinity());
158 _max_peak_power.push_back(minus_infinity());
161 assert(_peak_power.size() == limit);
162 assert(_visible_peak_power.size() == limit);
163 assert(_max_peak_power.size() == limit);
165 return Processor::configure_io (in, out);
168 /** To be driven by the Meter signal from IO.
169 * Caller MUST hold its own processor_lock to prevent reconfiguration
170 * of meter size during this call.
176 assert(_visible_peak_power.size() == _peak_power.size());
178 const size_t limit = _peak_power.size();
180 for (size_t n = 0; n < limit; ++n) {
182 /* grab peak since last read */
184 float new_peak = _peak_power[n]; /* XXX we should use atomic exchange from here ... */
185 _peak_power[n] = 0; /* ... to here */
187 /* compute new visible value using falloff */
189 if (new_peak > 0.0) {
190 new_peak = coefficient_to_dB (new_peak);
192 new_peak = minus_infinity();
195 /* update max peak */
197 _max_peak_power[n] = std::max (new_peak, _max_peak_power[n]);
199 if (Config->get_meter_falloff() == 0.0f || new_peak > _visible_peak_power[n]) {
200 _visible_peak_power[n] = new_peak;
203 new_peak = _visible_peak_power[n] - (Config->get_meter_falloff() * 0.01f);
204 _visible_peak_power[n] = std::max (new_peak, -INFINITY);
210 PeakMeter::state (bool full_state)
212 XMLNode& node (Processor::state (full_state));
213 node.add_property("type", "meter");