1 // Copyright 2015 Olivier Gillet.
2 // Copyright 2016 Matthias Puech.
3 //
4 // Author: Olivier Gillet (ol.gillet@gmail.com)
5 // Modified by: Matthias Puech (matthias.puech@gmail.com)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 // THE SOFTWARE.
24 //
25 // See http://creativecommons.org/licenses/MIT/ for more information.
26 //
27 // -----------------------------------------------------------------------------
28 //
29 // Dynamic Gate
30 
31 #ifndef GATE_H_
32 #define GATE_H_
33 
34 #include "stmlib/dsp/dsp.h"
35 
36 #include "parameters.h"
37 
38 const float kThresholdLow = 0.002f * 32768.0f;
39 const float kThresholdHigh = 0.05f * 32768.0f;
40 
41 class Gate {
42  public:
Gate()43   Gate() { }
~Gate()44   ~Gate() { }
45 
Init()46   void Init() {
47     peak_ = 0.5f;
48   }
49 
Process(ShortFrame * in_out)50   inline void Process(ShortFrame* in_out) {
51     size_t size = kBlockSize;
52     while (size--) {
53       short l = (*in_out).l;
54       short r = (*in_out).r;
55       SLOPE(peak_, fabs(l), 0.05f, 0.00003f);
56       float gain = peak_ < kThresholdLow ? 0.0f :
57         peak_ > kThresholdHigh ? 1.0f :
58         (peak_ - kThresholdLow) / (kThresholdHigh - kThresholdLow);
59       (*in_out).l = l * gain;
60       (*in_out).r = r * gain;
61       ++in_out;
62     }
63   }
64 
65  private:
66   float peak_;
67 
68   DISALLOW_COPY_AND_ASSIGN(Gate);
69 };
70 
71 #endif
72