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"
34 #define CONTROL_FIFO_SIZE 128
38 /* swap over the jack ports in two plugins */
40 plugin_swap_aux_ports (plugin_t
* plugin
, plugin_t
* other
)
43 jack_port_t
** aux_ports_tmp
;
45 for (copy
= 0; copy
< plugin
->copies
; copy
++)
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
;
53 /** connect up the ladspa instance's input buffers to the previous
54 plugin's audio memory. make sure to check that plugin->prev
57 plugin_connect_input_ports (plugin_t
* plugin
, LADSPA_Data
** inputs
)
60 unsigned long channel
;
61 unsigned long rack_channel
;
63 if (!plugin
|| !inputs
)
67 for (copy
= 0; copy
< plugin
->copies
; copy
++)
69 for (channel
= 0; channel
< plugin
->desc
->channels
; channel
++)
72 connect_port (plugin
->holders
[copy
].instance
,
73 plugin
->desc
->audio_input_port_indicies
[channel
],
74 inputs
[rack_channel
]);
79 plugin
->audio_input_memory
= inputs
;
82 /** connect up a plugin's output ports to its own audio_output_memory output memory */
84 plugin_connect_output_ports (plugin_t
* plugin
)
87 unsigned long channel
;
88 unsigned long rack_channel
= 0;
94 for (copy
= 0; copy
< plugin
->copies
; copy
++)
96 for (channel
= 0; channel
< plugin
->desc
->channels
; channel
++)
99 connect_port (plugin
->holders
[copy
].instance
,
100 plugin
->desc
->audio_output_port_indicies
[channel
],
101 plugin
->audio_output_memory
[rack_channel
]);
108 process_add_plugin (process_info_t
* procinfo
, plugin_t
* plugin
)
111 /* sort out list pointers */
113 plugin
->prev
= procinfo
->chain_end
;
115 if (procinfo
->chain_end
)
116 procinfo
->chain_end
->next
= plugin
;
118 procinfo
->chain
= plugin
;
120 procinfo
->chain_end
= plugin
;
125 /** remove a plugin from the chain */
127 process_remove_plugin (process_info_t
* procinfo
, plugin_t
*plugin
)
129 /* sort out chain pointers */
131 plugin
->prev
->next
= plugin
->next
;
133 procinfo
->chain
= plugin
->next
;
136 plugin
->next
->prev
= plugin
->prev
;
138 procinfo
->chain_end
= plugin
->prev
;
140 /* sort out the aux ports */
141 if (plugin
->desc
->aux_channels
> 0)
145 for (other
= plugin
->next
; other
; other
= other
->next
)
146 if (other
->desc
->id
== plugin
->desc
->id
)
147 plugin_swap_aux_ports (plugin
, other
);
153 /** enable/disable a plugin */
155 process_ablise_plugin (process_info_t
* procinfo
, plugin_t
*plugin
, gboolean enable
)
157 plugin
->enabled
= enable
;
160 /** enable/disable a plugin */
162 process_ablise_plugin_wet_dry (process_info_t
* procinfo
, plugin_t
*plugin
, gboolean enable
)
164 plugin
->wet_dry_enabled
= enable
;
167 /** move a plugin up or down one place in the chain */
169 process_move_plugin (process_info_t
* procinfo
, plugin_t
*plugin
, gint up
)
171 /* other plugins in the chain */
172 plugin_t
*pp
= NULL
, *p
, *n
, *nn
= NULL
;
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 */
178 /* these are pointers to the plugins surrounding the specified one:
179 { pp, p, plugin, n, nn } which makes things much clearer than
194 procinfo
->chain
= plugin
;
205 procinfo
->chain_end
= p
;
227 procinfo
->chain_end
= plugin
;
230 if (plugin
->desc
->aux_channels
> 0)
233 other
= up ? plugin
->next
: plugin
->prev
;
235 /* swap around the jack ports */
236 if (other
->desc
->id
== plugin
->desc
->id
)
237 plugin_swap_aux_ports (plugin
, other
);
241 /** exchange an existing plugin for a newly created one */
243 process_change_plugin (process_info_t
* procinfo
,
244 plugin_t
*plugin
, plugin_t
* new_plugin
)
246 new_plugin
->next
= plugin
->next
;
247 new_plugin
->prev
= plugin
->prev
;
250 plugin
->prev
->next
= new_plugin
;
252 procinfo
->chain
= new_plugin
;
255 plugin
->next
->prev
= new_plugin
;
257 procinfo
->chain_end
= new_plugin
;
259 /* sort out the aux ports */
260 if (plugin
->desc
->aux_channels
> 0)
264 for (other
= plugin
->next
; other
; other
= other
->next
)
265 if (other
->desc
->id
== plugin
->desc
->id
)
266 plugin_swap_aux_ports (plugin
, other
);
273 /******************************************
274 ************* non RT stuff ***************
275 ******************************************/
279 plugin_open_plugin (plugin_desc_t
* desc
,
280 void ** dl_handle_ptr
,
281 const LADSPA_Descriptor
** descriptor_ptr
)
285 LADSPA_Descriptor_Function get_descriptor
;
287 /* open the object file */
288 dl_handle
= dlopen (desc
->object_file
, RTLD_NOW
|RTLD_GLOBAL
);
291 fprintf (stderr
, "%s: error opening shared object file '%s': %s\n",
292 __FUNCTION__
, desc
->object_file
, dlerror());
297 /* get the get_descriptor function */
298 dlerror (); /* clear the error report */
300 get_descriptor
= (LADSPA_Descriptor_Function
)
301 dlsym (dl_handle
, "ladspa_descriptor");
306 fprintf (stderr
, "%s: error finding descriptor symbol in object file '%s': %s\n",
307 __FUNCTION__
, desc
->object_file
, dlerr
);
312 *descriptor_ptr
= get_descriptor (desc
->index
);
313 *dl_handle_ptr
= dl_handle
;
319 plugin_instantiate (const LADSPA_Descriptor
* descriptor
,
320 unsigned long plugin_index
,
322 LADSPA_Handle
* instances
)
326 for (i
= 0; i
< copies
; i
++)
328 instances
[i
] = descriptor
->instantiate (descriptor
, sample_rate
);
334 for (d
= 0; d
< i
; d
++)
335 descriptor
->cleanup (instances
[d
]);
345 plugin_create_aux_ports (plugin_t
* plugin
, guint copy
, jack_rack_t
* jack_rack
)
347 plugin_desc_t
* desc
;
348 // plugin_slot_t * slot;
349 unsigned long aux_channel
= 1;
350 unsigned long plugin_index
= 1;
356 ladspa_holder_t
* holder
;
359 holder
= plugin
->holders
+ copy
;
361 holder
->aux_ports
= g_malloc (sizeof (jack_port_t
*) * desc
->aux_channels
);
363 /* make the plugin name jack worthy */
364 ptr
= plugin_name
= g_strndup (plugin
->desc
->name
, 7);
370 *ptr
= tolower (*ptr
);
376 for (list = jack_rack->slots; list; list = g_list_next (list))
378 slot = (plugin_slot_t *) list->data;
380 if (slot->plugin->desc->id == plugin->desc->id)
385 for (i
= 0; i
< desc
->aux_channels
; i
++, aux_channel
++)
387 sprintf (port_name
, "%s_%ld-%d_%c%ld",
391 desc
->aux_are_input ?
'i' : 'o',
394 holder
->aux_ports
[i
] =
395 jack_port_register (jack_rack
->ui
->procinfo
->jack_client
,
397 JACK_DEFAULT_AUDIO_TYPE
,
398 desc
->aux_are_input ? JackPortIsInput
: JackPortIsOutput
,
401 if (!holder
->aux_ports
[i
])
403 fprintf (stderr
, "Could not register jack port '%s'; aborting\n", port_name
);
408 g_free (plugin_name
);
411 static LADSPA_Data unused_control_port_output
;
414 plugin_init_holder (plugin_t
* plugin
,
416 LADSPA_Handle instance
,
417 jack_rack_t
* jack_rack
)
420 plugin_desc_t
* desc
;
421 ladspa_holder_t
* holder
;
424 holder
= plugin
->holders
+ copy
;
426 holder
->instance
= instance
;
428 if (desc
->control_port_count
> 0)
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
);
435 holder
->ui_control_fifos
= NULL
;
436 holder
->control_memory
= NULL
;
439 for (i
= 0; i
< desc
->control_port_count
; i
++)
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
);
446 connect_port (instance
, desc
->control_port_indicies
[i
], holder
->control_memory
+ i
);
449 for (i
= 0; i
< desc
->port_count
; i
++)
451 if (!LADSPA_IS_PORT_CONTROL (desc
->port_descriptors
[i
]))
454 if (LADSPA_IS_PORT_OUTPUT (desc
->port_descriptors
[i
]))
455 plugin
->descriptor
-> connect_port (instance
, i
, &unused_control_port_output
);
458 if (plugin
->desc
->aux_channels
> 0)
459 plugin_create_aux_ports (plugin
, copy
, jack_rack
);
461 if (plugin
->descriptor
->activate
)
462 plugin
->descriptor
->activate (instance
);
467 plugin_new (plugin_desc_t
* desc
, jack_rack_t
* jack_rack
)
470 const LADSPA_Descriptor
* descriptor
;
471 LADSPA_Handle
* instances
;
477 /* open the plugin */
478 err
= plugin_open_plugin (desc
, &dl_handle
, &descriptor
);
482 /* create the instances */
483 copies
= plugin_desc_get_copies (desc
, jack_rack
->channels
);
484 instances
= g_malloc (sizeof (LADSPA_Handle
) * copies
);
486 err
= plugin_instantiate (descriptor
, desc
->index
, copies
, instances
);
495 plugin
= g_malloc (sizeof (plugin_t
));
497 plugin
->descriptor
= descriptor
;
498 plugin
->dl_handle
= dl_handle
;
500 plugin
->copies
= copies
;
501 plugin
->enabled
= FALSE
;
504 plugin
->wet_dry_enabled
= FALSE
;
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
, ui_t
*ui
)
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
->desc
->aux_channels
> 0)
555 for (j
= 0; j
< plugin
->desc
->aux_channels
; j
++)
557 err
= jack_port_unregister (ui
->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
< ui
->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 ());