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