~ubuntu-branches/ubuntu/trusty/lmms/trusty

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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <stdlib.h>
#include <string.h>
#ifndef WIN32
#include "config.h"
#endif

#ifdef ENABLE_NLS
#include <libintl.h>
#endif

#define         _ISOC9X_SOURCE  1
#define         _ISOC99_SOURCE  1
#define         __USE_ISOC99    1
#define         __USE_ISOC9X    1

#include <math.h>

#include "ladspa.h"

#ifdef WIN32
#define _WINDOWS_DLL_EXPORT_ __declspec(dllexport)
int bIsFirstTime = 1; 
void _init(); // forward declaration
#else
#define _WINDOWS_DLL_EXPORT_ 
#endif

#line 10 "valve_1209.xml"

#include "ladspa-util.h"

#define VALVE_Q_P                      0
#define VALVE_DIST_P                   1
#define VALVE_INPUT                    2
#define VALVE_OUTPUT                   3

static LADSPA_Descriptor *valveDescriptor = NULL;

typedef struct {
	LADSPA_Data *q_p;
	LADSPA_Data *dist_p;
	LADSPA_Data *input;
	LADSPA_Data *output;
	LADSPA_Data  itm1;
	LADSPA_Data  otm1;
	LADSPA_Data run_adding_gain;
} Valve;

_WINDOWS_DLL_EXPORT_
const LADSPA_Descriptor *ladspa_descriptor(unsigned long index) {

#ifdef WIN32
	if (bIsFirstTime) {
		_init();
		bIsFirstTime = 0;
	}
#endif
	switch (index) {
	case 0:
		return valveDescriptor;
	default:
		return NULL;
	}
}

static void activateValve(LADSPA_Handle instance) {
	Valve *plugin_data = (Valve *)instance;
	LADSPA_Data itm1 = plugin_data->itm1;
	LADSPA_Data otm1 = plugin_data->otm1;
#line 21 "valve_1209.xml"
	itm1 = 0.0f;
	otm1 = 0.0f;
	plugin_data->itm1 = itm1;
	plugin_data->otm1 = otm1;

}

static void cleanupValve(LADSPA_Handle instance) {
	free(instance);
}

static void connectPortValve(
 LADSPA_Handle instance,
 unsigned long port,
 LADSPA_Data *data) {
	Valve *plugin;

	plugin = (Valve *)instance;
	switch (port) {
	case VALVE_Q_P:
		plugin->q_p = data;
		break;
	case VALVE_DIST_P:
		plugin->dist_p = data;
		break;
	case VALVE_INPUT:
		plugin->input = data;
		break;
	case VALVE_OUTPUT:
		plugin->output = data;
		break;
	}
}

static LADSPA_Handle instantiateValve(
 const LADSPA_Descriptor *descriptor,
 unsigned long s_rate) {
	Valve *plugin_data = (Valve *)malloc(sizeof(Valve));
	plugin_data->run_adding_gain = 1.0f;

	return (LADSPA_Handle)plugin_data;
}

#undef buffer_write
#undef RUN_ADDING
#undef RUN_REPLACING

#define buffer_write(b, v) (b = v)
#define RUN_ADDING    0
#define RUN_REPLACING 1

static void runValve(LADSPA_Handle instance, unsigned long sample_count) {
	Valve *plugin_data = (Valve *)instance;

	/* Distortion level (float value) */
	const LADSPA_Data q_p = *(plugin_data->q_p);

	/* Distortion character (float value) */
	const LADSPA_Data dist_p = *(plugin_data->dist_p);

	/* Input (array of floats of length sample_count) */
	const LADSPA_Data * const input = plugin_data->input;

	/* Output (array of floats of length sample_count) */
	LADSPA_Data * const output = plugin_data->output;
	LADSPA_Data itm1 = plugin_data->itm1;
	LADSPA_Data otm1 = plugin_data->otm1;

#line 26 "valve_1209.xml"
	unsigned long pos;
	LADSPA_Data fx;
	
	const float q = q_p - 0.999f;
	const float dist = dist_p * 40.0f + 0.1f;
	
	if (q == 0.0f) {
	        for (pos = 0; pos < sample_count; pos++) {
	                if (input[pos] == q) {
	                        fx = 1.0f / dist;
	                } else {
	                        fx = input[pos] / (1.0f - f_exp(-dist * input[pos]));
	                }
	                otm1 = 0.999f * otm1 + fx - itm1;
	                round_to_zero(&otm1);
	                itm1 = fx;
	                buffer_write(output[pos], otm1);
	        }
	} else {
	        for (pos = 0; pos < sample_count; pos++) {
	                if (input[pos] == q) {
	                        fx = 1.0f / dist + q / (1.0f - f_exp(dist * q));
	                } else {
	                        fx = (input[pos] - q) /
	                         (1.0f - f_exp(-dist * (input[pos] - q))) +
	                         q / (1.0f - f_exp(dist * q));
	                }
	                otm1 = 0.999f * otm1 + fx - itm1;
	                round_to_zero(&otm1);
	                itm1 = fx;
	                buffer_write(output[pos], otm1);
	        }
	}
	
	plugin_data->itm1 = itm1;
	plugin_data->otm1 = otm1;
}
#undef buffer_write
#undef RUN_ADDING
#undef RUN_REPLACING

#define buffer_write(b, v) (b += (v) * run_adding_gain)
#define RUN_ADDING    1
#define RUN_REPLACING 0

static void setRunAddingGainValve(LADSPA_Handle instance, LADSPA_Data gain) {
	((Valve *)instance)->run_adding_gain = gain;
}

static void runAddingValve(LADSPA_Handle instance, unsigned long sample_count) {
	Valve *plugin_data = (Valve *)instance;
	LADSPA_Data run_adding_gain = plugin_data->run_adding_gain;

	/* Distortion level (float value) */
	const LADSPA_Data q_p = *(plugin_data->q_p);

	/* Distortion character (float value) */
	const LADSPA_Data dist_p = *(plugin_data->dist_p);

	/* Input (array of floats of length sample_count) */
	const LADSPA_Data * const input = plugin_data->input;

	/* Output (array of floats of length sample_count) */
	LADSPA_Data * const output = plugin_data->output;
	LADSPA_Data itm1 = plugin_data->itm1;
	LADSPA_Data otm1 = plugin_data->otm1;

#line 26 "valve_1209.xml"
	unsigned long pos;
	LADSPA_Data fx;
	
	const float q = q_p - 0.999f;
	const float dist = dist_p * 40.0f + 0.1f;
	
	if (q == 0.0f) {
	        for (pos = 0; pos < sample_count; pos++) {
	                if (input[pos] == q) {
	                        fx = 1.0f / dist;
	                } else {
	                        fx = input[pos] / (1.0f - f_exp(-dist * input[pos]));
	                }
	                otm1 = 0.999f * otm1 + fx - itm1;
	                round_to_zero(&otm1);
	                itm1 = fx;
	                buffer_write(output[pos], otm1);
	        }
	} else {
	        for (pos = 0; pos < sample_count; pos++) {
	                if (input[pos] == q) {
	                        fx = 1.0f / dist + q / (1.0f - f_exp(dist * q));
	                } else {
	                        fx = (input[pos] - q) /
	                         (1.0f - f_exp(-dist * (input[pos] - q))) +
	                         q / (1.0f - f_exp(dist * q));
	                }
	                otm1 = 0.999f * otm1 + fx - itm1;
	                round_to_zero(&otm1);
	                itm1 = fx;
	                buffer_write(output[pos], otm1);
	        }
	}
	
	plugin_data->itm1 = itm1;
	plugin_data->otm1 = otm1;
}

void _init() {
	char **port_names;
	LADSPA_PortDescriptor *port_descriptors;
	LADSPA_PortRangeHint *port_range_hints;

#ifdef ENABLE_NLS
#define D_(s) dgettext(PACKAGE, s)
	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE, PACKAGE_LOCALE_DIR);
#else
#define D_(s) (s)
#endif


	valveDescriptor =
	 (LADSPA_Descriptor *)malloc(sizeof(LADSPA_Descriptor));

	if (valveDescriptor) {
		valveDescriptor->UniqueID = 1209;
		valveDescriptor->Label = "valve";
		valveDescriptor->Properties =
		 LADSPA_PROPERTY_HARD_RT_CAPABLE;
		valveDescriptor->Name =
		 D_("Valve saturation");
		valveDescriptor->Maker =
		 "Steve Harris <steve@plugin.org.uk>";
		valveDescriptor->Copyright =
		 "GPL";
		valveDescriptor->PortCount = 4;

		port_descriptors = (LADSPA_PortDescriptor *)calloc(4,
		 sizeof(LADSPA_PortDescriptor));
		valveDescriptor->PortDescriptors =
		 (const LADSPA_PortDescriptor *)port_descriptors;

		port_range_hints = (LADSPA_PortRangeHint *)calloc(4,
		 sizeof(LADSPA_PortRangeHint));
		valveDescriptor->PortRangeHints =
		 (const LADSPA_PortRangeHint *)port_range_hints;

		port_names = (char **)calloc(4, sizeof(char*));
		valveDescriptor->PortNames =
		 (const char **)port_names;

		/* Parameters for Distortion level */
		port_descriptors[VALVE_Q_P] =
		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
		port_names[VALVE_Q_P] =
		 D_("Distortion level");
		port_range_hints[VALVE_Q_P].HintDescriptor =
		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_DEFAULT_0;
		port_range_hints[VALVE_Q_P].LowerBound = 0;
		port_range_hints[VALVE_Q_P].UpperBound = 1;

		/* Parameters for Distortion character */
		port_descriptors[VALVE_DIST_P] =
		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
		port_names[VALVE_DIST_P] =
		 D_("Distortion character");
		port_range_hints[VALVE_DIST_P].HintDescriptor =
		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_DEFAULT_0;
		port_range_hints[VALVE_DIST_P].LowerBound = 0;
		port_range_hints[VALVE_DIST_P].UpperBound = 1;

		/* Parameters for Input */
		port_descriptors[VALVE_INPUT] =
		 LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
		port_names[VALVE_INPUT] =
		 D_("Input");
		port_range_hints[VALVE_INPUT].HintDescriptor = 0;

		/* Parameters for Output */
		port_descriptors[VALVE_OUTPUT] =
		 LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
		port_names[VALVE_OUTPUT] =
		 D_("Output");
		port_range_hints[VALVE_OUTPUT].HintDescriptor = 0;

		valveDescriptor->activate = activateValve;
		valveDescriptor->cleanup = cleanupValve;
		valveDescriptor->connect_port = connectPortValve;
		valveDescriptor->deactivate = NULL;
		valveDescriptor->instantiate = instantiateValve;
		valveDescriptor->run = runValve;
		valveDescriptor->run_adding = runAddingValve;
		valveDescriptor->set_run_adding_gain = setRunAddingGainValve;
	}
}

void _fini() {
	if (valveDescriptor) {
		free((LADSPA_PortDescriptor *)valveDescriptor->PortDescriptors);
		free((char **)valveDescriptor->PortNames);
		free((LADSPA_PortRangeHint *)valveDescriptor->PortRangeHints);
		free(valveDescriptor);
	}

}