1 // Based in gate_1410.c LADSPA Swh-plugins
2 
3 
4 /*
5   rakarrack - a guitar effects software
6 
7  Gate.C  -  Noise Gate Effect
8  Based on Steve Harris LADSPA gate.
9 
10   Copyright (C) 2008 Josep Andreu
11   Author: Josep Andreu
12 
13  This program is free software; you can redistribute it and/or modify
14  it under the terms of version 2 of the GNU General Public License
15  as published by the Free Software Foundation.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  GNU General Public License (version 2) for more details.
21 
22  You should have received a copy of the GNU General Public License
23  (version2)  along with this program; if not, write to the Free Software
24  Foundation,
25  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
26 
27 */
28 
29 #include <math.h>
30 #include "Gate.h"
31 
32 
Gate(float * efxoutl_,float * efxoutr_,double samplerate,uint32_t intermediate_bufsize)33 Gate::Gate (float * efxoutl_, float * efxoutr_, double samplerate, uint32_t intermediate_bufsize)
34 {
35 
36     efxoutl = efxoutl_;
37     efxoutr = efxoutr_;
38 
39     interpbuf = new float[intermediate_bufsize];
40 
41     lpfl = new AnalogFilter (2, 22000, 1, 0, samplerate, interpbuf);
42     lpfr = new AnalogFilter (2, 22000, 1, 0, samplerate, interpbuf);
43     hpfl = new AnalogFilter (3, 20, 1, 0, samplerate, interpbuf);
44     hpfr = new AnalogFilter (3, 20, 1, 0, samplerate, interpbuf);
45 
46     env = 0.0;
47     gate = 0.0;
48     fs = samplerate;
49     state = CLOSED;
50     hold_count = 0;
51 
52 }
53 
~Gate()54 Gate::~Gate ()
55 {
56     delete[] interpbuf;
57     delete lpfl;
58     delete lpfr;
59     delete hpfl;
60     delete hpfr;
61 }
62 
63 
64 
65 void
cleanup()66 Gate::cleanup ()
67 {
68     lpfl->cleanup ();
69     hpfl->cleanup ();
70     lpfr->cleanup ();
71     hpfr->cleanup ();
72 }
73 
74 
75 
76 
77 void
setlpf(int value)78 Gate::setlpf (int value)
79 {
80     Plpf = value;
81     float fr = (float)Plpf;
82     lpfl->setfreq (fr);
83     lpfr->setfreq (fr);
84 };
85 
86 void
sethpf(int value)87 Gate::sethpf (int value)
88 {
89     Phpf = value;
90     float fr = (float)Phpf;
91     hpfl->setfreq (fr);
92     hpfr->setfreq (fr);
93 };
94 
95 
96 void
Gate_Change(int np,int value)97 Gate::Gate_Change (int np, int value)
98 {
99 
100     switch (np) {
101 
102     case 1:
103         Pthreshold = value;
104         t_level = dB2rap ((float)Pthreshold);
105         break;
106     case 2:
107         Prange = value;
108         cut = dB2rap ((float)Prange);
109         break;
110     case 3:
111         Pattack = value;
112         a_rate = 1000.0f / ((float)Pattack * fs);
113         break;
114     case 4:
115         Pdecay = value;
116         d_rate = 1000.0f / ((float)Pdecay * fs);
117         break;
118     case 5:
119         setlpf(value);
120         break;
121     case 6:
122         sethpf(value);
123         break;
124     case 7:
125         Phold = value;
126         hold = (float)Phold;
127         break;
128     }
129 }
130 
131 int
getpar(int np)132 Gate::getpar (int np)
133 {
134 
135     switch (np)
136 
137     {
138     case 1:
139         return (Pthreshold);
140         break;
141     case 2:
142         return (Prange);
143         break;
144     case 3:
145         return (Pattack);
146         break;
147     case 4:
148         return (Pdecay);
149         break;
150     case 5:
151         return (Plpf);
152         break;
153     case 6:
154         return (Phpf);
155         break;
156     case 7:
157         return (Phold);
158         break;
159 
160     }
161 
162     return (0);
163 
164 }
165 
166 
167 void
Gate_Change_Preset(int npreset)168 Gate::Gate_Change_Preset (int npreset)
169 {
170 
171     const int PRESET_SIZE = 7;
172     const int NUM_PRESETS = 3;
173     int pdata[PRESET_SIZE];
174     int presets[NUM_PRESETS][PRESET_SIZE] = {
175         //0
176         {0, 0, 1, 2, 6703, 76, 2},
177         //-10
178         {0, -10, 1, 2, 6703, 76, 2},
179         //-20
180         {0, -20, 1, 2, 6703, 76, 2}
181     };
182 
183     if(npreset>NUM_PRESETS-1) {
184 
185         Fpre->ReadPreset(16,npreset-NUM_PRESETS+1,pdata);
186         for (int n = 0; n < PRESET_SIZE; n++)
187             Gate_Change(n + 1, pdata[n]);
188     } else {
189         for (int n = 0; n < PRESET_SIZE; n++)
190             Gate_Change (n + 1, presets[npreset][n]);
191     }
192 
193 }
194 
195 
196 
197 void
out(float * efxoutl,float * efxoutr,uint32_t period)198 Gate::out (float *efxoutl, float *efxoutr, uint32_t period)
199 {
200     unsigned i;
201     float sum = 0.0f;
202 
203 
204     lpfl->filterout (efxoutl,period);
205     hpfl->filterout (efxoutl,period);
206     lpfr->filterout (efxoutr,period);
207     hpfr->filterout (efxoutr,period);
208 
209 
210     for (i = 0; i < period; i++) {
211 
212         sum = fabsf (efxoutl[i]) + fabsf (efxoutr[i]);
213 
214 
215         if (sum > env)
216             env = sum;
217         else
218             env = sum * ENV_TR + env * (1.0f - ENV_TR);
219 
220         if (state == CLOSED) {
221             if (env >= t_level)
222                 state = OPENING;
223         } else if (state == OPENING) {
224             gate += a_rate;
225             if (gate >= 1.0) {
226                 gate = 1.0f;
227                 state = OPEN;
228                 hold_count = lrintf (hold * fs * 0.001f);
229             }
230         } else if (state == OPEN) {
231             if (hold_count <= 0) {
232                 if (env < t_level) {
233                     state = CLOSING;
234                 }
235             } else
236                 hold_count--;
237 
238         } else if (state == CLOSING) {
239             gate -= d_rate;
240             if (env >= t_level)
241                 state = OPENING;
242             else if (gate <= 0.0) {
243                 gate = 0.0;
244                 state = CLOSED;
245             }
246         }
247 
248         efxoutl[i] *= (cut * (1.0f - gate) + gate);
249         efxoutr[i] *= (cut * (1.0f - gate) + gate);
250 
251     }
252 };
253