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"
38 #define CONTROL_FIFO_SIZE 128
42 /* swap over the jack ports in two plugins */
44 plugin_swap_aux_ports (plugin_t
* plugin
, plugin_t
* other
)
47 jack_port_t
** aux_ports_tmp
;
49 for (copy
= 0; copy
< plugin
->copies
; copy
++)
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
;
57 /** connect up the ladspa instance's input buffers to the previous
58 plugin's audio memory. make sure to check that plugin->prev
61 plugin_connect_input_ports (plugin_t
* plugin
, LADSPA_Data
** inputs
)
64 unsigned long channel
;
65 unsigned long rack_channel
;
67 if (!plugin
|| !inputs
)
71 for (copy
= 0; copy
< plugin
->copies
; copy
++)
73 for (channel
= 0; channel
< plugin
->desc
->channels
; channel
++)
76 connect_port (plugin
->holders
[copy
].instance
,
77 plugin
->desc
->audio_input_port_indicies
[channel
],
78 inputs
[rack_channel
]);
83 plugin
->audio_input_memory
= inputs
;
86 /** connect up a plugin's output ports to its own audio_output_memory output memory */
88 plugin_connect_output_ports (plugin_t
* plugin
)
91 unsigned long channel
;
92 unsigned long rack_channel
= 0;
98 for (copy
= 0; copy
< plugin
->copies
; copy
++)
100 for (channel
= 0; channel
< plugin
->desc
->channels
; channel
++)
103 connect_port (plugin
->holders
[copy
].instance
,
104 plugin
->desc
->audio_output_port_indicies
[channel
],
105 plugin
->audio_output_memory
[rack_channel
]);
112 process_add_plugin (process_info_t
* procinfo
, plugin_t
* plugin
)
115 /* sort out list pointers */
117 plugin
->prev
= procinfo
->chain_end
;
119 if (procinfo
->chain_end
)
120 procinfo
->chain_end
->next
= plugin
;
122 procinfo
->chain
= plugin
;
124 procinfo
->chain_end
= plugin
;
129 /** remove a plugin from the chain */
131 process_remove_plugin (process_info_t
* procinfo
, plugin_t
*plugin
)
133 /* sort out chain pointers */
135 plugin
->prev
->next
= plugin
->next
;
137 procinfo
->chain
= plugin
->next
;
140 plugin
->next
->prev
= plugin
->prev
;
142 procinfo
->chain_end
= plugin
->prev
;
144 /* sort out the aux ports */
145 if (procinfo
->jack_client
&& plugin
->desc
->aux_channels
> 0)
149 for (other
= plugin
->next
; other
; other
= other
->next
)
150 if (other
->desc
->id
== plugin
->desc
->id
)
151 plugin_swap_aux_ports (plugin
, other
);
157 /** enable/disable a plugin */
159 process_ablise_plugin (process_info_t
* procinfo
, plugin_t
*plugin
, gboolean enable
)
161 plugin
->enabled
= enable
;
164 /** enable/disable a plugin */
166 process_ablise_plugin_wet_dry (process_info_t
* procinfo
, plugin_t
*plugin
, gboolean enable
)
168 plugin
->wet_dry_enabled
= enable
;
171 /** move a plugin up or down one place in the chain */
173 process_move_plugin (process_info_t
* procinfo
, plugin_t
*plugin
, gint up
)
175 /* other plugins in the chain */
176 plugin_t
*pp
= NULL
, *p
, *n
, *nn
= NULL
;
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 */
182 /* these are pointers to the plugins surrounding the specified one:
183 { pp, p, plugin, n, nn } which makes things much clearer than
198 procinfo
->chain
= plugin
;
209 procinfo
->chain_end
= p
;
231 procinfo
->chain_end
= plugin
;
234 if (procinfo
->jack_client
&& plugin
->desc
->aux_channels
> 0)
237 other
= up ? plugin
->next
: plugin
->prev
;
239 /* swap around the jack ports */
240 if (other
->desc
->id
== plugin
->desc
->id
)
241 plugin_swap_aux_ports (plugin
, other
);
245 /** exchange an existing plugin for a newly created one */
247 process_change_plugin (process_info_t
* procinfo
,
248 plugin_t
*plugin
, plugin_t
* new_plugin
)
250 new_plugin
->next
= plugin
->next
;
251 new_plugin
->prev
= plugin
->prev
;
254 plugin
->prev
->next
= new_plugin
;
256 procinfo
->chain
= new_plugin
;
259 plugin
->next
->prev
= new_plugin
;
261 procinfo
->chain_end
= new_plugin
;
263 /* sort out the aux ports */
264 if (procinfo
->jack_client
&& plugin
->desc
->aux_channels
> 0)
268 for (other
= plugin
->next
; other
; other
= other
->next
)
269 if (other
->desc
->id
== plugin
->desc
->id
)
270 plugin_swap_aux_ports (plugin
, other
);
277 /******************************************
278 ************* non RT stuff ***************
279 ******************************************/
283 plugin_open_plugin (plugin_desc_t
* desc
,
284 void ** dl_handle_ptr
,
285 const LADSPA_Descriptor
** descriptor_ptr
)
289 LADSPA_Descriptor_Function get_descriptor
;
291 /* open the object file */
292 dl_handle
= dlopen (desc
->object_file
, RTLD_NOW
|RTLD_GLOBAL
);
295 fprintf (stderr
, "%s: error opening shared object file '%s': %s\n",
296 __FUNCTION__
, desc
->object_file
, dlerror());
301 /* get the get_descriptor function */
302 dlerror (); /* clear the error report */
304 get_descriptor
= (LADSPA_Descriptor_Function
)
305 dlsym (dl_handle
, "ladspa_descriptor");
310 fprintf (stderr
, "%s: error finding descriptor symbol in object file '%s': %s\n",
311 __FUNCTION__
, desc
->object_file
, dlerr
);
316 *descriptor_ptr
= get_descriptor (desc
->index
);
317 *dl_handle_ptr
= dl_handle
;
323 plugin_instantiate (const LADSPA_Descriptor
* descriptor
,
324 unsigned long plugin_index
,
326 LADSPA_Handle
* instances
)
330 for (i
= 0; i
< copies
; i
++)
332 instances
[i
] = descriptor
->instantiate (descriptor
, sample_rate
);
338 for (d
= 0; d
< i
; d
++)
339 descriptor
->cleanup (instances
[d
]);
349 plugin_create_aux_ports (plugin_t
* plugin
, guint copy
, jack_rack_t
* jack_rack
)
351 plugin_desc_t
* desc
;
352 // plugin_slot_t * slot;
353 unsigned long aux_channel
= 1;
354 unsigned long plugin_index
= 1;
360 ladspa_holder_t
* holder
;
363 holder
= plugin
->holders
+ copy
;
365 holder
->aux_ports
= g_malloc (sizeof (jack_port_t
*) * desc
->aux_channels
);
367 /* make the plugin name jack worthy */
368 ptr
= plugin_name
= g_strndup (plugin
->desc
->name
, 7);
374 *ptr
= tolower (*ptr
);
380 for (list = jack_rack->slots; list; list = g_list_next (list))
382 slot = (plugin_slot_t *) list->data;
384 if (slot->plugin->desc->id == plugin->desc->id)
389 for (i
= 0; i
< desc
->aux_channels
; i
++, aux_channel
++)
391 sprintf (port_name
, "%s_%ld-%d_%c%ld",
395 desc
->aux_are_input ?
'i' : 'o',
398 holder
->aux_ports
[i
] =
399 jack_port_register (jack_rack
->procinfo
->jack_client
,
401 JACK_DEFAULT_AUDIO_TYPE
,
402 desc
->aux_are_input ? JackPortIsInput
: JackPortIsOutput
,
405 if (!holder
->aux_ports
[i
])
407 fprintf (stderr
, "Could not register jack port '%s'; aborting\n", port_name
);
412 g_free (plugin_name
);
415 static LADSPA_Data unused_control_port_output
;
418 plugin_init_holder (plugin_t
* plugin
,
420 LADSPA_Handle instance
,
421 jack_rack_t
* jack_rack
)
424 plugin_desc_t
* desc
;
425 ladspa_holder_t
* holder
;
428 holder
= plugin
->holders
+ copy
;
430 holder
->instance
= instance
;
432 if (desc
->control_port_count
> 0)
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
);
439 holder
->ui_control_fifos
= NULL
;
440 holder
->control_memory
= NULL
;
443 for (i
= 0; i
< desc
->control_port_count
; i
++)
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
);
450 connect_port (instance
, desc
->control_port_indicies
[i
], holder
->control_memory
+ i
);
453 for (i
= 0; i
< desc
->port_count
; i
++)
455 if (!LADSPA_IS_PORT_CONTROL (desc
->port_descriptors
[i
]))
458 if (LADSPA_IS_PORT_OUTPUT (desc
->port_descriptors
[i
]))
459 plugin
->descriptor
-> connect_port (instance
, i
, &unused_control_port_output
);
462 if (jack_rack
->procinfo
->jack_client
&& plugin
->desc
->aux_channels
> 0)
463 plugin_create_aux_ports (plugin
, copy
, jack_rack
);
465 if (plugin
->descriptor
->activate
)
466 plugin
->descriptor
->activate (instance
);
471 plugin_new (plugin_desc_t
* desc
, jack_rack_t
* jack_rack
)
474 const LADSPA_Descriptor
* descriptor
;
475 LADSPA_Handle
* instances
;
481 /* open the plugin */
482 err
= plugin_open_plugin (desc
, &dl_handle
, &descriptor
);
486 /* create the instances */
487 copies
= plugin_desc_get_copies (desc
, jack_rack
->channels
);
488 instances
= g_malloc (sizeof (LADSPA_Handle
) * copies
);
490 err
= plugin_instantiate (descriptor
, desc
->index
, copies
, instances
);
499 plugin
= g_malloc (sizeof (plugin_t
));
501 plugin
->descriptor
= descriptor
;
502 plugin
->dl_handle
= dl_handle
;
504 plugin
->copies
= copies
;
505 plugin
->enabled
= FALSE
;
508 plugin
->wet_dry_enabled
= FALSE
;
509 plugin
->jack_rack
= jack_rack
;
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
);
516 for (i
= 0; i
< jack_rack
->channels
; i
++)
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;
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
);
533 plugin_destroy (plugin_t
* plugin
)
538 /* destroy holders */
539 for (i
= 0; i
< plugin
->copies
; i
++)
541 if (plugin
->descriptor
->deactivate
)
542 plugin
->descriptor
->deactivate (plugin
->holders
[i
].instance
);
544 /* if (plugin->descriptor->cleanup)
545 plugin->descriptor->cleanup (plugin->holders[i].instance); */
547 if (plugin
->desc
->control_port_count
> 0)
549 for (j
= 0; j
< plugin
->desc
->control_port_count
; j
++)
551 lff_free (plugin
->holders
[i
].ui_control_fifos
+ j
);
553 g_free (plugin
->holders
[i
].ui_control_fifos
);
554 g_free (plugin
->holders
[i
].control_memory
);
558 if (plugin
->jack_rack
->procinfo
->jack_client
&& plugin
->desc
->aux_channels
> 0)
560 for (j
= 0; j
< plugin
->desc
->aux_channels
; j
++)
562 err
= jack_port_unregister (plugin
->jack_rack
->procinfo
->jack_client
,
563 plugin
->holders
[i
].aux_ports
[j
]);
566 fprintf (stderr
, "%s: could not unregister jack port\n", __FUNCTION__
);
569 g_free (plugin
->holders
[i
].aux_ports
);
573 g_free (plugin
->holders
);
575 for (i
= 0; i
< plugin
->jack_rack
->channels
; i
++)
577 g_free (plugin
->audio_output_memory
[i
]);
578 lff_free (plugin
->wet_dry_fifos
+ i
);
581 g_free (plugin
->audio_output_memory
);
582 g_free (plugin
->wet_dry_fifos
);
583 g_free (plugin
->wet_dry_values
);
585 err
= dlclose (plugin
->dl_handle
);
588 fprintf (stderr
, "%s: error closing shared object '%s': %s\n",
589 __FUNCTION__
, plugin
->desc
->object_file
, dlerror ());