-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbe-hard-gate.c
319 lines (264 loc) · 6.91 KB
/
be-hard-gate.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "builtin-effect.h"
#include "common.h"
#include <math.h>
static char *label = "hardgate";
static char *description = "ignore sound below threshold";
static char *inputNames[] = { "threshold", "delay time (ms)", "open time (ms)" };
static int inputType[] = { INPUT_FLOAT, INPUT_FLOAT, INPUT_FLOAT };
static int iHasMin[] = { TRUE, TRUE, TRUE };
static float iMin[] = { -600.0f, 0.0f, 0.0f };
static int iHasMax[] = { TRUE, FALSE, FALSE };
static float iMax[] = { 600.0f, 0.0f, 0.0f };
static float iDefault[] = { 0.0f, 0.0f, 0.0f };
static int iIsLog[] = { FALSE, FALSE, FALSE };
static int iUsesSampleRate[] = { FALSE, FALSE, FALSE };
static int nInputs = 3;
static int nOutputs = 0;
static int nInputBuffers = 1;
static int nOutputBuffers = 1;
//FIXME alllow less than for nInputBuffers
typedef struct data_s data_t;
struct data_s {
float *input_buffer_left;
//float *input_buffer_right;
float *output_buffer_left;
//float *output_buffer_right;
float *threshold;
float *delayTime;
float *openTime;
long int openCount;
long int delayCount;
unsigned long rate;
};
static void *instantiate (unsigned long sampleRate)
{
void *r;
r = calloc(1, sizeof(data_t));
if (!r) {
xerror();
}
((data_t *)r)->rate = sampleRate;
return r;
}
static void cleanup (void *instance)
{
if (!instance) {
xerror();
}
free(instance);
}
static void activate (void *instance)
{
data_t *ins = (data_t *)instance;
if (!ins) {
xerror();
}
}
static void deactivate (void *instance)
{
}
static void connect_input_buffer (void *instance, int num, float *buffer)
{
data_t *ins = (data_t *)instance;
if (!ins) {
xerror();
}
if (!buffer) {
xerror();
}
if (num >= nInputBuffers) {
xerror();
}
if (num == 0) {
ins->input_buffer_left = buffer;
} else {
//ins->input_buffer_right = buffer;
}
}
static void connect_output_buffer (void *instance, int num, float *buffer)
{
data_t *ins = (data_t *)instance;
if (!ins) {
xerror();
}
if (!buffer) {
xerror();
}
if (num >= nOutputBuffers) {
xerror();
}
if (num == 0) {
ins->output_buffer_left = buffer;
} else {
//ins->output_buffer_right = buffer;
}
}
static void connect_input (void *instance, int num, void *f)
{
data_t *ins = (data_t *)instance;
if (!ins) {
xerror();
}
if (!f) {
xerror();
}
if (num >= nInputs) {
xerror();
}
if (num == 0) {
ins->threshold = (float *)f;
} else if (num == 1) {
ins->delayTime = (float *)f;
} else if (num == 2) {
ins->openTime = (float *)f;
}
}
static unsigned long ftime_to_sample_count (data_t *ins, float f)
{
unsigned long r;
r = (unsigned long)(f / (1000.0 / (float)ins->rate));
//printf("f %f %ld\n", f, r);
return r;
}
static int too_weak (data_t *ins, float val)
{
// ib[i] < *ins->threshold && ib[i] > -(*ins->threshold)
if (val < *ins->threshold && val > -(*ins->threshold))
return TRUE;
return FALSE;
}
static void reset_open_count (data_t *ins)
{
ins->openCount = ftime_to_sample_count(ins, *ins->openTime);
}
static void reset_delay_count (data_t *ins)
{
ins->delayCount = ftime_to_sample_count(ins, *ins->delayTime);
}
static void doit (data_t *ins, int bufNum, float *ib, float *ob, unsigned long sampleCount)
{
int i;
for (i = 0; i < sampleCount; i++) {
if (ins->openCount >= 0) {
ob[i] = ib[i];
if (!too_weak(ins, ib[i])) {
reset_open_count(ins);
continue;
}
ins->openCount--;
continue;
}
#if 1
if (ins->delayCount >= 0) {
ob[i] = 0.0f;
ins->delayCount--;
if (ins->delayCount < 0) {
// time to open the gate
ob[i] = ib[i];
reset_open_count(ins);
//printf("opening gate... %d\n", ins->startCount);
continue;
}
continue;
}
#endif
if (!too_weak(ins, ib[i])) {
//ob[i] = ib[i];
ob[i] = 0.0f;
//reset_start_count(ins);
//ins->startCount--;
reset_delay_count(ins);
//printf("reseting open count %d\n", ins->openCount);
continue;
} else {
ob[i] = 0.0f;
}
}
}
#if 0
static void doitx (data_t *ins, int bufNum, float *ib, float *ob, unsigned long sampleCount)
{
int i;
int pos;
if (ins->startCount > 0) {
// keep it open
for (i = 0; i < sampleCount; i++) {
ob[i] = ib[i];
ins->startCount--;
if (!(ib[i] < *ins->threshold && ib[i] > -(*ins->threshold))) {
// outside of threshold again, reset startCount
ins->startCount = ftime_to_sample_count(ins, *ins->closeTime);
}
if (ins->startCount <= 0)
break;
//printf("gate open! %ld\n", ins->startCount);
}
pos = i;
}
for (i = pos; i < sampleCount; i++) {
if (ib[i] < *ins->threshold && ib[i] > -(*ins->threshold)) {
ob[i] = 0.0f;
} else {
ob[i] = ib[i];
// gate now open
ins->startCount = ftime_to_sample_count(ins, *ins->closeTime);
}
}
}
#endif
static void run (void *instance, unsigned long sampleCount)
{
data_t *ins = (data_t *)instance;
if (!ins) {
xerror();
}
if (!ins->input_buffer_left) {
xerror();
}
//if (!ins->input_buffer_right) {
// xerror();
//}
if (!ins->output_buffer_left) {
xerror();
}
//if (!ins->output_buffer_right) {
// xerror();
//}
if (!ins->threshold) {
xerror();
}
if (!ins->delayTime) {
xerror();
}
if (!ins->openTime) {
xerror();
}
doit(ins, 0, ins->input_buffer_left, ins->output_buffer_left, sampleCount);
//doit(ins, 1, ins->input_buffer_right, ins->output_buffer_right, sampleCount);
}
void be_hard_gate_init (builtin_effect_t *be)
{
be->label = label;
be->description = description;
be->nInputs = nInputs;
be->inputNames = inputNames;
be->inputType = inputType;
be->iHasMin = iHasMin;
be->iMin = iMin;
be->iHasMax = iHasMax;
be->iMax = iMax;
be->iDefault = iDefault;
be->iIsLog = iIsLog;
be->iUsesSampleRate = iUsesSampleRate;
be->nOutputs = nOutputs;
be->nInputBuffers = nInputBuffers;
be->nOutputBuffers = nOutputBuffers;
be->instantiate = instantiate;
be->cleanup = cleanup;
be->activate = activate;
be->deactivate = deactivate;
be->connect_input_buffer = connect_input_buffer;
be->connect_output_buffer = connect_output_buffer;
be->connect_input = connect_input;
be->run = run;
}