#include <algorithm>
#include <cmath>
+#include <limits>
#include "pbd/compose.h"
using namespace ARDOUR;
-PBD::Signal0<void> Metering::Meter;
-
PeakMeter::PeakMeter (Session& s, const std::string& name)
: Processor (s, string_compose ("meter-%1", name))
{
- Kmeterdsp::init(s.nominal_frame_rate());
+ Kmeterdsp::init(s.nominal_sample_rate());
+ Iec1ppmdsp::init(s.nominal_sample_rate());
+ Iec2ppmdsp::init(s.nominal_sample_rate());
+ Vumeterdsp::init(s.nominal_sample_rate());
+ _pending_active = true;
+ _meter_type = MeterPeak;
+ _reset_dpm = true;
+ _reset_max = true;
+ _bufcnt = 0;
+ _combined_peak = 0;
}
PeakMeter::~PeakMeter ()
{
while (_kmeter.size() > 0) {
delete (_kmeter.back());
+ delete (_iec1meter.back());
+ delete (_iec2meter.back());
+ delete (_vumeter.back());
_kmeter.pop_back();
+ _iec1meter.pop_back();
+ _iec2meter.pop_back();
+ _vumeter.pop_back();
+ }
+ while (_peak_power.size() > 0) {
+ _peak_buffer.pop_back();
+ _peak_power.pop_back();
+ _max_peak_signal.pop_back();
}
}
* (runs in jack realtime context)
*/
void
-PeakMeter::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool)
+PeakMeter::run (BufferSet& bufs, samplepos_t /*start_sample*/, samplepos_t /*end_sample*/, double /*speed*/, pframes_t nframes, bool)
{
if (!_active && !_pending_active) {
return;
}
+ const bool do_reset_max = _reset_max;
+ const bool do_reset_dpm = _reset_dpm;
+ _reset_max = false;
+ _reset_dpm = false;
+ _combined_peak = 0;
// cerr << "meter " << name() << " runs with " << bufs.available() << " inputs\n";
uint32_t n = 0;
+ const float falloff_dB = Config->get_meter_falloff() * nframes / _session.nominal_sample_rate();
+ const uint32_t zoh = _session.nominal_sample_rate() * .021;
+ _bufcnt += nframes;
+
// Meter MIDI in to the first n_midi peaks
for (uint32_t i = 0; i < n_midi; ++i, ++n) {
float val = 0.0f;
- MidiBuffer& buf (bufs.get_midi(i));
-
- for (MidiBuffer::iterator e = buf.begin(); e != buf.end(); ++e) {
- const Evoral::MIDIEvent<framepos_t> ev(*e, false);
+ const MidiBuffer& buf (bufs.get_midi(i));
+
+ for (MidiBuffer::const_iterator e = buf.begin(); e != buf.end(); ++e) {
+ const Evoral::Event<samplepos_t> ev(*e, false);
if (ev.is_note_on()) {
const float this_vel = ev.buffer()[2] / 127.0;
if (this_vel > val) {
val = this_vel;
}
+ if (val > 0.01) {
+ if (_combined_peak < 0.01) {
+ _combined_peak = 0.01;
+ }
+ }
} else {
val += 1.0 / bufs.get_midi(n).capacity();
if (val > 1.0) {
}
}
}
- _peak_signal[n] = max (val, _peak_signal[n]);
+ if (_peak_power[n] < (1.0 / 512.0)) {
+ _peak_power[n] = 0;
+ } else {
+ /* empirical algorithm WRT to audio falloff times */
+ _peak_power[n] -= sqrtf (_peak_power[n]) * falloff_dB * 0.045f;
+ }
+ _peak_power[n] = max(_peak_power[n], val);
+ _max_peak_signal[n] = 0;
}
// Meter audio in to the rest of the peaks
for (uint32_t i = 0; i < n_audio; ++i, ++n) {
- _peak_signal[n] = compute_peak (bufs.get_audio(i).data(), nframes, _peak_signal[n]);
- if (_meter_type & MeterKrms) {
+ if (bufs.get_audio(i).silent()) {
+ _peak_buffer[n] = 0;
+ } else {
+ _peak_buffer[n] = compute_peak (bufs.get_audio(i).data(), nframes, _peak_buffer[n]);
+ _peak_buffer[n] = std::min (_peak_buffer[n], 100.f); // cut off at +40dBFS for falloff.
+ _max_peak_signal[n] = std::max(_peak_buffer[n], _max_peak_signal[n]); // todo sync reset
+ _combined_peak = std::max(_peak_buffer[n], _combined_peak);
+ }
+
+ if (do_reset_max) {
+ _max_peak_signal[n] = 0;
+ }
+
+ if (do_reset_dpm) {
+ _peak_buffer[n] = 0;
+ _peak_power[n] = -std::numeric_limits<float>::infinity();
+ } else {
+ // falloff
+ if (_peak_power[n] > -318.8f) {
+ _peak_power[n] -= falloff_dB;
+ } else {
+ _peak_power[n] = -std::numeric_limits<float>::infinity();
+ }
+ _peak_power[n] = max(_peak_power[n], accurate_coefficient_to_dB(_peak_buffer[n]));
+ // integration buffer, retain peaks > 49Hz
+ if (_bufcnt > zoh) {
+ _peak_buffer[n] = 0;
+ }
+ }
+
+ if (_meter_type & (MeterKrms | MeterK20 | MeterK14 | MeterK12)) {
_kmeter[i]->process(bufs.get_audio(i).data(), nframes);
}
+ if (_meter_type & (MeterIEC1DIN | MeterIEC1NOR)) {
+ _iec1meter[i]->process(bufs.get_audio(i).data(), nframes);
+ }
+ if (_meter_type & (MeterIEC2BBC | MeterIEC2EBU)) {
+ _iec2meter[i]->process(bufs.get_audio(i).data(), nframes);
+ }
+ if (_meter_type & MeterVU) {
+ _vumeter[i]->process(bufs.get_audio(i).data(), nframes);
+ }
}
// Zero any excess peaks
- for (uint32_t i = n; i < _peak_signal.size(); ++i) {
- _peak_signal[i] = 0.0f;
+ for (uint32_t i = n; i < _peak_power.size(); ++i) {
+ _peak_power[i] = -std::numeric_limits<float>::infinity();
+ _max_peak_signal[n] = 0;
+ }
+
+ if (_bufcnt > zoh) {
+ _bufcnt = 0;
}
_active = _pending_active;
void
PeakMeter::reset ()
{
- for (size_t i = 0; i < _peak_signal.size(); ++i) {
- _peak_signal[i] = 0.0f;
+ if (_active || _pending_active) {
+ _reset_dpm = true;
+ } else {
+ for (size_t i = 0; i < _peak_power.size(); ++i) {
+ _peak_power[i] = -std::numeric_limits<float>::infinity();
+ _peak_buffer[i] = 0;
+ }
}
+ // these are handled async just fine.
for (size_t n = 0; n < _kmeter.size(); ++n) {
_kmeter[n]->reset();
+ _iec1meter[n]->reset();
+ _iec2meter[n]->reset();
+ _vumeter[n]->reset();
}
}
void
PeakMeter::reset_max ()
{
- for (size_t i = 0; i < _max_peak_power.size(); ++i) {
- _max_peak_power[i] = -INFINITY;
+ if (_active || _pending_active) {
+ _reset_max = true;
+ return;
+ }
+ for (size_t i = 0; i < _max_peak_signal.size(); ++i) {
_max_peak_signal[i] = 0;
+ _peak_buffer[i] = 0;
}
}
bool
-PeakMeter::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
+PeakMeter::can_support_io_configuration (const ChanCount& in, ChanCount& out)
{
out = in;
return true;
bool
PeakMeter::configure_io (ChanCount in, ChanCount out)
{
+ bool changed = false;
if (out != in) { // always 1:1
return false;
}
+ if (current_meters != in) {
+ changed = true;
+ }
+
current_meters = in;
- reset_max_channels (in);
+ set_max_channels (in);
+
+ if (changed) {
+ reset_max();
+ }
return Processor::configure_io (in, out);
}
void
PeakMeter::reflect_inputs (const ChanCount& in)
{
+ reset();
current_meters = in;
-
- const size_t limit = min (_peak_signal.size(), (size_t) current_meters.n_total ());
- const size_t n_midi = min (_peak_signal.size(), (size_t) current_meters.n_midi());
- const size_t n_audio = current_meters.n_audio();
-
- for (size_t n = 0; n < limit; ++n) {
- if (n < n_midi) {
- _visible_peak_power[n] = 0;
- } else {
- _visible_peak_power[n] = -INFINITY;
- }
- }
-
- for (size_t n = 0; n < n_audio; ++n) {
- _kmeter[n]->reset();
- }
-
reset_max();
+ // ConfigurationChanged() postponed
+}
- ConfigurationChanged (in, in); /* EMIT SIGNAL */
+void
+PeakMeter::emit_configuration_changed () {
+ ConfigurationChanged (current_meters, current_meters); /* EMIT SIGNAL */
}
void
-PeakMeter::reset_max_channels (const ChanCount& chn)
+PeakMeter::set_max_channels (const ChanCount& chn)
{
uint32_t const limit = chn.n_total();
const size_t n_audio = chn.n_audio();
- while (_peak_signal.size() > limit) {
- _peak_signal.pop_back();
- _visible_peak_power.pop_back();
+ while (_peak_power.size() > limit) {
+ _peak_buffer.pop_back();
+ _peak_power.pop_back();
_max_peak_signal.pop_back();
- _max_peak_power.pop_back();
}
- while (_peak_signal.size() < limit) {
- _peak_signal.push_back(0);
- _visible_peak_power.push_back(minus_infinity());
+ while (_peak_power.size() < limit) {
+ _peak_buffer.push_back(0);
+ _peak_power.push_back(-std::numeric_limits<float>::infinity());
_max_peak_signal.push_back(0);
- _max_peak_power.push_back(minus_infinity());
}
- assert(_peak_signal.size() == limit);
- assert(_visible_peak_power.size() == limit);
+ assert(_peak_buffer.size() == limit);
+ assert(_peak_power.size() == limit);
assert(_max_peak_signal.size() == limit);
- assert(_max_peak_power.size() == limit);
/* alloc/free other audio-only meter types. */
while (_kmeter.size() > n_audio) {
delete (_kmeter.back());
+ delete (_iec1meter.back());
+ delete (_iec2meter.back());
+ delete (_vumeter.back());
_kmeter.pop_back();
+ _iec1meter.pop_back();
+ _iec2meter.pop_back();
+ _vumeter.pop_back();
}
while (_kmeter.size() < n_audio) {
_kmeter.push_back(new Kmeterdsp());
+ _iec1meter.push_back(new Iec1ppmdsp());
+ _iec2meter.push_back(new Iec2ppmdsp());
+ _vumeter.push_back(new Vumeterdsp());
}
assert(_kmeter.size() == n_audio);
+ assert(_iec1meter.size() == n_audio);
+ assert(_iec2meter.size() == n_audio);
+ assert(_vumeter.size() == n_audio);
+
+ reset();
+ reset_max();
}
/** To be driven by the Meter signal from IO.
* of meter size during this call.
*/
-void
-PeakMeter::meter ()
-{
- if (!_active) {
- return;
- }
-
- assert(_visible_peak_power.size() == _peak_signal.size());
-
- const size_t limit = min (_peak_signal.size(), (size_t) current_meters.n_total ());
- const size_t n_midi = min (_peak_signal.size(), (size_t) current_meters.n_midi());
-
- for (size_t n = 0; n < limit; ++n) {
-
- /* grab peak since last read */
-
- float new_peak = _peak_signal[n]; /* XXX we should use atomic exchange from here ... */
- _peak_signal[n] = 0; /* ... to here */
-
- if (n < n_midi) {
- _max_peak_power[n] = -INFINITY; // std::max (new_peak, _max_peak_power[n]); // XXX
- _max_peak_signal[n] = 0;
- if (Config->get_meter_falloff() == 0.0f || new_peak > _visible_peak_power[n]) {
- ;
- } else {
- /* empirical WRT to falloff times , 0.01f ^= 100 Hz update rate */
- new_peak = _visible_peak_power[n] - sqrt(_visible_peak_power[n] * Config->get_meter_falloff() * 0.01f * 0.0002f);
- if (new_peak < (1.0 / 512.0)) new_peak = 0;
- }
- _visible_peak_power[n] = new_peak;
- continue;
- }
-
- /* AUDIO */
-
- /* compute new visible value using falloff */
-
- _max_peak_signal[n] = std::max(new_peak, _max_peak_signal[n]);
-
- if (new_peak > 0.0) {
- new_peak = accurate_coefficient_to_dB (new_peak);
- } else {
- new_peak = minus_infinity();
- }
-
- /* update max peak */
-
- _max_peak_power[n] = std::max (new_peak, _max_peak_power[n]);
-
- if (Config->get_meter_falloff() == 0.0f || new_peak > _visible_peak_power[n]) {
- _visible_peak_power[n] = new_peak;
- } else {
- // do falloff
- new_peak = _visible_peak_power[n] - (Config->get_meter_falloff() * 0.01f);
- _visible_peak_power[n] = std::max (new_peak, -INFINITY);
- }
- }
-}
+#define CHECKSIZE(MTR) (n < MTR.size() + n_midi && n >= n_midi)
float
PeakMeter::meter_level(uint32_t n, MeterType type) {
+ float mcptmp;
switch (type) {
case MeterKrms:
+ case MeterK20:
+ case MeterK14:
+ case MeterK12:
+ {
+ const uint32_t n_midi = current_meters.n_midi();
+ if (CHECKSIZE(_kmeter)) {
+ return accurate_coefficient_to_dB (_kmeter[n - n_midi]->read());
+ }
+ }
+ break;
+ case MeterIEC1DIN:
+ case MeterIEC1NOR:
{
- const uint32_t n_midi = current_meters.n_midi();
- if ((n - n_midi) < _kmeter.size() && (n - n_midi) >= 0) {
-#if 0
- return fast_coefficient_to_dB (_kmeter[n-n_midi]->read());
-#else
- return accurate_coefficient_to_dB (_kmeter[n-n_midi]->read());
-#endif
+ const uint32_t n_midi = current_meters.n_midi();
+ if (CHECKSIZE(_iec1meter)) {
+ return accurate_coefficient_to_dB (_iec1meter[n - n_midi]->read());
}
- return minus_infinity();
}
+ break;
+ case MeterIEC2BBC:
+ case MeterIEC2EBU:
+ {
+ const uint32_t n_midi = current_meters.n_midi();
+ if (CHECKSIZE(_iec2meter)) {
+ return accurate_coefficient_to_dB (_iec2meter[n - n_midi]->read());
+ }
+ }
+ break;
+ case MeterVU:
+ {
+ const uint32_t n_midi = current_meters.n_midi();
+ if (CHECKSIZE(_vumeter)) {
+ return accurate_coefficient_to_dB (_vumeter[n - n_midi]->read());
+ }
+ }
+ break;
case MeterPeak:
- return peak_power(n);
- case MeterMaxSignal:
- if (n < _max_peak_signal.size()) {
- return _max_peak_signal[n];
- } else {
- return minus_infinity();
+ case MeterPeak0dB:
+ if (n < _peak_power.size()) {
+ return _peak_power[n];
}
+ break;
+ case MeterMCP:
+ mcptmp = _combined_peak;
+ return accurate_coefficient_to_dB(mcptmp);
+ case MeterMaxSignal:
+ assert(0);
+ break;
default:
case MeterMaxPeak:
- if (n < _max_peak_power.size()) {
- return _max_peak_power[n];
- } else {
- return minus_infinity();
+ if (n < _max_peak_signal.size()) {
+ return accurate_coefficient_to_dB(_max_peak_signal[n]);
}
+ break;
}
+ return minus_infinity();
}
void
_meter_type = t;
- if (t & MeterKrms) {
+ if (t & (MeterKrms | MeterK20 | MeterK14 | MeterK12)) {
const size_t n_audio = current_meters.n_audio();
for (size_t n = 0; n < n_audio; ++n) {
_kmeter[n]->reset();
}
}
+ if (t & (MeterIEC1DIN | MeterIEC1NOR)) {
+ const size_t n_audio = current_meters.n_audio();
+ for (size_t n = 0; n < n_audio; ++n) {
+ _iec1meter[n]->reset();
+ }
+ }
+ if (t & (MeterIEC2BBC | MeterIEC2EBU)) {
+ const size_t n_audio = current_meters.n_audio();
+ for (size_t n = 0; n < n_audio; ++n) {
+ _iec2meter[n]->reset();
+ }
+ }
+ if (t & MeterVU) {
+ const size_t n_audio = current_meters.n_audio();
+ for (size_t n = 0; n < n_audio; ++n) {
+ _vumeter[n]->reset();
+ }
+ }
+
TypeChanged(t);
}
XMLNode&
-PeakMeter::state (bool full_state)
+PeakMeter::state ()
{
- XMLNode& node (Processor::state (full_state));
- node.add_property("type", "meter");
+ XMLNode& node (Processor::state ());
+ node.set_property("type", "meter");
return node;
}