File: surround_encoder_1401.c

package info (click to toggle)
swh-plugins 0.4.15%2B1-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 8,268 kB
  • ctags: 12,249
  • sloc: ansic: 72,074; sh: 28,183; xml: 12,247; perl: 769; makefile: 212; sed: 16
file content (425 lines) | stat: -rw-r--r-- 12,652 bytes parent folder | download | duplicates (6)
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
#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 "surround_encoder_1401.xml"

#include "ladspa-util.h"

#define D_SIZE 256
#define NZEROS 200

/* The non-zero taps of the Hilbert transformer */
static float xcoeffs[] = {
     +0.0008103736f, +0.0008457886f, +0.0009017196f, +0.0009793364f,
     +0.0010798341f, +0.0012044365f, +0.0013544008f, +0.0015310235f,
     +0.0017356466f, +0.0019696659f, +0.0022345404f, +0.0025318040f,
     +0.0028630784f, +0.0032300896f, +0.0036346867f, +0.0040788644f,
     +0.0045647903f, +0.0050948365f, +0.0056716186f, +0.0062980419f,
     +0.0069773575f, +0.0077132300f, +0.0085098208f, +0.0093718901f,
     +0.0103049226f, +0.0113152847f, +0.0124104218f, +0.0135991079f,
     +0.0148917649f, +0.0163008758f, +0.0178415242f, +0.0195321089f,
     +0.0213953037f, +0.0234593652f, +0.0257599469f, +0.0283426636f,
     +0.0312667947f, +0.0346107648f, +0.0384804823f, +0.0430224431f,
     +0.0484451086f, +0.0550553725f, +0.0633242001f, +0.0740128560f,
     +0.0884368322f, +0.1090816773f, +0.1412745301f, +0.1988673273f,
     +0.3326528346f, +0.9997730178f, -0.9997730178f, -0.3326528346f,
     -0.1988673273f, -0.1412745301f, -0.1090816773f, -0.0884368322f,
     -0.0740128560f, -0.0633242001f, -0.0550553725f, -0.0484451086f,
     -0.0430224431f, -0.0384804823f, -0.0346107648f, -0.0312667947f,
     -0.0283426636f, -0.0257599469f, -0.0234593652f, -0.0213953037f,
     -0.0195321089f, -0.0178415242f, -0.0163008758f, -0.0148917649f,
     -0.0135991079f, -0.0124104218f, -0.0113152847f, -0.0103049226f,
     -0.0093718901f, -0.0085098208f, -0.0077132300f, -0.0069773575f,
     -0.0062980419f, -0.0056716186f, -0.0050948365f, -0.0045647903f,
     -0.0040788644f, -0.0036346867f, -0.0032300896f, -0.0028630784f,
     -0.0025318040f, -0.0022345404f, -0.0019696659f, -0.0017356466f,
     -0.0015310235f, -0.0013544008f, -0.0012044365f, -0.0010798341f,
     -0.0009793364f, -0.0009017196f, -0.0008457886f, -0.0008103736f,
};

#define SURROUNDENCODER_L              0
#define SURROUNDENCODER_R              1
#define SURROUNDENCODER_C              2
#define SURROUNDENCODER_S              3
#define SURROUNDENCODER_LT             4
#define SURROUNDENCODER_RT             5

static LADSPA_Descriptor *surroundEncoderDescriptor = NULL;

typedef struct {
	LADSPA_Data *l;
	LADSPA_Data *r;
	LADSPA_Data *c;
	LADSPA_Data *s;
	LADSPA_Data *lt;
	LADSPA_Data *rt;
	LADSPA_Data *buffer;
	unsigned int buffer_pos;
	unsigned int buffer_size;
	LADSPA_Data *delay;
	unsigned int dptr;
	LADSPA_Data run_adding_gain;
} SurroundEncoder;

_WINDOWS_DLL_EXPORT_
const LADSPA_Descriptor *ladspa_descriptor(unsigned long index) {

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

static void activateSurroundEncoder(LADSPA_Handle instance) {
	SurroundEncoder *plugin_data = (SurroundEncoder *)instance;
	LADSPA_Data *buffer = plugin_data->buffer;
	unsigned int buffer_pos = plugin_data->buffer_pos;
	unsigned int buffer_size = plugin_data->buffer_size;
	LADSPA_Data *delay = plugin_data->delay;
	unsigned int dptr = plugin_data->dptr;
#line 75 "surround_encoder_1401.xml"
	memset(buffer, 0, buffer_size * sizeof(LADSPA_Data));
	plugin_data->buffer = buffer;
	plugin_data->buffer_pos = buffer_pos;
	plugin_data->buffer_size = buffer_size;
	plugin_data->delay = delay;
	plugin_data->dptr = dptr;

}

static void cleanupSurroundEncoder(LADSPA_Handle instance) {
#line 79 "surround_encoder_1401.xml"
	SurroundEncoder *plugin_data = (SurroundEncoder *)instance;
	free(plugin_data->buffer);
	
	free(plugin_data->delay);
	free(instance);
}

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

	plugin = (SurroundEncoder *)instance;
	switch (port) {
	case SURROUNDENCODER_L:
		plugin->l = data;
		break;
	case SURROUNDENCODER_R:
		plugin->r = data;
		break;
	case SURROUNDENCODER_C:
		plugin->c = data;
		break;
	case SURROUNDENCODER_S:
		plugin->s = data;
		break;
	case SURROUNDENCODER_LT:
		plugin->lt = data;
		break;
	case SURROUNDENCODER_RT:
		plugin->rt = data;
		break;
	}
}

static LADSPA_Handle instantiateSurroundEncoder(
 const LADSPA_Descriptor *descriptor,
 unsigned long s_rate) {
	SurroundEncoder *plugin_data = (SurroundEncoder *)malloc(sizeof(SurroundEncoder));
	LADSPA_Data *buffer = NULL;
	unsigned int buffer_pos;
	unsigned int buffer_size;
	LADSPA_Data *delay = NULL;
	unsigned int dptr;

#line 65 "surround_encoder_1401.xml"
	buffer_size = (int)(0.0072f * s_rate);
	buffer_pos = 0;
	buffer = calloc(buffer_size, sizeof(LADSPA_Data));
	
	delay = calloc(D_SIZE, sizeof(LADSPA_Data));

	dptr = 0;

	plugin_data->buffer = buffer;
	plugin_data->buffer_pos = buffer_pos;
	plugin_data->buffer_size = buffer_size;
	plugin_data->delay = delay;
	plugin_data->dptr = dptr;

	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 runSurroundEncoder(LADSPA_Handle instance, unsigned long sample_count) {
	SurroundEncoder *plugin_data = (SurroundEncoder *)instance;

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

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

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

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

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

	/* Rt (array of floats of length sample_count) */
	LADSPA_Data * const rt = plugin_data->rt;
	LADSPA_Data * buffer = plugin_data->buffer;
	unsigned int buffer_pos = plugin_data->buffer_pos;
	unsigned int buffer_size = plugin_data->buffer_size;
	LADSPA_Data * delay = plugin_data->delay;
	unsigned int dptr = plugin_data->dptr;

#line 85 "surround_encoder_1401.xml"
	unsigned long pos;
	LADSPA_Data s_delayed;
	unsigned int i;
	float hilb;

	for (pos = 0; pos < sample_count; pos++) {
	  delay[dptr] = s[pos];
	  hilb = 0.0f;
	  for (i = 0; i <= NZEROS/2; i++) {
	    hilb += (xcoeffs[i] * delay[(dptr - i*2) & (D_SIZE - 1)]);
	  }
	  dptr = (dptr + 1) & (D_SIZE - 1);
	
	
	  s_delayed = buffer[buffer_pos];
	  buffer[buffer_pos++] = hilb;
	  buffer_pos %= buffer_size;

	  buffer_write(lt[pos], l[pos] + c[pos] * 0.707946f -
	               s_delayed * 0.707946f);
	  buffer_write(rt[pos], r[pos] + c[pos] * 0.707946f +
	               s_delayed * 0.707946f);
	}
	
	plugin_data->dptr = dptr;

	plugin_data->buffer_pos = buffer_pos;
}
#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 setRunAddingGainSurroundEncoder(LADSPA_Handle instance, LADSPA_Data gain) {
	((SurroundEncoder *)instance)->run_adding_gain = gain;
}

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

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

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

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

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

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

	/* Rt (array of floats of length sample_count) */
	LADSPA_Data * const rt = plugin_data->rt;
	LADSPA_Data * buffer = plugin_data->buffer;
	unsigned int buffer_pos = plugin_data->buffer_pos;
	unsigned int buffer_size = plugin_data->buffer_size;
	LADSPA_Data * delay = plugin_data->delay;
	unsigned int dptr = plugin_data->dptr;

#line 85 "surround_encoder_1401.xml"
	unsigned long pos;
	LADSPA_Data s_delayed;
	unsigned int i;
	float hilb;

	for (pos = 0; pos < sample_count; pos++) {
	  delay[dptr] = s[pos];
	  hilb = 0.0f;
	  for (i = 0; i <= NZEROS/2; i++) {
	    hilb += (xcoeffs[i] * delay[(dptr - i*2) & (D_SIZE - 1)]);
	  }
	  dptr = (dptr + 1) & (D_SIZE - 1);
	
	
	  s_delayed = buffer[buffer_pos];
	  buffer[buffer_pos++] = hilb;
	  buffer_pos %= buffer_size;

	  buffer_write(lt[pos], l[pos] + c[pos] * 0.707946f -
	               s_delayed * 0.707946f);
	  buffer_write(rt[pos], r[pos] + c[pos] * 0.707946f +
	               s_delayed * 0.707946f);
	}
	
	plugin_data->dptr = dptr;

	plugin_data->buffer_pos = buffer_pos;
}

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


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

	if (surroundEncoderDescriptor) {
		surroundEncoderDescriptor->UniqueID = 1401;
		surroundEncoderDescriptor->Label = "surroundEncoder";
		surroundEncoderDescriptor->Properties =
		 LADSPA_PROPERTY_HARD_RT_CAPABLE;
		surroundEncoderDescriptor->Name =
		 D_("Surround matrix encoder");
		surroundEncoderDescriptor->Maker =
		 "Steve Harris <steve@plugin.org.uk>";
		surroundEncoderDescriptor->Copyright =
		 "GPL";
		surroundEncoderDescriptor->PortCount = 6;

		port_descriptors = (LADSPA_PortDescriptor *)calloc(6,
		 sizeof(LADSPA_PortDescriptor));
		surroundEncoderDescriptor->PortDescriptors =
		 (const LADSPA_PortDescriptor *)port_descriptors;

		port_range_hints = (LADSPA_PortRangeHint *)calloc(6,
		 sizeof(LADSPA_PortRangeHint));
		surroundEncoderDescriptor->PortRangeHints =
		 (const LADSPA_PortRangeHint *)port_range_hints;

		port_names = (char **)calloc(6, sizeof(char*));
		surroundEncoderDescriptor->PortNames =
		 (const char **)port_names;

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

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

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

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

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

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

		surroundEncoderDescriptor->activate = activateSurroundEncoder;
		surroundEncoderDescriptor->cleanup = cleanupSurroundEncoder;
		surroundEncoderDescriptor->connect_port = connectPortSurroundEncoder;
		surroundEncoderDescriptor->deactivate = NULL;
		surroundEncoderDescriptor->instantiate = instantiateSurroundEncoder;
		surroundEncoderDescriptor->run = runSurroundEncoder;
		surroundEncoderDescriptor->run_adding = runAddingSurroundEncoder;
		surroundEncoderDescriptor->set_run_adding_gain = setRunAddingGainSurroundEncoder;
	}
}

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

}