4 * Copyright (C) Robert Ham 2002, 2003 (node@users.sourceforge.net)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
30 #include "jack_rack.h"
33 #define CONTROL_FIFO_SIZE 128
37 /* swap over the jack ports in two plugins */
39 plugin_swap_aux_ports (plugin_t * plugin, plugin_t * other)
42 jack_port_t ** aux_ports_tmp;
44 for (copy = 0; copy < plugin->copies; copy++)
46 aux_ports_tmp = other->holders[copy].aux_ports;
47 other->holders[copy].aux_ports = plugin->holders[copy].aux_ports;
48 plugin->holders[copy].aux_ports = aux_ports_tmp;
52 /** connect up the ladspa instance's input buffers to the previous
53 plugin's audio memory. make sure to check that plugin->prev
56 plugin_connect_input_ports (plugin_t * plugin, LADSPA_Data ** inputs)
59 unsigned long channel;
60 unsigned long rack_channel;
62 if (!plugin || !inputs)
66 for (copy = 0; copy < plugin->copies; copy++)
68 for (channel = 0; channel < plugin->desc->channels; channel++)
71 connect_port (plugin->holders[copy].instance,
72 plugin->desc->audio_input_port_indicies[channel],
73 inputs[rack_channel]);
78 plugin->audio_input_memory = inputs;
81 /** connect up a plugin's output ports to its own audio_output_memory output memory */
83 plugin_connect_output_ports (plugin_t * plugin)
86 unsigned long channel;
87 unsigned long rack_channel = 0;
93 for (copy = 0; copy < plugin->copies; copy++)
95 for (channel = 0; channel < plugin->desc->channels; channel++)
98 connect_port (plugin->holders[copy].instance,
99 plugin->desc->audio_output_port_indicies[channel],
100 plugin->audio_output_memory[rack_channel]);
107 process_add_plugin (process_info_t * procinfo, plugin_t * plugin)
110 /* sort out list pointers */
112 plugin->prev = procinfo->chain_end;
114 if (procinfo->chain_end)
115 procinfo->chain_end->next = plugin;
117 procinfo->chain = plugin;
119 procinfo->chain_end = plugin;
124 /** remove a plugin from the chain */
126 process_remove_plugin (process_info_t * procinfo, plugin_t *plugin)
128 /* sort out chain pointers */
130 plugin->prev->next = plugin->next;
132 procinfo->chain = plugin->next;
135 plugin->next->prev = plugin->prev;
137 procinfo->chain_end = plugin->prev;
139 /* sort out the aux ports */
140 if (procinfo->jack_client && plugin->desc->aux_channels > 0)
144 for (other = plugin->next; other; other = other->next)
145 if (other->desc->id == plugin->desc->id)
146 plugin_swap_aux_ports (plugin, other);
152 /** enable/disable a plugin */
154 process_ablise_plugin (process_info_t * procinfo, plugin_t *plugin, gboolean enable)
156 plugin->enabled = enable;
159 /** enable/disable a plugin */
161 process_ablise_plugin_wet_dry (process_info_t * procinfo, plugin_t *plugin, gboolean enable)
163 plugin->wet_dry_enabled = enable;
166 /** move a plugin up or down one place in the chain */
168 process_move_plugin (process_info_t * procinfo, plugin_t *plugin, gint up)
170 /* other plugins in the chain */
171 plugin_t *pp = NULL, *p, *n, *nn = NULL;
173 /* note that we should never recieve an illogical move request
174 ie, there will always be at least 1 plugin before for an up
175 request or 1 plugin after for a down request */
177 /* these are pointers to the plugins surrounding the specified one:
178 { pp, p, plugin, n, nn } which makes things much clearer than
193 procinfo->chain = plugin;
204 procinfo->chain_end = p;
226 procinfo->chain_end = plugin;
229 if (procinfo->jack_client && plugin->desc->aux_channels > 0)
232 other = up ? plugin->next : plugin->prev;
234 /* swap around the jack ports */
235 if (other->desc->id == plugin->desc->id)
236 plugin_swap_aux_ports (plugin, other);
240 /** exchange an existing plugin for a newly created one */
242 process_change_plugin (process_info_t * procinfo,
243 plugin_t *plugin, plugin_t * new_plugin)
245 new_plugin->next = plugin->next;
246 new_plugin->prev = plugin->prev;
249 plugin->prev->next = new_plugin;
251 procinfo->chain = new_plugin;
254 plugin->next->prev = new_plugin;
256 procinfo->chain_end = new_plugin;
258 /* sort out the aux ports */
259 if (procinfo->jack_client && plugin->desc->aux_channels > 0)
263 for (other = plugin->next; other; other = other->next)
264 if (other->desc->id == plugin->desc->id)
265 plugin_swap_aux_ports (plugin, other);
272 /******************************************
273 ************* non RT stuff ***************
274 ******************************************/
278 plugin_open_plugin (plugin_desc_t * desc,
279 void ** dl_handle_ptr,
280 const LADSPA_Descriptor ** descriptor_ptr)
284 LADSPA_Descriptor_Function get_descriptor;
286 /* open the object file */
287 dl_handle = dlopen (desc->object_file, RTLD_NOW|RTLD_GLOBAL);
290 fprintf (stderr, "%s: error opening shared object file '%s': %s\n",
291 __FUNCTION__, desc->object_file, dlerror());
296 /* get the get_descriptor function */
297 dlerror (); /* clear the error report */
299 get_descriptor = (LADSPA_Descriptor_Function)
300 dlsym (dl_handle, "ladspa_descriptor");
305 fprintf (stderr, "%s: error finding descriptor symbol in object file '%s': %s\n",
306 __FUNCTION__, desc->object_file, dlerr);
311 *descriptor_ptr = get_descriptor (desc->index);
312 *dl_handle_ptr = dl_handle;
318 plugin_instantiate (const LADSPA_Descriptor * descriptor,
319 unsigned long plugin_index,
321 LADSPA_Handle * instances)
325 for (i = 0; i < copies; i++)
327 instances[i] = descriptor->instantiate (descriptor, sample_rate);
333 for (d = 0; d < i; d++)
334 descriptor->cleanup (instances[d]);
344 plugin_create_aux_ports (plugin_t * plugin, guint copy, jack_rack_t * jack_rack)
346 plugin_desc_t * desc;
347 // plugin_slot_t * slot;
348 unsigned long aux_channel = 1;
349 unsigned long plugin_index = 1;
355 ladspa_holder_t * holder;
358 holder = plugin->holders + copy;
360 holder->aux_ports = g_malloc (sizeof (jack_port_t *) * desc->aux_channels);
362 /* make the plugin name jack worthy */
363 ptr = plugin_name = g_strndup (plugin->desc->name, 7);
369 *ptr = tolower (*ptr);
375 for (list = jack_rack->slots; list; list = g_list_next (list))
377 slot = (plugin_slot_t *) list->data;
379 if (slot->plugin->desc->id == plugin->desc->id)
384 for (i = 0; i < desc->aux_channels; i++, aux_channel++)
386 sprintf (port_name, "%s_%ld-%d_%c%ld",
390 desc->aux_are_input ? 'i' : 'o',
393 holder->aux_ports[i] =
394 jack_port_register (jack_rack->procinfo->jack_client,
396 JACK_DEFAULT_AUDIO_TYPE,
397 desc->aux_are_input ? JackPortIsInput : JackPortIsOutput,
400 if (!holder->aux_ports[i])
402 fprintf (stderr, "Could not register jack port '%s'; aborting\n", port_name);
407 g_free (plugin_name);
410 static LADSPA_Data unused_control_port_output;
413 plugin_init_holder (plugin_t * plugin,
415 LADSPA_Handle instance,
416 jack_rack_t * jack_rack)
419 plugin_desc_t * desc;
420 ladspa_holder_t * holder;
423 holder = plugin->holders + copy;
425 holder->instance = instance;
427 if (desc->control_port_count > 0)
429 holder->ui_control_fifos = g_malloc (sizeof (lff_t) * desc->control_port_count);
430 holder->control_memory = g_malloc (sizeof (LADSPA_Data) * desc->control_port_count);
434 holder->ui_control_fifos = NULL;
435 holder->control_memory = NULL;
438 for (i = 0; i < desc->control_port_count; i++)
440 lff_init (holder->ui_control_fifos + i, CONTROL_FIFO_SIZE, sizeof (LADSPA_Data));
441 holder->control_memory[i] =
442 plugin_desc_get_default_control_value (desc, desc->control_port_indicies[i], sample_rate);
445 connect_port (instance, desc->control_port_indicies[i], holder->control_memory + i);
448 for (i = 0; i < desc->port_count; i++)
450 if (!LADSPA_IS_PORT_CONTROL (desc->port_descriptors[i]))
453 if (LADSPA_IS_PORT_OUTPUT (desc->port_descriptors[i]))
454 plugin->descriptor-> connect_port (instance, i, &unused_control_port_output);
457 if (jack_rack->procinfo->jack_client && plugin->desc->aux_channels > 0)
458 plugin_create_aux_ports (plugin, copy, jack_rack);
460 if (plugin->descriptor->activate)
461 plugin->descriptor->activate (instance);
466 plugin_new (plugin_desc_t * desc, jack_rack_t * jack_rack)
469 const LADSPA_Descriptor * descriptor;
470 LADSPA_Handle * instances;
476 /* open the plugin */
477 err = plugin_open_plugin (desc, &dl_handle, &descriptor);
481 /* create the instances */
482 copies = plugin_desc_get_copies (desc, jack_rack->channels);
483 instances = g_malloc (sizeof (LADSPA_Handle) * copies);
485 err = plugin_instantiate (descriptor, desc->index, copies, instances);
494 plugin = g_malloc (sizeof (plugin_t));
496 plugin->descriptor = descriptor;
497 plugin->dl_handle = dl_handle;
499 plugin->copies = copies;
500 plugin->enabled = FALSE;
503 plugin->wet_dry_enabled = FALSE;
504 plugin->jack_rack = jack_rack;
506 /* create audio memory and wet/dry stuff */
507 plugin->audio_output_memory = g_malloc (sizeof (LADSPA_Data *) * jack_rack->channels);
508 plugin->wet_dry_fifos = g_malloc (sizeof (lff_t) * jack_rack->channels);
509 plugin->wet_dry_values = g_malloc (sizeof (LADSPA_Data) * jack_rack->channels);
511 for (i = 0; i < jack_rack->channels; i++)
513 plugin->audio_output_memory[i] = g_malloc (sizeof (LADSPA_Data) * buffer_size);
514 lff_init (plugin->wet_dry_fifos + i, CONTROL_FIFO_SIZE, sizeof (LADSPA_Data));
515 plugin->wet_dry_values[i] = 1.0;
518 /* create holders and fill them out */
519 plugin->holders = g_malloc (sizeof (ladspa_holder_t) * copies);
520 for (i = 0; i < copies; i++)
521 plugin_init_holder (plugin, i, instances[i], jack_rack);
528 plugin_destroy (plugin_t * plugin)
533 /* destroy holders */
534 for (i = 0; i < plugin->copies; i++)
536 if (plugin->descriptor->deactivate)
537 plugin->descriptor->deactivate (plugin->holders[i].instance);
539 /* if (plugin->descriptor->cleanup)
540 plugin->descriptor->cleanup (plugin->holders[i].instance); */
542 if (plugin->desc->control_port_count > 0)
544 for (j = 0; j < plugin->desc->control_port_count; j++)
546 lff_free (plugin->holders[i].ui_control_fifos + j);
548 g_free (plugin->holders[i].ui_control_fifos);
549 g_free (plugin->holders[i].control_memory);
553 if (plugin->jack_rack->procinfo->jack_client && plugin->desc->aux_channels > 0)
555 for (j = 0; j < plugin->desc->aux_channels; j++)
557 err = jack_port_unregister (plugin->jack_rack->procinfo->jack_client,
558 plugin->holders[i].aux_ports[j]);
561 fprintf (stderr, "%s: could not unregister jack port\n", __FUNCTION__);
564 g_free (plugin->holders[i].aux_ports);
568 g_free (plugin->holders);
570 for (i = 0; i < plugin->jack_rack->channels; i++)
572 g_free (plugin->audio_output_memory[i]);
573 lff_free (plugin->wet_dry_fifos + i);
576 g_free (plugin->audio_output_memory);
577 g_free (plugin->wet_dry_fifos);
578 g_free (plugin->wet_dry_values);
580 err = dlclose (plugin->dl_handle);
583 fprintf (stderr, "%s: error closing shared object '%s': %s\n",
584 __FUNCTION__, plugin->desc->object_file, dlerror ());