From e3a6fab05ecfc22ea9c2c98b2858567998cdc6db Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Sat, 3 Apr 2021 23:42:09 +0200 Subject: [PATCH] EBU/VAMP: clang-format and reduce scope --- libs/vamp-plugins/EBUr128.cpp | 200 +++++----- libs/vamp-plugins/EBUr128.h | 52 +-- libs/vamp-plugins/ebu_r128_proc.cc | 593 +++++++++++++++-------------- libs/vamp-plugins/ebu_r128_proc.h | 188 +++++---- 4 files changed, 525 insertions(+), 508 deletions(-) diff --git a/libs/vamp-plugins/EBUr128.cpp b/libs/vamp-plugins/EBUr128.cpp index 269ee356a2..a6df378c4c 100644 --- a/libs/vamp-plugins/EBUr128.cpp +++ b/libs/vamp-plugins/EBUr128.cpp @@ -1,5 +1,3 @@ -/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ - /* Vamp @@ -31,170 +29,170 @@ #include "EBUr128.h" -using std::string; -using std::vector; using std::cerr; using std::endl; +using std::string; +using std::vector; - -VampEBUr128::VampEBUr128(float inputSampleRate) - : Plugin(inputSampleRate) - , m_stepSize(0) +VampEBUr128::VampEBUr128 (float inputSampleRate) + : Plugin (inputSampleRate) + , m_stepSize (0) { } -VampEBUr128::~VampEBUr128() +VampEBUr128::~VampEBUr128 () { } string -VampEBUr128::getIdentifier() const +VampEBUr128::getIdentifier () const { - return "ebur128"; + return "ebur128"; } string -VampEBUr128::getName() const +VampEBUr128::getName () const { - return "EBU R128 Loudness"; + return "EBU R128 Loudness"; } string -VampEBUr128::getDescription() const +VampEBUr128::getDescription () const { - return "Loudness measurements according to the EBU Recommendation 128"; + return "Loudness measurements according to the EBU Recommendation 128"; } string -VampEBUr128::getMaker() const +VampEBUr128::getMaker () const { - return "Harrison Consoles"; + return "Harrison Consoles"; } int -VampEBUr128::getPluginVersion() const +VampEBUr128::getPluginVersion () const { - return 2; + return 2; } string -VampEBUr128::getCopyright() const +VampEBUr128::getCopyright () const { - return "GPL version 2 or later"; + return "GPL version 2 or later"; } bool -VampEBUr128::initialise(size_t channels, size_t stepSize, size_t blockSize) +VampEBUr128::initialise (size_t channels, size_t stepSize, size_t blockSize) { - if (channels < getMinChannelCount() || - channels > getMaxChannelCount()) return false; + if (channels < getMinChannelCount () || channels > getMaxChannelCount ()) { + return false; + } - m_stepSize = std::min(stepSize, blockSize); - m_channels = channels; + m_stepSize = std::min (stepSize, blockSize); + m_channels = channels; - ebu.init (m_channels, m_inputSampleRate); + ebu.init (m_channels, m_inputSampleRate); - return true; + return true; } void -VampEBUr128::reset() +VampEBUr128::reset () { - ebu.reset (); + ebu.reset (); } VampEBUr128::OutputList -VampEBUr128::getOutputDescriptors() const +VampEBUr128::getOutputDescriptors () const { - OutputList list; + OutputList list; - OutputDescriptor zc; - zc.identifier = "loundless"; - zc.name = "Integrated loudness"; - zc.description = "Loudness (integrated, short, momentary)"; - zc.unit = "LUFS"; - zc.hasFixedBinCount = true; - zc.binCount = 0; - zc.hasKnownExtents = false; - zc.isQuantized = false; - zc.sampleType = OutputDescriptor::OneSamplePerStep; - list.push_back(zc); + OutputDescriptor zc; + zc.identifier = "loundless"; + zc.name = "Integrated loudness"; + zc.description = "Loudness (integrated, short, momentary)"; + zc.unit = "LUFS"; + zc.hasFixedBinCount = true; + zc.binCount = 0; + zc.hasKnownExtents = false; + zc.isQuantized = false; + zc.sampleType = OutputDescriptor::OneSamplePerStep; + list.push_back (zc); - zc.identifier = "range"; - zc.name = "Integrated Loudness Range"; - zc.description = "Dynamic Range of the Audio"; - zc.unit = "LU"; - zc.hasFixedBinCount = true; - zc.binCount = 0; - zc.hasKnownExtents = false; - zc.isQuantized = false; - zc.sampleType = OutputDescriptor::OneSamplePerStep; - list.push_back(zc); + zc.identifier = "range"; + zc.name = "Integrated Loudness Range"; + zc.description = "Dynamic Range of the Audio"; + zc.unit = "LU"; + zc.hasFixedBinCount = true; + zc.binCount = 0; + zc.hasKnownExtents = false; + zc.isQuantized = false; + zc.sampleType = OutputDescriptor::OneSamplePerStep; + list.push_back (zc); - zc.identifier = "histogram"; - zc.name = "Loudness Histogram"; - zc.description = "Dynamic Range of the audio"; - zc.unit = ""; - zc.hasFixedBinCount = false; - zc.binCount = 0; - zc.hasKnownExtents = false; - zc.isQuantized = false; - zc.sampleType = OutputDescriptor::OneSamplePerStep; - list.push_back(zc); + zc.identifier = "histogram"; + zc.name = "Loudness Histogram"; + zc.description = "Dynamic Range of the audio"; + zc.unit = ""; + zc.hasFixedBinCount = false; + zc.binCount = 0; + zc.hasKnownExtents = false; + zc.isQuantized = false; + zc.sampleType = OutputDescriptor::OneSamplePerStep; + list.push_back (zc); - return list; + return list; } VampEBUr128::FeatureSet -VampEBUr128::process(const float *const *inputBuffers, - Vamp::RealTime timestamp) +VampEBUr128::process (const float* const* inputBuffers, + Vamp::RealTime timestamp) { - if (m_stepSize == 0) { - cerr << "ERROR: VampEBUr128::process: " - << "VampEBUr128 has not been initialised" - << endl; - return FeatureSet(); - } + if (m_stepSize == 0) { + cerr << "ERROR: VampEBUr128::process: " + << "VampEBUr128 has not been initialised" + << endl; + return FeatureSet (); + } - ebu.integr_start (); // noop if already started - ebu.process (m_stepSize, inputBuffers); + ebu.integr_start (); // noop if already started + ebu.process (m_stepSize, inputBuffers); - return FeatureSet(); + return FeatureSet (); } VampEBUr128::FeatureSet -VampEBUr128::getRemainingFeatures() +VampEBUr128::getRemainingFeatures () { - FeatureSet returnFeatures; + FeatureSet returnFeatures; - Feature loudness_integrated; - loudness_integrated.hasTimestamp = false; - loudness_integrated.values.push_back(ebu.integrated()); + Feature loudness_integrated; + loudness_integrated.hasTimestamp = false; + loudness_integrated.values.push_back (ebu.integrated ()); - Feature loudness_short; - loudness_short.hasTimestamp = false; - loudness_short.values.push_back(ebu.maxloudn_S()); + Feature loudness_short; + loudness_short.hasTimestamp = false; + loudness_short.values.push_back (ebu.maxloudn_S ()); - Feature loudness_momentary; - loudness_momentary.hasTimestamp = false; - loudness_momentary.values.push_back(ebu.maxloudn_M()); + Feature loudness_momentary; + loudness_momentary.hasTimestamp = false; + loudness_momentary.values.push_back (ebu.maxloudn_M ()); - returnFeatures[0].push_back(loudness_integrated); - returnFeatures[0].push_back(loudness_short); - returnFeatures[0].push_back(loudness_momentary); + returnFeatures[0].push_back (loudness_integrated); + returnFeatures[0].push_back (loudness_short); + returnFeatures[0].push_back (loudness_momentary); - Feature range; - range.hasTimestamp = false; - range.values.push_back(ebu.range_max () - ebu.range_min ()); - returnFeatures[1].push_back(range); + Feature range; + range.hasTimestamp = false; + range.values.push_back (ebu.range_max () - ebu.range_min ()); + returnFeatures[1].push_back (range); - Feature hist; - hist.hasTimestamp = false; - const int * hist_S = ebu.histogram_S(); - for (int i = 110; i < 650; ++i) { - hist.values.push_back(hist_S[i]); - } - returnFeatures[2].push_back(hist); + Feature hist; + hist.hasTimestamp = false; + const int* hist_S = ebu.histogram_S (); + for (int i = 110; i < 650; ++i) { + hist.values.push_back (hist_S[i]); + } + returnFeatures[2].push_back (hist); - return returnFeatures; + return returnFeatures; } diff --git a/libs/vamp-plugins/EBUr128.h b/libs/vamp-plugins/EBUr128.h index 026aa400db..23ab50c4b2 100644 --- a/libs/vamp-plugins/EBUr128.h +++ b/libs/vamp-plugins/EBUr128.h @@ -1,5 +1,3 @@ -/* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ - /* Vamp @@ -32,42 +30,50 @@ #define _EBUR128_PLUGIN_H_ #include + #include "ebu_r128_proc.h" class VampEBUr128 : public Vamp::Plugin { public: - VampEBUr128(float inputSampleRate); - virtual ~VampEBUr128(); + VampEBUr128 (float inputSampleRate); + virtual ~VampEBUr128 (); - size_t getMinChannelCount() const { return 1; } - size_t getMaxChannelCount() const { return 2; } - bool initialise(size_t channels, size_t stepSize, size_t blockSize); - void reset(); + size_t getMinChannelCount () const + { + return 1; + } + size_t getMaxChannelCount () const + { + return 2; + } + bool initialise (size_t channels, size_t stepSize, size_t blockSize); + void reset (); - InputDomain getInputDomain() const { return TimeDomain; } + InputDomain getInputDomain () const + { + return TimeDomain; + } - std::string getIdentifier() const; - std::string getName() const; - std::string getDescription() const; - std::string getMaker() const; - int getPluginVersion() const; - std::string getCopyright() const; + std::string getIdentifier () const; + std::string getName () const; + std::string getDescription () const; + std::string getMaker () const; + int getPluginVersion () const; + std::string getCopyright () const; - OutputList getOutputDescriptors() const; + OutputList getOutputDescriptors () const; - FeatureSet process(const float *const *inputBuffers, - Vamp::RealTime timestamp); + FeatureSet process (const float* const* inputBuffers, Vamp::RealTime timestamp); - FeatureSet getRemainingFeatures(); + FeatureSet getRemainingFeatures (); protected: - size_t m_stepSize; - size_t m_channels; + size_t m_stepSize; + size_t m_channels; private: - Fons::Ebu_r128_proc ebu; + FonsEBU::Ebu_r128_proc ebu; }; - #endif diff --git a/libs/vamp-plugins/ebu_r128_proc.cc b/libs/vamp-plugins/ebu_r128_proc.cc index b18b9bc30e..0c6678682a 100644 --- a/libs/vamp-plugins/ebu_r128_proc.cc +++ b/libs/vamp-plugins/ebu_r128_proc.cc @@ -2,7 +2,7 @@ // // Copyright (C) 2010-2011 Fons Adriaensen // Copyright (C) 2015 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 @@ -19,10 +19,9 @@ // // ------------------------------------------------------------------------ - -#include -#include #include "ebu_r128_proc.h" +#include +#include #ifdef COMPILER_MSVC #include @@ -32,317 +31,333 @@ #define isfinite_local isfinite #endif -namespace Fons { +using namespace FonsEBU; -float Ebu_r128_hist::_bin_power [100] = { 0.0f }; -float Ebu_r128_proc::_chan_gain [5] = { 1.0f, 1.0f, 1.0f, 1.41f, 1.41f }; +float Ebu_r128_proc::Ebu_r128_hist::_bin_power[100] = { 0.0f }; +float Ebu_r128_proc::_chan_gain[5] = { 1.0f, 1.0f, 1.0f, 1.41f, 1.41f }; - -Ebu_r128_hist::Ebu_r128_hist (void) +Ebu_r128_proc::Ebu_r128_hist::Ebu_r128_hist () { - _histc = new int [751]; - initstat (); - reset (); + _histc = new int[751]; + initstat (); + reset (); } - -Ebu_r128_hist::~Ebu_r128_hist (void) +Ebu_r128_proc::Ebu_r128_hist::~Ebu_r128_hist () { - delete[] _histc; + delete[] _histc; } - -void Ebu_r128_hist::reset (void) +void +Ebu_r128_proc::Ebu_r128_hist::reset () { - memset (_histc, 0, 751 * sizeof (float)); - _count = 0; - _error = 0; + memset (_histc, 0, 751 * sizeof (float)); + _count = 0; + _error = 0; } - -void Ebu_r128_hist::initstat (void) +void +Ebu_r128_proc::Ebu_r128_hist::initstat () { - int i; - - if (_bin_power [0]) return; - for (i = 0; i < 100; i++) - { - _bin_power [i] = powf (10.0f, i / 100.0f); - } -} - - -void Ebu_r128_hist::addpoint (float v) -{ - int k; - - k = (int) floorf (10 * v + 700.5f); - if (k < 0) return; - if (k > 750) - { - k = 750; - _error++; - } - _histc [k]++; - _count++; -} - - -float Ebu_r128_hist::integrate (int i) -{ - int j, k, n; - float s; - - j = i % 100; - n = 0; - s = 0; - while (i <= 750) - { - k = _histc [i++]; - n += k; - s += k * _bin_power [j++]; - if (j == 100) - { - j = 0; - s /= 10.0f; + if (_bin_power[0]) + return; + for (int i = 0; i < 100; i++) { + _bin_power[i] = powf (10.0f, i / 100.0f); } - } - return s / n; } - -void Ebu_r128_hist::calc_integ (float *vi, float *th) +void +Ebu_r128_proc::Ebu_r128_hist::addpoint (float v) { - int k; - float s; - - if (_count < 50) - { - *vi = -200.0f; - return; - } - s = integrate (0); -// Original threshold was -8 dB below result of first integration -// if (th) *th = 10 * log10f (s) - 8.0f; -// k = (int)(floorf (100 * log10f (s) + 0.5f)) + 620; -// Threshold redefined to -10 dB below result of first integration - if (th) *th = 10 * log10f (s) - 10.0f; - k = (int)(floorf (100 * log10f (s) + 0.5f)) + 600; - if (k < 0) k = 0; - s = integrate (k); - *vi = 10 * log10f (s); + int k = (int)floorf (10 * v + 700.5f); + if (k < 0) + return; + if (k > 750) { + k = 750; + _error++; + } + _histc[k]++; + _count++; } - -void Ebu_r128_hist::calc_range (float *v0, float *v1, float *th) +float +Ebu_r128_proc::Ebu_r128_hist::integrate (int i) { - int i, j, k, n; - float a, b, s; + int j, k, n; + float s; - if (_count < 20) - { - *v0 = -200.0f; - *v1 = -200.0f; - return; - } - s = integrate (0); - if (th) *th = 10 * log10f (s) - 20.0f; - k = (int)(floorf (100 * log10f (s) + 0.5)) + 500; - if (k < 0) k = 0; - for (i = k, n = 0; i <= 750; i++) n += _histc [i]; - a = 0.10f * n; - b = 0.95f * n; - for (i = k, s = 0; s < a; i++) s += _histc [i]; - for (j = 750, s = n; s > b; j--) s -= _histc [j]; - *v0 = (i - 701) / 10.0f; - *v1 = (j - 699) / 10.0f; -} - - - - -Ebu_r128_proc::Ebu_r128_proc (void) -{ - reset (); -} - - -Ebu_r128_proc::~Ebu_r128_proc (void) -{ -} - - -void Ebu_r128_proc::init (int nchan, float fsamp) -{ - _nchan = nchan; - _fsamp = fsamp; - _fragm = (int) fsamp / 20; - detect_init (_fsamp); - reset (); -} - - -void Ebu_r128_proc::reset (void) -{ - _integr = false; - _frcnt = _fragm; - _frpwr = 1e-30f; - _wrind = 0; - _div1 = 0; - _div2 = 0; - _loudness_M = -200.0f; - _loudness_S = -200.0f; - memset (_power, 0, 64 * sizeof (float)); - integr_reset (); - detect_reset (); -} - - -void Ebu_r128_proc::integr_reset (void) -{ - _hist_M.reset (); - _hist_S.reset (); - _maxloudn_M = -200.0f; - _maxloudn_S = -200.0f; - _integrated = -200.0f; - _integ_thr = -200.0f; - _range_min = -200.0f; - _range_max = -200.0f; - _range_thr = -200.0f; - _div1 = _div2 = 0; -} - - -void Ebu_r128_proc::process (int nfram, const float *const *input) -{ - int i, k; - - for (i = 0; i < _nchan; i++) _ipp [i] = input [i]; - while (nfram) - { - k = (_frcnt < nfram) ? _frcnt : nfram; - _frpwr += detect_process (k); - _frcnt -= k; - if (_frcnt == 0) - { - _power [_wrind++] = _frpwr / _fragm; - _frcnt = _fragm; - _frpwr = 1e-30f; - _wrind &= 63; - _loudness_M = addfrags (8); - _loudness_S = addfrags (60); - if (!isfinite_local(_loudness_M) || _loudness_M < -200.f) _loudness_M = -200.0f; - if (!isfinite_local(_loudness_S) || _loudness_S < -200.f) _loudness_S = -200.0f; - if (_loudness_M > _maxloudn_M) _maxloudn_M = _loudness_M; - if (_loudness_S > _maxloudn_S) _maxloudn_S = _loudness_S; - if (_integr) - { - if (++_div1 == 2) - { - _hist_M.addpoint (_loudness_M); - _div1 = 0; - } - if (++_div2 == 10) - { - _hist_S.addpoint (_loudness_S); - _div2 = 0; - _hist_M.calc_integ (&_integrated, &_integ_thr); - _hist_S.calc_range (&_range_min, &_range_max, &_range_thr); + j = i % 100; + n = 0; + s = 0; + while (i <= 750) { + k = _histc[i++]; + n += k; + s += k * _bin_power[j++]; + if (j == 100) { + j = 0; + s /= 10.0f; } - } } - for (i = 0; i < _nchan; i++) _ipp [i] += k; - nfram -= k; - } + return s / n; } - -float Ebu_r128_proc::addfrags (int nfrag) +void +Ebu_r128_proc::Ebu_r128_hist::calc_integ (float* vi, float* th) { - int i, k; - float s; + int k; + float s; - s = 0; - k = (_wrind - nfrag) & 63; - for (i = 0; i < nfrag; i++) s += _power [(i + k) & 63]; - return -0.6976f + 10 * log10f (s / nfrag); -} - - -void Ebu_r128_proc::detect_init (float fsamp) -{ - float a, b, c, d, r, u1, u2, w1, w2; - - r = 1 / tan (4712.3890f / fsamp); - w1 = r / 1.12201f; - w2 = r * 1.12201f; - u1 = u2 = 1.4085f + 210.0f / fsamp; - a = u1 * w1; - b = w1 * w1; - c = u2 * w2; - d = w2 * w2; - r = 1 + a + b; - _a0 = (1 + c + d) / r; - _a1 = (2 - 2 * d) / r; - _a2 = (1 - c + d) / r; - _b1 = (2 - 2 * b) / r; - _b2 = (1 - a + b) / r; - r = 48.0f / fsamp; - a = 4.9886075f * r; - b = 6.2298014f * r * r; - r = 1 + a + b; - a *= 2 / r; - b *= 4 / r; - _c3 = a + b; - _c4 = b; - r = 1.004995f / r; - _a0 *= r; - _a1 *= r; - _a2 *= r; -} - - -void Ebu_r128_proc::detect_reset (void) -{ - for (int i = 0; i < MAXCH; i++) _fst [i].reset (); -} - - -float Ebu_r128_proc::detect_process (int nfram) -{ - int i, j; - float si, sj; - float x, y, z1, z2, z3, z4; - float const *p; - Ebu_r128_fst *S; - - si = 0; - for (i = 0, S = _fst; i < _nchan; i++, S++) - { - z1 = S->_z1; - z2 = S->_z2; - z3 = S->_z3; - z4 = S->_z4; - p = _ipp [i]; - sj = 0; - for (j = 0; j < nfram; j++) - { - x = p [j] - _b1 * z1 - _b2 * z2 + 1e-15f; - y = _a0 * x + _a1 * z1 + _a2 * z2 - _c3 * z3 - _c4 * z4; - z2 = z1; - z1 = x; - z4 += z3; - z3 += y; - sj += y * y; + if (_count < 50) { + *vi = -200.0f; + return; } - if (_nchan == 1) si = 2 * sj; - else si += _chan_gain [i] * sj; - S->_z1 = !isfinite_local(z1) ? 0 : z1; - S->_z2 = !isfinite_local(z2) ? 0 : z2; - S->_z3 = !isfinite_local(z3) ? 0 : z3; - S->_z4 = !isfinite_local(z4) ? 0 : z4; - } - return si; + s = integrate (0); + // Original threshold was -8 dB below result of first integration + // if (th) *th = 10 * log10f (s) - 8.0f; + // k = (int)(floorf (100 * log10f (s) + 0.5f)) + 620; + // Threshold redefined to -10 dB below result of first integration + if (th) { + *th = 10 * log10f (s) - 10.0f; + } + k = (int)(floorf (100 * log10f (s) + 0.5f)) + 600; + if (k < 0) { + k = 0; + } + s = integrate (k); + *vi = 10 * log10f (s); } -}; +void +Ebu_r128_proc::Ebu_r128_hist::calc_range (float* v0, float* v1, float* th) +{ + int i, j, k, n; + float a, b, s; + + if (_count < 20) { + *v0 = -200.0f; + *v1 = -200.0f; + return; + } + s = integrate (0); + if (th) { + *th = 10 * log10f (s) - 20.0f; + } + k = (int)(floorf (100 * log10f (s) + 0.5)) + 500; + if (k < 0) { + k = 0; + } + for (i = k, n = 0; i <= 750; i++) { + n += _histc[i]; + } + a = 0.10f * n; + b = 0.95f * n; + for (i = k, s = 0; s < a; i++) { + s += _histc[i]; + } + for (j = 750, s = n; s > b; j--) { + s -= _histc[j]; + } + *v0 = (i - 701) / 10.0f; + *v1 = (j - 699) / 10.0f; +} + +Ebu_r128_proc::Ebu_r128_proc () +{ + reset (); +} + +Ebu_r128_proc::~Ebu_r128_proc () +{ +} + +void +Ebu_r128_proc::init (int nchan, float fsamp) +{ + _nchan = nchan; + _fsamp = fsamp; + _fragm = (int)fsamp / 20; + detect_init (_fsamp); + reset (); +} + +void +Ebu_r128_proc::reset () +{ + _integr = false; + _frcnt = _fragm; + _frpwr = 1e-30f; + _wrind = 0; + _div1 = 0; + _div2 = 0; + _loudness_M = -200.0f; + _loudness_S = -200.0f; + memset (_power, 0, 64 * sizeof (float)); + integr_reset (); + detect_reset (); +} + +void +Ebu_r128_proc::integr_reset () +{ + _hist_M.reset (); + _hist_S.reset (); + _maxloudn_M = -200.0f; + _maxloudn_S = -200.0f; + _integrated = -200.0f; + _integ_thr = -200.0f; + _range_min = -200.0f; + _range_max = -200.0f; + _range_thr = -200.0f; + _div1 = _div2 = 0; +} + +void +Ebu_r128_proc::process (int nfram, const float* const* input) +{ + int i, k; + + for (i = 0; i < _nchan; i++) { + _ipp[i] = input[i]; + } + while (nfram) { + k = (_frcnt < nfram) ? _frcnt : nfram; + _frpwr += detect_process (k); + _frcnt -= k; + if (_frcnt == 0) { + _power[_wrind++] = _frpwr / _fragm; + _frcnt = _fragm; + _frpwr = 1e-30f; + _wrind &= 63; + _loudness_M = addfrags (8); + _loudness_S = addfrags (60); + + if (!isfinite_local (_loudness_M) || _loudness_M < -200.f) { + _loudness_M = -200.0f; + } + if (!isfinite_local (_loudness_S) || _loudness_S < -200.f) { + _loudness_S = -200.0f; + } + if (_loudness_M > _maxloudn_M) { + _maxloudn_M = _loudness_M; + } + if (_loudness_S > _maxloudn_S) { + _maxloudn_S = _loudness_S; + } + + if (_integr) { + if (++_div1 == 2) { + _hist_M.addpoint (_loudness_M); + _div1 = 0; + } + if (++_div2 == 10) { + _hist_S.addpoint (_loudness_S); + _div2 = 0; + _hist_M.calc_integ (&_integrated, &_integ_thr); + _hist_S.calc_range (&_range_min, &_range_max, &_range_thr); + } + } + } + + for (i = 0; i < _nchan; i++) { + _ipp[i] += k; + } + nfram -= k; + } +} + +float +Ebu_r128_proc::addfrags (int nfrag) +{ + int i, k; + float s; + + s = 0; + k = (_wrind - nfrag) & 63; + for (i = 0; i < nfrag; i++) { + s += _power[(i + k) & 63]; + } + return -0.6976f + 10 * log10f (s / nfrag); +} + +void +Ebu_r128_proc::detect_init (float fsamp) +{ + float a, b, c, d, r, u1, u2, w1, w2; + + r = 1 / tan (4712.3890f / fsamp); + w1 = r / 1.12201f; + w2 = r * 1.12201f; + u1 = u2 = 1.4085f + 210.0f / fsamp; + + a = u1 * w1; + b = w1 * w1; + c = u2 * w2; + d = w2 * w2; + + r = 1 + a + b; + _a0 = (1 + c + d) / r; + _a1 = (2 - 2 * d) / r; + _a2 = (1 - c + d) / r; + _b1 = (2 - 2 * b) / r; + _b2 = (1 - a + b) / r; + + r = 48.0f / fsamp; + a = 4.9886075f * r; + b = 6.2298014f * r * r; + r = 1 + a + b; + a *= 2 / r; + b *= 4 / r; + _c3 = a + b; + _c4 = b; + + r = 1.004995f / r; + _a0 *= r; + _a1 *= r; + _a2 *= r; +} + +void +Ebu_r128_proc::detect_reset () +{ + for (int i = 0; i < MAXCH; i++) + _fst[i].reset (); +} + +float +Ebu_r128_proc::detect_process (int nfram) +{ + int i, j; + float si, sj; + float x, y, z1, z2, z3, z4; + float const* p; + Ebu_r128_fst* S; + + si = 0; + for (i = 0, S = _fst; i < _nchan; i++, S++) { + z1 = S->_z1; + z2 = S->_z2; + z3 = S->_z3; + z4 = S->_z4; + p = _ipp[i]; + sj = 0; + for (j = 0; j < nfram; j++) { + x = p[j] - _b1 * z1 - _b2 * z2 + 1e-15f; + y = _a0 * x + _a1 * z1 + _a2 * z2 - _c3 * z3 - _c4 * z4; + z2 = z1; + z1 = x; + z4 += z3; + z3 += y; + sj += y * y; + } + if (_nchan == 1) { + si = 2 * sj; + } else { + si += _chan_gain[i] * sj; + } + S->_z1 = !isfinite_local (z1) ? 0 : z1; + S->_z2 = !isfinite_local (z2) ? 0 : z2; + S->_z3 = !isfinite_local (z3) ? 0 : z3; + S->_z4 = !isfinite_local (z4) ? 0 : z4; + } + return si; +} diff --git a/libs/vamp-plugins/ebu_r128_proc.h b/libs/vamp-plugins/ebu_r128_proc.h index 126be8b0f2..87a2f33820 100644 --- a/libs/vamp-plugins/ebu_r128_proc.h +++ b/libs/vamp-plugins/ebu_r128_proc.h @@ -1,7 +1,7 @@ // ------------------------------------------------------------------------ // // Copyright (C) 2010-2011 Fons Adriaensen -// +// // 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 @@ -18,118 +18,116 @@ // // ------------------------------------------------------------------------ - #ifndef _EBU_R128_PROC_H #define _EBU_R128_PROC_H #define MAXCH 5 -namespace Fons { - -class Ebu_r128_fst -{ -private: - - friend class Ebu_r128_proc; - - void reset (void) { _z1 = _z2 = _z3 = _z4 = 0; } - - float _z1, _z2, _z3, _z4; -}; - - -class Ebu_r128_hist -{ -private: - - Ebu_r128_hist (void); - ~Ebu_r128_hist (void); - - friend class Ebu_r128_proc; - - void reset (void); - void initstat (void); - void addpoint (float v); - float integrate (int ind); - void calc_integ (float *vi, float *th); - void calc_range (float *v0, float *v1, float *th); - - int *_histc; - int _count; - int _error; - - static float _bin_power [100]; -}; - - +namespace FonsEBU { class Ebu_r128_proc { public: + Ebu_r128_proc (); + ~Ebu_r128_proc (); - Ebu_r128_proc (void); - ~Ebu_r128_proc (void); + void init (int nchan, float fsamp); + void reset (); + void process (int nfram, const float* const* input); - void init (int nchan, float fsamp); - void reset (void); - void process (int nfram, const float *const *input); - void integr_reset (void); - void integr_pause (void) { _integr = false; } - void integr_start (void) { _integr = true; } + void integr_reset (); + void integr_pause () { _integr = false; } + void integr_start () { _integr = true; } - float loudness_M (void) const { return _loudness_M; } - float maxloudn_M (void) const { return _maxloudn_M; } - float loudness_S (void) const { return _loudness_S; } - float maxloudn_S (void) const { return _maxloudn_S; } - float integrated (void) const { return _integrated; } - float integ_thr (void) const { return _integ_thr; } - float range_min (void) const { return _range_min; } - float range_max (void) const { return _range_max; } - float range_thr (void) const { return _range_thr; } + float loudness_M () const { return _loudness_M; } + float maxloudn_M () const { return _maxloudn_M; } + float loudness_S () const { return _loudness_S; } + float maxloudn_S () const { return _maxloudn_S; } + float integrated () const { return _integrated; } - const int *histogram_M (void) const { return _hist_M._histc; } - const int *histogram_S (void) const { return _hist_S._histc; } - int hist_M_count (void) const { return _hist_M._count; } - int hist_S_count (void) const { return _hist_S._count; } + float integ_thr () const { return _integ_thr; } + float range_min () const { return _range_min; } + float range_max () const { return _range_max; } + float range_thr () const { return _range_thr; } + + const int* histogram_M () const { return _hist_M._histc; } + const int* histogram_S () const { return _hist_S._histc; } + + int hist_M_count () const { return _hist_M._count; } + int hist_S_count () const { return _hist_S._count; } private: + class Ebu_r128_fst + { + private: + friend class Ebu_r128_proc; - float addfrags (int nfrag); - void detect_init (float fsamp); - void detect_reset (void); - float detect_process (int nfram); + void reset () + { + _z1 = _z2 = _z3 = _z4 = 0; + } - bool _integr; // Integration on/off. - int _nchan; // Number of channels, 2 or 5. - float _fsamp; // Sample rate. - int _fragm; // Fragmenst size, 1/20 second. - int _frcnt; // Number of samples remaining in current fragment. - float _frpwr; // Power accumulated for current fragment. - float _power [64]; // Array of fragment powers. - int _wrind; // Write index into _frpwr - int _div1; // M period counter, 200 ms; - int _div2; // S period counter, 1s; - float _loudness_M; - float _maxloudn_M; - float _loudness_S; - float _maxloudn_S; - float _integrated; - float _integ_thr; - float _range_min; - float _range_max; - float _range_thr; - - // Filter coefficients and states. - float _a0, _a1, _a2; - float _b1, _b2; - float _c3, _c4; - float const *_ipp [MAXCH]; - Ebu_r128_fst _fst [MAXCH]; - Ebu_r128_hist _hist_M; - Ebu_r128_hist _hist_S; + float _z1, _z2, _z3, _z4; + }; - // Default channel gains. - static float _chan_gain [5]; + class Ebu_r128_hist + { + private: + Ebu_r128_hist (); + ~Ebu_r128_hist (); + + friend class Ebu_r128_proc; + + void reset (); + void initstat (); + void addpoint (float v); + float integrate (int ind); + void calc_integ (float* vi, float* th); + void calc_range (float* v0, float* v1, float* th); + + int* _histc; + int _count; + int _error; + + static float _bin_power[100]; + }; + + float addfrags (int nfrag); + void detect_init (float fsamp); + void detect_reset (); + float detect_process (int nfram); + + bool _integr; // Integration on/off. + int _nchan; // Number of channels, 2 or 5. + float _fsamp; // Sample rate. + int _fragm; // Fragmenst size, 1/20 second. + int _frcnt; // Number of samples remaining in current fragment. + float _frpwr; // Power accumulated for current fragment. + float _power[64]; // Array of fragment powers. + int _wrind; // Write index into _frpwr + int _div1; // M period counter, 200 ms; + int _div2; // S period counter, 1s; + float _loudness_M; + float _maxloudn_M; + float _loudness_S; + float _maxloudn_S; + float _integrated; + float _integ_thr; + float _range_min; + float _range_max; + float _range_thr; + + // Filter coefficients and states. + float _a0, _a1, _a2; + float _b1, _b2; + float _c3, _c4; + float const* _ipp[MAXCH]; + Ebu_r128_fst _fst[MAXCH]; + Ebu_r128_hist _hist_M; + Ebu_r128_hist _hist_S; + + // Default channel gains. + static float _chan_gain[5]; }; };