swift
SimpleLimitProcess.inl
1 /*
2  * Simple Limiter (runtime function)
3  *
4  * File : SimpleLimitProcess.inl
5  * Library : SimpleSource
6  * Version : 1.12
7  * Implements : void SimpleLimit::process( double &in1, double &in2 )
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 #ifndef __SIMPLE_LIMIT_PROCESS_INL__
32 #define __SIMPLE_LIMIT_PROCESS_INL__
33 
34 namespace chunkware_simple
35 {
36  //-------------------------------------------------------------
37  INLINE void SimpleLimit::process( double &in1, double &in2 )
38  {
39  // create sidechain
40 
41  double rect1 = fabs( in1 ); // rectify input
42  double rect2 = fabs( in2 );
43 
44  double keyLink = std::max( rect1, rect2 ); // link channels with greater of 2
45 
46  // threshold
47  // we always want to feed the sidechain AT LEATS the threshold value
48  if ( keyLink < thresh_ )
49  keyLink = thresh_;
50 
51  // test:
52  // a) whether peak timer has "expired"
53  // b) whether new peak is greater than previous max peak
54  if ( (++peakTimer_ >= peakHold_) || (keyLink > maxPeak_) ) {
55  // if either condition is met:
56  peakTimer_ = 0; // reset peak timer
57  maxPeak_ = keyLink; // assign new peak to max peak
58  }
59 
60  /* REGARDING THE MAX PEAK: This method assumes that the only important
61  * sample in a look-ahead buffer would be the highest peak. As such,
62  * instead of storing all samples in a look-ahead buffer, it only stores
63  * the max peak, and compares all incoming samples to that one.
64  * The max peak has a hold time equal to what the look-ahead buffer
65  * would have been, which is tracked by a timer (counter). When this
66  * timer expires, the sample would have exited from the buffer. Therefore,
67  * a new sample must be assigned to the max peak. We assume that the next
68  * highest sample in our theoretical buffer is the current input sample.
69  * In reality, we know this is probably NOT the case, and that there has
70  * been another sample, slightly lower than the one before it, that has
71  * passed the input. If we do not account for this possibility, our gain
72  * reduction could be insufficient, resulting in an "over" at the output.
73  * To remedy this, we simply apply a suitably long release stage in the
74  * envelope follower.
75  */
76 
77  // attack/release
78  if ( maxPeak_ > env_ )
79  att_.run( maxPeak_, env_ ); // run attack phase
80  else
81  rel_.run( maxPeak_, env_ ); // run release phase
82 
83  /* REGARDING THE ATTACK: This limiter achieves "look-ahead" detection
84  * by allowing the envelope follower to attack the max peak, which is
85  * held for the duration of the attack phase -- unless a new, higher
86  * peak is detected. The output signal is buffered so that the gain
87  * reduction is applied in advance of the "offending" sample.
88  */
89 
90  /* NOTE: a DC offset is not necessary for the envelope follower,
91  * as neither the max peak nor envelope should fall below the
92  * threshold (which is assumed to be around 1.0 linear).
93  */
94 
95  // gain reduction
96  double gR = thresh_ / env_;
97 
98  // unload current buffer index
99  // ( cur_ - delay ) & mask_ gets sample from [delay] samples ago
100  // mask_ variable wraps index
101  unsigned int delayIndex = ( cur_ - peakHold_ ) & mask_;
102  double delay1 = outBuffer_[ 0 ][ delayIndex ];
103  double delay2 = outBuffer_[ 1 ][ delayIndex ];
104 
105  // load current buffer index and advance current index
106  // mask_ wraps cur_ index
107  outBuffer_[ 0 ][ cur_ ] = in1;
108  outBuffer_[ 1 ][ cur_ ] = in2;
109  ++cur_ &= mask_;
110 
111  // output gain
112  in1 = delay1 * gR; // apply gain reduction to input
113  in2 = delay2 * gR;
114 
115  /* REGARDING THE GAIN REDUCTION: Due to the logarithmic nature
116  * of the attack phase, the sidechain will never achieve "full"
117  * attack. (Actually, it is only guaranteed to achieve 99% of
118  * the input value over the given time constant.) As such, the
119  * limiter cannot achieve "brick-wall" limiting. There are 2
120  * workarounds:
121  *
122  * 1) Set the threshold slightly lower than the desired threshold.
123  * i.e. 0.0dB -> -0.1dB or even -0.5dB
124  *
125  * 2) Clip the output at the threshold, as such:
126  *
127  * if ( in1 > thresh_ ) in1 = thresh_;
128  * else if ( in1 < -thresh_ ) in1 = -thresh_;
129  *
130  * if ( in2 > thresh_ ) in2 = thresh_;
131  * else if ( in2 < -thresh_ ) in2 = -thresh_;
132  *
133  * (... or replace with your favorite branchless clipper ...)
134  */
135  }
136 
137 } // end namespace chunkware_simple
138 
139 #endif // end __SIMPLE_LIMIT_PROCESS_INL__
INLINE void run(double in, double &state)
runtime function
void process(double &in1, double &in2)
limiter runtime process