2 * Copyright (C) 2008-2011 Fons Adriaensen <fons@linuxaudio.org>
3 * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "ardour/kmeterdsp.h"
23 float Kmeterdsp::_omega;
25 Kmeterdsp::Kmeterdsp (void)
32 Kmeterdsp::~Kmeterdsp (void) {}
35 Kmeterdsp::init (int fsamp)
37 _omega = 9.72f / fsamp; // ballistic filter coefficient
41 Kmeterdsp::process (float const* p, int n)
46 z1 = _z1 > 50 ? 50 : (_z1 < 0 ? 0 : _z1);
47 z2 = _z2 > 50 ? 50 : (_z2 < 0 ? 0 : _z2);
49 // Perform filtering. The second filter is evaluated
50 // only every 4th sample - this is just an optimisation.
51 n /= 4; // Loop is unrolled by 4.
55 z1 += _omega * (s - z1); // Update first filter.
58 z1 += _omega * (s - z1); // Update first filter.
61 z1 += _omega * (s - z1); // Update first filter.
64 z1 += _omega * (s - z1); // Update first filter.
65 z2 += 4 * _omega * (z1 - z2); // Update second filter.
68 if (isnan(z1)) z1 = 0;
69 if (isnan(z2)) z2 = 0;
71 // Save filter state. The added constants avoid denormals.
75 s = sqrtf (2.0f * z2);
78 // Display thread has read the rms value.
82 // Adjust RMS value and update maximum since last read().
83 if (s > _rms) _rms = s;
87 /* Returns highest _rms value since last call */
92 _flag = true; // Resets _rms in next process().
99 _z1 = _z2 = _rms = .0f;