Cleanup copyrights and attributions, and move Jean-Baptiste's services to a new kdenl...
[melted] / src / modules / jackrack / plugin_settings.c
1 /*
2 * JACK Rack
3 *
4 * Original:
5 * Copyright (C) Robert Ham 2002, 2003 (node@users.sourceforge.net)
6 *
7 * Modification for MLT:
8 * Copyright (C) 2004 Ushodaya Enterprises Limited
9 * Author: Dan Dennedy <dan@dennedy.org>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 */
25
26 #define _GNU_SOURCE
27
28 #include <math.h>
29
30 #include "plugin_settings.h"
31
32
33 static void
34 settings_set_to_default (settings_t * settings, guint32 sample_rate)
35 {
36 unsigned long control;
37 guint copy;
38 LADSPA_Data value;
39
40 for (control = 0; control < settings->desc->control_port_count; control++)
41 {
42 value = plugin_desc_get_default_control_value (settings->desc, control, sample_rate);
43
44 for (copy = 0; copy < settings->copies; copy++)
45 {
46 settings->control_values[copy][control] = value;
47 }
48
49 settings->locks[control] = TRUE;
50 }
51 }
52
53 settings_t *
54 settings_new (plugin_desc_t * desc, unsigned long channels, guint32 sample_rate)
55 {
56 settings_t * settings;
57 unsigned long channel;
58 guint copies;
59
60 settings = g_malloc (sizeof (settings_t));
61 copies = plugin_desc_get_copies (desc, channels);
62
63 settings->sample_rate = sample_rate;
64 settings->desc = desc;
65 settings->copies = copies;
66 settings->channels = channels;
67 settings->lock_all = TRUE;
68 settings->enabled = FALSE;
69 settings->locks = NULL;
70 settings->control_values = NULL;
71 settings->wet_dry_enabled = FALSE;
72 settings->wet_dry_locked = TRUE;
73
74 /* control settings */
75 if (desc->control_port_count > 0)
76 {
77 guint copy;
78
79 settings->locks = g_malloc (sizeof (gboolean) * desc->control_port_count);
80
81 settings->control_values = g_malloc (sizeof (LADSPA_Data *) * copies);
82 for (copy = 0; copy < copies; copy++)
83 {
84 settings->control_values[copy] = g_malloc (sizeof (LADSPA_Data) * desc->control_port_count);
85 }
86
87 settings_set_to_default (settings, sample_rate);
88 }
89
90 /* wet/dry settings */
91 settings->wet_dry_values = g_malloc (sizeof (LADSPA_Data) * channels);
92 for (channel = 0; channel < channels; channel++)
93 settings->wet_dry_values[channel] = 1.0;
94
95 return settings;
96 }
97
98 settings_t *
99 settings_dup (settings_t * other)
100 {
101 settings_t * settings;
102 plugin_desc_t * desc;
103 unsigned long channel;
104
105 settings = g_malloc (sizeof (settings_t));
106
107 settings->sample_rate = other->sample_rate;
108 settings->desc = other->desc;
109 settings->copies = settings_get_copies (other);
110 settings->channels = settings_get_channels (other);
111 settings->wet_dry_enabled = settings_get_wet_dry_enabled (other);
112 settings->wet_dry_locked = settings_get_wet_dry_locked (other);
113 settings->lock_all = settings_get_lock_all (other);
114 settings->enabled = settings_get_enabled (other);
115 settings->locks = NULL;
116 settings->control_values = NULL;
117
118 desc = other->desc;
119
120 if (desc->control_port_count > 0)
121 {
122 guint copy;
123 unsigned long control;
124
125 settings->locks = g_malloc (sizeof (gboolean) * desc->control_port_count);
126 for (control = 0; control < desc->control_port_count; control++)
127 settings_set_lock (settings, control, settings_get_lock (other, control));
128
129 settings->control_values = g_malloc (sizeof (LADSPA_Data *) * settings->copies);
130 for (copy = 0; copy < settings->copies; copy++)
131 {
132 settings->control_values[copy] = g_malloc (sizeof (LADSPA_Data) * desc->control_port_count);
133
134 for (control = 0; control < desc->control_port_count; control++)
135 {
136 settings->control_values[copy][control] = settings_get_control_value (other, copy, control);
137 }
138 }
139 }
140
141 settings->wet_dry_values = g_malloc (sizeof (LADSPA_Data) * settings->channels);
142 for (channel = 0; channel < settings->channels; channel++)
143 settings->wet_dry_values[channel] = settings_get_wet_dry_value (other, channel);
144
145 return settings;
146 }
147
148 void
149 settings_destroy (settings_t * settings)
150 {
151 if (settings->desc->control_port_count > 0)
152 {
153 guint i;
154 for (i = 0; i < settings->copies; i++)
155 g_free (settings->control_values[i]);
156
157 g_free (settings->control_values);
158 g_free (settings->locks);
159 }
160
161 g_free (settings->wet_dry_values);
162
163 g_free (settings);
164 }
165
166 static void
167 settings_set_copies (settings_t * settings, guint copies)
168 {
169 guint copy;
170 guint last_copy;
171 unsigned long control;
172
173 if (copies <= settings->copies)
174 return;
175
176 last_copy = settings->copies - 1;
177
178 settings->control_values = g_realloc (settings->control_values,
179 sizeof (LADSPA_Data *) * copies);
180
181 /* copy over the last settings to the new copies */
182 for (copy = settings->copies; copy < copies; copy++)
183 {
184 for (control = 0; control < settings->desc->control_port_count; control++)
185 {
186 settings->control_values[copy][control] =
187 settings->control_values[last_copy][control];
188 }
189 }
190
191 settings->copies = copies;
192 }
193
194 static void
195 settings_set_channels (settings_t * settings, unsigned long channels)
196 {
197 unsigned long channel;
198 LADSPA_Data last_value;
199
200 if (channels <= settings->channels)
201 return;
202
203 settings->wet_dry_values = g_realloc (settings->wet_dry_values, sizeof (LADSPA_Data) * channels);
204
205 last_value = settings->wet_dry_values[settings->channels - 1];
206
207 for (channel = settings->channels; channel < channels; channel++)
208 settings->wet_dry_values[channel] = last_value;
209
210 settings->channels = channels;
211 }
212
213 void
214 settings_set_sample_rate (settings_t * settings, guint32 sample_rate)
215 {
216 LADSPA_Data old_sample_rate;
217 LADSPA_Data new_sample_rate;
218
219 g_return_if_fail (settings != NULL);
220
221 if (settings->sample_rate == sample_rate)
222 return;
223
224 if (settings->desc->control_port_count > 0)
225 {
226 unsigned long control;
227 guint copy;
228
229 new_sample_rate = (LADSPA_Data) sample_rate;
230 old_sample_rate = (LADSPA_Data) settings->sample_rate;
231
232 for (control = 0; control < settings->desc->control_port_count; control++)
233 {
234 for (copy = 0; copy < settings->copies; copy++)
235 {
236 if (LADSPA_IS_HINT_SAMPLE_RATE (settings->desc->port_range_hints[control].HintDescriptor))
237 {
238 settings->control_values[copy][control] =
239 (settings->control_values[copy][control] / old_sample_rate) * new_sample_rate;
240 }
241 }
242 }
243 }
244
245 settings->sample_rate = sample_rate;
246 }
247
248 void
249 settings_set_control_value (settings_t * settings, guint copy, unsigned long control_index, LADSPA_Data value)
250 {
251 g_return_if_fail (settings != NULL);
252 g_return_if_fail (control_index < settings->desc->control_port_count);
253
254 if (copy >= settings->copies)
255 settings_set_copies (settings, copy + 1);
256
257 settings->control_values[copy][control_index] = value;
258 }
259
260 void
261 settings_set_lock (settings_t * settings, unsigned long control_index, gboolean locked)
262 {
263 g_return_if_fail (settings != NULL);
264 g_return_if_fail (control_index < settings->desc->control_port_count);
265
266 settings->locks[control_index] = locked;
267 }
268
269 void
270 settings_set_lock_all (settings_t * settings, gboolean lock_all)
271 {
272 g_return_if_fail (settings != NULL);
273
274 settings->lock_all = lock_all;
275 }
276
277 void
278 settings_set_enabled (settings_t * settings, gboolean enabled)
279 {
280 g_return_if_fail (settings != NULL);
281
282 settings->enabled = enabled;
283 }
284
285 void
286 settings_set_wet_dry_enabled (settings_t * settings, gboolean enabled)
287 {
288 g_return_if_fail (settings != NULL);
289
290 settings->wet_dry_enabled = enabled;
291 }
292
293 void
294 settings_set_wet_dry_locked (settings_t * settings, gboolean locked)
295 {
296 g_return_if_fail (settings != NULL);
297
298 settings->wet_dry_locked = locked;
299 }
300
301 void
302 settings_set_wet_dry_value (settings_t * settings, unsigned long channel, LADSPA_Data value)
303 {
304 g_return_if_fail (settings != NULL);
305
306 if (channel >= settings->channels)
307 settings_set_channels (settings, channel + 1);
308
309 settings->wet_dry_values[channel] = value;
310 }
311
312
313 LADSPA_Data
314 settings_get_control_value (settings_t * settings, guint copy, unsigned long control_index)
315 {
316 g_return_val_if_fail (settings != NULL, NAN);
317 g_return_val_if_fail (control_index < settings->desc->control_port_count, NAN);
318
319 if (copy >= settings->copies)
320 settings_set_copies (settings, copy - 1);
321
322 return settings->control_values[copy][control_index];
323 }
324
325 gboolean
326 settings_get_lock (const settings_t * settings, unsigned long control_index)
327 {
328 g_return_if_fail (settings != NULL);
329
330 return settings->locks[control_index];
331 }
332
333 gboolean
334 settings_get_lock_all (const settings_t * settings)
335 {
336 g_return_if_fail (settings != NULL);
337
338 return settings->lock_all;
339 }
340
341 gboolean
342 settings_get_enabled (const settings_t * settings)
343 {
344 g_return_if_fail (settings != NULL);
345
346 return settings->enabled;
347 }
348
349 guint
350 settings_get_copies (const settings_t * settings)
351 {
352 g_return_if_fail (settings != NULL);
353
354 return settings->copies;
355 }
356
357
358 unsigned long
359 settings_get_channels (const settings_t * settings)
360 {
361 g_return_if_fail (settings != NULL);
362
363 return settings->channels;
364 }
365
366 gboolean
367 settings_get_wet_dry_enabled (const settings_t * settings)
368 {
369 g_return_if_fail (settings != NULL);
370
371 return settings->wet_dry_enabled;
372 }
373
374 gboolean
375 settings_get_wet_dry_locked (const settings_t * settings)
376 {
377 g_return_if_fail (settings != NULL);
378
379 return settings->wet_dry_locked;
380 }
381
382 LADSPA_Data
383 settings_get_wet_dry_value (settings_t * settings, unsigned long channel)
384 {
385 g_return_if_fail (settings != NULL);
386
387 if (channel >= settings->channels)
388 settings_set_channels (settings, channel + 1);
389
390 return settings->wet_dry_values[channel];
391 }
392
393
394
395 /* EOF */