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 #line 10 "rate_shifter_1417.xml"
29 
30 #include "ladspa-util.h"
31 
32 #define RATESHIFTER_RATE               0
33 #define RATESHIFTER_INPUT              1
34 #define RATESHIFTER_OUTPUT             2
35 
36 static LADSPA_Descriptor *rateShifterDescriptor = NULL;
37 
38 typedef struct {
39 	LADSPA_Data *rate;
40 	LADSPA_Data *input;
41 	LADSPA_Data *output;
42 	LADSPA_Data *buffer;
43 	unsigned int buffer_mask;
44 	fixp32       read_ptr;
45 	unsigned int write_ptr;
46 	LADSPA_Data run_adding_gain;
47 } RateShifter;
48 
49 _WINDOWS_DLL_EXPORT_
ladspa_descriptor(unsigned long index)50 const LADSPA_Descriptor *ladspa_descriptor(unsigned long index) {
51 
52 #ifdef WIN32
53 	if (bIsFirstTime) {
54 		swh_init();
55 		bIsFirstTime = 0;
56 	}
57 #endif
58 	switch (index) {
59 	case 0:
60 		return rateShifterDescriptor;
61 	default:
62 		return NULL;
63 	}
64 }
65 
activateRateShifter(LADSPA_Handle instance)66 static void activateRateShifter(LADSPA_Handle instance) {
67 	RateShifter *plugin_data = (RateShifter *)instance;
68 	LADSPA_Data *buffer = plugin_data->buffer;
69 	unsigned int buffer_mask = plugin_data->buffer_mask;
70 	fixp32 read_ptr = plugin_data->read_ptr;
71 	unsigned int write_ptr = plugin_data->write_ptr;
72 #line 36 "rate_shifter_1417.xml"
73 	memset(buffer, 0, buffer_mask + 1);
74 	read_ptr.all = 0;
75 	write_ptr = (buffer_mask + 1) / 2;
76 	write_ptr = 0;
77 	plugin_data->buffer = buffer;
78 	plugin_data->buffer_mask = buffer_mask;
79 	plugin_data->read_ptr = read_ptr;
80 	plugin_data->write_ptr = write_ptr;
81 
82 }
83 
cleanupRateShifter(LADSPA_Handle instance)84 static void cleanupRateShifter(LADSPA_Handle instance) {
85 #line 43 "rate_shifter_1417.xml"
86 	RateShifter *plugin_data = (RateShifter *)instance;
87 	free(plugin_data->buffer);
88 	free(instance);
89 }
90 
connectPortRateShifter(LADSPA_Handle instance,unsigned long port,LADSPA_Data * data)91 static void connectPortRateShifter(
92  LADSPA_Handle instance,
93  unsigned long port,
94  LADSPA_Data *data) {
95 	RateShifter *plugin;
96 
97 	plugin = (RateShifter *)instance;
98 	switch (port) {
99 	case RATESHIFTER_RATE:
100 		plugin->rate = data;
101 		break;
102 	case RATESHIFTER_INPUT:
103 		plugin->input = data;
104 		break;
105 	case RATESHIFTER_OUTPUT:
106 		plugin->output = data;
107 		break;
108 	}
109 }
110 
instantiateRateShifter(const LADSPA_Descriptor * descriptor,unsigned long s_rate)111 static LADSPA_Handle instantiateRateShifter(
112  const LADSPA_Descriptor *descriptor,
113  unsigned long s_rate) {
114 	RateShifter *plugin_data = (RateShifter *)calloc(1, sizeof(RateShifter));
115 	LADSPA_Data *buffer = NULL;
116 	unsigned int buffer_mask;
117 	fixp32 read_ptr;
118 	unsigned int write_ptr;
119 
120 #line 23 "rate_shifter_1417.xml"
121 	unsigned int size = 32768;
122 	const float fs = s_rate;
123 
124 	while (size < 2.7f * fs) {
125 	  size *= 2;
126 	}
127 	buffer = calloc(size, sizeof(LADSPA_Data));
128 	buffer_mask = size - 1;
129 	read_ptr.all = 0;
130 	write_ptr = size / 2;
131 
132 	plugin_data->buffer = buffer;
133 	plugin_data->buffer_mask = buffer_mask;
134 	plugin_data->read_ptr = read_ptr;
135 	plugin_data->write_ptr = write_ptr;
136 
137 	return (LADSPA_Handle)plugin_data;
138 }
139 
140 #undef buffer_write
141 #undef RUN_ADDING
142 #undef RUN_REPLACING
143 
144 #define buffer_write(b, v) (b = v)
145 #define RUN_ADDING    0
146 #define RUN_REPLACING 1
147 
runRateShifter(LADSPA_Handle instance,unsigned long sample_count)148 static void runRateShifter(LADSPA_Handle instance, unsigned long sample_count) {
149 	RateShifter *plugin_data = (RateShifter *)instance;
150 
151 	/* Rate (float value) */
152 	const LADSPA_Data rate = *(plugin_data->rate);
153 
154 	/* Input (array of floats of length sample_count) */
155 	const LADSPA_Data * const input = plugin_data->input;
156 
157 	/* Output (array of floats of length sample_count) */
158 	LADSPA_Data * const output = plugin_data->output;
159 	LADSPA_Data * buffer = plugin_data->buffer;
160 	unsigned int buffer_mask = plugin_data->buffer_mask;
161 	fixp32 read_ptr = plugin_data->read_ptr;
162 	unsigned int write_ptr = plugin_data->write_ptr;
163 
164 #line 47 "rate_shifter_1417.xml"
165 	unsigned long pos;
166 	fixp32 read_inc;
167 
168 	read_inc.all = (long long)(rate * 4294967296.0f);
169 
170 	for (pos = 0; pos < sample_count; pos++) {
171 	  const unsigned int rp = read_ptr.part.in;
172 
173 	  /* Do write pointer stuff */
174 	  buffer[write_ptr] = input[pos];
175 	  write_ptr = (write_ptr + 1) & buffer_mask;
176 
177 	  /* And now read pointer */
178 	  buffer_write(output[pos], cube_interp((float)read_ptr.part.fr / 4294967296.0f, buffer[(rp - 1) & buffer_mask], buffer[rp],  buffer[(rp + 1) & buffer_mask], buffer[(rp + 2) & buffer_mask]));
179 	  read_ptr.all += read_inc.all;
180 	  read_ptr.part.in &= buffer_mask;
181 	}
182 
183 	plugin_data->read_ptr.all = read_ptr.all;
184 	plugin_data->write_ptr = write_ptr;
185 }
186 #undef buffer_write
187 #undef RUN_ADDING
188 #undef RUN_REPLACING
189 
190 #define buffer_write(b, v) (b += (v) * run_adding_gain)
191 #define RUN_ADDING    1
192 #define RUN_REPLACING 0
193 
setRunAddingGainRateShifter(LADSPA_Handle instance,LADSPA_Data gain)194 static void setRunAddingGainRateShifter(LADSPA_Handle instance, LADSPA_Data gain) {
195 	((RateShifter *)instance)->run_adding_gain = gain;
196 }
197 
runAddingRateShifter(LADSPA_Handle instance,unsigned long sample_count)198 static void runAddingRateShifter(LADSPA_Handle instance, unsigned long sample_count) {
199 	RateShifter *plugin_data = (RateShifter *)instance;
200 	LADSPA_Data run_adding_gain = plugin_data->run_adding_gain;
201 
202 	/* Rate (float value) */
203 	const LADSPA_Data rate = *(plugin_data->rate);
204 
205 	/* Input (array of floats of length sample_count) */
206 	const LADSPA_Data * const input = plugin_data->input;
207 
208 	/* Output (array of floats of length sample_count) */
209 	LADSPA_Data * const output = plugin_data->output;
210 	LADSPA_Data * buffer = plugin_data->buffer;
211 	unsigned int buffer_mask = plugin_data->buffer_mask;
212 	fixp32 read_ptr = plugin_data->read_ptr;
213 	unsigned int write_ptr = plugin_data->write_ptr;
214 
215 #line 47 "rate_shifter_1417.xml"
216 	unsigned long pos;
217 	fixp32 read_inc;
218 
219 	read_inc.all = (long long)(rate * 4294967296.0f);
220 
221 	for (pos = 0; pos < sample_count; pos++) {
222 	  const unsigned int rp = read_ptr.part.in;
223 
224 	  /* Do write pointer stuff */
225 	  buffer[write_ptr] = input[pos];
226 	  write_ptr = (write_ptr + 1) & buffer_mask;
227 
228 	  /* And now read pointer */
229 	  buffer_write(output[pos], cube_interp((float)read_ptr.part.fr / 4294967296.0f, buffer[(rp - 1) & buffer_mask], buffer[rp],  buffer[(rp + 1) & buffer_mask], buffer[(rp + 2) & buffer_mask]));
230 	  read_ptr.all += read_inc.all;
231 	  read_ptr.part.in &= buffer_mask;
232 	}
233 
234 	plugin_data->read_ptr.all = read_ptr.all;
235 	plugin_data->write_ptr = write_ptr;
236 }
237 
swh_init()238 static void __attribute__((constructor)) swh_init() {
239 	char **port_names;
240 	LADSPA_PortDescriptor *port_descriptors;
241 	LADSPA_PortRangeHint *port_range_hints;
242 
243 #ifdef ENABLE_NLS
244 #define D_(s) dgettext(PACKAGE, s)
245 	bindtextdomain(PACKAGE, PACKAGE_LOCALE_DIR);
246 #else
247 #define D_(s) (s)
248 #endif
249 
250 
251 	rateShifterDescriptor =
252 	 (LADSPA_Descriptor *)malloc(sizeof(LADSPA_Descriptor));
253 
254 	if (rateShifterDescriptor) {
255 		rateShifterDescriptor->UniqueID = 1417;
256 		rateShifterDescriptor->Label = "rateShifter";
257 		rateShifterDescriptor->Properties =
258 		 LADSPA_PROPERTY_HARD_RT_CAPABLE;
259 		rateShifterDescriptor->Name =
260 		 D_("Rate shifter");
261 		rateShifterDescriptor->Maker =
262 		 "Steve Harris <steve@plugin.org.uk>";
263 		rateShifterDescriptor->Copyright =
264 		 "GPL";
265 		rateShifterDescriptor->PortCount = 3;
266 
267 		port_descriptors = (LADSPA_PortDescriptor *)calloc(3,
268 		 sizeof(LADSPA_PortDescriptor));
269 		rateShifterDescriptor->PortDescriptors =
270 		 (const LADSPA_PortDescriptor *)port_descriptors;
271 
272 		port_range_hints = (LADSPA_PortRangeHint *)calloc(3,
273 		 sizeof(LADSPA_PortRangeHint));
274 		rateShifterDescriptor->PortRangeHints =
275 		 (const LADSPA_PortRangeHint *)port_range_hints;
276 
277 		port_names = (char **)calloc(3, sizeof(char*));
278 		rateShifterDescriptor->PortNames =
279 		 (const char **)port_names;
280 
281 		/* Parameters for Rate */
282 		port_descriptors[RATESHIFTER_RATE] =
283 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
284 		port_names[RATESHIFTER_RATE] =
285 		 D_("Rate");
286 		port_range_hints[RATESHIFTER_RATE].HintDescriptor =
287 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_DEFAULT_1;
288 		port_range_hints[RATESHIFTER_RATE].LowerBound = -4;
289 		port_range_hints[RATESHIFTER_RATE].UpperBound = 4;
290 
291 		/* Parameters for Input */
292 		port_descriptors[RATESHIFTER_INPUT] =
293 		 LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
294 		port_names[RATESHIFTER_INPUT] =
295 		 D_("Input");
296 		port_range_hints[RATESHIFTER_INPUT].HintDescriptor = 0;
297 
298 		/* Parameters for Output */
299 		port_descriptors[RATESHIFTER_OUTPUT] =
300 		 LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
301 		port_names[RATESHIFTER_OUTPUT] =
302 		 D_("Output");
303 		port_range_hints[RATESHIFTER_OUTPUT].HintDescriptor = 0;
304 
305 		rateShifterDescriptor->activate = activateRateShifter;
306 		rateShifterDescriptor->cleanup = cleanupRateShifter;
307 		rateShifterDescriptor->connect_port = connectPortRateShifter;
308 		rateShifterDescriptor->deactivate = NULL;
309 		rateShifterDescriptor->instantiate = instantiateRateShifter;
310 		rateShifterDescriptor->run = runRateShifter;
311 		rateShifterDescriptor->run_adding = runAddingRateShifter;
312 		rateShifterDescriptor->set_run_adding_gain = setRunAddingGainRateShifter;
313 	}
314 }
315 
swh_fini()316 static void __attribute__((destructor)) swh_fini() {
317 	if (rateShifterDescriptor) {
318 		free((LADSPA_PortDescriptor *)rateShifterDescriptor->PortDescriptors);
319 		free((char **)rateShifterDescriptor->PortNames);
320 		free((LADSPA_PortRangeHint *)rateShifterDescriptor->PortRangeHints);
321 		free(rateShifterDescriptor);
322 	}
323 	rateShifterDescriptor = NULL;
324 
325 }
326