add new debug bit for backend callbacks
[ardour.git] / libs / ardour / kmeterdsp.cc
1 /*
2  * Copyright (C) 2008-2011 Fons Adriaensen <fons@linuxaudio.org>
3  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
4  *
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.
9  *
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.
14  *
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.
18  */
19
20 #include <math.h>
21 #include "ardour/kmeterdsp.h"
22
23 float  Kmeterdsp::_omega;
24
25 Kmeterdsp::Kmeterdsp (void)
26         : _z1 (0)
27         , _z2 (0)
28         , _rms (0)
29         , _flag (false)
30 {}
31
32 Kmeterdsp::~Kmeterdsp (void) {}
33
34 void
35 Kmeterdsp::init (int fsamp)
36 {
37         _omega = 9.72f / fsamp; // ballistic filter coefficient
38 }
39
40 void
41 Kmeterdsp::process (float const* p, int n)
42 {
43         float  s, z1, z2;
44
45         // Get filter state.
46         z1 = _z1 > 50 ? 50 : (_z1 < 0 ? 0 : _z1);
47         z2 = _z2 > 50 ? 50 : (_z2 < 0 ? 0 : _z2);
48
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.
52         while (n--) {
53                 s = *p++;
54                 s *= s;
55                 z1 += _omega * (s - z1);      // Update first filter.
56                 s = *p++;
57                 s *= s;
58                 z1 += _omega * (s - z1);      // Update first filter.
59                 s = *p++;
60                 s *= s;
61                 z1 += _omega * (s - z1);      // Update first filter.
62                 s = *p++;
63                 s *= s;
64                 z1 += _omega * (s - z1);      // Update first filter.
65                 z2 += 4 * _omega * (z1 - z2); // Update second filter.
66         }
67
68         if (isnan(z1)) z1 = 0;
69         if (isnan(z2)) z2 = 0;
70
71         // Save filter state. The added constants avoid denormals.
72         _z1 = z1 + 1e-20f;
73         _z2 = z2 + 1e-20f;
74
75         s = sqrtf (2.0f * z2);
76
77         if (_flag) {
78                 // Display thread has read the rms value.
79                 _rms  = s;
80                 _flag = false;
81         } else {
82                 // Adjust RMS value and update maximum since last read().
83                 if (s > _rms) _rms = s;
84         }
85 }
86
87 /* Returns highest _rms value since last call */
88 float
89 Kmeterdsp::read ()
90 {
91         float rv= _rms;
92         _flag = true; // Resets _rms in next process().
93         return rv;
94 }
95
96 void
97 Kmeterdsp::reset ()
98 {
99         _z1 = _z2 = _rms = .0f;
100         _flag = false;
101 }