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