File: gate.cpp

package info (click to toggle)
abgate 1.1.9-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,108 kB
  • sloc: cpp: 1,476; makefile: 25; sh: 5
file content (219 lines) | stat: -rw-r--r-- 5,348 bytes parent folder | download | duplicates (3)
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
/* abGate - LV2 Noise Gate Plugin
 *
 * Copyright 2011 Antanas Bružas
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
#include <stdlib.h>
#include <lv2.h>
#include <cmath>

#include "gate_const.h"

#define CLOSED 1
#define ATTACK 2
#define OPENED 3
#define DECAY 4

static LV2_Descriptor *gateDescriptor = NULL;

class Gate {
public:
	Gate() {
		state = CLOSED;
		gate = 0;
		holding = 0;
	}
	~Gate() {
	}

	float *switch_button, *threshold, *attack, *hold, *decay, *range, sample_rate, gate, *output;
	const float *input;
	int state, holding;
};

static LV2_Handle instantiateGate(const _LV2_Descriptor *descriptor, double s_rate, const char *path, const LV2_Feature * const * features) {
	Gate *plugin_data = new Gate;
	plugin_data->sample_rate = s_rate;
	return (LV2_Handle) plugin_data;
}

static void connectPortGate(LV2_Handle instance, uint32_t port, void *data) {
	Gate *plugin = (Gate *) instance;

	switch (port) {
	case p_switch:
		plugin->switch_button = (float*) data;
		break;
	case p_threshold:
		plugin->threshold = (float*) data;
		break;
	case p_attack:
		plugin->attack = (float*) data;
		break;
	case p_hold:
		plugin->hold = (float*) data;
		break;
	case p_decay:
		plugin->decay = (float*) data;
		break;
	case p_gaterange:
		plugin->range = (float*) data;
		break;
	case p_input:
		plugin->input = (const float*) data;
		break;
	case p_output:
		plugin->output = (float*) data;
		break;
	}
}

static void runGate(LV2_Handle instance, uint32_t sample_count) {

	Gate *plugin_data = (Gate *) instance;

	float * const output = plugin_data->output;
	const float * const input = plugin_data->input;

	// Checking bypass state
	float switch_button = *(plugin_data->switch_button);
	switch_button = switch_button < 0 ? 0 : switch_button;
	switch_button = switch_button > 1 ? 1 : switch_button;

	bool active = switch_button > 0;
	if (active) {

		// Getting port values
		const float threshold = *(plugin_data->threshold);
		const float attack = *(plugin_data->attack);
		const float hold = *(plugin_data->hold);
		const float decay = *(plugin_data->decay);
		const float range = *(plugin_data->range);

		const float sample_rate = plugin_data->sample_rate;

		const float threshold_value = pow(10, threshold * 0.05);
		const float attack_coef = 1000 / (attack * sample_rate);
		const int hold_samples = round(hold * sample_rate * 0.001);
		const float decay_coef = 1000 / (decay * sample_rate);
		const float range_coef = range > -90 ? pow(10, range * 0.05) : 0;

		int state = plugin_data->state;
		float gate = plugin_data->gate;
		int holding = plugin_data->holding;

		for (uint32_t i = 0; i < sample_count; ++i) {

			// Counting input dB
			float sample = input[i];
			float abs_sample = fabs(sample);

			switch (state) {
			case CLOSED:
			case DECAY:
				if (abs_sample >= threshold_value) {
					state = ATTACK;
				}
				break;
			case ATTACK:
				break;
			case OPENED:
				if (abs_sample >= threshold_value) {
					holding = hold_samples;
				} else if (holding <= 0) {
					state = DECAY;
				} else {
					holding--;
				}
				break;
			default:
				// shouldn't happen
				state = CLOSED;
			}

			// handle attack/decay in a second pass to avoid unnecessary one-sample delay
			switch (state) {
			case CLOSED:
				output[i] = sample * range_coef;
				break;
			case DECAY:
				gate -= decay_coef;
				if (gate <= 0) {
					gate = 0;
					state = CLOSED;
				}
				output[i] = sample * (range_coef * (1 - gate) + gate);
				break;
			case ATTACK:
				gate += attack_coef;
				if (gate >= 1) {
					gate = 1;
					state = OPENED;
					holding = hold_samples;
				}
				output[i] = sample * (range_coef * (1 - gate) + gate);
				break;
			case OPENED:
				output[i] = sample;
				break;
			}
		}

		plugin_data->gate = gate;
		plugin_data->state = state;
		plugin_data->holding = holding;
	} else {
		// Bypassing
		if (output != input) {
			for (uint32_t i = 0; i < sample_count; ++i) {
				output[i] = input[i];
			}
		}
	}
}

static void cleanupGate(LV2_Handle instance) {
	Gate *plugin_data = (Gate *) instance;
	delete plugin_data;
}

static void init() {

	gateDescriptor = (LV2_Descriptor *) malloc(sizeof(LV2_Descriptor));
	gateDescriptor->URI = p_uri;
	gateDescriptor->instantiate = instantiateGate;
	gateDescriptor->connect_port = connectPortGate;
	gateDescriptor->activate = NULL;
	gateDescriptor->run = runGate;
	gateDescriptor->deactivate = NULL;
	gateDescriptor->cleanup = cleanupGate;
	gateDescriptor->extension_data = NULL;
}

LV2_SYMBOL_EXPORT
const LV2_Descriptor *lv2_descriptor(uint32_t index) {

	if (!gateDescriptor) {
		init();
	}

	switch (index) {
	case 0:
		return gateDescriptor;
	default:
		return NULL;
	}
}