1 /**
2  *  Simple Limiter
3  *
4  *  Copyright (C) 2006-2018 Teru Kamogashira
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20 
_FV3_(slimit)21 class _FV3_(slimit)
22 {
23  public:
24   _FV3_(slimit)();
25   _FV3_(~slimit)();
26   void setRMS(long value);
27   long getRMS();
28   void setLookahead(long value)
29     throw(std::bad_alloc);
30   long getLookahead();
31   void setLookaheadRatio(_fv3_float_t value);
32   _fv3_float_t getLookaheadRatio();
33   void setAttack(_fv3_float_t value);
34   _fv3_float_t getAttack();
35   void setRelease(_fv3_float_t value);
36   _fv3_float_t getRelease();
37   void setThreshold(_fv3_float_t value);
38   _fv3_float_t getThreshold();
39   void setCeiling(_fv3_float_t value);
40   _fv3_float_t getCeiling();
41   inline _fv3_float_t process(_fv3_float_t input)
42   {
43     _fv3_float_t rmsf = Rms.process(input);
44 
45     if(lookahead > 0)
46       {
47 	for(long i = 0;i < bufsize;i ++)
48 	  buffer[i] += lookaheadDelta;
49 	buffer[bufidx] = rmsf-LookaheadRatio;
50 	rmsf = 0;
51 	for(long i = 0;i < bufsize;i ++)
52 	  if(rmsf < buffer[i]) rmsf = buffer[i];
53 	bufidx++;
54 	if(bufidx >= bufsize) bufidx = 0;
55       }
56 
57     _fv3_float_t theta = rmsf > env ? attackDelta : releaseDelta;
58     env = (1.0-theta)*rmsf + theta*env;
59     UNDENORMAL(env);
60     if(env < 0) env = 0;
61 
62     // gain reduction
63     if(env >= Threshold)
64       {
65 	_fv3_float_t log_env = std::log(env);
66 	return std::exp(R2-R1*C_T2/(log_env/R1+C_2T)-log_env);
67       }
68     return 1;
69   }
70 
71   _fv3_float_t getEnv();
72   void mute();
73 
74  private:
75   _FV3_(slimit)(const _FV3_(slimit)& x);
76   _FV3_(slimit)& operator=(const _FV3_(slimit)& x);
77 
78   void update();
79   long lookahead, bufidx, bufsize;
80   _fv3_float_t Lookahead, LookaheadRatio, Attack, Release;
81   _fv3_float_t attackDelta, releaseDelta, lookaheadDelta;
82   _fv3_float_t Threshold, Ceiling;
83   _fv3_float_t env, R1, C_T2, C_2T, R2;
84   _FV3_(rms) Rms;
85   _fv3_float_t * buffer;
86 };
87