Apply ldflags-order part of ldflags_order patch from Alberto Villa.
[melted] / src / modules / jackrack / plugin.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 #include <stdio.h>
27 #include <stdlib.h>
28 #include <ladspa.h>
29 #include <dlfcn.h>
30 #include <ctype.h>
31
32 #include <glib.h>
33
34 #include "plugin.h"
35 #include "jack_rack.h"
36 #include "process.h"
37
38 #define CONTROL_FIFO_SIZE 128
39
40
41
42 /* swap over the jack ports in two plugins */
43 static void
44 plugin_swap_aux_ports (plugin_t * plugin, plugin_t * other)
45 {
46 guint copy;
47 jack_port_t ** aux_ports_tmp;
48
49 for (copy = 0; copy < plugin->copies; copy++)
50 {
51 aux_ports_tmp = other->holders[copy].aux_ports;
52 other->holders[copy].aux_ports = plugin->holders[copy].aux_ports;
53 plugin->holders[copy].aux_ports = aux_ports_tmp;
54 }
55 }
56
57 /** connect up the ladspa instance's input buffers to the previous
58 plugin's audio memory. make sure to check that plugin->prev
59 exists. */
60 void
61 plugin_connect_input_ports (plugin_t * plugin, LADSPA_Data ** inputs)
62 {
63 gint copy;
64 unsigned long channel;
65 unsigned long rack_channel;
66
67 if (!plugin || !inputs)
68 return;
69
70 rack_channel = 0;
71 for (copy = 0; copy < plugin->copies; copy++)
72 {
73 for (channel = 0; channel < plugin->desc->channels; channel++)
74 {
75 plugin->descriptor->
76 connect_port (plugin->holders[copy].instance,
77 plugin->desc->audio_input_port_indicies[channel],
78 inputs[rack_channel]);
79 rack_channel++;
80 }
81 }
82
83 plugin->audio_input_memory = inputs;
84 }
85
86 /** connect up a plugin's output ports to its own audio_output_memory output memory */
87 void
88 plugin_connect_output_ports (plugin_t * plugin)
89 {
90 gint copy;
91 unsigned long channel;
92 unsigned long rack_channel = 0;
93
94 if (!plugin)
95 return;
96
97
98 for (copy = 0; copy < plugin->copies; copy++)
99 {
100 for (channel = 0; channel < plugin->desc->channels; channel++)
101 {
102 plugin->descriptor->
103 connect_port (plugin->holders[copy].instance,
104 plugin->desc->audio_output_port_indicies[channel],
105 plugin->audio_output_memory[rack_channel]);
106 rack_channel++;
107 }
108 }
109 }
110
111 void
112 process_add_plugin (process_info_t * procinfo, plugin_t * plugin)
113 {
114
115 /* sort out list pointers */
116 plugin->next = NULL;
117 plugin->prev = procinfo->chain_end;
118
119 if (procinfo->chain_end)
120 procinfo->chain_end->next = plugin;
121 else
122 procinfo->chain = plugin;
123
124 procinfo->chain_end = plugin;
125
126 }
127
128
129 /** remove a plugin from the chain */
130 plugin_t *
131 process_remove_plugin (process_info_t * procinfo, plugin_t *plugin)
132 {
133 /* sort out chain pointers */
134 if (plugin->prev)
135 plugin->prev->next = plugin->next;
136 else
137 procinfo->chain = plugin->next;
138
139 if (plugin->next)
140 plugin->next->prev = plugin->prev;
141 else
142 procinfo->chain_end = plugin->prev;
143
144 /* sort out the aux ports */
145 if (procinfo->jack_client && plugin->desc->aux_channels > 0)
146 {
147 plugin_t * other;
148
149 for (other = plugin->next; other; other = other->next)
150 if (other->desc->id == plugin->desc->id)
151 plugin_swap_aux_ports (plugin, other);
152 }
153
154 return plugin;
155 }
156
157 /** enable/disable a plugin */
158 void
159 process_ablise_plugin (process_info_t * procinfo, plugin_t *plugin, gboolean enable)
160 {
161 plugin->enabled = enable;
162 }
163
164 /** enable/disable a plugin */
165 void
166 process_ablise_plugin_wet_dry (process_info_t * procinfo, plugin_t *plugin, gboolean enable)
167 {
168 plugin->wet_dry_enabled = enable;
169 }
170
171 /** move a plugin up or down one place in the chain */
172 void
173 process_move_plugin (process_info_t * procinfo, plugin_t *plugin, gint up)
174 {
175 /* other plugins in the chain */
176 plugin_t *pp = NULL, *p, *n, *nn = NULL;
177
178 /* note that we should never recieve an illogical move request
179 ie, there will always be at least 1 plugin before for an up
180 request or 1 plugin after for a down request */
181
182 /* these are pointers to the plugins surrounding the specified one:
183 { pp, p, plugin, n, nn } which makes things much clearer than
184 tptr, tptr2 etc */
185 p = plugin->prev;
186 if (p) pp = p->prev;
187 n = plugin->next;
188 if (n) nn = n->next;
189
190 if (up)
191 {
192 if (!p)
193 return;
194
195 if (pp)
196 pp->next = plugin;
197 else
198 procinfo->chain = plugin;
199
200 p->next = n;
201 p->prev = plugin;
202
203 plugin->prev = pp;
204 plugin->next = p;
205
206 if (n)
207 n->prev = p;
208 else
209 procinfo->chain_end = p;
210
211 }
212 else
213 {
214 if (!n)
215 return;
216
217 if (p)
218 p->next = n;
219 else
220 procinfo->chain = n;
221
222 n->prev = p;
223 n->next = plugin;
224
225 plugin->prev = n;
226 plugin->next = nn;
227
228 if (nn)
229 nn->prev = plugin;
230 else
231 procinfo->chain_end = plugin;
232 }
233
234 if (procinfo->jack_client && plugin->desc->aux_channels > 0)
235 {
236 plugin_t * other;
237 other = up ? plugin->next : plugin->prev;
238
239 /* swap around the jack ports */
240 if (other->desc->id == plugin->desc->id)
241 plugin_swap_aux_ports (plugin, other);
242 }
243 }
244
245 /** exchange an existing plugin for a newly created one */
246 plugin_t *
247 process_change_plugin (process_info_t * procinfo,
248 plugin_t *plugin, plugin_t * new_plugin)
249 {
250 new_plugin->next = plugin->next;
251 new_plugin->prev = plugin->prev;
252
253 if (plugin->prev)
254 plugin->prev->next = new_plugin;
255 else
256 procinfo->chain = new_plugin;
257
258 if (plugin->next)
259 plugin->next->prev = new_plugin;
260 else
261 procinfo->chain_end = new_plugin;
262
263 /* sort out the aux ports */
264 if (procinfo->jack_client && plugin->desc->aux_channels > 0)
265 {
266 plugin_t * other;
267
268 for (other = plugin->next; other; other = other->next)
269 if (other->desc->id == plugin->desc->id)
270 plugin_swap_aux_ports (plugin, other);
271 }
272
273 return plugin;
274 }
275
276
277 /******************************************
278 ************* non RT stuff ***************
279 ******************************************/
280
281
282 static int
283 plugin_open_plugin (plugin_desc_t * desc,
284 void ** dl_handle_ptr,
285 const LADSPA_Descriptor ** descriptor_ptr)
286 {
287 void * dl_handle;
288 const char * dlerr;
289 LADSPA_Descriptor_Function get_descriptor;
290
291 /* open the object file */
292 dl_handle = dlopen (desc->object_file, RTLD_NOW|RTLD_GLOBAL);
293 if (!dl_handle)
294 {
295 fprintf (stderr, "%s: error opening shared object file '%s': %s\n",
296 __FUNCTION__, desc->object_file, dlerror());
297 return 1;
298 }
299
300
301 /* get the get_descriptor function */
302 dlerror (); /* clear the error report */
303
304 get_descriptor = (LADSPA_Descriptor_Function)
305 dlsym (dl_handle, "ladspa_descriptor");
306
307 dlerr = dlerror();
308 if (dlerr)
309 {
310 fprintf (stderr, "%s: error finding descriptor symbol in object file '%s': %s\n",
311 __FUNCTION__, desc->object_file, dlerr);
312 dlclose (dl_handle);
313 return 1;
314 }
315
316 *descriptor_ptr = get_descriptor (desc->index);
317 *dl_handle_ptr = dl_handle;
318
319 return 0;
320 }
321
322 static int
323 plugin_instantiate (const LADSPA_Descriptor * descriptor,
324 unsigned long plugin_index,
325 gint copies,
326 LADSPA_Handle * instances)
327 {
328 gint i;
329
330 for (i = 0; i < copies; i++)
331 {
332 instances[i] = descriptor->instantiate (descriptor, sample_rate);
333
334 if (!instances[i])
335 {
336 unsigned long d;
337
338 for (d = 0; d < i; d++)
339 descriptor->cleanup (instances[d]);
340
341 return 1;
342 }
343 }
344
345 return 0;
346 }
347
348 static void
349 plugin_create_aux_ports (plugin_t * plugin, guint copy, jack_rack_t * jack_rack)
350 {
351 plugin_desc_t * desc;
352 // plugin_slot_t * slot;
353 unsigned long aux_channel = 1;
354 unsigned long plugin_index = 1;
355 unsigned long i;
356 char port_name[64];
357 char * plugin_name;
358 char * ptr;
359 // GList * list;
360 ladspa_holder_t * holder;
361
362 desc = plugin->desc;
363 holder = plugin->holders + copy;
364
365 holder->aux_ports = g_malloc (sizeof (jack_port_t *) * desc->aux_channels);
366
367 /* make the plugin name jack worthy */
368 ptr = plugin_name = g_strndup (plugin->desc->name, 7);
369 while (*ptr != '\0')
370 {
371 if (*ptr == ' ')
372 *ptr = '_';
373 else
374 *ptr = tolower (*ptr);
375
376 ptr++;
377 }
378
379 /*
380 for (list = jack_rack->slots; list; list = g_list_next (list))
381 {
382 slot = (plugin_slot_t *) list->data;
383
384 if (slot->plugin->desc->id == plugin->desc->id)
385 plugin_index++;
386 }
387 */
388
389 for (i = 0; i < desc->aux_channels; i++, aux_channel++)
390 {
391 sprintf (port_name, "%s_%ld-%d_%c%ld",
392 plugin_name,
393 plugin_index,
394 copy + 1,
395 desc->aux_are_input ? 'i' : 'o',
396 aux_channel);
397
398 holder->aux_ports[i] =
399 jack_port_register (jack_rack->procinfo->jack_client,
400 port_name,
401 JACK_DEFAULT_AUDIO_TYPE,
402 desc->aux_are_input ? JackPortIsInput : JackPortIsOutput,
403 0);
404
405 if (!holder->aux_ports[i])
406 {
407 fprintf (stderr, "Could not register jack port '%s'; aborting\n", port_name);
408 abort ();
409 }
410 }
411
412 g_free (plugin_name);
413 }
414
415 static LADSPA_Data unused_control_port_output;
416
417 static void
418 plugin_init_holder (plugin_t * plugin,
419 guint copy,
420 LADSPA_Handle instance,
421 jack_rack_t * jack_rack)
422 {
423 unsigned long i;
424 plugin_desc_t * desc;
425 ladspa_holder_t * holder;
426
427 desc = plugin->desc;
428 holder = plugin->holders + copy;
429
430 holder->instance = instance;
431
432 if (desc->control_port_count > 0)
433 {
434 holder->ui_control_fifos = g_malloc (sizeof (lff_t) * desc->control_port_count);
435 holder->control_memory = g_malloc (sizeof (LADSPA_Data) * desc->control_port_count);
436 }
437 else
438 {
439 holder->ui_control_fifos = NULL;
440 holder->control_memory = NULL;
441 }
442
443 for (i = 0; i < desc->control_port_count; i++)
444 {
445 lff_init (holder->ui_control_fifos + i, CONTROL_FIFO_SIZE, sizeof (LADSPA_Data));
446 holder->control_memory[i] =
447 plugin_desc_get_default_control_value (desc, desc->control_port_indicies[i], sample_rate);
448
449 plugin->descriptor->
450 connect_port (instance, desc->control_port_indicies[i], holder->control_memory + i);
451 }
452
453 for (i = 0; i < desc->port_count; i++)
454 {
455 if (!LADSPA_IS_PORT_CONTROL (desc->port_descriptors[i]))
456 continue;
457
458 if (LADSPA_IS_PORT_OUTPUT (desc->port_descriptors[i]))
459 plugin->descriptor-> connect_port (instance, i, &unused_control_port_output);
460 }
461
462 if (jack_rack->procinfo->jack_client && plugin->desc->aux_channels > 0)
463 plugin_create_aux_ports (plugin, copy, jack_rack);
464
465 if (plugin->descriptor->activate)
466 plugin->descriptor->activate (instance);
467 }
468
469
470 plugin_t *
471 plugin_new (plugin_desc_t * desc, jack_rack_t * jack_rack)
472 {
473 void * dl_handle;
474 const LADSPA_Descriptor * descriptor;
475 LADSPA_Handle * instances;
476 gint copies;
477 unsigned long i;
478 int err;
479 plugin_t * plugin;
480
481 /* open the plugin */
482 err = plugin_open_plugin (desc, &dl_handle, &descriptor);
483 if (err)
484 return NULL;
485
486 /* create the instances */
487 copies = plugin_desc_get_copies (desc, jack_rack->channels);
488 instances = g_malloc (sizeof (LADSPA_Handle) * copies);
489
490 err = plugin_instantiate (descriptor, desc->index, copies, instances);
491 if (err)
492 {
493 g_free (instances);
494 dlclose (dl_handle);
495 return NULL;
496 }
497
498
499 plugin = g_malloc (sizeof (plugin_t));
500
501 plugin->descriptor = descriptor;
502 plugin->dl_handle = dl_handle;
503 plugin->desc = desc;
504 plugin->copies = copies;
505 plugin->enabled = FALSE;
506 plugin->next = NULL;
507 plugin->prev = NULL;
508 plugin->wet_dry_enabled = FALSE;
509 plugin->jack_rack = jack_rack;
510
511 /* create audio memory and wet/dry stuff */
512 plugin->audio_output_memory = g_malloc (sizeof (LADSPA_Data *) * jack_rack->channels);
513 plugin->wet_dry_fifos = g_malloc (sizeof (lff_t) * jack_rack->channels);
514 plugin->wet_dry_values = g_malloc (sizeof (LADSPA_Data) * jack_rack->channels);
515
516 for (i = 0; i < jack_rack->channels; i++)
517 {
518 plugin->audio_output_memory[i] = g_malloc (sizeof (LADSPA_Data) * buffer_size);
519 lff_init (plugin->wet_dry_fifos + i, CONTROL_FIFO_SIZE, sizeof (LADSPA_Data));
520 plugin->wet_dry_values[i] = 1.0;
521 }
522
523 /* create holders and fill them out */
524 plugin->holders = g_malloc (sizeof (ladspa_holder_t) * copies);
525 for (i = 0; i < copies; i++)
526 plugin_init_holder (plugin, i, instances[i], jack_rack);
527
528 return plugin;
529 }
530
531
532 void
533 plugin_destroy (plugin_t * plugin)
534 {
535 unsigned long i, j;
536 int err;
537
538 /* destroy holders */
539 for (i = 0; i < plugin->copies; i++)
540 {
541 if (plugin->descriptor->deactivate)
542 plugin->descriptor->deactivate (plugin->holders[i].instance);
543
544 /* if (plugin->descriptor->cleanup)
545 plugin->descriptor->cleanup (plugin->holders[i].instance); */
546
547 if (plugin->desc->control_port_count > 0)
548 {
549 for (j = 0; j < plugin->desc->control_port_count; j++)
550 {
551 lff_free (plugin->holders[i].ui_control_fifos + j);
552 }
553 g_free (plugin->holders[i].ui_control_fifos);
554 g_free (plugin->holders[i].control_memory);
555 }
556
557 /* aux ports */
558 if (plugin->jack_rack->procinfo->jack_client && plugin->desc->aux_channels > 0)
559 {
560 for (j = 0; j < plugin->desc->aux_channels; j++)
561 {
562 err = jack_port_unregister (plugin->jack_rack->procinfo->jack_client,
563 plugin->holders[i].aux_ports[j]);
564
565 if (err)
566 fprintf (stderr, "%s: could not unregister jack port\n", __FUNCTION__);
567 }
568
569 g_free (plugin->holders[i].aux_ports);
570 }
571 }
572
573 g_free (plugin->holders);
574
575 for (i = 0; i < plugin->jack_rack->channels; i++)
576 {
577 g_free (plugin->audio_output_memory[i]);
578 lff_free (plugin->wet_dry_fifos + i);
579 }
580
581 g_free (plugin->audio_output_memory);
582 g_free (plugin->wet_dry_fifos);
583 g_free (plugin->wet_dry_values);
584
585 err = dlclose (plugin->dl_handle);
586 if (err)
587 {
588 fprintf (stderr, "%s: error closing shared object '%s': %s\n",
589 __FUNCTION__, plugin->desc->object_file, dlerror ());
590 }
591
592 g_free (plugin);
593 }
594
595
596 /* EOF */