swift
SimpleLimit.cpp
1 /*
2  * Simple Limiter (source)
3  *
4  * File : SimpleLimit.cpp
5  * Library : SimpleSource
6  * Version : 1.12
7  * Implements : SimpleLimit
8  *
9  * 2006, ChunkWare Music Software, OPEN-SOURCE
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  */
29 
30 
31 #include "SimpleLimit.h"
32 
33 namespace chunkware_simple
34 {
35  //-------------------------------------------------------------
37  : threshdB_(0.0)
38  , thresh_(1.0)
39  , peakHold_(0)
40  , peakTimer_(0)
41  , maxPeak_(1.0)
42  , att_(1.0)
43  , rel_(10.0)
44  , env_(1.0)
45  , mask_(BUFFER_SIZE - 1)
46  , cur_(0)
47  {
48  setAttackImpl(1.0);
49  outBuffer_[ 0 ].resize(BUFFER_SIZE, 0.0);
50  outBuffer_[ 1 ].resize(BUFFER_SIZE, 0.0);
51  }
52 
53  //-------------------------------------------------------------
54  void SimpleLimit::setThresh(double dB)
55  {
56  threshdB_ = dB;
57  thresh_ = dB2lin(dB);
58  }
59 
60  //-------------------------------------------------------------
61  void SimpleLimit::setAttackImpl(double ms)
62  {
63  unsigned int samp = static_cast<unsigned>(0.001 * ms * att_.getSampleRate());
64 
65  assert(samp < BUFFER_SIZE);
66 
67  peakHold_ = samp;
68  att_.setTc(ms);
69  }
70 
71  //-------------------------------------------------------------
72  void SimpleLimit::setRelease(double ms)
73  {
74  rel_.setTc(ms);
75  }
76 
77  //-------------------------------------------------------------
78  void SimpleLimit::setSampleRate(double sampleRate)
79  {
80  att_.setSampleRate(sampleRate);
81  rel_.setSampleRate(sampleRate);
82  }
83 
84  //-------------------------------------------------------------
86  {
87  peakTimer_ = 0;
88  maxPeak_ = thresh_;
89  env_ = thresh_;
90  cur_ = 0;
91  outBuffer_[ 0 ].assign(BUFFER_SIZE, 0.0);
92  outBuffer_[ 1 ].assign(BUFFER_SIZE, 0.0);
93  }
94 
95  //-------------------------------------------------------------
97  {
98  // rises to 99% of in value over duration of time constant
99  coef_ = pow(0.01, (1000.0 / (ms_ * sampleRate_)));
100  }
101 
102 } // end namespace chunkware_simple
virtual void setSampleRate(double sampleRate)
set sample rate
virtual void setTc(double ms)
set time constant
double ms_
time constant in ms
double coef_
runtime coefficient
virtual double getSampleRate(void) const
get sample rate
virtual void setCoef(void)
Override setCoef() - coefficient calculation.
Definition: SimpleLimit.cpp:96
virtual void setThresh(double dB)
set parameters
Definition: SimpleLimit.cpp:54
virtual void setSampleRate(double sampleRate)
sample rate
Definition: SimpleLimit.cpp:78
virtual void setRelease(double ms)
set parameters
Definition: SimpleLimit.cpp:72
virtual void initRuntime(void)
call before runtime (in resume())
Definition: SimpleLimit.cpp:85