5 * Copyright (C) Robert Ham 2002, 2003 (node@users.sourceforge.net)
7 * Modification for MLT:
8 * Copyright (C) 2004 Ushodaya Enterprises Limited
9 * Author: Dan Dennedy <dan@dennedy.org>
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.
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.
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.
35 #include "jack_rack.h"
37 #include "framework/mlt_log.h"
39 #define CONTROL_FIFO_SIZE 128
43 /* swap over the jack ports in two plugins */
45 plugin_swap_aux_ports (plugin_t * plugin, plugin_t * other)
48 jack_port_t ** aux_ports_tmp;
50 for (copy = 0; copy < plugin->copies; copy++)
52 aux_ports_tmp = other->holders[copy].aux_ports;
53 other->holders[copy].aux_ports = plugin->holders[copy].aux_ports;
54 plugin->holders[copy].aux_ports = aux_ports_tmp;
58 /** connect up the ladspa instance's input buffers to the previous
59 plugin's audio memory. make sure to check that plugin->prev
62 plugin_connect_input_ports (plugin_t * plugin, LADSPA_Data ** inputs)
65 unsigned long channel;
66 unsigned long rack_channel;
68 if (!plugin || !inputs)
72 for (copy = 0; copy < plugin->copies; copy++)
74 for (channel = 0; channel < plugin->desc->channels; channel++)
77 connect_port (plugin->holders[copy].instance,
78 plugin->desc->audio_input_port_indicies[channel],
79 inputs[rack_channel]);
84 plugin->audio_input_memory = inputs;
87 /** connect up a plugin's output ports to its own audio_output_memory output memory */
89 plugin_connect_output_ports (plugin_t * plugin)
92 unsigned long channel;
93 unsigned long rack_channel = 0;
99 for (copy = 0; copy < plugin->copies; copy++)
101 for (channel = 0; channel < plugin->desc->channels; channel++)
104 connect_port (plugin->holders[copy].instance,
105 plugin->desc->audio_output_port_indicies[channel],
106 plugin->audio_output_memory[rack_channel]);
113 process_add_plugin (process_info_t * procinfo, plugin_t * plugin)
116 /* sort out list pointers */
118 plugin->prev = procinfo->chain_end;
120 if (procinfo->chain_end)
121 procinfo->chain_end->next = plugin;
123 procinfo->chain = plugin;
125 procinfo->chain_end = plugin;
130 /** remove a plugin from the chain */
132 process_remove_plugin (process_info_t * procinfo, plugin_t *plugin)
134 /* sort out chain pointers */
136 plugin->prev->next = plugin->next;
138 procinfo->chain = plugin->next;
141 plugin->next->prev = plugin->prev;
143 procinfo->chain_end = plugin->prev;
145 /* sort out the aux ports */
146 if (procinfo->jack_client && plugin->desc->aux_channels > 0)
150 for (other = plugin->next; other; other = other->next)
151 if (other->desc->id == plugin->desc->id)
152 plugin_swap_aux_ports (plugin, other);
158 /** enable/disable a plugin */
160 process_ablise_plugin (process_info_t * procinfo, plugin_t *plugin, gboolean enable)
162 plugin->enabled = enable;
165 /** enable/disable a plugin */
167 process_ablise_plugin_wet_dry (process_info_t * procinfo, plugin_t *plugin, gboolean enable)
169 plugin->wet_dry_enabled = enable;
172 /** move a plugin up or down one place in the chain */
174 process_move_plugin (process_info_t * procinfo, plugin_t *plugin, gint up)
176 /* other plugins in the chain */
177 plugin_t *pp = NULL, *p, *n, *nn = NULL;
179 /* note that we should never recieve an illogical move request
180 ie, there will always be at least 1 plugin before for an up
181 request or 1 plugin after for a down request */
183 /* these are pointers to the plugins surrounding the specified one:
184 { pp, p, plugin, n, nn } which makes things much clearer than
199 procinfo->chain = plugin;
210 procinfo->chain_end = p;
232 procinfo->chain_end = plugin;
235 if (procinfo->jack_client && plugin->desc->aux_channels > 0)
238 other = up ? plugin->next : plugin->prev;
240 /* swap around the jack ports */
241 if (other->desc->id == plugin->desc->id)
242 plugin_swap_aux_ports (plugin, other);
246 /** exchange an existing plugin for a newly created one */
248 process_change_plugin (process_info_t * procinfo,
249 plugin_t *plugin, plugin_t * new_plugin)
251 new_plugin->next = plugin->next;
252 new_plugin->prev = plugin->prev;
255 plugin->prev->next = new_plugin;
257 procinfo->chain = new_plugin;
260 plugin->next->prev = new_plugin;
262 procinfo->chain_end = new_plugin;
264 /* sort out the aux ports */
265 if (procinfo->jack_client && plugin->desc->aux_channels > 0)
269 for (other = plugin->next; other; other = other->next)
270 if (other->desc->id == plugin->desc->id)
271 plugin_swap_aux_ports (plugin, other);
278 /******************************************
279 ************* non RT stuff ***************
280 ******************************************/
284 plugin_open_plugin (plugin_desc_t * desc,
285 void ** dl_handle_ptr,
286 const LADSPA_Descriptor ** descriptor_ptr)
290 LADSPA_Descriptor_Function get_descriptor;
292 /* open the object file */
293 dl_handle = dlopen (desc->object_file, RTLD_NOW|RTLD_GLOBAL);
296 mlt_log_warning( NULL, "%s: error opening shared object file '%s': %s\n",
297 __FUNCTION__, desc->object_file, dlerror());
302 /* get the get_descriptor function */
303 dlerror (); /* clear the error report */
305 get_descriptor = (LADSPA_Descriptor_Function)
306 dlsym (dl_handle, "ladspa_descriptor");
311 mlt_log_warning( NULL, "%s: error finding descriptor symbol in object file '%s': %s\n",
312 __FUNCTION__, desc->object_file, dlerr);
317 *descriptor_ptr = get_descriptor (desc->index);
318 *dl_handle_ptr = dl_handle;
324 plugin_instantiate (const LADSPA_Descriptor * descriptor,
325 unsigned long plugin_index,
327 LADSPA_Handle * instances)
331 for (i = 0; i < copies; i++)
333 instances[i] = descriptor->instantiate (descriptor, sample_rate);
339 for (d = 0; d < i; d++)
340 descriptor->cleanup (instances[d]);
350 plugin_create_aux_ports (plugin_t * plugin, guint copy, jack_rack_t * jack_rack)
352 plugin_desc_t * desc;
353 // plugin_slot_t * slot;
354 unsigned long aux_channel = 1;
355 unsigned long plugin_index = 1;
361 ladspa_holder_t * holder;
364 holder = plugin->holders + copy;
366 holder->aux_ports = g_malloc (sizeof (jack_port_t *) * desc->aux_channels);
368 /* make the plugin name jack worthy */
369 ptr = plugin_name = g_strndup (plugin->desc->name, 7);
375 *ptr = tolower (*ptr);
381 for (list = jack_rack->slots; list; list = g_list_next (list))
383 slot = (plugin_slot_t *) list->data;
385 if (slot->plugin->desc->id == plugin->desc->id)
390 for (i = 0; i < desc->aux_channels; i++, aux_channel++)
392 sprintf (port_name, "%s_%ld-%d_%c%ld",
396 desc->aux_are_input ? 'i' : 'o',
399 holder->aux_ports[i] =
400 jack_port_register (jack_rack->procinfo->jack_client,
402 JACK_DEFAULT_AUDIO_TYPE,
403 desc->aux_are_input ? JackPortIsInput : JackPortIsOutput,
406 if (!holder->aux_ports[i])
408 mlt_log_panic( NULL, "Could not register jack port '%s'; aborting\n", port_name);
413 g_free (plugin_name);
416 static LADSPA_Data unused_control_port_output;
419 plugin_init_holder (plugin_t * plugin,
421 LADSPA_Handle instance,
422 jack_rack_t * jack_rack)
425 plugin_desc_t * desc;
426 ladspa_holder_t * holder;
429 holder = plugin->holders + copy;
431 holder->instance = instance;
433 if (desc->control_port_count > 0)
435 holder->ui_control_fifos = g_malloc (sizeof (lff_t) * desc->control_port_count);
436 holder->control_memory = g_malloc (sizeof (LADSPA_Data) * desc->control_port_count);
440 holder->ui_control_fifos = NULL;
441 holder->control_memory = NULL;
444 for (i = 0; i < desc->control_port_count; i++)
446 lff_init (holder->ui_control_fifos + i, CONTROL_FIFO_SIZE, sizeof (LADSPA_Data));
447 holder->control_memory[i] =
448 plugin_desc_get_default_control_value (desc, desc->control_port_indicies[i], sample_rate);
451 connect_port (instance, desc->control_port_indicies[i], holder->control_memory + i);
454 for (i = 0; i < desc->port_count; i++)
456 if (!LADSPA_IS_PORT_CONTROL (desc->port_descriptors[i]))
459 if (LADSPA_IS_PORT_OUTPUT (desc->port_descriptors[i]))
460 plugin->descriptor-> connect_port (instance, i, &unused_control_port_output);
463 if (jack_rack->procinfo->jack_client && plugin->desc->aux_channels > 0)
464 plugin_create_aux_ports (plugin, copy, jack_rack);
466 if (plugin->descriptor->activate)
467 plugin->descriptor->activate (instance);
472 plugin_new (plugin_desc_t * desc, jack_rack_t * jack_rack)
475 const LADSPA_Descriptor * descriptor;
476 LADSPA_Handle * instances;
482 /* open the plugin */
483 err = plugin_open_plugin (desc, &dl_handle, &descriptor);
487 /* create the instances */
488 copies = plugin_desc_get_copies (desc, jack_rack->channels);
489 instances = g_malloc (sizeof (LADSPA_Handle) * copies);
491 err = plugin_instantiate (descriptor, desc->index, copies, instances);
500 plugin = g_malloc (sizeof (plugin_t));
502 plugin->descriptor = descriptor;
503 plugin->dl_handle = dl_handle;
505 plugin->copies = copies;
506 plugin->enabled = FALSE;
509 plugin->wet_dry_enabled = FALSE;
510 plugin->jack_rack = jack_rack;
512 /* create audio memory and wet/dry stuff */
513 plugin->audio_output_memory = g_malloc (sizeof (LADSPA_Data *) * jack_rack->channels);
514 plugin->wet_dry_fifos = g_malloc (sizeof (lff_t) * jack_rack->channels);
515 plugin->wet_dry_values = g_malloc (sizeof (LADSPA_Data) * jack_rack->channels);
517 for (i = 0; i < jack_rack->channels; i++)
519 plugin->audio_output_memory[i] = g_malloc (sizeof (LADSPA_Data) * buffer_size);
520 lff_init (plugin->wet_dry_fifos + i, CONTROL_FIFO_SIZE, sizeof (LADSPA_Data));
521 plugin->wet_dry_values[i] = 1.0;
524 /* create holders and fill them out */
525 plugin->holders = g_malloc (sizeof (ladspa_holder_t) * copies);
526 for (i = 0; i < copies; i++)
527 plugin_init_holder (plugin, i, instances[i], jack_rack);
534 plugin_destroy (plugin_t * plugin)
539 /* destroy holders */
540 for (i = 0; i < plugin->copies; i++)
542 if (plugin->descriptor->deactivate)
543 plugin->descriptor->deactivate (plugin->holders[i].instance);
545 /* if (plugin->descriptor->cleanup)
546 plugin->descriptor->cleanup (plugin->holders[i].instance); */
548 if (plugin->desc->control_port_count > 0)
550 for (j = 0; j < plugin->desc->control_port_count; j++)
552 lff_free (plugin->holders[i].ui_control_fifos + j);
554 g_free (plugin->holders[i].ui_control_fifos);
555 g_free (plugin->holders[i].control_memory);
559 if (plugin->jack_rack->procinfo->jack_client && plugin->desc->aux_channels > 0)
561 for (j = 0; j < plugin->desc->aux_channels; j++)
563 err = jack_port_unregister (plugin->jack_rack->procinfo->jack_client,
564 plugin->holders[i].aux_ports[j]);
567 mlt_log_warning( NULL, "%s: could not unregister jack port\n", __FUNCTION__);
570 g_free (plugin->holders[i].aux_ports);
574 g_free (plugin->holders);
576 for (i = 0; i < plugin->jack_rack->channels; i++)
578 g_free (plugin->audio_output_memory[i]);
579 lff_free (plugin->wet_dry_fifos + i);
582 g_free (plugin->audio_output_memory);
583 g_free (plugin->wet_dry_fifos);
584 g_free (plugin->wet_dry_values);
586 err = dlclose (plugin->dl_handle);
589 mlt_log_warning( NULL, "%s: error closing shared object '%s': %s\n",
590 __FUNCTION__, plugin->desc->object_file, dlerror ());