fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / iec1ppmdsp.cc
1 /*
2     Copyright (C) 2012 Fons Adriaensen <fons@linuxaudio.org>
3     Adopted for Ardour 2013 by 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
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include <math.h>
21 #include "ardour/iec1ppmdsp.h"
22
23
24 float Iec1ppmdsp::_w1;
25 float Iec1ppmdsp::_w2;
26 float Iec1ppmdsp::_w3;
27 float Iec1ppmdsp::_g;
28
29
30 Iec1ppmdsp::Iec1ppmdsp (void) :
31     _z1 (0),
32     _z2 (0),
33     _m (0),
34     _res (true)
35 {
36 }
37
38
39 Iec1ppmdsp::~Iec1ppmdsp (void)
40 {
41 }
42
43
44 void Iec1ppmdsp::process (float const *p, int n)
45 {
46     float z1, z2, m, t;
47
48     z1 = _z1 > 20 ? 20 : (_z1 < 0 ? 0 : _z1);
49     z2 = _z2 > 20 ? 20 : (_z2 < 0 ? 0 : _z2);
50     m = _res ? 0: _m;
51     _res = false;
52
53     n /= 4;
54     while (n--)
55     {
56         z1 *= _w3;
57         z2 *= _w3;
58         t = fabsf (*p++);
59         if (t > z1) z1 += _w1 * (t - z1);
60         if (t > z2) z2 += _w2 * (t - z2);
61         t = fabsf (*p++);
62         if (t > z1) z1 += _w1 * (t - z1);
63         if (t > z2) z2 += _w2 * (t - z2);
64         t = fabsf (*p++);
65         if (t > z1) z1 += _w1 * (t - z1);
66         if (t > z2) z2 += _w2 * (t - z2);
67         t = fabsf (*p++);
68         if (t > z1) z1 += _w1 * (t - z1);
69         if (t > z2) z2 += _w2 * (t - z2);
70         t = z1 + z2;
71         if (t > m) m = t;
72     }
73
74     _z1 = z1 + 1e-10f;
75     _z2 = z2 + 1e-10f;
76     _m = m;
77 }
78
79
80 float Iec1ppmdsp::read (void)
81 {
82     _res = true;
83     return _g * _m;
84 }
85
86 void Iec1ppmdsp::reset ()
87 {
88     _z1 = _z2 = _m = .0f;
89     _res = true;
90 }
91
92 void Iec1ppmdsp::init (float fsamp)
93 {
94     _w1 =  450.0f / fsamp;
95     _w2 = 1300.0f / fsamp;
96     _w3 = 1.0f - 5.4f / fsamp;
97     _g  = 0.5108f;
98 }
99
100 /* vi:set ts=8 sts=8 sw=4: */