From 6e1781e5e33d6d97233a2e46aa98fd570d09e31f Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Sun, 7 Jul 2013 02:00:43 +0200 Subject: [PATCH] experimental RMS-meter and peak-signal (vs peak-power) query --- gtk2_ardour/level_meter.cc | 12 +++- gtk2_ardour/level_meter.h | 4 ++ gtk2_ardour/meter_strip.cc | 45 ++++++++++++++ gtk2_ardour/meter_strip.h | 6 ++ libs/ardour/ardour/meter.h | 14 ++++- libs/ardour/ardour/types.h | 7 +++ libs/ardour/enums.cc | 7 +++ libs/ardour/kmeterdsp.cc | 116 +++++++++++++++++++++++++++++++++++++ libs/ardour/kmeterdsp.h | 45 ++++++++++++++ libs/ardour/meter.cc | 115 +++++++++++++++++++++++++++++------- libs/ardour/wscript | 1 + 11 files changed, 348 insertions(+), 24 deletions(-) create mode 100644 libs/ardour/kmeterdsp.cc create mode 100644 libs/ardour/kmeterdsp.h diff --git a/gtk2_ardour/level_meter.cc b/gtk2_ardour/level_meter.cc index 34d34608c6..cc45e45f70 100644 --- a/gtk2_ardour/level_meter.cc +++ b/gtk2_ardour/level_meter.cc @@ -59,6 +59,7 @@ LevelMeter::LevelMeter (Session* s) UI::instance()->theme_changed.connect (sigc::mem_fun(*this, &LevelMeter::on_theme_changed)); ColorsChanged.connect (sigc::mem_fun (*this, &LevelMeter::color_handler)); max_peak = minus_infinity(); + meter_type = MeterPeak; } void @@ -100,7 +101,7 @@ LevelMeter::update_meters () for (n = 0, i = meters.begin(); i != meters.end(); ++i, ++n) { if ((*i).packed) { - mpeak = _meter->max_peak_power(n); + mpeak = _meter->meter_level(n, MeterMaxPeak); if (mpeak > (*i).max_peak) { (*i).max_peak = mpeak; (*i).meter->set_highlight(mpeak > Config->get_meter_peak()); @@ -109,7 +110,7 @@ LevelMeter::update_meters () max_peak = mpeak; } - peak = _meter->peak_power (n); + peak = _meter->meter_level (n, meter_type); if (n < nmidi) { (*i).meter->set (peak); } else { @@ -280,6 +281,13 @@ LevelMeter::setup_meters (int len, int initial_width, int thin_width) color_changed = false; } +void +LevelMeter::set_type(MeterType t) +{ + meter_type = t; + _meter->set_type(t); +} + bool LevelMeter::meter_button_press (GdkEventButton* ev) { diff --git a/gtk2_ardour/level_meter.h b/gtk2_ardour/level_meter.h index 0792872755..0fa8388428 100644 --- a/gtk2_ardour/level_meter.h +++ b/gtk2_ardour/level_meter.h @@ -67,6 +67,9 @@ class LevelMeter : public Gtk::HBox, public ARDOUR::SessionHandlePtr void hide_meters (); void setup_meters (int len=0, int width=3, int thin=2); + void set_type (ARDOUR::MeterType); + ARDOUR::MeterType get_type () { return meter_type; } + /** Emitted in the GUI thread when a button is pressed over the meter */ PBD::Signal1 ButtonPress; @@ -96,6 +99,7 @@ class LevelMeter : public Gtk::HBox, public ARDOUR::SessionHandlePtr guint16 thin_meter_width; std::vector meters; float max_peak; + ARDOUR::MeterType meter_type; PBD::ScopedConnection _configuration_connection; PBD::ScopedConnection _parameter_connection; diff --git a/gtk2_ardour/meter_strip.cc b/gtk2_ardour/meter_strip.cc index a2872063d4..1b4938694d 100644 --- a/gtk2_ardour/meter_strip.cc +++ b/gtk2_ardour/meter_strip.cc @@ -112,6 +112,7 @@ MeterStrip::MeterStrip (Session* sess, boost::shared_ptr rt) level_meter->set_meter (_route->shared_peak_meter().get()); level_meter->clear_meters(); level_meter->setup_meters (220, meter_width, 6); + level_meter->ButtonPress.connect_same_thread (level_meter_connection, boost::bind (&MeterStrip::level_meter_button_press, this, _1)); meter_align.set(0.5, 0.5, 0.0, 1.0); meter_align.add(*level_meter); @@ -426,3 +427,47 @@ MeterStrip::parameter_changed (std::string const & p) max_peak = -INFINITY; } } + + +bool +MeterStrip::level_meter_button_press (GdkEventButton* ev) +{ + if (ev->button == 3) { + popup_level_meter_menu (ev); + return true; + } + + return false; +} + +void +MeterStrip::popup_level_meter_menu (GdkEventButton* ev) +{ + using namespace Gtk::Menu_Helpers; + + Gtk::Menu* m = manage (new Menu); + MenuList& items = m->items (); + + RadioMenuItem::Group group; + + add_level_meter_item (items, group, _("Peak"), MeterPeak); + add_level_meter_item (items, group, _("RMS"), MeterKrms); + + m->popup (ev->button, ev->time); +} + +void +MeterStrip::add_level_meter_item (Menu_Helpers::MenuList& items, RadioMenuItem::Group& group, string const & name, MeterType type) +{ + using namespace Menu_Helpers; + + items.push_back (RadioMenuElem (group, name, sigc::bind (sigc::mem_fun (*this, &MeterStrip::set_meter_point), type))); + RadioMenuItem* i = dynamic_cast (&items.back ()); + i->set_active (level_meter->get_type() == type); +} + +void +MeterStrip::set_meter_point (MeterType m) +{ + level_meter->set_type (m); +} diff --git a/gtk2_ardour/meter_strip.h b/gtk2_ardour/meter_strip.h index 4589742916..7bbed6370a 100644 --- a/gtk2_ardour/meter_strip.h +++ b/gtk2_ardour/meter_strip.h @@ -66,6 +66,7 @@ class MeterStrip : public Gtk::VBox, public RouteUI protected: boost::shared_ptr _route; PBD::ScopedConnectionList route_connections; + PBD::ScopedConnectionList level_meter_connection; void self_delete (); gint meter_metrics_expose (GdkEventExpose *); @@ -114,6 +115,11 @@ class MeterStrip : public Gtk::VBox, public RouteUI void parameter_changed (std::string const & p); void redraw_metrics (); + + bool level_meter_button_press (GdkEventButton* ev); + void popup_level_meter_menu (GdkEventButton* ev); + void add_level_meter_item (Gtk::Menu_Helpers::MenuList& items, Gtk::RadioMenuItem::Group& group, std::string const & name, ARDOUR::MeterType mode); + void set_meter_point (ARDOUR::MeterType mode); }; #endif /* __ardour_mixer_strip__ */ diff --git a/libs/ardour/ardour/meter.h b/libs/ardour/ardour/meter.h index eea240f821..001178a226 100644 --- a/libs/ardour/ardour/meter.h +++ b/libs/ardour/ardour/meter.h @@ -23,6 +23,7 @@ #include "ardour/types.h" #include "ardour/processor.h" #include "pbd/fastlog.h" +#include "kmeterdsp.h" namespace ARDOUR { @@ -45,6 +46,7 @@ class Metering { class PeakMeter : public Processor { public: PeakMeter(Session& s, const std::string& name); + ~PeakMeter(); void meter(); void reset (); @@ -88,6 +90,11 @@ public: } } + float meter_level (uint32_t n, MeterType type); + + void set_type(MeterType t); + MeterType get_type() { return _meter_type; } + XMLNode& state (bool full); private: @@ -99,11 +106,14 @@ private: */ ChanCount current_meters; - std::vector _peak_power; + std::vector _peak_signal; std::vector _visible_peak_power; + std::vector _max_peak_signal; std::vector _max_peak_power; -}; + std::vector _kmeter; + MeterType _meter_type; +}; } // namespace ARDOUR diff --git a/libs/ardour/ardour/types.h b/libs/ardour/ardour/types.h index 01f8e87c74..05d6d0b27d 100644 --- a/libs/ardour/ardour/types.h +++ b/libs/ardour/ardour/types.h @@ -177,6 +177,13 @@ namespace ARDOUR { MeterCustom }; + enum MeterType { + MeterMaxSignal = 0x01, + MeterMaxPeak = 0x02, + MeterPeak = 0x04, + MeterKrms = 0x08 + }; + enum TrackMode { Normal, NonLayered, diff --git a/libs/ardour/enums.cc b/libs/ardour/enums.cc index 0509d4a2c8..495ff0b4c3 100644 --- a/libs/ardour/enums.cc +++ b/libs/ardour/enums.cc @@ -55,6 +55,7 @@ setup_enum_writer () AlignStyle _AlignStyle; AlignChoice _AlignChoice; MeterPoint _MeterPoint; + MeterType _MeterType; TrackMode _TrackMode; NoteMode _NoteMode; ChannelMode _ChannelMode; @@ -172,6 +173,12 @@ setup_enum_writer () REGISTER_ENUM (MeterCustom); REGISTER (_MeterPoint); + REGISTER_ENUM (MeterMaxSignal); + REGISTER_ENUM (MeterMaxPeak); + REGISTER_ENUM (MeterPeak); + REGISTER_ENUM (MeterKrms); + REGISTER (_MeterType); + REGISTER_ENUM (Normal); REGISTER_ENUM (NonLayered); REGISTER_ENUM (Destructive); diff --git a/libs/ardour/kmeterdsp.cc b/libs/ardour/kmeterdsp.cc new file mode 100644 index 0000000000..77d1a87a81 --- /dev/null +++ b/libs/ardour/kmeterdsp.cc @@ -0,0 +1,116 @@ +/* + Copyright (C) 2008-2011 Fons Adriaensen + Adopted for Ardour 2013 by Robin Gareus + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + + +#include +#include "kmeterdsp.h" + +float Kmeterdsp::_omega; + +Kmeterdsp::Kmeterdsp (void) : + _z1 (0), + _z2 (0), + _rms (0), + _flag (false) +{ +} + + +Kmeterdsp::~Kmeterdsp (void) +{ +} + + +void Kmeterdsp::process (float *p, int n) +{ + // Called by JACK's process callback. + // + // p : pointer to sample buffer + // n : number of samples to process + + float s, t, z1, z2; + + if (_flag) // Display thread has read the rms value. + { + _rms = 0; + _flag = false; + } + + // Get filter state. + z1 = _z1; + z2 = _z2; + + // Process n samples. Find digital peak value for this + // period and perform filtering. The second filter is + // evaluated only every 4th sample - this is just an + // optimisation. + t = 0; + n /= 4; // Loop is unrolled by 4. + while (n--) + { + s = *p++; + s *= s; + if (t < s) t = s; // Update digital peak. + z1 += _omega * (s - z1); // Update first filter. + s = *p++; + s *= s; + if (t < s) t = s; // Update digital peak. + z1 += _omega * (s - z1); // Update first filter. + s = *p++; + s *= s; + if (t < s) t = s; // Update digital peak. + z1 += _omega * (s - z1); // Update first filter. + s = *p++; + s *= s; + if (t < s) t = s; // Update digital peak. + z1 += _omega * (s - z1); // Update first filter. + z2 += 4 * _omega * (z1 - z2); // Update second filter. + } + t = sqrtf (t); + + // Save filter state. The added constants avoid denormals. + _z1 = z1 + 1e-20f; + _z2 = z2 + 1e-20f; + + // Adjust RMS value and update maximum since last read(). + s = sqrtf (2 * z2); + if (s > _rms) _rms = s; +} + + +/* Returns highest _rms value since last call */ +float Kmeterdsp::read () +{ + float rv= _rms; + _flag = true; // Resets _rms in next process(). + return rv; +} + +void Kmeterdsp::init (int fsamp) +{ + _omega = 9.72f / fsamp; // ballistic filter coefficient +} + +void Kmeterdsp::reset () +{ + _z1 = _z2 = _rms = 0.0; + _flag=false; +} + +/* vi:set ts=8 sts=8 sw=8: */ diff --git a/libs/ardour/kmeterdsp.h b/libs/ardour/kmeterdsp.h new file mode 100644 index 0000000000..9c2309e09c --- /dev/null +++ b/libs/ardour/kmeterdsp.h @@ -0,0 +1,45 @@ +/* + Copyright (C) 2008-2011 Fons Adriaensen + Adopted for Ardour 2013 by Robin Gareus + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#ifndef __KMETERDSP_H +#define __KMETERDSP_H + +class Kmeterdsp +{ +public: + + Kmeterdsp (void); + ~Kmeterdsp (void); + + void process (float *p, int n); + float read (); + static void init (int fsamp); + void reset (); + +private: + + float _z1; // filter state + float _z2; // filter state + float _rms; // max rms value since last read() + bool _flag; // flag set by read(), resets _rms + + static float _omega; // ballistics filter constant. +}; + +#endif diff --git a/libs/ardour/meter.cc b/libs/ardour/meter.cc index 80257f37b5..ccb3ff32d0 100644 --- a/libs/ardour/meter.cc +++ b/libs/ardour/meter.cc @@ -26,6 +26,7 @@ #include "ardour/dB.h" #include "ardour/meter.h" #include "ardour/midi_buffer.h" +#include "ardour/session.h" #include "ardour/rc_configuration.h" #include "ardour/runtime_functions.h" @@ -36,8 +37,17 @@ using namespace ARDOUR; PBD::Signal0 Metering::Meter; PeakMeter::PeakMeter (Session& s, const std::string& name) - : Processor (s, string_compose ("meter-%1", name)) + : Processor (s, string_compose ("meter-%1", name)) { + Kmeterdsp::init(s.nominal_frame_rate()); +} + +PeakMeter::~PeakMeter () +{ + while (_kmeter.size() > 0) { + delete (_kmeter.back()); + _kmeter.pop_back(); + } } @@ -79,17 +89,20 @@ PeakMeter::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_fr } } } - _peak_power[n] = max (val, _peak_power[n]); + _peak_signal[n] = max (val, _peak_signal[n]); } // Meter audio in to the rest of the peaks for (uint32_t i = 0; i < n_audio; ++i, ++n) { - _peak_power[n] = compute_peak (bufs.get_audio(i).data(), nframes, _peak_power[n]); + _peak_signal[n] = compute_peak (bufs.get_audio(i).data(), nframes, _peak_signal[n]); + if (_meter_type & MeterKrms) { + _kmeter[i]->process(bufs.get_audio(i).data(), nframes); + } } // Zero any excess peaks - for (uint32_t i = n; i < _peak_power.size(); ++i) { - _peak_power[i] = 0.0f; + for (uint32_t i = n; i < _peak_signal.size(); ++i) { + _peak_signal[i] = 0.0f; } _active = _pending_active; @@ -98,8 +111,8 @@ PeakMeter::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_fr void PeakMeter::reset () { - for (size_t i = 0; i < _peak_power.size(); ++i) { - _peak_power[i] = 0.0f; + for (size_t i = 0; i < _peak_signal.size(); ++i) { + _peak_signal[i] = 0.0f; } } @@ -108,6 +121,7 @@ PeakMeter::reset_max () { for (size_t i = 0; i < _max_peak_power.size(); ++i) { _max_peak_power[i] = -INFINITY; + _max_peak_signal[i] = 0; } } @@ -137,8 +151,9 @@ PeakMeter::reflect_inputs (const ChanCount& in) { current_meters = in; - const size_t limit = min (_peak_power.size(), (size_t) current_meters.n_total ()); - const size_t n_midi = min (_peak_power.size(), (size_t) current_meters.n_midi()); + 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) { @@ -147,6 +162,11 @@ PeakMeter::reflect_inputs (const ChanCount& in) _visible_peak_power[n] = -INFINITY; } } + + for (size_t n = 0; n < n_audio; ++n) { + _kmeter[n]->reset(); + } + reset_max(); ConfigurationChanged (in, in); /* EMIT SIGNAL */ @@ -156,22 +176,36 @@ void PeakMeter::reset_max_channels (const ChanCount& chn) { uint32_t const limit = chn.n_total(); + const size_t n_audio = chn.n_audio(); - while (_peak_power.size() > limit) { - _peak_power.pop_back(); + while (_peak_signal.size() > limit) { + _peak_signal.pop_back(); _visible_peak_power.pop_back(); + _max_peak_signal.pop_back(); _max_peak_power.pop_back(); } - while (_peak_power.size() < limit) { - _peak_power.push_back(0); + while (_peak_signal.size() < limit) { + _peak_signal.push_back(0); _visible_peak_power.push_back(minus_infinity()); + _max_peak_signal.push_back(0); _max_peak_power.push_back(minus_infinity()); } - assert(_peak_power.size() == limit); + assert(_peak_signal.size() == limit); assert(_visible_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()); + _kmeter.pop_back(); + } + while (_kmeter.size() < n_audio) { + _kmeter.push_back(new Kmeterdsp()); + } + assert(_kmeter.size() == n_audio); } /** To be driven by the Meter signal from IO. @@ -186,21 +220,23 @@ PeakMeter::meter () return; } - assert(_visible_peak_power.size() == _peak_power.size()); + assert(_visible_peak_power.size() == _peak_signal.size()); - const size_t limit = min (_peak_power.size(), (size_t) current_meters.n_total ()); - const size_t n_midi = min (_peak_power.size(), (size_t) current_meters.n_midi()); + 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_power[n]; /* XXX we should use atomic exchange from here ... */ - _peak_power[n] = 0; /* ... to here */ + 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 (fast_coefficient_to_dB(new_peak), _max_peak_power[n]); // XXX + _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 */ #if 1 @@ -218,6 +254,8 @@ PeakMeter::meter () /* 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 = fast_coefficient_to_dB (new_peak); } else { @@ -238,6 +276,43 @@ PeakMeter::meter () } } +float +PeakMeter::meter_level(uint32_t n, MeterType type) { + switch (type) { + case MeterKrms: + { + const uint32_t n_midi = current_meters.n_midi(); + if ((n - n_midi) < _kmeter.size()) { + return fast_coefficient_to_dB(_kmeter[n]->read()); + } + return minus_infinity(); + } + case MeterPeak: + return peak_power(n); + case MeterMaxSignal: + if (n < _max_peak_signal.size()) { + return _max_peak_signal[n]; + } else { + return minus_infinity(); + } + default: + case MeterMaxPeak: + return max_peak_power(n); + } +} +void +PeakMeter::set_type(MeterType t) +{ + _meter_type = t; + + if (t & MeterKrms) { + const size_t n_audio = current_meters.n_audio(); + for (size_t n = 0; n < n_audio; ++n) { + _kmeter[n]->reset(); + } + } +} + XMLNode& PeakMeter::state (bool full_state) { diff --git a/libs/ardour/wscript b/libs/ardour/wscript index 5bb924397a..78bf01c062 100644 --- a/libs/ardour/wscript +++ b/libs/ardour/wscript @@ -99,6 +99,7 @@ libardour_sources = [ 'io.cc', 'io_processor.cc', 'jack_slave.cc', + 'kmeterdsp.cc', 'ladspa_plugin.cc', 'location.cc', 'location_importer.cc',