1 #include <stdlib.h>
2 #include <string.h>
3 #ifndef WIN32
4 #include "config.h"
5 #endif
6 
7 #ifdef ENABLE_NLS
8 #include <libintl.h>
9 #endif
10 
11 #define         _ISOC9X_SOURCE  1
12 #define         _ISOC99_SOURCE  1
13 #define         __USE_ISOC99    1
14 #define         __USE_ISOC9X    1
15 
16 #include <math.h>
17 
18 #include "ladspa.h"
19 
20 #ifdef WIN32
21 #define _WINDOWS_DLL_EXPORT_ __declspec(dllexport)
22 int bIsFirstTime = 1;
23 static void __attribute__((constructor)) swh_init(); // forward declaration
24 #else
25 #define _WINDOWS_DLL_EXPORT_
26 #endif
27 
28 
29 #define ALIAS_LEVEL                    0
30 #define ALIAS_INPUT                    1
31 #define ALIAS_OUTPUT                   2
32 
33 static LADSPA_Descriptor *aliasDescriptor = NULL;
34 
35 typedef struct {
36 	LADSPA_Data *level;
37 	LADSPA_Data *input;
38 	LADSPA_Data *output;
39 	LADSPA_Data run_adding_gain;
40 } Alias;
41 
42 _WINDOWS_DLL_EXPORT_
ladspa_descriptor(unsigned long index)43 const LADSPA_Descriptor *ladspa_descriptor(unsigned long index) {
44 
45 #ifdef WIN32
46 	if (bIsFirstTime) {
47 		swh_init();
48 		bIsFirstTime = 0;
49 	}
50 #endif
51 	switch (index) {
52 	case 0:
53 		return aliasDescriptor;
54 	default:
55 		return NULL;
56 	}
57 }
58 
cleanupAlias(LADSPA_Handle instance)59 static void cleanupAlias(LADSPA_Handle instance) {
60 	free(instance);
61 }
62 
connectPortAlias(LADSPA_Handle instance,unsigned long port,LADSPA_Data * data)63 static void connectPortAlias(
64  LADSPA_Handle instance,
65  unsigned long port,
66  LADSPA_Data *data) {
67 	Alias *plugin;
68 
69 	plugin = (Alias *)instance;
70 	switch (port) {
71 	case ALIAS_LEVEL:
72 		plugin->level = data;
73 		break;
74 	case ALIAS_INPUT:
75 		plugin->input = data;
76 		break;
77 	case ALIAS_OUTPUT:
78 		plugin->output = data;
79 		break;
80 	}
81 }
82 
instantiateAlias(const LADSPA_Descriptor * descriptor,unsigned long s_rate)83 static LADSPA_Handle instantiateAlias(
84  const LADSPA_Descriptor *descriptor,
85  unsigned long s_rate) {
86 	Alias *plugin_data = (Alias *)calloc(1, sizeof(Alias));
87 	plugin_data->run_adding_gain = 1.0f;
88 
89 	return (LADSPA_Handle)plugin_data;
90 }
91 
92 #undef buffer_write
93 #undef RUN_ADDING
94 #undef RUN_REPLACING
95 
96 #define buffer_write(b, v) (b = v)
97 #define RUN_ADDING    0
98 #define RUN_REPLACING 1
99 
runAlias(LADSPA_Handle instance,unsigned long sample_count)100 static void runAlias(LADSPA_Handle instance, unsigned long sample_count) {
101 	Alias *plugin_data = (Alias *)instance;
102 
103 	/* Aliasing level (float value) */
104 	const LADSPA_Data level = *(plugin_data->level);
105 
106 	/* Input (array of floats of length sample_count) */
107 	const LADSPA_Data * const input = plugin_data->input;
108 
109 	/* Output (array of floats of length sample_count) */
110 	LADSPA_Data * const output = plugin_data->output;
111 
112 #line 17 "alias_1407.xml"
113 	unsigned long pos;
114 	float coef = 1.0f - 2.0f * level;
115 
116 	if (output != input) {
117 	  for (pos = 0; pos < sample_count; pos+=2) {
118 	    buffer_write(output[pos], input[pos]);
119 	  }
120 	}
121 	for (pos = 1; pos < sample_count; pos+=2) {
122 	  buffer_write(output[pos], input[pos] * coef);
123 	}
124 }
125 #undef buffer_write
126 #undef RUN_ADDING
127 #undef RUN_REPLACING
128 
129 #define buffer_write(b, v) (b += (v) * run_adding_gain)
130 #define RUN_ADDING    1
131 #define RUN_REPLACING 0
132 
setRunAddingGainAlias(LADSPA_Handle instance,LADSPA_Data gain)133 static void setRunAddingGainAlias(LADSPA_Handle instance, LADSPA_Data gain) {
134 	((Alias *)instance)->run_adding_gain = gain;
135 }
136 
runAddingAlias(LADSPA_Handle instance,unsigned long sample_count)137 static void runAddingAlias(LADSPA_Handle instance, unsigned long sample_count) {
138 	Alias *plugin_data = (Alias *)instance;
139 	LADSPA_Data run_adding_gain = plugin_data->run_adding_gain;
140 
141 	/* Aliasing level (float value) */
142 	const LADSPA_Data level = *(plugin_data->level);
143 
144 	/* Input (array of floats of length sample_count) */
145 	const LADSPA_Data * const input = plugin_data->input;
146 
147 	/* Output (array of floats of length sample_count) */
148 	LADSPA_Data * const output = plugin_data->output;
149 
150 #line 17 "alias_1407.xml"
151 	unsigned long pos;
152 	float coef = 1.0f - 2.0f * level;
153 
154 	if (output != input) {
155 	  for (pos = 0; pos < sample_count; pos+=2) {
156 	    buffer_write(output[pos], input[pos]);
157 	  }
158 	}
159 	for (pos = 1; pos < sample_count; pos+=2) {
160 	  buffer_write(output[pos], input[pos] * coef);
161 	}
162 }
163 
swh_init()164 static void __attribute__((constructor)) swh_init() {
165 	char **port_names;
166 	LADSPA_PortDescriptor *port_descriptors;
167 	LADSPA_PortRangeHint *port_range_hints;
168 
169 #ifdef ENABLE_NLS
170 #define D_(s) dgettext(PACKAGE, s)
171 	bindtextdomain(PACKAGE, PACKAGE_LOCALE_DIR);
172 #else
173 #define D_(s) (s)
174 #endif
175 
176 
177 	aliasDescriptor =
178 	 (LADSPA_Descriptor *)malloc(sizeof(LADSPA_Descriptor));
179 
180 	if (aliasDescriptor) {
181 		aliasDescriptor->UniqueID = 1407;
182 		aliasDescriptor->Label = "alias";
183 		aliasDescriptor->Properties =
184 		 LADSPA_PROPERTY_HARD_RT_CAPABLE;
185 		aliasDescriptor->Name =
186 		 D_("Aliasing");
187 		aliasDescriptor->Maker =
188 		 "Steve Harris <steve@plugin.org.uk>";
189 		aliasDescriptor->Copyright =
190 		 "GPL";
191 		aliasDescriptor->PortCount = 3;
192 
193 		port_descriptors = (LADSPA_PortDescriptor *)calloc(3,
194 		 sizeof(LADSPA_PortDescriptor));
195 		aliasDescriptor->PortDescriptors =
196 		 (const LADSPA_PortDescriptor *)port_descriptors;
197 
198 		port_range_hints = (LADSPA_PortRangeHint *)calloc(3,
199 		 sizeof(LADSPA_PortRangeHint));
200 		aliasDescriptor->PortRangeHints =
201 		 (const LADSPA_PortRangeHint *)port_range_hints;
202 
203 		port_names = (char **)calloc(3, sizeof(char*));
204 		aliasDescriptor->PortNames =
205 		 (const char **)port_names;
206 
207 		/* Parameters for Aliasing level */
208 		port_descriptors[ALIAS_LEVEL] =
209 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
210 		port_names[ALIAS_LEVEL] =
211 		 D_("Aliasing level");
212 		port_range_hints[ALIAS_LEVEL].HintDescriptor =
213 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_DEFAULT_0;
214 		port_range_hints[ALIAS_LEVEL].LowerBound = 0;
215 		port_range_hints[ALIAS_LEVEL].UpperBound = 1;
216 
217 		/* Parameters for Input */
218 		port_descriptors[ALIAS_INPUT] =
219 		 LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
220 		port_names[ALIAS_INPUT] =
221 		 D_("Input");
222 		port_range_hints[ALIAS_INPUT].HintDescriptor = 0;
223 
224 		/* Parameters for Output */
225 		port_descriptors[ALIAS_OUTPUT] =
226 		 LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
227 		port_names[ALIAS_OUTPUT] =
228 		 D_("Output");
229 		port_range_hints[ALIAS_OUTPUT].HintDescriptor = 0;
230 
231 		aliasDescriptor->activate = NULL;
232 		aliasDescriptor->cleanup = cleanupAlias;
233 		aliasDescriptor->connect_port = connectPortAlias;
234 		aliasDescriptor->deactivate = NULL;
235 		aliasDescriptor->instantiate = instantiateAlias;
236 		aliasDescriptor->run = runAlias;
237 		aliasDescriptor->run_adding = runAddingAlias;
238 		aliasDescriptor->set_run_adding_gain = setRunAddingGainAlias;
239 	}
240 }
241 
swh_fini()242 static void __attribute__((destructor)) swh_fini() {
243 	if (aliasDescriptor) {
244 		free((LADSPA_PortDescriptor *)aliasDescriptor->PortDescriptors);
245 		free((char **)aliasDescriptor->PortNames);
246 		free((LADSPA_PortRangeHint *)aliasDescriptor->PortRangeHints);
247 		free(aliasDescriptor);
248 	}
249 	aliasDescriptor = NULL;
250 
251 }
252