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 9 "pitch_scale_1194.xml"
29 
30 #include "util/pitchscale.h"
31 
32 #define FRAME_LENGTH 4096
33 #define OVER_SAMP    16
34 
35 #define PITCHSCALEHQ_MULT              0
36 #define PITCHSCALEHQ_INPUT             1
37 #define PITCHSCALEHQ_OUTPUT            2
38 #define PITCHSCALEHQ_LATENCY           3
39 
40 static LADSPA_Descriptor *pitchScaleHQDescriptor = NULL;
41 
42 typedef struct {
43 	LADSPA_Data *mult;
44 	LADSPA_Data *input;
45 	LADSPA_Data *output;
46 	LADSPA_Data *latency;
47 	sbuffers *   buffers;
48 	long         sample_rate;
49 	LADSPA_Data run_adding_gain;
50 } PitchScaleHQ;
51 
52 _WINDOWS_DLL_EXPORT_
ladspa_descriptor(unsigned long index)53 const LADSPA_Descriptor *ladspa_descriptor(unsigned long index) {
54 
55 #ifdef WIN32
56 	if (bIsFirstTime) {
57 		swh_init();
58 		bIsFirstTime = 0;
59 	}
60 #endif
61 	switch (index) {
62 	case 0:
63 		return pitchScaleHQDescriptor;
64 	default:
65 		return NULL;
66 	}
67 }
68 
activatePitchScaleHQ(LADSPA_Handle instance)69 static void activatePitchScaleHQ(LADSPA_Handle instance) {
70 	PitchScaleHQ *plugin_data = (PitchScaleHQ *)instance;
71 	sbuffers *buffers = plugin_data->buffers;
72 	long sample_rate = plugin_data->sample_rate;
73 #line 57 "pitch_scale_1194.xml"
74 	memset(buffers->gInFIFO, 0, FRAME_LENGTH*sizeof(float));
75 	memset(buffers->gOutFIFO, 0, FRAME_LENGTH*sizeof(float));
76 	memset(buffers->gLastPhase, 0, FRAME_LENGTH*sizeof(float)/2);
77 	memset(buffers->gSumPhase, 0, FRAME_LENGTH*sizeof(float)/2);
78 	memset(buffers->gOutputAccum, 0, 2*FRAME_LENGTH*sizeof(float));
79 	memset(buffers->gAnaFreq, 0, FRAME_LENGTH*sizeof(float));
80 	memset(buffers->gAnaMagn, 0, FRAME_LENGTH*sizeof(float));
81 	buffers->gRover = 0;
82 	pitch_scale(buffers, 1.0, FRAME_LENGTH, 16, FRAME_LENGTH, sample_rate, buffers->gInFIFO, buffers->gOutFIFO, 0, 0.0f);
83 	plugin_data->buffers = buffers;
84 	plugin_data->sample_rate = sample_rate;
85 
86 }
87 
cleanupPitchScaleHQ(LADSPA_Handle instance)88 static void cleanupPitchScaleHQ(LADSPA_Handle instance) {
89 #line 69 "pitch_scale_1194.xml"
90 	PitchScaleHQ *plugin_data = (PitchScaleHQ *)instance;
91 	free (plugin_data->buffers->gInFIFO);
92 	free (plugin_data->buffers->gOutFIFO);
93 	free (plugin_data->buffers->gLastPhase);
94 	free (plugin_data->buffers->gSumPhase);
95 	free (plugin_data->buffers->gOutputAccum);
96 	free (plugin_data->buffers->gAnaFreq);
97 	free (plugin_data->buffers->gAnaMagn);
98 	free (plugin_data->buffers->gSynFreq);
99 	free (plugin_data->buffers->gSynMagn);
100 	free (plugin_data->buffers->gWindow);
101 	free (plugin_data->buffers);
102 	free(instance);
103 }
104 
connectPortPitchScaleHQ(LADSPA_Handle instance,unsigned long port,LADSPA_Data * data)105 static void connectPortPitchScaleHQ(
106  LADSPA_Handle instance,
107  unsigned long port,
108  LADSPA_Data *data) {
109 	PitchScaleHQ *plugin;
110 
111 	plugin = (PitchScaleHQ *)instance;
112 	switch (port) {
113 	case PITCHSCALEHQ_MULT:
114 		plugin->mult = data;
115 		break;
116 	case PITCHSCALEHQ_INPUT:
117 		plugin->input = data;
118 		break;
119 	case PITCHSCALEHQ_OUTPUT:
120 		plugin->output = data;
121 		break;
122 	case PITCHSCALEHQ_LATENCY:
123 		plugin->latency = data;
124 		break;
125 	}
126 }
127 
instantiatePitchScaleHQ(const LADSPA_Descriptor * descriptor,unsigned long s_rate)128 static LADSPA_Handle instantiatePitchScaleHQ(
129  const LADSPA_Descriptor *descriptor,
130  unsigned long s_rate) {
131 	PitchScaleHQ *plugin_data = (PitchScaleHQ *)calloc(1, sizeof(PitchScaleHQ));
132 	sbuffers *buffers = NULL;
133 	long sample_rate;
134 
135 #line 29 "pitch_scale_1194.xml"
136 	int i;
137 	float arg;
138 
139 	buffers = malloc(sizeof(sbuffers));
140 	sample_rate = s_rate;
141 	buffers->gInFIFO = malloc(FRAME_LENGTH * sizeof(float));
142 	buffers->gOutFIFO = malloc(FRAME_LENGTH * sizeof(float));
143 	buffers->gLastPhase = malloc(FRAME_LENGTH * sizeof(float));
144 	buffers->gSumPhase = malloc(FRAME_LENGTH * sizeof(float));
145 	buffers->gOutputAccum = malloc(2*FRAME_LENGTH * sizeof(float));
146 	buffers->gAnaFreq = malloc(FRAME_LENGTH * sizeof(float));
147 	buffers->gAnaMagn = malloc(FRAME_LENGTH * sizeof(float));
148 	buffers->gSynFreq = malloc(FRAME_LENGTH * sizeof(float));
149 	buffers->gSynMagn = malloc(FRAME_LENGTH * sizeof(float));
150 	buffers->gWindow = malloc(FRAME_LENGTH * sizeof(float));
151 
152 	arg = 2.0f * M_PI / (float)(FRAME_LENGTH-1);
153 	for (i=0; i < FRAME_LENGTH; i++) {
154 	        // Blackman-Harris
155 	        buffers->gWindow[i] =  0.35875f - 0.48829f * cos(arg * (float)i) + 0.14128f * cos(2.0f * arg * (float)i) - 0.01168f * cos(3.0f * arg * (float)i);
156 	        // Gain correction
157 	        buffers->gWindow[i] *= 0.761f;
158 
159 	}
160 
161 	plugin_data->buffers = buffers;
162 	plugin_data->sample_rate = sample_rate;
163 
164 	return (LADSPA_Handle)plugin_data;
165 }
166 
167 #undef buffer_write
168 #undef RUN_ADDING
169 #undef RUN_REPLACING
170 
171 #define buffer_write(b, v) (b = v)
172 #define RUN_ADDING    0
173 #define RUN_REPLACING 1
174 
runPitchScaleHQ(LADSPA_Handle instance,unsigned long sample_count)175 static void runPitchScaleHQ(LADSPA_Handle instance, unsigned long sample_count) {
176 	PitchScaleHQ *plugin_data = (PitchScaleHQ *)instance;
177 	LADSPA_Data run_adding_gain = plugin_data->run_adding_gain;
178 
179 	/* Pitch co-efficient (float value) */
180 	const LADSPA_Data mult = *(plugin_data->mult);
181 
182 	/* Input (array of floats of length sample_count) */
183 	const LADSPA_Data * const input = plugin_data->input;
184 
185 	/* Output (array of floats of length sample_count) */
186 	LADSPA_Data * const output = plugin_data->output;
187 	sbuffers * buffers = plugin_data->buffers;
188 	long sample_rate = plugin_data->sample_rate;
189 
190 #line 23 "pitch_scale_1194.xml"
191 	pitch_scale(buffers, mult, FRAME_LENGTH, OVER_SAMP, sample_count, sample_rate, input, output, RUN_ADDING, run_adding_gain);
192 	*(plugin_data->latency) = FRAME_LENGTH - (FRAME_LENGTH
193 	                                / OVER_SAMP);
194 }
195 #undef buffer_write
196 #undef RUN_ADDING
197 #undef RUN_REPLACING
198 
199 #define buffer_write(b, v) (b += (v) * run_adding_gain)
200 #define RUN_ADDING    1
201 #define RUN_REPLACING 0
202 
setRunAddingGainPitchScaleHQ(LADSPA_Handle instance,LADSPA_Data gain)203 static void setRunAddingGainPitchScaleHQ(LADSPA_Handle instance, LADSPA_Data gain) {
204 	((PitchScaleHQ *)instance)->run_adding_gain = gain;
205 }
206 
runAddingPitchScaleHQ(LADSPA_Handle instance,unsigned long sample_count)207 static void runAddingPitchScaleHQ(LADSPA_Handle instance, unsigned long sample_count) {
208 	PitchScaleHQ *plugin_data = (PitchScaleHQ *)instance;
209 	LADSPA_Data run_adding_gain = plugin_data->run_adding_gain;
210 
211 	/* Pitch co-efficient (float value) */
212 	const LADSPA_Data mult = *(plugin_data->mult);
213 
214 	/* Input (array of floats of length sample_count) */
215 	const LADSPA_Data * const input = plugin_data->input;
216 
217 	/* Output (array of floats of length sample_count) */
218 	LADSPA_Data * const output = plugin_data->output;
219 	sbuffers * buffers = plugin_data->buffers;
220 	long sample_rate = plugin_data->sample_rate;
221 
222 #line 23 "pitch_scale_1194.xml"
223 	pitch_scale(buffers, mult, FRAME_LENGTH, OVER_SAMP, sample_count, sample_rate, input, output, RUN_ADDING, run_adding_gain);
224 	*(plugin_data->latency) = FRAME_LENGTH - (FRAME_LENGTH
225 	                                / OVER_SAMP);
226 }
227 
swh_init()228 static void __attribute__((constructor)) swh_init() {
229 	char **port_names;
230 	LADSPA_PortDescriptor *port_descriptors;
231 	LADSPA_PortRangeHint *port_range_hints;
232 
233 #ifdef ENABLE_NLS
234 #define D_(s) dgettext(PACKAGE, s)
235 	bindtextdomain(PACKAGE, PACKAGE_LOCALE_DIR);
236 #else
237 #define D_(s) (s)
238 #endif
239 
240 
241 	pitchScaleHQDescriptor =
242 	 (LADSPA_Descriptor *)malloc(sizeof(LADSPA_Descriptor));
243 
244 	if (pitchScaleHQDescriptor) {
245 		pitchScaleHQDescriptor->UniqueID = 1194;
246 		pitchScaleHQDescriptor->Label = "pitchScaleHQ";
247 		pitchScaleHQDescriptor->Properties =
248 		 LADSPA_PROPERTY_HARD_RT_CAPABLE;
249 		pitchScaleHQDescriptor->Name =
250 		 D_("Higher Quality Pitch Scaler");
251 		pitchScaleHQDescriptor->Maker =
252 		 "Steve Harris <steve@plugin.org.uk>";
253 		pitchScaleHQDescriptor->Copyright =
254 		 "GPL";
255 		pitchScaleHQDescriptor->PortCount = 4;
256 
257 		port_descriptors = (LADSPA_PortDescriptor *)calloc(4,
258 		 sizeof(LADSPA_PortDescriptor));
259 		pitchScaleHQDescriptor->PortDescriptors =
260 		 (const LADSPA_PortDescriptor *)port_descriptors;
261 
262 		port_range_hints = (LADSPA_PortRangeHint *)calloc(4,
263 		 sizeof(LADSPA_PortRangeHint));
264 		pitchScaleHQDescriptor->PortRangeHints =
265 		 (const LADSPA_PortRangeHint *)port_range_hints;
266 
267 		port_names = (char **)calloc(4, sizeof(char*));
268 		pitchScaleHQDescriptor->PortNames =
269 		 (const char **)port_names;
270 
271 		/* Parameters for Pitch co-efficient */
272 		port_descriptors[PITCHSCALEHQ_MULT] =
273 		 LADSPA_PORT_INPUT | LADSPA_PORT_CONTROL;
274 		port_names[PITCHSCALEHQ_MULT] =
275 		 D_("Pitch co-efficient");
276 		port_range_hints[PITCHSCALEHQ_MULT].HintDescriptor =
277 		 LADSPA_HINT_BOUNDED_BELOW | LADSPA_HINT_BOUNDED_ABOVE | LADSPA_HINT_DEFAULT_1;
278 		port_range_hints[PITCHSCALEHQ_MULT].LowerBound = 0.5;
279 		port_range_hints[PITCHSCALEHQ_MULT].UpperBound = 2;
280 
281 		/* Parameters for Input */
282 		port_descriptors[PITCHSCALEHQ_INPUT] =
283 		 LADSPA_PORT_INPUT | LADSPA_PORT_AUDIO;
284 		port_names[PITCHSCALEHQ_INPUT] =
285 		 D_("Input");
286 		port_range_hints[PITCHSCALEHQ_INPUT].HintDescriptor = 0;
287 
288 		/* Parameters for Output */
289 		port_descriptors[PITCHSCALEHQ_OUTPUT] =
290 		 LADSPA_PORT_OUTPUT | LADSPA_PORT_AUDIO;
291 		port_names[PITCHSCALEHQ_OUTPUT] =
292 		 D_("Output");
293 		port_range_hints[PITCHSCALEHQ_OUTPUT].HintDescriptor = 0;
294 
295 		/* Parameters for latency */
296 		port_descriptors[PITCHSCALEHQ_LATENCY] =
297 		 LADSPA_PORT_OUTPUT | LADSPA_PORT_CONTROL;
298 		port_names[PITCHSCALEHQ_LATENCY] =
299 		 D_("latency");
300 		port_range_hints[PITCHSCALEHQ_LATENCY].HintDescriptor = 0;
301 
302 		pitchScaleHQDescriptor->activate = activatePitchScaleHQ;
303 		pitchScaleHQDescriptor->cleanup = cleanupPitchScaleHQ;
304 		pitchScaleHQDescriptor->connect_port = connectPortPitchScaleHQ;
305 		pitchScaleHQDescriptor->deactivate = NULL;
306 		pitchScaleHQDescriptor->instantiate = instantiatePitchScaleHQ;
307 		pitchScaleHQDescriptor->run = runPitchScaleHQ;
308 		pitchScaleHQDescriptor->run_adding = runAddingPitchScaleHQ;
309 		pitchScaleHQDescriptor->set_run_adding_gain = setRunAddingGainPitchScaleHQ;
310 	}
311 }
312 
swh_fini()313 static void __attribute__((destructor)) swh_fini() {
314 	if (pitchScaleHQDescriptor) {
315 		free((LADSPA_PortDescriptor *)pitchScaleHQDescriptor->PortDescriptors);
316 		free((char **)pitchScaleHQDescriptor->PortNames);
317 		free((LADSPA_PortRangeHint *)pitchScaleHQDescriptor->PortRangeHints);
318 		free(pitchScaleHQDescriptor);
319 	}
320 	pitchScaleHQDescriptor = NULL;
321 
322 }
323