variable plugin port config.
[ardour.git] / libs / ardour / ardour / lv2_plugin.h
1 /*
2     Copyright (C) 2008-2012 Paul Davis
3     Author: David Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifndef __ardour_lv2_plugin_h__
21 #define __ardour_lv2_plugin_h__
22
23 #include <set>
24 #include <string>
25 #include <vector>
26 #include <boost/enable_shared_from_this.hpp>
27
28 #include "ardour/plugin.h"
29 #include "ardour/uri_map.h"
30 #include "ardour/worker.h"
31 #include "pbd/ringbuffer.h"
32
33 #ifdef LV2_EXTENDED // -> needs to eventually go upstream to lv2plug.in
34 #include "ardour/lv2_extensions.h"
35 #endif
36
37 #ifndef PATH_MAX
38 #define PATH_MAX 1024
39 #endif
40
41 typedef struct LV2_Evbuf_Impl LV2_Evbuf;
42
43 namespace ARDOUR {
44
45 // a callback function for lilv_state_new_from_instance(). friend of LV2Plugin
46 // so we can pass an LV2Plugin* in user_data and access its private members.
47 const void* lv2plugin_get_port_value(const char* port_symbol,
48                                      void*       user_data,
49                                      uint32_t*   size,
50                                      uint32_t*   type);
51
52 class AudioEngine;
53 class Session;
54
55 class LIBARDOUR_API LV2Plugin : public ARDOUR::Plugin, public ARDOUR::Workee
56 {
57   public:
58         LV2Plugin (ARDOUR::AudioEngine& engine,
59                    ARDOUR::Session&     session,
60                    const void*          c_plugin,
61                    framecnt_t           sample_rate);
62         LV2Plugin (const LV2Plugin &);
63         ~LV2Plugin ();
64
65         std::string unique_id () const;
66         const char* uri () const;
67         const char* label () const;
68         const char* name () const;
69         const char* maker () const;
70
71         uint32_t    num_ports () const;
72         uint32_t    parameter_count () const;
73         float       default_value (uint32_t port);
74         framecnt_t  max_latency () const;
75         framecnt_t  signal_latency () const;
76         void        set_parameter (uint32_t port, float val);
77         float       get_parameter (uint32_t port) const;
78         std::string get_docs() const;
79         std::string get_parameter_docs(uint32_t which) const;
80         int         get_parameter_descriptor (uint32_t which, ParameterDescriptor&) const;
81         uint32_t    nth_parameter (uint32_t port, bool& ok) const;
82
83         IOPortDescription describe_io_port (DataType dt, bool input, uint32_t id) const;
84
85         const void* extension_data (const char* uri) const;
86
87         const void* c_plugin();
88         const void* c_ui();
89         const void* c_ui_type();
90
91         bool is_external_ui () const;
92         bool is_external_kx () const;
93         bool ui_is_resizable () const;
94
95         const char* port_symbol (uint32_t port) const;
96         uint32_t    port_index (const char* symbol) const;
97
98         const LV2_Feature* const* features () { return _features; }
99
100         std::set<Evoral::Parameter> automatable () const;
101         virtual void set_automation_control (uint32_t, boost::shared_ptr<AutomationControl>);
102
103         void activate ();
104         void deactivate ();
105         void cleanup ();
106
107         int set_block_size (pframes_t);
108         bool requires_fixed_sized_buffers () const;
109
110         int connect_and_run (BufferSet& bufs,
111                              ChanMapping in, ChanMapping out,
112                              pframes_t nframes, framecnt_t offset);
113
114         std::string describe_parameter (Evoral::Parameter);
115         std::string state_node_name () const { return "lv2"; }
116
117         void print_parameter (uint32_t param,
118                               char*    buf,
119                               uint32_t len) const;
120
121         bool parameter_is_audio (uint32_t) const;
122         bool parameter_is_control (uint32_t) const;
123         bool parameter_is_event (uint32_t) const;
124         bool parameter_is_input (uint32_t) const;
125         bool parameter_is_output (uint32_t) const;
126         bool parameter_is_toggled (uint32_t) const;
127
128         boost::shared_ptr<ScalePoints>
129         get_scale_points(uint32_t port_index) const;
130
131         void set_insert_id(PBD::ID id);
132         void set_state_dir (const std::string& d = "");
133
134         int      set_state (const XMLNode& node, int version);
135         bool     save_preset (std::string uri);
136         void     remove_preset (std::string uri);
137         bool     load_preset (PresetRecord);
138         std::string current_preset () const;
139
140         bool has_editor () const;
141         bool has_message_output () const;
142
143         bool write_from_ui(uint32_t       index,
144                            uint32_t       protocol,
145                            uint32_t       size,
146                            const uint8_t* body);
147
148         typedef void UIMessageSink(void*       controller,
149                                    uint32_t    index,
150                                    uint32_t    size,
151                                    uint32_t    format,
152                                    const void* buffer);
153
154         void enable_ui_emission();
155         void emit_to_ui(void* controller, UIMessageSink sink);
156
157         Worker* worker() { return _worker; }
158
159         URIMap&       uri_map()       { return _uri_map; }
160         const URIMap& uri_map() const { return _uri_map; }
161
162         int work(uint32_t size, const void* data);
163         int work_response(uint32_t size, const void* data);
164
165         void                       set_property(uint32_t key, const Variant& value);
166         const PropertyDescriptors& get_supported_properties() const { return _property_descriptors; }
167         const ParameterDescriptor& get_property_descriptor(uint32_t id) const;
168         void                       announce_property_values();
169
170   private:
171         struct Impl;
172         Impl*         _impl;
173         void*         _module;
174         LV2_Feature** _features;
175         Worker*       _worker;
176         framecnt_t    _sample_rate;
177         float*        _control_data;
178         float*        _shadow_data;
179         float*        _defaults;
180         LV2_Evbuf**   _ev_buffers;
181         LV2_Evbuf**   _atom_ev_buffers;
182         float*        _bpm_control_port;  ///< Special input set by ardour
183         float*        _freewheel_control_port;  ///< Special input set by ardour
184         float*        _latency_control_port;  ///< Special output set by ardour
185         framepos_t    _next_cycle_start;  ///< Expected start frame of next run cycle
186         double        _next_cycle_speed;  ///< Expected start frame of next run cycle
187         PBD::ID       _insert_id;
188         std::string   _plugin_state_dir;
189         uint32_t      _patch_port_in_index;
190         uint32_t      _patch_port_out_index;
191         URIMap&       _uri_map;
192         bool          _no_sample_accurate_ctrl;
193         bool          _can_write_automation;
194         framecnt_t    _max_latency;
195         framecnt_t    _current_latency;
196
197         friend const void* lv2plugin_get_port_value(const char* port_symbol,
198                                                     void*       user_data,
199                                                     uint32_t*   size,
200                                                     uint32_t*   type);
201
202         typedef enum {
203                 PORT_INPUT    = 1,       ///< Input port
204                 PORT_OUTPUT   = 1 << 1,  ///< Output port
205                 PORT_AUDIO    = 1 << 2,  ///< Audio (buffer of float)
206                 PORT_CONTROL  = 1 << 3,  ///< Control (single float)
207                 PORT_EVENT    = 1 << 4,  ///< Old event API event port
208                 PORT_SEQUENCE = 1 << 5,  ///< New atom API event port
209                 PORT_MIDI     = 1 << 6,  ///< Event port understands MIDI
210                 PORT_POSITION = 1 << 7,  ///< Event port understands position
211                 PORT_PATCHMSG = 1 << 8,  ///< Event port supports patch:Message
212                 PORT_AUTOCTRL = 1 << 9,  ///< Event port supports auto:AutomationControl
213                 PORT_CTRLED   = 1 << 10  ///< Port prop auto:AutomationControlled (can be self controlled)
214         } PortFlag;
215
216         typedef unsigned PortFlags;
217
218         std::vector<PortFlags>         _port_flags;
219         std::vector<size_t>            _port_minimumSize;
220         std::map<std::string,uint32_t> _port_indices;
221
222         PropertyDescriptors _property_descriptors;
223
224         struct AutomationCtrl {
225                 AutomationCtrl (const AutomationCtrl &other)
226                         : ac (other.ac)
227                         , guard (other.guard)
228                 { }
229
230                 AutomationCtrl (boost::shared_ptr<ARDOUR::AutomationControl> c)
231                         : ac (c)
232                         , guard (false)
233                 { }
234                 boost::shared_ptr<ARDOUR::AutomationControl> ac;
235                 bool guard;
236         };
237
238         typedef boost::shared_ptr<AutomationCtrl> AutomationCtrlPtr;
239         typedef std::map<uint32_t, AutomationCtrlPtr> AutomationCtrlMap;
240         AutomationCtrlMap _ctrl_map;
241         AutomationCtrlPtr get_automation_control (uint32_t);
242
243         /// Message send to/from UI via ports
244         struct UIMessage {
245                 uint32_t index;
246                 uint32_t protocol;
247                 uint32_t size;
248         };
249
250         bool write_to_ui(uint32_t       index,
251                          uint32_t       protocol,
252                          uint32_t       size,
253                          const uint8_t* body);
254
255         bool write_to(RingBuffer<uint8_t>* dest,
256                       uint32_t             index,
257                       uint32_t             protocol,
258                       uint32_t             size,
259                       const uint8_t*       body);
260
261         // Created on demand so the space is only consumed if necessary
262         RingBuffer<uint8_t>* _to_ui;
263         RingBuffer<uint8_t>* _from_ui;
264
265 #ifdef LV2_EXTENDED
266         const LV2_Inline_Display_Interface* _display_interface;
267 #endif
268
269         typedef struct {
270                 const void* (*extension_data) (const char* uri);
271         } LV2_DataAccess;
272
273         LV2_DataAccess _data_access_extension_data;
274         LV2_Feature    _data_access_feature;
275         LV2_Feature    _instance_access_feature;
276         LV2_Feature    _make_path_feature;
277         LV2_Feature    _log_feature;
278         LV2_Feature    _work_schedule_feature;
279         LV2_Feature    _options_feature;
280         LV2_Feature    _def_state_feature;
281 #ifdef LV2_EXTENDED
282         LV2_Feature    _queue_draw_feature;
283 #endif
284
285         // Options passed to plugin
286         int32_t _seq_size;
287
288         mutable unsigned _state_version;
289
290         bool _was_activated;
291         bool _has_state_interface;
292
293         const std::string plugin_dir () const;
294         const std::string scratch_dir () const;
295         const std::string file_dir () const;
296         const std::string state_dir (unsigned num) const;
297
298         static char* lv2_state_make_path (void*       host_data,
299                                           const char* path);
300
301         void init (const void* c_plugin, framecnt_t rate);
302         void allocate_atom_event_buffers ();
303         void run (pframes_t nsamples);
304
305         void load_supported_properties(PropertyDescriptors& descs);
306
307 #ifdef LV2_EXTENDED
308         bool has_inline_display ();
309         Plugin::Display_Image_Surface* render_inline_display (uint32_t, uint32_t);
310 #endif
311
312         void latency_compute_run ();
313         std::string do_save_preset (std::string);
314         void do_remove_preset (std::string);
315         void find_presets ();
316         void add_state (XMLNode *) const;
317 };
318
319
320 class LIBARDOUR_API LV2PluginInfo : public PluginInfo , public boost::enable_shared_from_this<ARDOUR::LV2PluginInfo> {
321 public:
322         LV2PluginInfo (const char* plugin_uri);
323         ~LV2PluginInfo ();
324
325         static PluginInfoList* discover ();
326
327         PluginPtr load (Session& session);
328         std::vector<Plugin::PresetRecord> get_presets (bool user_only) const;
329         virtual bool in_category (const std::string &c) const;
330         virtual bool is_instrument() const;
331
332         char * _plugin_uri;
333 };
334
335 typedef boost::shared_ptr<LV2PluginInfo> LV2PluginInfoPtr;
336
337 } // namespace ARDOUR
338
339 #endif /* __ardour_lv2_plugin_h__ */