1 /* Code for LV2 plugins */
3 #include "carla_backend.h"
4 #include "audio_plugin.h"
10 #include <QtGui/QDialog>
11 #include <QtGui/QLayout>
14 #include "lv2/data-access.h"
15 #include "lv2/dynparam.h"
16 #include "lv2/event.h"
17 #include "lv2/event-helpers.h"
18 #include "lv2/instance-access.h"
19 #include "lv2/state.h"
21 #include "lv2/uri-map.h"
24 #include "lv2/ui-resize.h"
25 #include "lv2/lv2_external_ui.h"
26 #include "lv2/lv2_rtmempool.h"
30 #include "lv2-rtmempool/rtmempool.h"
33 #define NS_ATOM "http://lv2plug.in/ns/ext/atom#"
34 #define NS_LV2UI "http://lv2plug.in/ns/extensions/ui#"
37 extern const char* unique_names
[MAX_PLUGINS
];
38 extern AudioPlugin
* AudioPlugins
[MAX_PLUGINS
];
39 extern ExternalMidiNote ExternalMidiNotes
[MAX_MIDI_EVENTS
];
41 extern volatile double ains_peak
[MAX_PLUGINS
*2];
42 extern volatile double aouts_peak
[MAX_PLUGINS
*2];
45 extern jack_client_t
* carla_jack_client
;
48 extern carla_options_t carla_options
;
51 void carla_jack_register_plugin(AudioPlugin
* plugin
);
54 const unsigned int MAX_EVENT_BUFFER
= 0x7FFF; // 32767
57 const uint16_t lv2_feature_id_uri_map
= 0;
58 const uint16_t lv2_feature_id_urid_map
= 1;
59 const uint16_t lv2_feature_id_urid_unmap
= 2;
60 const uint16_t lv2_feature_id_event
= 3;
61 const uint16_t lv2_feature_id_rtmempool
= 4;
62 const uint16_t lv2_feature_id_data_access
= 5;
63 const uint16_t lv2_feature_id_instance_access
= 6;
64 const uint16_t lv2_feature_id_ui_resize
= 7;
65 const uint16_t lv2_feature_id_ui_parent
= 8;
66 const uint16_t lv2_feature_id_external_ui
= 9;
67 const uint16_t lv2_feature_id_external_ui_old
= 10;
68 const uint16_t lv2_feature_count
= 11;
71 const uint16_t CARLA_URI_MAP_ID_EVENT_MIDI
= 1; // 0x1
72 const uint16_t CARLA_URI_MAP_ID_EVENT_TIME
= 2; // 0x2
73 const uint16_t CARLA_URI_MAP_ID_ATOM_STRING
= 3;
74 const uint16_t CARLA_URI_MAP_ID_COUNT
= 4;
77 const unsigned int PLUGIN_HAS_EXTENSION_STATE
= 0x100;
78 const unsigned int PLUGIN_HAS_EXTENSION_DYNPARAM
= 0x200;
80 static LV2_Time_Position carla_lv2_time_pos
= { 0, 0, LV2_TIME_STOPPED
, 0, 0, 0, 0, 0, 0, 0.0 };
82 struct CarlaLv2Event
{
85 LV2_Event_Buffer
* buffer
;
88 struct CarlaLv2EventData
{
90 CarlaLv2Event
* events
;
93 class Lv2OscGuiThread
: public QThread
96 Lv2OscGuiThread(QObject
* parent
=0);
97 void set_plugin_id(short plugin_id
);
98 void set_ui_type(LV2_Property ui_type
);
103 LV2_Property ui_type
;
106 class Lv2AudioPlugin
: public AudioPlugin
109 CarlaLv2EventData evin
;
110 CarlaLv2EventData evout
;
113 const LV2_Descriptor
* descriptor
;
114 const LV2_RDF_Descriptor
* rdf_descriptor
;
115 LV2_Feature
* features
[lv2_feature_count
+1];
121 const LV2UI_Descriptor
* descriptor
;
122 const LV2_RDF_UI
* rdf_descriptor
;
126 QList
<const char*> custom_uri_ids
;
128 lv2_rtsafe_memory_pool_provider rtmempool_allocator
;
131 lv2dynparam_host_callbacks dynparam_host
;
132 lv2dynparam_plugin_callbacks
* dynparam_plugin
;
136 qDebug("Lv2AudioPlugin::Lv2AudioPlugin()");
140 evin
.events
= nullptr;
143 evout
.events
= nullptr;
146 descriptor
= nullptr;
147 rdf_descriptor
= nullptr;
151 ui
.descriptor
= nullptr;
152 ui
.rdf_descriptor
= nullptr;
154 wants_time_pos
= false;
156 for (uint16_t i
=0; i
<lv2_feature_count
+1; i
++)
157 features
[i
] = nullptr;
159 custom_uri_ids
.clear();
161 // Fill pre-set URI keys
162 for (uint16_t i
=0; i
< CARLA_URI_MAP_ID_COUNT
; i
++)
163 custom_uri_ids
.append(nullptr);
165 dynparam_host
.group_appear
= carla_lv2_dynparam_group_appear
;
166 dynparam_host
.group_disappear
= carla_lv2_dynparam_group_disappear
;
167 dynparam_host
.parameter_appear
= carla_lv2_dynparam_parameter_appear
;
168 dynparam_host
.parameter_disappear
= carla_lv2_dynparam_parameter_disappear
;
169 dynparam_host
.parameter_change
= carla_lv2_dynparam_parameter_change
;
170 dynparam_host
.command_appear
= carla_lv2_dynparam_command_appear
;
171 dynparam_host
.command_disappear
= carla_lv2_dynparam_command_disappear
;
177 qDebug("Lv2AudioPlugin::~Lv2AudioPlugin()");
180 if (hints
& PLUGIN_HAS_GUI
)
184 case GUI_INTERNAL_QT4
:
185 case GUI_INTERNAL_X11
:
188 case GUI_EXTERNAL_OSC
:
190 osc_send_hide(&osc
.data
);
192 osc_send_quit(&osc
.data
);
198 if (!osc
.thread
->wait(3000)) // 3 sec
199 qWarning("Failed to properly stop LV2 OSC GUI thread");
204 osc_clear_data(&osc
.data
);
208 case GUI_EXTERNAL_LV2
:
210 LV2_EXTERNAL_UI_HIDE((lv2_external_ui
*)ui
.widget
);
218 if (ui
.handle
&& ui
.descriptor
&& ui
.descriptor
->cleanup
)
219 ui
.descriptor
->cleanup(ui
.handle
);
221 if (features
[lv2_feature_id_data_access
] && features
[lv2_feature_id_data_access
]->data
)
222 delete (LV2_Extension_Data_Feature
*)features
[lv2_feature_id_data_access
]->data
;
224 if (features
[lv2_feature_id_ui_resize
] && features
[lv2_feature_id_ui_resize
]->data
)
225 delete (LV2_UI_Resize_Feature
*)features
[lv2_feature_id_ui_resize
]->data
;
227 if (features
[lv2_feature_id_external_ui
] && features
[lv2_feature_id_external_ui
]->data
)
228 delete (lv2_external_ui_host
*)features
[lv2_feature_id_external_ui
]->data
;
233 for (int i
=0; i
< custom_uri_ids
.count(); i
++)
235 if (custom_uri_ids
[i
])
236 free((void*)custom_uri_ids
[i
]);
239 custom_uri_ids
.clear();
241 if (handle
&& descriptor
->deactivate
&& active_before
)
242 descriptor
->deactivate(handle
);
244 if (handle
&& descriptor
->cleanup
)
245 descriptor
->cleanup(handle
);
247 if (features
[lv2_feature_id_uri_map
] && features
[lv2_feature_id_uri_map
]->data
)
248 delete (LV2_URI_Map_Feature
*)features
[lv2_feature_id_uri_map
]->data
;
250 if (features
[lv2_feature_id_urid_map
] && features
[lv2_feature_id_urid_map
]->data
)
251 delete (LV2_URID_Map
*)features
[lv2_feature_id_urid_map
]->data
;
253 if (features
[lv2_feature_id_urid_unmap
] && features
[lv2_feature_id_urid_unmap
]->data
)
254 delete (LV2_URID_Unmap
*)features
[lv2_feature_id_urid_unmap
]->data
;
256 if (features
[lv2_feature_id_event
] && features
[lv2_feature_id_event
]->data
)
257 delete (LV2_Event_Feature
*)features
[lv2_feature_id_event
]->data
;
259 for (uint16_t j
=0; j
<lv2_feature_count
&& features
[j
]; j
++)
263 lv2_rdf_free(rdf_descriptor
);
265 lv2_remove_from_jack();
267 lv2_delete_buffers();
270 void lv2_remove_from_jack()
272 qDebug("Lv2AudioPlugin::lv2_remove_from_jack()");
275 for (i
=0; i
< evin
.count
; i
++)
276 jack_port_unregister(jack_client
, evin
.events
[i
].port
);
278 for (i
=0; i
< evout
.count
; i
++)
279 jack_port_unregister(jack_client
, evout
.events
[i
].port
);
281 // We set fake min/mout values for easy port-count-info
285 qDebug("Lv2AudioPlugin::lv2_remove_from_jack() - end");
288 void lv2_delete_buffers()
290 qDebug("Lv2AudioPlugin::lv2_delete_buffers()");
294 for (uint32_t i
=0; i
< evin
.count
; i
++)
295 free(evin
.events
[i
].buffer
);
297 delete[] evin
.events
;
302 for (uint32_t i
=0; i
< evout
.count
; i
++)
303 free(evout
.events
[i
].buffer
);
305 delete[] evout
.events
;
309 evin
.events
= nullptr;
312 evout
.events
= nullptr;
314 qDebug("Lv2AudioPlugin::lv2_delete_buffers() - end");
317 // Used for preset ports
318 int32_t get_parameter_id_from_symbol(const char* symbol
)
320 int32_t param_id
= -1;
321 for (uint32_t i
=0; i
< rdf_descriptor
->PortCount
; i
++)
323 if (LV2_IS_PORT_CONTROL(rdf_descriptor
->Ports
[i
].Type
))
327 if (strcmp(rdf_descriptor
->Ports
[i
].Symbol
, symbol
) == 0)
334 bool is_ui_resizable()
336 if (ui
.rdf_descriptor
)
338 for (uint32_t i
=0; i
< ui
.rdf_descriptor
->FeatureCount
; i
++)
340 if (strcmp(ui
.rdf_descriptor
->Features
[i
].URI
, NS_LV2UI
"fixedSize") == 0 || strcmp(ui
.rdf_descriptor
->Features
[i
].URI
, NS_LV2UI
"noUserResize") == 0)
348 bool is_ui_bridgeable(uint32_t ui_id
)
350 LV2_RDF_UI
* rdf_ui
= &rdf_descriptor
->UIs
[ui_id
];
352 for (uint32_t i
=0; i
< rdf_ui
->FeatureCount
; i
++)
354 if (strcmp(rdf_ui
->Features
[i
].URI
, LV2_INSTANCE_ACCESS_URI
) == 0 || strcmp(rdf_ui
->Features
[i
].URI
, LV2_DATA_ACCESS_URI
) == 0)
361 void reinit_external_ui()
363 qDebug("Lv2AudioPlugin::reinit_external_ui()");
366 ui
.handle
= ui
.descriptor
->instantiate(ui
.descriptor
,
368 ui
.rdf_descriptor
->Bundle
,
369 carla_lv2_ui_write_function
,
374 if (ui
.handle
&& ui
.widget
)
378 qDebug("Lv2AudioPlugin::reinit_external_ui() - Failed to re-initiate external UI");
382 callback_action(CALLBACK_SHOW_GUI
, id
, -1, 0, 0.0);
386 void update_ui_ports()
388 qDebug("Lv2AudioPlugin::lv2_ui_update_ports()");
390 if (ui
.descriptor
->port_event
)
392 for (uint32_t i
=0; i
< param
.count
; i
++)
393 ui
.descriptor
->port_event(ui
.handle
, param
.data
[i
].rindex
, sizeof(float), 0, ¶m
.buffers
[i
]);
397 uint32_t get_custom_uri_id(const char* uri
)
399 qDebug("Lv2AudioPlugin::get_custom_uri_id(%s)", uri
);
401 for (int i
=0; i
< custom_uri_ids
.count(); i
++)
403 if (custom_uri_ids
[i
] && strcmp(custom_uri_ids
[i
], uri
) == 0)
407 custom_uri_ids
.append(strdup(uri
));
408 return custom_uri_ids
.count()-1;
411 const char* get_custom_uri_string(int uri_id
)
413 qDebug("Lv2AudioPlugin::get_custom_uri_string(%i)", uri_id
);
415 if (uri_id
< custom_uri_ids
.count())
416 return custom_uri_ids
.at(uri_id
);
423 qDebug("Lv2AudioPlugin::delete_me()");
427 int set_osc_bridge_info(PluginOscBridgeInfoType
, lo_arg
**)
432 PluginCategory
get_category()
434 LV2_Property Category
= rdf_descriptor
->Type
;
437 if (Category
& LV2_CLASS_REVERB
)
438 return PLUGIN_CATEGORY_DELAY
;
441 else if (LV2_IS_GENERATOR(Category
))
442 return PLUGIN_CATEGORY_SYNTH
;
443 else if (LV2_IS_UTILITY(Category
))
444 return PLUGIN_CATEGORY_UTILITY
;
445 else if (LV2_IS_SIMULATOR(Category
))
446 return PLUGIN_CATEGORY_OUTRO
;
447 else if (LV2_IS_DELAY(Category
))
448 return PLUGIN_CATEGORY_DELAY
;
449 else if (LV2_IS_MODULATOR(Category
))
450 return PLUGIN_CATEGORY_MODULATOR
;
451 else if (LV2_IS_FILTER(Category
))
452 return PLUGIN_CATEGORY_FILTER
;
453 else if (LV2_IS_EQUALISER(Category
))
454 return PLUGIN_CATEGORY_EQ
;
455 else if (LV2_IS_SPECTRAL(Category
))
456 return PLUGIN_CATEGORY_UTILITY
;
457 else if (LV2_IS_DISTORTION(Category
))
458 return PLUGIN_CATEGORY_OUTRO
;
459 else if (LV2_IS_DYNAMICS(Category
))
460 return PLUGIN_CATEGORY_DYNAMICS
;
462 return PLUGIN_CATEGORY_NONE
;
465 void get_label(char* buf_str
)
467 strncpy(buf_str
, rdf_descriptor
->URI
, STR_MAX
);
470 void get_maker(char* buf_str
)
472 strncpy(buf_str
, rdf_descriptor
->Author
, STR_MAX
);
475 void get_copyright(char* buf_str
)
477 strncpy(buf_str
, rdf_descriptor
->License
, STR_MAX
);
480 void get_real_name(char* buf_str
)
482 strncpy(buf_str
, rdf_descriptor
->Name
, STR_MAX
);
487 return rdf_descriptor
->UniqueID
;
490 void get_parameter_name(uint32_t rindex
, char* buf_str
)
492 strncpy(buf_str
, rdf_descriptor
->Ports
[rindex
].Name
, STR_MAX
);
495 void get_parameter_symbol(uint32_t rindex
, char* buf_str
)
497 strncpy(buf_str
, rdf_descriptor
->Ports
[rindex
].Symbol
, STR_MAX
);
500 void get_parameter_label(uint32_t rindex
, char* buf_str
)
502 LV2_RDF_Port
* Port
= &rdf_descriptor
->Ports
[rindex
];
504 if (LV2_HAVE_UNIT_SYMBOL(Port
->Unit
.Hints
))
505 strncpy(buf_str
, Port
->Unit
.Symbol
, STR_MAX
);
507 else if (LV2_HAVE_UNIT(Port
->Unit
.Hints
))
509 switch (Port
->Unit
.Type
)
512 strncpy(buf_str
, "bars", STR_MAX
);
515 strncpy(buf_str
, "beats", STR_MAX
);
518 strncpy(buf_str
, "BPM", STR_MAX
);
521 strncpy(buf_str
, "ct", STR_MAX
);
524 strncpy(buf_str
, "cm", STR_MAX
);
527 strncpy(buf_str
, "(coef)", STR_MAX
);
530 strncpy(buf_str
, "dB", STR_MAX
);
532 case LV2_UNIT_DEGREE
:
533 strncpy(buf_str
, "deg", STR_MAX
);
536 strncpy(buf_str
, "Hz", STR_MAX
);
539 strncpy(buf_str
, "in", STR_MAX
);
542 strncpy(buf_str
, "kHz", STR_MAX
);
545 strncpy(buf_str
, "km", STR_MAX
);
548 strncpy(buf_str
, "m", STR_MAX
);
551 strncpy(buf_str
, "MHz", STR_MAX
);
553 case LV2_UNIT_MIDINOTE
:
554 strncpy(buf_str
, "note", STR_MAX
);
557 strncpy(buf_str
, "mi", STR_MAX
);
560 strncpy(buf_str
, "min", STR_MAX
);
563 strncpy(buf_str
, "mm", STR_MAX
);
566 strncpy(buf_str
, "ms", STR_MAX
);
569 strncpy(buf_str
, "oct", STR_MAX
);
572 strncpy(buf_str
, "%", STR_MAX
);
575 strncpy(buf_str
, "s", STR_MAX
);
577 case LV2_UNIT_SEMITONE
:
578 strncpy(buf_str
, "semi", STR_MAX
);
585 uint32_t get_scalepoint_count(uint32_t rindex
)
587 return rdf_descriptor
->Ports
[rindex
].ScalePointCount
;
590 double get_scalepoint_value(uint32_t rindex
, uint32_t scalepoint_id
)
592 return rdf_descriptor
->Ports
[rindex
].ScalePoints
[scalepoint_id
].Value
;
595 void get_scalepoint_label(uint32_t rindex
, uint32_t scalepoint_id
, char* buf_str
)
597 strncpy(buf_str
, rdf_descriptor
->Ports
[rindex
].ScalePoints
[scalepoint_id
].Label
, STR_MAX
);
600 int32_t get_chunk_data(void**)
605 void x_set_parameter_value(uint32_t parameter_id
, double value
, bool gui_send
)
607 if (gui_send
&& gui
.visible
)
611 case GUI_INTERNAL_QT4
:
612 case GUI_INTERNAL_X11
:
613 case GUI_EXTERNAL_LV2
:
614 if (ui
.descriptor
->port_event
)
616 float fvalue
= value
;
617 ui
.descriptor
->port_event(ui
.handle
, param
.data
[parameter_id
].rindex
, sizeof(float), 0, &fvalue
);
621 case GUI_EXTERNAL_OSC
:
622 osc_send_control(&osc
.data
, param
.data
[parameter_id
].rindex
, value
);
631 void x_set_program(uint32_t program_id
, bool, bool)
633 // FIXME - state or ports first? should we block audio thread while restoring state?
634 LV2_RDF_Preset
* Preset
= &rdf_descriptor
->Presets
[program_id
];
637 for (uint32_t i
=0; i
< Preset
->StateCount
; i
++)
638 set_custom_data(CUSTOM_DATA_STRING
, Preset
->States
[i
].Key
, Preset
->States
[i
].Value
, true);
643 for (uint32_t i
=0; i
< Preset
->PortCount
; i
++)
645 param_id
= get_parameter_id_from_symbol(Preset
->Ports
[i
].Symbol
);
648 set_parameter_value(param_id
, Preset
->Ports
[i
].Value
, true, false, false);
650 qWarning("Lv2Plugin::x_set_program(%i, ...) - Failed to find parameter '%s'", program_id
, Preset
->Ports
[i
].Symbol
);
654 void x_set_midi_program(uint32_t, bool, bool)
658 void x_set_custom_data(CustomDataType
, const char*, const char*, bool)
660 if ((hints
& PLUGIN_HAS_EXTENSION_STATE
) > 0 && descriptor
->extension_data
)
662 LV2_State_Interface
* state
= (LV2_State_Interface
*)descriptor
->extension_data(LV2_STATE_INTERFACE_URI
);
665 state
->restore(handle
, carla_lv2_state_retrieve
, this, 0, features
);
669 void set_chunk_data(const char*)
673 void set_gui_data(int, void* ptr
)
677 case GUI_INTERNAL_QT4
:
680 QDialog
* qtPtr
= (QDialog
*)ptr
;
681 QWidget
* widget
= (QWidget
*)ui
.widget
;
683 qtPtr
->layout()->addWidget(widget
);
684 widget
->setParent(qtPtr
);
689 case GUI_INTERNAL_X11
:
692 QDialog
* qtPtr
= (QDialog
*)ptr
;
693 features
[lv2_feature_id_ui_parent
]->data
= (void*)qtPtr
->winId();
695 ui
.handle
= ui
.descriptor
->instantiate(ui
.descriptor
,
697 ui
.rdf_descriptor
->Bundle
,
698 carla_lv2_ui_write_function
,
703 if (ui
.handle
&& ui
.descriptor
->port_event
)
713 void show_gui(bool yesno
)
717 case GUI_INTERNAL_QT4
:
721 case GUI_INTERNAL_X11
:
724 if (gui
.visible
&& gui
.width
> 0 && gui
.height
> 0)
725 callback_action(CALLBACK_RESIZE_GUI
, id
, gui
.width
, gui
.height
, 0.0);
729 case GUI_EXTERNAL_OSC
:
734 // 40 re-tries, 4 secs
735 for (int j
=1; j
<40 && gui
.show_now
; j
++)
739 osc_send_show(&osc
.data
);
748 qDebug("Lv2AudioPlugin::show_gui() - GUI timeout");
749 callback_action(CALLBACK_SHOW_GUI
, id
, 0, 0, 0.0);
754 osc_send_hide(&osc
.data
);
755 osc_send_quit(&osc
.data
);
756 osc_clear_data(&osc
.data
);
760 case GUI_EXTERNAL_LV2
:
762 reinit_external_ui();
764 if (ui
.handle
&& ui
.widget
)
768 LV2_EXTERNAL_UI_SHOW((lv2_external_ui
*)ui
.widget
);
773 LV2_EXTERNAL_UI_HIDE((lv2_external_ui
*)ui
.widget
);
776 if (ui
.descriptor
->cleanup
)
777 ui
.descriptor
->cleanup(ui
.handle
);
786 callback_action(CALLBACK_SHOW_GUI
, id
, -1, 0, 0.0);
799 case GUI_INTERNAL_QT4
:
800 case GUI_INTERNAL_X11
:
801 case GUI_EXTERNAL_LV2
:
802 if (ui
.descriptor
->port_event
)
804 for (uint32_t i
=0; i
< param
.count
; i
++)
806 if (param
.data
[i
].type
== PARAMETER_OUTPUT
&& (param
.data
[i
].hints
& PARAMETER_IS_AUTOMABLE
) > 0)
807 ui
.descriptor
->port_event(ui
.handle
, param
.data
[i
].rindex
, sizeof(float), 0, ¶m
.buffers
[i
]);
811 if (gui
.type
== GUI_EXTERNAL_LV2
)
812 LV2_EXTERNAL_UI_RUN((lv2_external_ui
*)ui
.widget
);
823 qDebug("Lv2AudioPlugin::reload()");
826 // Safely disable plugin for reload
830 if (carla_options
.global_jack_client
== false)
831 jack_deactivate(jack_client
);
835 // Unregister jack ports
836 lv2_remove_from_jack();
840 lv2_delete_buffers();
843 // All invada plugins have broken output ports
844 bool force_strict_bounds
= false;
845 if (QString(rdf_descriptor
->URI
).startsWith("http://invadarecords.com/plugins/lv2/"))
846 force_strict_bounds
= true;
848 uint32_t ains
, aouts
, cv_ins
, cv_outs
, ev_ins
, ev_outs
, params
, j
;
849 ains
= aouts
= cv_ins
= cv_outs
= ev_ins
= ev_outs
= params
= 0;
851 const uint32_t PortCount
= rdf_descriptor
->PortCount
;
853 for (uint32_t i
=0; i
<PortCount
; i
++)
855 LV2_Property PortType
= rdf_descriptor
->Ports
[i
].Type
;
856 if (LV2_IS_PORT_AUDIO(PortType
))
858 if (LV2_IS_PORT_INPUT(PortType
))
860 else if (LV2_IS_PORT_OUTPUT(PortType
))
863 else if (LV2_IS_PORT_CONTROL(PortType
))
867 else if (LV2_IS_PORT_CV(PortType
))
869 if (LV2_IS_PORT_INPUT(PortType
))
871 else if (LV2_IS_PORT_OUTPUT(PortType
))
874 else if (LV2_IS_PORT_EVENT(PortType
))
876 if (LV2_IS_PORT_INPUT(PortType
))
878 else if (LV2_IS_PORT_OUTPUT(PortType
))
883 if (params
== 0 && (hints
& PLUGIN_HAS_EXTENSION_DYNPARAM
) > 0)
885 dynparam_plugin
= (lv2dynparam_plugin_callbacks
*)descriptor
->extension_data(LV2DYNPARAM_URI
);
887 dynparam_plugin
->host_attach(handle
, &dynparam_host
, this);
892 ain
.rindexes
= new uint32_t[ains
];
893 ain
.ports
= new jack_port_t
*[ains
];
898 aout
.rindexes
= new uint32_t[aouts
];
899 aout
.ports
= new jack_port_t
*[aouts
];
904 evin
.events
= new CarlaLv2Event
[ev_ins
];
906 for (j
=0; j
< ev_ins
; j
++)
908 evin
.events
[j
].type
= 0;
909 evin
.events
[j
].port
= nullptr;
910 evin
.events
[j
].buffer
= lv2_event_buffer_new(MAX_EVENT_BUFFER
, LV2_EVENT_AUDIO_STAMP
);
916 evout
.events
= new CarlaLv2Event
[ev_outs
];
918 for (j
=0; j
<ev_outs
; j
++)
920 evout
.events
[j
].type
= 0;
921 evout
.events
[j
].port
= nullptr;
922 evout
.events
[j
].buffer
= lv2_event_buffer_new(MAX_EVENT_BUFFER
, LV2_EVENT_AUDIO_STAMP
);
928 param
.buffers
= new float[params
];
929 param
.data
= new ParameterData
[params
];
930 param
.ranges
= new ParameterRanges
[params
];
933 const int port_name_size
= jack_port_name_size();
934 char port_name
[port_name_size
];
935 bool needs_cin
= false;
936 bool needs_cout
= false;
938 for (uint32_t i
=0; i
<PortCount
; i
++)
940 LV2_Property PortType
= rdf_descriptor
->Ports
[i
].Type
;
941 LV2_Property PortProps
= rdf_descriptor
->Ports
[i
].Properties
;
942 LV2_RDF_PortPoints PortPoints
= rdf_descriptor
->Ports
[i
].Points
;
944 if (LV2_IS_PORT_AUDIO(PortType
) || LV2_IS_PORT_EVENT_MIDI(PortType
))
946 if (carla_options
.global_jack_client
)
948 strncpy(port_name
, name
, (port_name_size
/2)-2);
949 strcat(port_name
, ":");
950 strncat(port_name
, rdf_descriptor
->Ports
[i
].Name
, port_name_size
/2);
953 strncpy(port_name
, rdf_descriptor
->Ports
[i
].Name
, port_name_size
);
956 if (LV2_IS_PORT_AUDIO(PortType
))
958 if (LV2_IS_PORT_INPUT(PortType
))
961 ain
.ports
[j
] = jack_port_register(jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsInput
, 0);
964 else if (LV2_IS_PORT_OUTPUT(PortType
))
967 aout
.ports
[j
] = jack_port_register(jack_client
, port_name
, JACK_DEFAULT_AUDIO_TYPE
, JackPortIsOutput
, 0);
968 aout
.rindexes
[j
] = i
;
972 qWarning("WARNING - Got a broken Port (Audio, but not input or output)");
974 else if (LV2_IS_PORT_CONTROL(PortType
))
977 param
.data
[j
].index
= j
;
978 param
.data
[j
].rindex
= i
;
979 param
.data
[j
].hints
= 0;
980 param
.data
[j
].midi_channel
= 0;
981 param
.data
[j
].midi_cc
= -1;
983 double min
, max
, def
, step
, step_small
, step_large
;
986 if (LV2_HAVE_MINIMUM_PORT_POINT(PortPoints
.Hints
))
987 min
= PortPoints
.Minimum
;
992 if (LV2_HAVE_MAXIMUM_PORT_POINT(PortPoints
.Hints
))
993 max
= PortPoints
.Maximum
;
1003 if (LV2_HAVE_DEFAULT_PORT_POINT(PortPoints
.Hints
))
1004 def
= PortPoints
.Default
;
1008 if (min
< 0.0 && max
> 0.0)
1019 if (max
- min
== 0.0)
1021 qWarning("Broken plugin parameter -> max - min == 0");
1025 if (LV2_IS_PORT_SAMPLE_RATE(PortProps
))
1027 double sample_rate
= get_sample_rate();
1031 param
.data
[j
].hints
|= PARAMETER_USES_SAMPLERATE
;
1034 if (LV2_IS_PORT_INTEGER(PortProps
))
1040 else if (LV2_IS_PORT_TOGGLED(PortProps
))
1048 double range
= max
- min
;
1050 step_small
= range
/1000.0;
1051 step_large
= range
/10.0;
1054 if (LV2_IS_PORT_INPUT(PortType
))
1056 param
.data
[j
].type
= PARAMETER_INPUT
;
1057 param
.data
[j
].hints
|= PARAMETER_IS_ENABLED
;
1058 param
.data
[j
].hints
|= PARAMETER_IS_AUTOMABLE
;
1062 LV2_RDF_PortMidiMap
* PortMidiMap
= &rdf_descriptor
->Ports
[i
].MidiMap
;
1063 if (LV2_IS_PORT_MIDI_MAP_CC(PortMidiMap
->Type
))
1064 param
.data
[j
].midi_cc
= PortMidiMap
->Number
;
1066 else if (LV2_IS_PORT_OUTPUT(PortType
))
1068 param
.data
[j
].type
= PARAMETER_OUTPUT
;
1069 param
.data
[j
].hints
|= PARAMETER_IS_ENABLED
;
1071 if (LV2_IS_PORT_LATENCY(PortProps
) == false)
1073 param
.data
[j
].hints
|= PARAMETER_IS_AUTOMABLE
;
1078 // latency parameter
1080 max
= get_sample_rate();
1089 param
.data
[j
].type
= PARAMETER_UNKNOWN
;
1090 qWarning("WARNING - Got a broken Port (Control, but not input or output)");
1093 // extra plugin hints
1094 if (LV2_IS_PORT_ENUMERATION(PortProps
))
1095 param
.data
[j
].hints
|= PARAMETER_USES_SCALEPOINTS
;
1097 if (LV2_IS_PORT_HAS_STRICT_BOUNDS(PortProps
) || (force_strict_bounds
&& LV2_IS_PORT_OUTPUT(PortType
)))
1098 param
.data
[j
].hints
|= PARAMETER_HAS_STRICT_BOUNDS
;
1100 // check if parameter is not automable
1101 if (LV2_IS_PORT_HAS_STRICT_BOUNDS(PortProps
))
1103 //param.data[j].hints ~= ~PARAMETER_IS_AUTOMABLE;
1106 param
.ranges
[j
].min
= min
;
1107 param
.ranges
[j
].max
= max
;
1108 param
.ranges
[j
].def
= def
;
1109 param
.ranges
[j
].step
= step
;
1110 param
.ranges
[j
].step_small
= step_small
;
1111 param
.ranges
[j
].step_large
= step_large
;
1113 // Start parameters in their default values
1114 param
.buffers
[j
] = def
;
1116 descriptor
->connect_port(handle
, i
, ¶m
.buffers
[j
]);
1118 else if (LV2_IS_PORT_CV(PortType
))
1120 if (LV2_IS_PORT_INPUT(PortType
))
1122 qWarning("WARNING - CV Ports are not supported yet");
1124 else if (LV2_IS_PORT_OUTPUT(PortType
))
1126 qWarning("WARNING - CV Ports are not supported yet");
1129 qWarning("WARNING - Got a broken Port (CV, but not input or output)");
1131 descriptor
->connect_port(handle
, i
, nullptr);
1133 else if (LV2_IS_PORT_EVENT(PortType
))
1135 if (LV2_IS_PORT_INPUT(PortType
))
1138 descriptor
->connect_port(handle
, i
, evin
.events
[j
].buffer
);
1140 if (LV2_IS_PORT_EVENT_MIDI(PortType
))
1143 evin
.events
[j
].type
|= CARLA_URI_MAP_ID_EVENT_MIDI
;
1144 evin
.events
[j
].port
= jack_port_register(jack_client
, port_name
, JACK_DEFAULT_MIDI_TYPE
, JackPortIsInput
, 0);
1146 if (LV2_IS_PORT_EVENT_TIME(PortType
))
1148 evin
.events
[j
].type
|= CARLA_URI_MAP_ID_EVENT_TIME
;
1149 wants_time_pos
= true;
1152 else if (LV2_IS_PORT_OUTPUT(PortType
))
1155 descriptor
->connect_port(handle
, i
, evout
.events
[j
].buffer
);
1157 if (LV2_IS_PORT_EVENT_MIDI(PortType
))
1160 evout
.events
[j
].type
|= CARLA_URI_MAP_ID_EVENT_MIDI
;
1161 evout
.events
[j
].port
= jack_port_register(jack_client
, port_name
, JACK_DEFAULT_MIDI_TYPE
, JackPortIsOutput
, 0);
1163 if (LV2_IS_PORT_EVENT_TIME(PortType
))
1165 evout
.events
[j
].type
|= CARLA_URI_MAP_ID_EVENT_TIME
;
1169 qWarning("WARNING - Got a broken Port (Event, but not input or output)");
1172 // Port Type not supported, but it's optional anyway
1173 descriptor
->connect_port(handle
, i
, nullptr);
1178 if (carla_options
.global_jack_client
)
1180 strncpy(port_name
, name
, (port_name_size
/2)-2);
1181 strcat(port_name
, ":control-in");
1184 strcpy(port_name
, "control-in");
1186 param
.port_in
= jack_port_register(jack_client
, port_name
, JACK_DEFAULT_MIDI_TYPE
, JackPortIsInput
, 0);
1191 if (carla_options
.global_jack_client
)
1193 strncpy(port_name
, name
, (port_name_size
/2)-2);
1194 strcat(port_name
, ":control-out");
1197 strcpy(port_name
, "control-out");
1199 param
.port_out
= jack_port_register(jack_client
, port_name
, JACK_DEFAULT_MIDI_TYPE
, JackPortIsOutput
, 0);
1204 evin
.count
= ev_ins
;
1205 evout
.count
= ev_outs
;
1206 param
.count
= params
;
1208 reload_programs(true);
1210 // Check if plugin is synth
1211 if (LV2_IS_GENERATOR(rdf_descriptor
->Type
))
1212 hints
|= PLUGIN_IS_SYNTH
;
1214 // Other plugin checks
1215 if (aouts
> 0 && (ains
== aouts
|| ains
== 1))
1216 hints
|= PLUGIN_CAN_DRYWET
;
1219 hints
|= PLUGIN_CAN_VOL
;
1221 if (aouts
>= 2 && aouts
%2 == 0)
1222 hints
|= PLUGIN_CAN_BALANCE
;
1226 carla_proc_unlock();
1228 if (carla_options
.global_jack_client
== false)
1229 jack_activate(jack_client
);
1232 void reload_programs(bool init
)
1234 qDebug("Lv2AudioPlugin::reload_programs(%s)", bool2str(init
));
1237 // Delete old programs
1240 for (uint32_t i
=0; i
< prog
.count
; i
++)
1241 free((void*)prog
.names
[i
]);
1243 delete[] prog
.names
;
1247 // Query new programs
1248 prog
.count
= rdf_descriptor
->PresetCount
;
1251 prog
.names
= new const char* [prog
.count
];
1254 for (i
=0; i
< prog
.count
; i
++)
1255 prog
.names
[i
] = strdup(rdf_descriptor
->Presets
[i
].Label
);
1258 osc_send_set_program_count(&osc
.data
, id
, prog
.count
);
1262 for (i
=0; i
< prog
.count
; i
++)
1263 osc_send_set_program_name(&osc
.data
, id
, i
, prog
.names
[i
]);
1266 callback_action(CALLBACK_RELOAD_PROGRAMS
, id
, 0, 0, 0.0);
1271 set_program(0, false, false, false, true);
1274 // (LV2 programs don't change during runtime)
1277 void prepare_for_save()
1279 if ((hints
& PLUGIN_HAS_EXTENSION_STATE
) > 0 && descriptor
->extension_data
)
1281 LV2_State_Interface
* state
= (LV2_State_Interface
*)descriptor
->extension_data(LV2_STATE_INTERFACE_URI
);
1284 state
->save(handle
, carla_lv2_state_store
, this, 0, features
);
1288 void process(jack_nframes_t nframes
)
1291 unsigned short plugin_id
= id
;
1292 uint32_t midi_event_count
= 0;
1293 LV2_Event_Iterator evin_iters
[evin
.count
];
1295 double ains_peak_tmp
[2] = { 0.0 };
1296 double aouts_peak_tmp
[2] = { 0.0 };
1298 jack_default_audio_sample_t
* ains_buffer
[ain
.count
];
1299 jack_default_audio_sample_t
* aouts_buffer
[aout
.count
];
1300 jack_default_audio_sample_t
* evins_buffer
[evin
.count
];
1301 jack_default_audio_sample_t
* evouts_buffer
[evout
.count
];
1303 for (i
=0; i
< ain
.count
; i
++)
1304 ains_buffer
[i
] = (jack_default_audio_sample_t
*)jack_port_get_buffer(ain
.ports
[i
], nframes
);
1306 for (i
=0; i
< aout
.count
; i
++)
1307 aouts_buffer
[i
] = (jack_default_audio_sample_t
*)jack_port_get_buffer(aout
.ports
[i
], nframes
);
1309 for (i
=0; i
< evin
.count
; i
++)
1311 lv2_event_buffer_reset(evin
.events
[i
].buffer
, LV2_EVENT_AUDIO_STAMP
, (uint8_t*)(evin
.events
[i
].buffer
+ 1));
1312 lv2_event_begin(&evin_iters
[i
], evin
.events
[i
].buffer
);
1314 if (evin
.events
[i
].port
)
1315 evins_buffer
[i
] = (jack_default_audio_sample_t
*)jack_port_get_buffer(evin
.events
[i
].port
, nframes
);
1317 evins_buffer
[i
] = nullptr;
1320 for (i
=0; i
< evout
.count
; i
++)
1322 lv2_event_buffer_reset(evout
.events
[i
].buffer
, LV2_EVENT_AUDIO_STAMP
, (uint8_t*)(evout
.events
[i
].buffer
+ 1));
1324 if (evout
.events
[i
].port
)
1325 evouts_buffer
[i
] = (jack_default_audio_sample_t
*)jack_port_get_buffer(evout
.events
[i
].port
, nframes
);
1327 evouts_buffer
[i
] = nullptr;
1330 // --------------------------------------------------------------------------------------------------------
1335 short j2
= (ain
.count
== 1) ? 0 : 1;
1337 for (k
=0; k
<nframes
; k
++)
1339 if (ains_buffer
[0][k
] > ains_peak_tmp
[0])
1340 ains_peak_tmp
[0] = ains_buffer
[0][k
];
1341 if (ains_buffer
[j2
][k
] > ains_peak_tmp
[1])
1342 ains_peak_tmp
[1] = ains_buffer
[j2
][k
];
1346 CARLA_PROCESS_CONTINUE_CHECK
;
1348 // --------------------------------------------------------------------------------------------------------
1349 // Parameters Input [Automation]
1353 jack_default_audio_sample_t
* pin_buffer
= (jack_default_audio_sample_t
*)jack_port_get_buffer(param
.port_in
, nframes
);
1355 jack_midi_event_t pin_event
;
1356 uint32_t n_pin_events
= jack_midi_get_event_count(pin_buffer
);
1358 for (i
=0; i
<n_pin_events
; i
++)
1360 if (jack_midi_event_get(&pin_event
, pin_buffer
, i
) != 0)
1363 unsigned char channel
= pin_event
.buffer
[0] & 0x0F;
1364 unsigned char mode
= pin_event
.buffer
[0] & 0xF0;
1369 unsigned char status
= pin_event
.buffer
[1] & 0x7F;
1370 unsigned char velo
= pin_event
.buffer
[2] & 0x7F;
1371 double value
, velo_per
= double(velo
)/127;
1373 // Control GUI stuff (channel 0 only)
1379 set_active(false, false, false);
1380 postpone_event(PostEventParameter
, PARAMETER_ACTIVE
, 0.0);
1383 else if (status
== 0x09 && hints
& PLUGIN_CAN_DRYWET
)
1385 // Dry/Wet (using '0x09', undefined)
1386 set_drywet(velo_per
, false, false);
1387 postpone_event(PostEventParameter
, PARAMETER_DRYWET
, velo_per
);
1389 else if (status
== 0x07 && hints
& PLUGIN_CAN_VOL
)
1392 value
= double(velo
)/100;
1393 set_vol(value
, false, false);
1394 postpone_event(PostEventParameter
, PARAMETER_VOLUME
, value
);
1396 else if (status
== 0x08 && hints
& PLUGIN_CAN_BALANCE
)
1400 value
= (double(velo
)-63.5)/63.5;
1405 right
= (value
*2)+1.0;
1409 left
= (value
*2)-1.0;
1418 set_balance_left(left
, false, false);
1419 set_balance_right(right
, false, false);
1420 postpone_event(PostEventParameter
, PARAMETER_BALANCE_LEFT
, left
);
1421 postpone_event(PostEventParameter
, PARAMETER_BALANCE_RIGHT
, right
);
1425 // Control plugin parameters
1426 for (k
=0; k
< param
.count
; k
++)
1428 if (param
.data
[k
].type
== PARAMETER_INPUT
&& (param
.data
[k
].hints
& PARAMETER_IS_AUTOMABLE
) > 0 &&
1429 param
.data
[k
].midi_channel
== channel
&& param
.data
[k
].midi_cc
== status
)
1431 value
= (velo_per
* (param
.ranges
[k
].max
- param
.ranges
[k
].min
)) + param
.ranges
[k
].min
;
1432 set_parameter_value(k
, value
, false, false, false);
1433 postpone_event(PostEventParameter
, k
, value
);
1438 else if (mode
== 0xC0)
1440 unsigned char program
= pin_event
.buffer
[1] & 0x7F;
1442 if (program
< prog
.count
)
1444 set_program(program
, false, false, false, false);
1445 postpone_event(PostEventProgram
, program
, 0.0);
1449 } // End of Parameters Input
1451 CARLA_PROCESS_CONTINUE_CHECK
;
1453 // --------------------------------------------------------------------------------------------------------
1454 // MIDI Input (External)
1460 for (i
=0; i
<MAX_MIDI_EVENTS
&& midi_event_count
< MAX_MIDI_EVENTS
; i
++)
1462 if (ExternalMidiNotes
[i
].valid
)
1464 if (ExternalMidiNotes
[i
].plugin_id
== plugin_id
)
1466 ExternalMidiNote
* enote
= &ExternalMidiNotes
[i
];
1467 enote
->valid
= false;
1469 for (i
=0; i
< evin
.count
; i
++)
1471 if (evin
.events
[i
].type
& CARLA_URI_MAP_ID_EVENT_MIDI
)
1473 uint8_t* midi_event
= lv2_event_reserve(&evin_iters
[i
], 0, 0, CARLA_URI_MAP_ID_EVENT_MIDI
, 3);
1477 midi_event
[0] = enote
->onoff
? 0x90 : 0x80;
1478 midi_event
[1] = enote
->note
;
1479 midi_event
[2] = enote
->velo
;
1484 midi_event_count
+= 1;
1490 carla_midi_unlock();
1492 } // End of MIDI Input (External)
1494 CARLA_PROCESS_CONTINUE_CHECK
;
1496 // --------------------------------------------------------------------------------------------------------
1497 // MIDI Input (JACK)
1499 for (i
=0; i
< evin
.count
; i
++)
1501 if (!evins_buffer
[i
])
1504 uint32_t n_min_events
= jack_midi_get_event_count(evins_buffer
[i
]);
1505 jack_midi_event_t min_event
;
1507 for (k
=0; k
<n_min_events
&& midi_event_count
< MAX_MIDI_EVENTS
; k
++)
1509 if (jack_midi_event_get(&min_event
, evins_buffer
[i
], k
) != 0)
1512 //unsigned char channel = min_event.buffer[0] & 0x0F;
1513 unsigned char mode
= min_event
.buffer
[0] & 0xF0;
1514 unsigned char note
= min_event
.buffer
[1] & 0x7F;
1515 unsigned char velo
= min_event
.buffer
[2] & 0x7F;
1517 // fix bad note off (GUI only)
1518 if (mode
== 0x90 && velo
== 0)
1525 postpone_event(PostEventNoteOff
, note
, velo
);
1526 else if (mode
== 0x90)
1527 postpone_event(PostEventNoteOn
, note
, velo
);
1529 lv2_event_write(&evin_iters
[i
], min_event
.time
, 0, CARLA_URI_MAP_ID_EVENT_MIDI
, min_event
.size
, min_event
.buffer
);
1531 midi_event_count
+= 1;
1533 } // End of MIDI Input (JACK)
1535 CARLA_PROCESS_CONTINUE_CHECK
;
1537 // --------------------------------------------------------------------------------------------------------
1542 jack_position_t pos
;
1543 jack_transport_state_t state
= jack_transport_query(jack_client
, &pos
);
1545 carla_lv2_time_pos
.frame
= 0;
1546 carla_lv2_time_pos
.flags
= 0;
1547 carla_lv2_time_pos
.bar
= 0;
1548 carla_lv2_time_pos
.beat
= 0;
1549 carla_lv2_time_pos
.tick
= 0;
1550 carla_lv2_time_pos
.beats_per_bar
= 0;
1551 carla_lv2_time_pos
.beat_type
= 0;
1552 carla_lv2_time_pos
.ticks_per_beat
= 0;
1553 carla_lv2_time_pos
.beats_per_minute
= 0.0;
1555 if (state
== JackTransportRolling
)
1556 carla_lv2_time_pos
.state
= LV2_TIME_ROLLING
;
1558 carla_lv2_time_pos
.state
= LV2_TIME_STOPPED
;
1560 if (pos
.unique_1
== pos
.unique_2
)
1562 carla_lv2_time_pos
.frame
= pos
.frame
;
1564 if (pos
.valid
& JackPositionBBT
)
1566 carla_lv2_time_pos
.bar
= pos
.bar
;
1567 carla_lv2_time_pos
.beat
= pos
.beat
;
1568 carla_lv2_time_pos
.tick
= pos
.tick
;
1569 carla_lv2_time_pos
.beats_per_bar
= pos
.beats_per_bar
;
1570 carla_lv2_time_pos
.beat_type
= pos
.beat_type
;
1571 carla_lv2_time_pos
.ticks_per_beat
= pos
.ticks_per_beat
;
1572 carla_lv2_time_pos
.beats_per_minute
= pos
.beats_per_minute
;
1574 carla_lv2_time_pos
.flags
|= LV2_TIME_HAS_BBT
;
1578 for (i
=0; i
< evin
.count
; i
++)
1580 if (evin
.events
[i
].type
& CARLA_URI_MAP_ID_EVENT_TIME
)
1581 lv2_event_write(&evin_iters
[i
], 0, 0, CARLA_URI_MAP_ID_EVENT_TIME
, sizeof(LV2_Time_Position
), (uint8_t*)&carla_lv2_time_pos
);
1585 // End of LV2 Transport
1587 CARLA_PROCESS_CONTINUE_CHECK
;
1589 // --------------------------------------------------------------------------------------------------------
1590 // Plugin processing
1596 if (descriptor
->activate
)
1597 descriptor
->activate(handle
);
1600 for (i
=0; i
< ain
.count
; i
++)
1601 descriptor
->connect_port(handle
, ain
.rindexes
[i
], ains_buffer
[i
]);
1603 for (i
=0; i
< aout
.count
; i
++)
1604 descriptor
->connect_port(handle
, aout
.rindexes
[i
], aouts_buffer
[i
]);
1606 if (descriptor
->run
)
1607 descriptor
->run(handle
, nframes
);
1613 if (descriptor
->deactivate
)
1614 descriptor
->deactivate(handle
);
1618 CARLA_PROCESS_CONTINUE_CHECK
;
1620 // --------------------------------------------------------------------------------------------------------
1621 // Post-processing (dry/wet, volume and balance)
1625 double bal_rangeL
, bal_rangeR
;
1626 jack_default_audio_sample_t old_bal_left
[nframes
];
1628 for (i
=0; i
< aout
.count
; i
++)
1630 // Dry/Wet and Volume
1631 for (k
=0; k
<nframes
; k
++)
1633 if (hints
& PLUGIN_CAN_DRYWET
&& x_drywet
!= 1.0)
1635 if (aout
.count
== 1)
1636 aouts_buffer
[i
][k
] = (aouts_buffer
[i
][k
]*x_drywet
)+(ains_buffer
[0][k
]*(1.0-x_drywet
));
1638 aouts_buffer
[i
][k
] = (aouts_buffer
[i
][k
]*x_drywet
)+(ains_buffer
[i
][k
]*(1.0-x_drywet
));
1641 if (hints
& PLUGIN_CAN_VOL
)
1642 aouts_buffer
[i
][k
] *= x_vol
;
1646 if (hints
& PLUGIN_CAN_BALANCE
)
1649 memcpy(&old_bal_left
, aouts_buffer
[i
], sizeof(jack_default_audio_sample_t
)*nframes
);
1651 bal_rangeL
= (x_bal_left
+1.0)/2;
1652 bal_rangeR
= (x_bal_right
+1.0)/2;
1654 for (k
=0; k
<nframes
; k
++)
1659 aouts_buffer
[i
][k
] = old_bal_left
[k
]*(1.0-bal_rangeL
);
1660 aouts_buffer
[i
][k
] += aouts_buffer
[i
+1][k
]*(1.0-bal_rangeR
);
1665 aouts_buffer
[i
][k
] = aouts_buffer
[i
][k
]*bal_rangeR
;
1666 aouts_buffer
[i
][k
] += old_bal_left
[k
]*bal_rangeL
;
1674 for (k
=0; k
<nframes
; k
++)
1676 if (aouts_buffer
[i
][k
] > aouts_peak_tmp
[i
])
1677 aouts_peak_tmp
[i
] = aouts_buffer
[i
][k
];
1684 // disable any output sound if not active
1685 for (i
=0; i
< aout
.count
; i
++)
1686 memset(aouts_buffer
[i
], 0.0f
, sizeof(jack_default_audio_sample_t
)*nframes
);
1688 aouts_peak_tmp
[0] = 0.0;
1689 aouts_peak_tmp
[1] = 0.0;
1691 } // End of Post-processing
1693 CARLA_PROCESS_CONTINUE_CHECK
;
1695 // --------------------------------------------------------------------------------------------------------
1700 jack_default_audio_sample_t
* cout_buffer
= (jack_default_audio_sample_t
*)jack_port_get_buffer(param
.port_out
, nframes
);
1701 jack_midi_clear_buffer(cout_buffer
);
1705 for (k
=0; k
< param
.count
; k
++)
1707 if (param
.data
[k
].type
== PARAMETER_OUTPUT
&& param
.data
[k
].midi_cc
>= 0)
1709 value_per
= (param
.buffers
[k
] - param
.ranges
[k
].min
)/(param
.ranges
[k
].max
- param
.ranges
[k
].min
);
1711 jack_midi_data_t
* event_buffer
= jack_midi_event_reserve(cout_buffer
, 0, 3);
1712 event_buffer
[0] = 0xB0 + param
.data
[k
].midi_channel
;
1713 event_buffer
[1] = param
.data
[k
].midi_cc
;
1714 event_buffer
[2] = 127*value_per
;
1717 } // End of Control Output
1719 CARLA_PROCESS_CONTINUE_CHECK
;
1721 // --------------------------------------------------------------------------------------------------------
1724 for (i
=0; i
< evout
.count
; i
++)
1726 if (!evouts_buffer
[i
])
1729 jack_midi_clear_buffer(evouts_buffer
[i
]);
1734 LV2_Event_Iterator iter
;
1735 lv2_event_begin(&iter
, evout
.events
[i
].buffer
);
1737 for (k
=0; k
< iter
.buf
->event_count
; k
++)
1739 ev
= lv2_event_get(&iter
, &data
);
1741 jack_midi_event_write(evouts_buffer
[i
], ev
->frames
, data
, ev
->size
);
1743 lv2_event_increment(&iter
);
1745 } // End of MIDI Output
1747 CARLA_PROCESS_CONTINUE_CHECK
;
1749 // --------------------------------------------------------------------------------------------------------
1752 ains_peak
[(plugin_id
*2)+0] = ains_peak_tmp
[0];
1753 ains_peak
[(plugin_id
*2)+1] = ains_peak_tmp
[1];
1754 aouts_peak
[(plugin_id
*2)+0] = aouts_peak_tmp
[0];
1755 aouts_peak
[(plugin_id
*2)+1] = aouts_peak_tmp
[1];
1757 active_before
= active
;
1760 void buffer_size_changed(jack_nframes_t
)
1764 // ----------------- URI-Map Feature -------------------------------------------------
1765 static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data
, const char* map
, const char* uri
)
1767 qDebug("Lv2AudioPlugin::carla_lv2_uri_to_id(%p, %s, %s)", data
, map
, uri
);
1769 if (map
&& strcmp(map
, LV2_EVENT_URI
) == 0)
1772 if (strcmp(uri
, "http://lv2plug.in/ns/ext/midi#MidiEvent") == 0)
1773 return CARLA_URI_MAP_ID_EVENT_MIDI
;
1774 else if (strcmp(uri
, "http://lv2plug.in/ns/ext/time#Position") == 0)
1775 return CARLA_URI_MAP_ID_EVENT_TIME
;
1777 else if (strcmp(uri
, NS_ATOM
"String") == 0)
1779 return CARLA_URI_MAP_ID_ATOM_STRING
;
1785 Lv2AudioPlugin
* plugin
= (Lv2AudioPlugin
*)data
;
1786 return plugin
->get_custom_uri_id(uri
);
1792 // ----------------- URID Feature ----------------------------------------------------
1793 static LV2_URID
carla_lv2_urid_map(LV2_URID_Map_Handle handle
, const char* uri
)
1795 qDebug("Lv2AudioPlugin::carla_lv2_urid_map(%p, %s)", handle
, uri
);
1797 if (strcmp(uri
, "http://lv2plug.in/ns/ext/midi#MidiEvent") == 0)
1798 return CARLA_URI_MAP_ID_EVENT_MIDI
;
1799 else if (strcmp(uri
, "http://lv2plug.in/ns/ext/time#Position") == 0)
1800 return CARLA_URI_MAP_ID_EVENT_TIME
;
1801 else if (strcmp(uri
, NS_ATOM
"String") == 0)
1802 return CARLA_URI_MAP_ID_ATOM_STRING
;
1807 Lv2AudioPlugin
* plugin
= (Lv2AudioPlugin
*)handle
;
1808 return plugin
->get_custom_uri_id(uri
);
1814 static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle
, LV2_URID urid
)
1816 qDebug("Lv2AudioPlugin::carla_lv2_urid_unmap(%p, %i)", handle
, urid
);
1818 if (urid
== CARLA_URI_MAP_ID_EVENT_MIDI
)
1819 return "http://lv2plug.in/ns/ext/midi#MidiEvent";
1820 else if (urid
== CARLA_URI_MAP_ID_EVENT_TIME
)
1821 return "http://lv2plug.in/ns/ext/time#Position";
1822 else if (urid
== CARLA_URI_MAP_ID_ATOM_STRING
)
1823 return NS_ATOM
"String";
1828 Lv2AudioPlugin
* plugin
= (Lv2AudioPlugin
*)handle
;
1829 return plugin
->get_custom_uri_string(urid
);
1835 // ----------------- DynParam Feature ------------------------------------------------
1836 static unsigned char carla_lv2_dynparam_group_appear(void* instance_host_context
,
1837 void* parent_group_host_context
,
1838 lv2dynparam_group_handle group
,
1839 const lv2dynparam_hints
* hints_ptr
,
1840 void** group_host_context
)
1842 qWarning("Lv2AudioPlugin::carla_lv2_dynparam_group_appear(%p, %p, %p, %p, %p) - %i",
1843 instance_host_context
, parent_group_host_context
, group
, hints_ptr
, group_host_context
, hints_ptr
->count
);
1845 for (unsigned char i
=0; i
< hints_ptr
->count
; i
++)
1846 qWarning("Lv2AudioPlugin::carla_lv2_dynparam_group_appear -> %s | %s", hints_ptr
->names
[i
], hints_ptr
->values
[i
]);
1851 static unsigned char carla_lv2_dynparam_group_disappear(void* instance_host_context
, void* group_host_context
)
1853 qWarning("Lv2AudioPlugin::carla_lv2_dynparam_group_disappear(%p, %p)", instance_host_context
, group_host_context
);
1857 static unsigned char carla_lv2_dynparam_parameter_appear(void* instance_host_context
,
1858 void* group_host_context
,
1859 lv2dynparam_parameter_handle parameter
,
1860 const lv2dynparam_hints
* hints_ptr
,
1861 void** parameter_host_context
)
1863 qWarning("Lv2AudioPlugin::carla_lv2_dynparam_parameter_appear(%p, %p, %p, %p, %p) - %i",
1864 instance_host_context
, group_host_context
, parameter
, hints_ptr
, parameter_host_context
, hints_ptr
->count
);
1866 for (unsigned char i
=0; i
< hints_ptr
->count
; i
++)
1867 qWarning("Lv2AudioPlugin::carla_lv2_dynparam_parameter_appear -> %s | %s", hints_ptr
->names
[i
], hints_ptr
->values
[i
]);
1872 static unsigned char carla_lv2_dynparam_parameter_disappear(void* instance_host_context
, void* parameter_host_context
)
1874 qWarning("Lv2AudioPlugin::carla_lv2_dynparam_parameter_disappear(%p, %p)", instance_host_context
, parameter_host_context
);
1878 static unsigned char carla_lv2_dynparam_parameter_change(void* instance_host_context
, void* parameter_host_context
)
1880 qWarning("Lv2AudioPlugin::carla_lv2_dynparam_parameter_change(%p, %p)", instance_host_context
, parameter_host_context
);
1884 static unsigned char carla_lv2_dynparam_command_appear(void* instance_host_context
,
1885 void* group_host_context
,
1886 lv2dynparam_command_handle command
,
1887 const lv2dynparam_hints
* hints_ptr
,
1888 void** command_host_context
)
1890 qWarning("Lv2AudioPlugin::carla_lv2_dynparam_command_appear(%p, %p, %p, %p, %p)",
1891 instance_host_context
, group_host_context
, command
, hints_ptr
, command_host_context
);
1895 static unsigned char carla_lv2_dynparam_command_disappear(void* instance_host_context
, void* command_host_context
)
1897 qWarning("Lv2AudioPlugin::carla_lv2_dynparam_(%p, %p)", instance_host_context
, command_host_context
);
1901 // ----------------- Event Feature ---------------------------------------------------
1902 static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data data
, LV2_Event
* event
)
1904 qDebug("Lv2AudioPlugin::carla_lv2_event_ref(%p, %p)", data
, event
);
1910 static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data data
, LV2_Event
* event
)
1912 qDebug("Lv2AudioPlugin::carla_lv2_event_unref(%p, %p)", data
, event
);
1918 // ----------------- State Feature ---------------------------------------------------
1919 static int carla_lv2_state_store(LV2_State_Handle handle
, uint32_t key
, const void* value
, size_t size
, uint32_t type
, uint32_t flags
)
1921 qDebug("Lv2AudioPlugin::carla_lv2_state_store(%p, %i, %p, " P_SIZE
", %i, %i)", handle
, key
, value
, size
, type
, flags
);
1925 Lv2AudioPlugin
* plugin
= (Lv2AudioPlugin
*)handle
;
1926 const char* uri_key
= plugin
->get_custom_uri_string(key
);
1928 if (uri_key
> 0 && (flags
& LV2_STATE_IS_POD
) > 0)
1930 qDebug("Lv2AudioPlugin::carla_lv2_state_store(%p, %i, %p, " P_SIZE
", %i, %i) - Got uri_key and flags", handle
, key
, value
, size
, type
, flags
);
1932 CustomDataType dtype
;
1934 if (type
== CARLA_URI_MAP_ID_ATOM_STRING
)
1935 dtype
= CUSTOM_DATA_STRING
;
1936 else if (type
>= CARLA_URI_MAP_ID_COUNT
)
1937 dtype
= CUSTOM_DATA_BINARY
;
1939 dtype
= CUSTOM_DATA_INVALID
;
1941 if (value
&& dtype
!= CUSTOM_DATA_INVALID
)
1943 // Check if we already have this key
1944 for (int i
=0; i
< plugin
->custom
.count(); i
++)
1946 if (strcmp(plugin
->custom
[i
].key
, uri_key
) == 0)
1948 free((void*)plugin
->custom
[i
].value
);
1950 if (dtype
== CUSTOM_DATA_STRING
)
1951 plugin
->custom
[i
].value
= strdup((const char*)value
);
1954 QByteArray
chunk((const char*)value
, size
);
1955 plugin
->custom
[i
].value
= strdup(chunk
.toBase64().data());
1962 // Add a new one then
1963 CustomData new_data
;
1964 new_data
.type
= dtype
;
1965 new_data
.key
= strdup(uri_key
);
1967 if (dtype
== CUSTOM_DATA_STRING
)
1968 new_data
.value
= strdup((const char*)value
);
1971 QByteArray
chunk((const char*)value
, size
);
1972 new_data
.value
= strdup(chunk
.toBase64().data());
1975 plugin
->custom
.append(new_data
);
1980 qCritical("Lv2AudioPlugin::carla_lv2_state_store(%p, %i, %p, " P_SIZE
", %i, %i) - Invalid data", handle
, key
, value
, size
, type
, flags
);
1983 qWarning("Lv2AudioPlugin::carla_lv2_state_store(%p, %i, %p, " P_SIZE
", %i, %i) - Invalid attributes", handle
, key
, value
, size
, type
, flags
);
1986 qCritical("Lv2AudioPlugin::carla_lv2_state_store(%p, %i, %p, " P_SIZE
", %i, %i) - Invalid handle", handle
, key
, value
, size
, type
, flags
);
1991 static const void* carla_lv2_state_retrieve(LV2_State_Handle handle
, uint32_t key
, size_t* size
, uint32_t* type
, uint32_t* flags
)
1993 qDebug("Lv2AudioPlugin::carla_lv2_state_retrieve(%p, %i, %p, %p, %p)", handle
, key
, size
, type
, flags
);
1997 Lv2AudioPlugin
* plugin
= (Lv2AudioPlugin
*)handle
;
1998 const char* uri_key
= plugin
->get_custom_uri_string(key
);
2002 const char* string_data
= nullptr;
2003 CustomDataType dtype
= CUSTOM_DATA_INVALID
;
2005 for (int i
=0; i
< plugin
->custom
.count(); i
++)
2007 if (strcmp(plugin
->custom
[i
].key
, uri_key
) == 0)
2009 dtype
= plugin
->custom
[i
].type
;
2010 string_data
= plugin
->custom
[i
].value
;
2021 if (dtype
== CUSTOM_DATA_STRING
)
2023 *type
= CARLA_URI_MAP_ID_ATOM_STRING
;
2026 else if (dtype
== CUSTOM_DATA_BINARY
)
2028 static QByteArray chunk
;
2029 chunk
= QByteArray::fromBase64(string_data
);
2031 *size
= chunk
.size();
2033 return chunk
.data();
2036 qCritical("Lv2AudioPlugin::carla_lv2_state_retrieve(%p, %i, %p, %p, %p) - Invalid key type", handle
, key
, size
, type
, flags
);
2039 qCritical("Lv2AudioPlugin::carla_lv2_state_retrieve(%p, %i, %p, %p, %p) - Invalid key", handle
, key
, size
, type
, flags
);
2042 qCritical("Lv2AudioPlugin::carla_lv2_state_retrieve(%p, %i, %p, %p, %p) - Failed to find key", handle
, key
, size
, type
, flags
);
2045 qCritical("Lv2AudioPlugin::carla_lv2_state_retrieve(%p, %i, %p, %p, %p) - Invalid handle", handle
, key
, size
, type
, flags
);
2051 static char* carla_lv2_state_get_abstract_path(LV2_State_Map_Path_Handle handle
, const char* absolute_path
)
2053 qDebug("Lv2AudioPlugin::carla_lv2_state_get_abstract_path(%p, %s)", handle
, absolute_path
);
2057 static char* carla_lv2_state_get_absolute_path(LV2_State_Map_Path_Handle handle
, const char* abstract_path
)
2059 qDebug("Lv2AudioPlugin::carla_lv2_state_get_absolute_path(%p, %s)", handle
, abstract_path
);
2063 static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle
, const char* path
)
2065 qDebug("Lv2AudioPlugin::carla_lv2_state_make_path(%p, %s)", handle
, path
);
2070 // ----------------- External UI Feature ---------------------------------------------
2071 static void carla_lv2_external_ui_closed(LV2UI_Controller controller
)
2073 qDebug("Lv2AudioPlugin::carla_lv2_external_ui_closed(%p)", controller
);
2077 Lv2AudioPlugin
* plugin
= (Lv2AudioPlugin
*)controller
;
2078 plugin
->gui
.visible
= false;
2080 if (plugin
->ui
.descriptor
->cleanup
)
2081 plugin
->ui
.descriptor
->cleanup(plugin
->ui
.handle
);
2083 plugin
->ui
.handle
= nullptr;
2084 callback_action(CALLBACK_SHOW_GUI
, plugin
->id
, 0, 0, 0.0);
2088 // ----------------- UI Resize Feature -----------------------------------------------
2089 static int carla_lv2_ui_resize(LV2_UI_Resize_Feature_Data data
, int width
, int height
)
2091 qDebug("Lv2AudioPlugin::carla_lv2_ui_resized(%p, %i, %i)", data
, width
, height
);
2095 Lv2AudioPlugin
* plugin
= (Lv2AudioPlugin
*)data
;
2096 plugin
->gui
.width
= width
;
2097 plugin
->gui
.height
= height
;
2098 callback_action(CALLBACK_RESIZE_GUI
, plugin
->id
, width
, height
, 0.0);
2105 // ----------------- UI Extension ----------------------------------------------------
2106 static void carla_lv2_ui_write_function(LV2UI_Controller controller
, uint32_t port_index
, uint32_t buffer_size
, uint32_t format
, const void* buffer
)
2108 qDebug("Lv2AudioPlugin::carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller
, port_index
, buffer_size
, format
, buffer
);
2112 Lv2AudioPlugin
* plugin
= (Lv2AudioPlugin
*)controller
;
2114 if (format
== 0 && buffer_size
== sizeof(float))
2116 int32_t param_id
= -1;
2117 float value
= *(float*)buffer
;
2119 for (uint32_t i
=0; i
< plugin
->param
.count
; i
++)
2121 if (plugin
->param
.data
[i
].rindex
== (int32_t)port_index
)
2123 param_id
= i
; //plugin->param.data[i].index;
2128 if (param_id
> -1) // && plugin->ctrl.data[param_id].type == PARAMETER_INPUT
2129 plugin
->set_parameter_value(param_id
, value
, false, true, true);
2134 bool ui_lib_open(const char* filename
)
2137 ui
.lib
= LoadLibraryA(filename
);
2139 ui
.lib
= dlopen(filename
, RTLD_NOW
);
2141 return bool(ui
.lib
);
2148 return FreeLibrary((HMODULE
)ui
.lib
) != 0;
2150 return dlclose(ui
.lib
) != 0;
2156 void* ui_lib_symbol(const char* symbol
)
2160 return (void*)GetProcAddress((HMODULE
)ui
.lib
, symbol
);
2162 return dlsym(ui
.lib
, symbol
);
2168 const char* ui_lib_error()
2171 return "Unknown error";
2178 Lv2OscGuiThread::Lv2OscGuiThread(QObject
* parent
) :
2185 void Lv2OscGuiThread::set_plugin_id(short plugin_id_
)
2187 plugin_id
= plugin_id_
;
2190 void Lv2OscGuiThread::set_ui_type(LV2_Property ui_type_
)
2195 void Lv2OscGuiThread::run()
2197 if (plugin_id
< 0 || plugin_id
> MAX_PLUGINS
)
2199 qCritical("Lv2OscGuiThread::run() - invalid plugin id '%i'", plugin_id
);
2203 Lv2AudioPlugin
* plugin
= (Lv2AudioPlugin
*)AudioPlugins
[plugin_id
];
2207 qCritical("Lv2OscGuiThread::run() - invalid plugin");
2211 if (plugin
->id
!= plugin_id
)
2213 qCritical("Lv2OscGuiThread::run() - plugin mismatch '%i'' != '%i'", plugin
->id
, plugin_id
);
2217 const char* bridge_bin
= nullptr;
2222 bridge_bin
= carla_options
.bridge_path_lv2_x11
;
2225 bridge_bin
= carla_options
.bridge_path_lv2_gtk2
;
2228 bridge_bin
= carla_options
.bridge_path_lv2_qt4
;
2231 qCritical("Lv2OscGuiThread::run() - invalid UI Type");
2235 QString cmd
= QString("%1 '%2' '%3 (GUI)' '%4' '%5' '%6' '%7/%8' %9").arg(bridge_bin
).arg(plugin
->descriptor
->URI
).arg(plugin
->name
).arg(plugin
->ui
.descriptor
->URI
).arg(
2236 plugin
->ui
.rdf_descriptor
->Binary
).arg(plugin
->ui
.rdf_descriptor
->Bundle
).arg(get_host_osc_url()).arg(plugin_id
).arg(plugin
->is_ui_resizable() ? "true" : "false");
2238 qDebug("Lv2OscGuiThread::run() - command: %s", cmd
.toStdString().data());
2242 while (plugin
&& plugin
->id
== plugin_id
&& carla_is_engine_running())
2244 if (plugin
->gui
.show_now
)
2246 ret
= system(cmd
.toStdString().data());
2247 plugin
->gui
.visible
= false;
2248 plugin
->gui
.show_now
= false;
2253 callback_action(CALLBACK_SHOW_GUI
, plugin_id
, 0, 0, 0.0);
2258 callback_action(CALLBACK_SHOW_GUI
, plugin_id
, -1, 0, 0.0);
2259 qWarning("Lv2OscGuiThread::run() - LV2 OSC GUI crashed");
2267 if (plugin
->gui
.name
)
2268 free((void*)plugin
->gui
.name
);
2270 plugin
->gui
.name
= nullptr;
2271 plugin
->gui
.visible
= false;
2274 short add_plugin_lv2(const char* filename
, const char* label
, void* extra_stuff
)
2276 qDebug("add_plugin_lv2(%s, %s, %p)", filename
, label
, extra_stuff
);
2278 int id
= get_new_plugin_id();
2282 LV2_RDF_Descriptor
* rdf_descriptor
= (LV2_RDF_Descriptor
*)extra_stuff
;
2286 Lv2AudioPlugin
* plugin
= new Lv2AudioPlugin
;
2288 if ((plugin
->lib_open(rdf_descriptor
->Binary
)))
2290 LV2_Descriptor_Function descfn
= (LV2_Descriptor_Function
)plugin
->lib_symbol("lv2_descriptor");
2295 while ((plugin
->descriptor
= descfn(i
++)))
2297 if (strcmp(plugin
->descriptor
->URI
, label
) == 0)
2301 if (plugin
->descriptor
)
2303 bool can_continue
= true;
2305 // Check supported ports
2306 for (i
=0; i
< rdf_descriptor
->PortCount
; i
++)
2308 LV2_Property PortType
= rdf_descriptor
->Ports
[i
].Type
;
2309 if (!LV2_IS_PORT_AUDIO(PortType
) && !LV2_IS_PORT_CONTROL(PortType
) && !LV2_IS_PORT_CV(PortType
) && !LV2_IS_PORT_EVENT(PortType
))
2311 if (!LV2_IS_PORT_OPTIONAL(rdf_descriptor
->Ports
[i
].Properties
))
2313 set_last_error("Plugin requires a port that is not currently supported");
2314 can_continue
= false;
2320 // Check supported features
2321 for (i
=0; i
< rdf_descriptor
->FeatureCount
; i
++)
2323 if (is_lv2_feature_supported(rdf_descriptor
->Features
[i
].URI
) == false)
2325 if (LV2_IS_FEATURE_REQUIRED(rdf_descriptor
->Features
[i
].Type
))
2327 QString msg
= QString("Plugin requires a feature that is not supported:\n%1").arg(rdf_descriptor
->Features
[i
].URI
);
2328 set_last_error(msg
.toStdString().data());
2329 can_continue
= false;
2333 qDebug("Plugin wants a feature that is not supported:\n%s", rdf_descriptor
->Features
[i
].URI
);
2338 for (i
=0; i
< rdf_descriptor
->ExtensionCount
; i
++)
2340 if (strcmp(rdf_descriptor
->Extensions
[i
], LV2_STATE_INTERFACE_URI
) == 0)
2341 plugin
->hints
|= PLUGIN_HAS_EXTENSION_STATE
;
2342 else if (strcmp(rdf_descriptor
->Extensions
[i
], LV2DYNPARAM_URI
) == 0)
2343 plugin
->hints
|= PLUGIN_HAS_EXTENSION_DYNPARAM
;
2345 qDebug("Plugin has non-supported extension: '%s'", rdf_descriptor
->Extensions
[i
]);
2350 // Initialize features
2351 LV2_URI_Map_Feature
* URI_Map_Feature
= new LV2_URI_Map_Feature
;
2352 URI_Map_Feature
->callback_data
= plugin
;
2353 URI_Map_Feature
->uri_to_id
= plugin
->carla_lv2_uri_to_id
;
2355 LV2_URID_Map
* URID_Map_Feature
= new LV2_URID_Map
;
2356 URID_Map_Feature
->handle
= plugin
;
2357 URID_Map_Feature
->map
= plugin
->carla_lv2_urid_map
;
2359 LV2_URID_Unmap
* URID_Unmap_Feature
= new LV2_URID_Unmap
;
2360 URID_Unmap_Feature
->handle
= plugin
;
2361 URID_Unmap_Feature
->unmap
= plugin
->carla_lv2_urid_unmap
;
2363 LV2_Event_Feature
* Event_Feature
= new LV2_Event_Feature
;
2364 Event_Feature
->callback_data
= plugin
;
2365 Event_Feature
->lv2_event_ref
= plugin
->carla_lv2_event_ref
;
2366 Event_Feature
->lv2_event_unref
= plugin
->carla_lv2_event_unref
;
2369 LV2_State_Map_Path
* State_Map_Path_Feature
= new LV2_State_Map_Path
;
2370 State_Map_Path_Feature
->handle
= plugin
;
2371 State_Map_Path_Feature
->abstract_path
= plugin
->carla_lv2_state_get_abstract_path
;
2372 State_Map_Path_Feature
->absolute_path
= plugin
->carla_lv2_state_get_absolute_path
;
2374 LV2_State_Make_Path
* State_Make_Path_Feature
= new LV2_State_Make_Path
;
2375 State_Make_Path_Feature
->handle
= plugin
;
2376 State_Make_Path_Feature
->path
= plugin
->carla_lv2_state_make_path
;
2379 plugin
->features
[lv2_feature_id_uri_map
] = new LV2_Feature
;
2380 plugin
->features
[lv2_feature_id_uri_map
]->URI
= LV2_URI_MAP_URI
;
2381 plugin
->features
[lv2_feature_id_uri_map
]->data
= URI_Map_Feature
;
2383 plugin
->features
[lv2_feature_id_urid_map
] = new LV2_Feature
;
2384 plugin
->features
[lv2_feature_id_urid_map
]->URI
= LV2_URID_MAP_URI
;
2385 plugin
->features
[lv2_feature_id_urid_map
]->data
= URID_Map_Feature
;
2387 plugin
->features
[lv2_feature_id_urid_unmap
] = new LV2_Feature
;
2388 plugin
->features
[lv2_feature_id_urid_unmap
]->URI
= LV2_URID_UNMAP_URI
;
2389 plugin
->features
[lv2_feature_id_urid_unmap
]->data
= URID_Unmap_Feature
;
2391 plugin
->features
[lv2_feature_id_event
] = new LV2_Feature
;
2392 plugin
->features
[lv2_feature_id_event
]->URI
= LV2_EVENT_URI
;
2393 plugin
->features
[lv2_feature_id_event
]->data
= Event_Feature
;
2395 /* initialize rtsafe mempool host feature */
2396 rtmempool_allocator_init(&plugin
->rtmempool_allocator
);
2398 plugin
->features
[lv2_feature_id_rtmempool
] = new LV2_Feature
;
2399 plugin
->features
[lv2_feature_id_rtmempool
]->URI
= LV2_RTSAFE_MEMORY_POOL_URI
;
2400 plugin
->features
[lv2_feature_id_rtmempool
]->data
= &plugin
->rtmempool_allocator
;
2403 plugin
->features
[lv2_feature_id_state_map_path
] = new LV2_Feature
;
2404 plugin
->features
[lv2_feature_id_state_map_path
]->URI
= LV2_STATE_MAP_PATH_URI
;
2405 plugin
->features
[lv2_feature_id_state_map_path
]->data
= State_Map_Path_Feature
;
2407 plugin
->features
[lv2_feature_id_state_make_path
] = new LV2_Feature
;
2408 plugin
->features
[lv2_feature_id_state_make_path
]->URI
= LV2_STATE_MAKE_PATH_URI
;
2409 plugin
->features
[lv2_feature_id_state_make_path
]->data
= State_Make_Path_Feature
;
2412 plugin
->handle
= plugin
->descriptor
->instantiate(plugin
->descriptor
, get_sample_rate(), rdf_descriptor
->Bundle
, plugin
->features
);
2416 plugin
->name
= get_unique_name(rdf_descriptor
->Name
);
2417 plugin
->filename
= strdup(filename
);
2419 plugin
->rdf_descriptor
= lv2_rdf_dup(rdf_descriptor
);
2421 if (carla_options
.global_jack_client
)
2422 plugin
->jack_client
= carla_jack_client
;
2424 carla_jack_register_plugin(plugin
);
2426 if (plugin
->jack_client
)
2431 unique_names
[id
] = plugin
->name
;
2432 AudioPlugins
[id
] = plugin
;
2434 // ----------------- GUI Stuff -------------------------------------------------------
2436 uint32_t UICount
= plugin
->rdf_descriptor
->UICount
;
2440 // Find more appropriate UI (Qt4 -> X11 -> Gtk2 -> External, use bridges whenever possible)
2441 int eQt4
, eX11
, eGtk2
, iX11
, iQt4
, iExt
, iFinal
;
2442 eQt4
= eX11
= eGtk2
= iQt4
= iX11
= iExt
= iFinal
= -1;
2444 for (i
=0; i
< UICount
; i
++)
2446 switch(plugin
->rdf_descriptor
->UIs
[i
].Type
)
2449 if (plugin
->is_ui_bridgeable(i
))
2456 if (plugin
->is_ui_bridgeable(i
))
2466 case LV2_UI_EXTERNAL
:
2467 case LV2_UI_OLD_EXTERNAL
:
2480 else if (eGtk2
>= 0)
2489 bool is_bridged
= (iFinal
== eQt4
|| iFinal
== eX11
|| iFinal
== eGtk2
);
2491 // Use proper UI now
2494 plugin
->ui
.rdf_descriptor
= &plugin
->rdf_descriptor
->UIs
[iFinal
];
2496 // Check supported UI features
2497 can_continue
= true;
2499 for (i
=0; i
< plugin
->ui
.rdf_descriptor
->FeatureCount
; i
++)
2501 if (is_lv2_ui_feature_supported(plugin
->ui
.rdf_descriptor
->Features
[i
].URI
) == false)
2503 if (LV2_IS_FEATURE_REQUIRED(plugin
->ui
.rdf_descriptor
->Features
[i
].Type
))
2505 qCritical("Plugin requires a feature that is not supported:\n%s", plugin
->ui
.rdf_descriptor
->Features
[i
].URI
);
2506 can_continue
= false;
2510 qDebug("UI wants a feature that is not supported:\n%s", plugin
->ui
.rdf_descriptor
->Features
[i
].URI
);
2516 if ((plugin
->ui_lib_open(plugin
->ui
.rdf_descriptor
->Binary
)))
2518 LV2UI_DescriptorFunction ui_descfn
= (LV2UI_DescriptorFunction
)plugin
->ui_lib_symbol("lv2ui_descriptor");
2523 while ((plugin
->ui
.descriptor
= ui_descfn(i
++)))
2525 if (strcmp(plugin
->ui
.descriptor
->URI
, plugin
->ui
.rdf_descriptor
->URI
) == 0)
2529 if (plugin
->ui
.descriptor
)
2532 char gui_name
[strlen(plugin
->name
)+7];
2533 sprintf(gui_name
, "%s (GUI)", plugin
->name
);
2534 plugin
->gui
.name
= strdup(gui_name
);
2536 LV2_Property UiType
= plugin
->ui
.rdf_descriptor
->Type
;
2540 Lv2OscGuiThread
* lv2_thread
= new Lv2OscGuiThread();
2541 lv2_thread
->set_plugin_id(plugin
->id
);
2542 lv2_thread
->set_ui_type(UiType
);
2543 lv2_thread
->start();
2545 plugin
->gui
.type
= GUI_EXTERNAL_OSC
;
2546 plugin
->osc
.thread
= lv2_thread
;
2551 qDebug("Will use LV2 Qt4 UI, bridged");
2555 qDebug("Will use LV2 X11 UI, bridged");
2559 qDebug("Will use LV2 Gtk2 UI, bridged");
2563 qDebug("Will use LV2 Unknown UI, bridged");
2569 // Initialize UI features
2570 LV2_Extension_Data_Feature
* UI_Data_Feature
= new LV2_Extension_Data_Feature
;
2571 UI_Data_Feature
->data_access
= plugin
->descriptor
->extension_data
;
2573 LV2_UI_Resize_Feature
* UI_Resize_Feature
= new LV2_UI_Resize_Feature
;
2574 UI_Resize_Feature
->data
= plugin
;
2575 UI_Resize_Feature
->ui_resize
= plugin
->carla_lv2_ui_resize
;
2577 lv2_external_ui_host
* External_UI_Feature
= new lv2_external_ui_host
;
2578 External_UI_Feature
->ui_closed
= plugin
->carla_lv2_external_ui_closed
;
2579 External_UI_Feature
->plugin_human_id
= plugin
->gui
.name
;
2581 plugin
->features
[lv2_feature_id_data_access
] = new LV2_Feature
;
2582 plugin
->features
[lv2_feature_id_data_access
]->URI
= LV2_DATA_ACCESS_URI
;
2583 plugin
->features
[lv2_feature_id_data_access
]->data
= UI_Data_Feature
;
2585 plugin
->features
[lv2_feature_id_instance_access
] = new LV2_Feature
;
2586 plugin
->features
[lv2_feature_id_instance_access
]->URI
= LV2_INSTANCE_ACCESS_URI
;
2587 plugin
->features
[lv2_feature_id_instance_access
]->data
= plugin
->handle
;
2589 plugin
->features
[lv2_feature_id_ui_resize
] = new LV2_Feature
;
2590 plugin
->features
[lv2_feature_id_ui_resize
]->URI
= LV2_UI_RESIZE_URI
"#UIResize";
2591 plugin
->features
[lv2_feature_id_ui_resize
]->data
= UI_Resize_Feature
;
2593 plugin
->features
[lv2_feature_id_ui_parent
] = new LV2_Feature
;
2594 plugin
->features
[lv2_feature_id_ui_parent
]->URI
= LV2_UI_URI
"#parent";
2595 plugin
->features
[lv2_feature_id_ui_parent
]->data
= nullptr;
2597 plugin
->features
[lv2_feature_id_external_ui
] = new LV2_Feature
;
2598 plugin
->features
[lv2_feature_id_external_ui
]->URI
= LV2_EXTERNAL_UI_URI
;
2599 plugin
->features
[lv2_feature_id_external_ui
]->data
= External_UI_Feature
;
2601 plugin
->features
[lv2_feature_id_external_ui_old
] = new LV2_Feature
;
2602 plugin
->features
[lv2_feature_id_external_ui_old
]->URI
= LV2_EXTERNAL_UI_DEPRECATED_URI
;
2603 plugin
->features
[lv2_feature_id_external_ui_old
]->data
= External_UI_Feature
;
2608 qDebug("Will use LV2 Qt4 UI");
2609 plugin
->gui
.type
= GUI_INTERNAL_QT4
;
2610 plugin
->gui
.resizable
= plugin
->is_ui_resizable();
2612 plugin
->ui
.handle
= plugin
->ui
.descriptor
->instantiate(plugin
->ui
.descriptor
,
2613 plugin
->descriptor
->URI
,
2614 plugin
->ui
.rdf_descriptor
->Bundle
,
2615 plugin
->carla_lv2_ui_write_function
,
2620 if (plugin
->ui
.handle
&& plugin
->ui
.descriptor
->port_event
)
2621 plugin
->update_ui_ports();
2626 qDebug("Will use LV2 X11 UI");
2627 plugin
->gui
.type
= GUI_INTERNAL_X11
;
2628 plugin
->gui
.resizable
= plugin
->is_ui_resizable();
2632 qDebug("Will use LV2 Gtk2 UI, NOT!");
2635 case LV2_UI_EXTERNAL
:
2636 case LV2_UI_OLD_EXTERNAL
:
2637 qDebug("Will use LV2 External UI");
2638 plugin
->gui
.type
= GUI_EXTERNAL_LV2
;
2648 qCritical("Could not find the requested GUI in the plugin UI library");
2649 plugin
->ui_lib_close();
2650 plugin
->ui
.lib
= nullptr;
2651 plugin
->ui
.descriptor
= nullptr;
2652 plugin
->ui
.rdf_descriptor
= nullptr;
2657 qCritical("Could not find the LV2UI Descriptor in the UI library");
2658 plugin
->ui_lib_close();
2659 plugin
->ui
.lib
= nullptr;
2660 plugin
->ui
.rdf_descriptor
= nullptr;
2665 qCritical("Could not load UI library, error was: \n%s", plugin
->ui_lib_error());
2666 plugin
->ui
.lib
= nullptr;
2671 // Some UI Feature not supported
2672 plugin
->ui
.lib
= nullptr;
2673 plugin
->ui
.rdf_descriptor
= nullptr;
2677 qWarning("Failed to find an appropriate LV2 UI for this plugin");
2680 // ----------------- End of GUI Stuff ------------------------------------------------
2682 if (plugin
->gui
.type
!= GUI_NONE
)
2683 plugin
->hints
|= PLUGIN_HAS_GUI
;
2685 osc_new_plugin(plugin
);
2689 set_last_error("Failed to register plugin in JACK");
2696 set_last_error("Plugin failed to initialize");
2709 set_last_error("Could not find the requested plugin URI in the plugin library");
2716 set_last_error("Could not find the LV2 Descriptor in the plugin library");
2723 set_last_error(plugin
->lib_error());
2730 set_last_error("Failed to find the requested plugin in the LV2 Bundle");
2735 set_last_error("Maximum number of plugins reached");