fix a bad transition in the transportFSM.
[ardour.git] / libs / ardour / iec2ppmdsp.cc
1 /*
2  * Copyright (C) 2012 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/iec2ppmdsp.h"
22
23 float Iec2ppmdsp::_w1;
24 float Iec2ppmdsp::_w2;
25 float Iec2ppmdsp::_w3;
26 float Iec2ppmdsp::_g;
27
28 Iec2ppmdsp::Iec2ppmdsp (void)
29         : _z1 (0)
30         , _z2 (0)
31         , _m (0)
32         , _res (true)
33 {}
34
35 Iec2ppmdsp::~Iec2ppmdsp (void) {}
36
37 void
38 Iec2ppmdsp::process (float const* p, int n)
39 {
40         float z1, z2, m, t;
41
42         z1 = _z1 > 20 ? 20 : (_z1 < 0 ? 0 : _z1);
43         z2 = _z2 > 20 ? 20 : (_z2 < 0 ? 0 : _z2);
44         m = _res ? 0: _m;
45         _res = false;
46
47         n /= 4;
48         while (n--) {
49                 z1 *= _w3;
50                 z2 *= _w3;
51                 t = fabsf (*p++);
52                 if (t > z1) z1 += _w1 * (t - z1);
53                 if (t > z2) z2 += _w2 * (t - z2);
54                 t = fabsf (*p++);
55                 if (t > z1) z1 += _w1 * (t - z1);
56                 if (t > z2) z2 += _w2 * (t - z2);
57                 t = fabsf (*p++);
58                 if (t > z1) z1 += _w1 * (t - z1);
59                 if (t > z2) z2 += _w2 * (t - z2);
60                 t = fabsf (*p++);
61                 if (t > z1) z1 += _w1 * (t - z1);
62                 if (t > z2) z2 += _w2 * (t - z2);
63                 t = z1 + z2;
64                 if (t > m) m = t;
65         }
66
67         _z1 = z1 + 1e-10f;
68         _z2 = z2 + 1e-10f;
69         _m = m;
70 }
71
72 float
73 Iec2ppmdsp::read (void)
74 {
75         _res = true;
76         return _g * _m;
77 }
78
79 void
80 Iec2ppmdsp::reset ()
81 {
82         _z1 = _z2 = _m = .0f;
83         _res = true;
84 }
85
86 void
87 Iec2ppmdsp::init (float fsamp)
88 {
89         _w1 = 200.0f / fsamp;
90         _w2 = 860.0f / fsamp;
91         _w3 = 1.0f - 4.0f / fsamp;
92         _g = 0.5141f;
93 }