2 Copyright (C) 2008-2011 Fons Adriaensen <fons@linuxaudio.org>
3 Adopted for Ardour 2013 by 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
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "ardour/kmeterdsp.h"
24 float Kmeterdsp::_omega;
27 Kmeterdsp::Kmeterdsp (void) :
36 Kmeterdsp::~Kmeterdsp (void)
40 void Kmeterdsp::init (int fsamp)
42 _omega = 9.72f / fsamp; // ballistic filter coefficient
45 void Kmeterdsp::process (float *p, int n)
47 // Called by JACK's process callback.
49 // p : pointer to sample buffer
50 // n : number of samples to process
54 if (_flag) // Display thread has read the rms value.
64 // Process n samples. Find digital peak value for this
65 // period and perform filtering. The second filter is
66 // evaluated only every 4th sample - this is just an
69 n /= 4; // Loop is unrolled by 4.
74 if (t < s) t = s; // Update digital peak.
75 z1 += _omega * (s - z1); // Update first filter.
78 if (t < s) t = s; // Update digital peak.
79 z1 += _omega * (s - z1); // Update first filter.
82 if (t < s) t = s; // Update digital peak.
83 z1 += _omega * (s - z1); // Update first filter.
86 if (t < s) t = s; // Update digital peak.
87 z1 += _omega * (s - z1); // Update first filter.
88 z2 += 4 * _omega * (z1 - z2); // Update second filter.
92 // Save filter state. The added constants avoid denormals.
96 // Adjust RMS value and update maximum since last read().
98 if (s > _rms) _rms = s;
101 /* Returns highest _rms value since last call */
102 float Kmeterdsp::read ()
105 _flag = true; // Resets _rms in next process().
109 void Kmeterdsp::reset ()
111 _z1 = _z2 = _rms = .0f;
115 /* vi:set ts=8 sts=8 sw=4: */