From 8641139a1723ad36df0f6a59b364b20b291f0213 Mon Sep 17 00:00:00 2001 From: Krzysztof Foltman Date: Wed, 2 Dec 2009 21:27:48 +0000 Subject: [PATCH] + VU meter: move to separate header to enable reuse, modify falloff behaviour, add clipping detection --- src/calf/Makefile.am | 2 +- src/calf/jackhost.h | 26 +----------------- src/calf/vumeter.h | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 26 deletions(-) create mode 100644 src/calf/vumeter.h diff --git a/src/calf/Makefile.am b/src/calf/Makefile.am index 56963f6..48b0877 100644 --- a/src/calf/Makefile.am +++ b/src/calf/Makefile.am @@ -5,4 +5,4 @@ noinst_HEADERS = audio_fx.h benchmark.h biquad.h buffer.h custom_ctl.h \ lv2_uri_map.h lv2-midiport.h lv2helpers.h lv2wrap.h \ main_win.h metadata.h modmatrix.h modules.h modules_dev.h modules_small.h modules_synths.h modulelist.h \ multichorus.h onepole.h organ.h osc.h osctl.h osctlnet.h osctlserv.h plugininfo.h preset.h \ - preset_gui.h primitives.h synth.h utils.h wave.h waveshaping.h + preset_gui.h primitives.h synth.h utils.h vumeter.h wave.h waveshaping.h diff --git a/src/calf/jackhost.h b/src/calf/jackhost.h index 38b7b02..d6a1816 100644 --- a/src/calf/jackhost.h +++ b/src/calf/jackhost.h @@ -27,6 +27,7 @@ #include #include "gui.h" #include "utils.h" +#include "vumeter.h" #include namespace calf_plugins { @@ -164,31 +165,6 @@ public: } }; -struct vumeter -{ - float level, falloff; - - vumeter() - { - falloff = 0.999f; - level = 0; - } - - inline void update(float *src, unsigned int len) - { - double tmp = level; - for (unsigned int i = 0; i < len; i++) - tmp = std::max(tmp * falloff, (double)fabs(src[i])); - level = tmp; - dsp::sanitize(level); - } - inline void update_zeros(unsigned int len) - { - level *= pow((double)falloff, (double)len); - dsp::sanitize(level); - } -}; - template class jack_host: public jack_host_base, public Module { public: diff --git a/src/calf/vumeter.h b/src/calf/vumeter.h new file mode 100644 index 0000000..de174c1 --- /dev/null +++ b/src/calf/vumeter.h @@ -0,0 +1,74 @@ +/* Calf DSP Library + * Peak metering facilities. + * + * Copyright (C) 2007 Krzysztof Foltman + * + * 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, 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., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301, USA. + */ +#ifndef __CALF_VUMETER_H +#define __CALF_VUMETER_H + +/// Peak meter class +struct vumeter +{ + /// Measured signal level + float level; + /// Falloff of signal level (b1 coefficient of a 1-pole filter) + float falloff; + /// Clip indicator (set to 1 when |value| >= 1, fading otherwise) + float clip; + /// Falloff of clip indicator (b1 coefficient of a 1-pole filter); set to 1 if no falloff is required (manual reset of clip indicator) + float clip_falloff; + + vumeter() + { + falloff = 0.999f; + level = 0; + clip_falloff = 0.999f; + clip = 0; + } + + /// Update peak meter based on input signal + inline void update(float *src, unsigned int len) + { + // "Age" the old level by falloff^length + float tmp = level * pow(falloff, len); + // Same for clip level (using different fade constant) + double tmp_clip = clip * pow(clip_falloff, len); + // Process input samples - to get peak value, take a max of all values in the input signal and "aged" old peak + // Clip is set to 1 if any sample is out-of-range, if no clip occurs, the "aged" value is assumed + for (unsigned int i = 0; i < len; i++) { + float sig = fabs(src[i]); + tmp = std::max(tmp, sig); + if (sig >= 1.f) + tmp_clip = 1.f; + } + level = tmp; + clip = tmp_clip; + dsp::sanitize(level); + dsp::sanitize(clip); + } + /// Update clip meter as if update was called with all-zero input signal + inline void update_zeros(unsigned int len) + { + level *= pow((double)falloff, (double)len); + clip *= pow((double)clip_falloff, (double)len); + dsp::sanitize(level); + dsp::sanitize(clip); + } +}; + +#endif -- 2.11.4.GIT