revert inadvertently committed debug settings
[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         bool        get_layout (uint32_t which, UILayoutHint&) const;
83
84         IOPortDescription describe_io_port (DataType dt, bool input, uint32_t id) const;
85
86         const void* extension_data (const char* uri) const;
87
88         const void* c_plugin();
89         const void* c_ui();
90         const void* c_ui_type();
91
92         bool is_external_ui () const;
93         bool is_external_kx () const;
94         bool ui_is_resizable () const;
95
96         const char* port_symbol (uint32_t port) const;
97         uint32_t    port_index (const char* symbol) const;
98
99         const LV2_Feature* const* features () { return _features; }
100
101         std::set<Evoral::Parameter> automatable () const;
102         virtual void set_automation_control (uint32_t, boost::shared_ptr<AutomationControl>);
103
104         void activate ();
105         void deactivate ();
106         void cleanup ();
107
108         int set_block_size (pframes_t);
109         bool requires_fixed_sized_buffers () const;
110
111         int connect_and_run (BufferSet& bufs,
112                              framepos_t start, framepos_t end, double speed,
113                              ChanMapping in, ChanMapping out,
114                              pframes_t nframes, framecnt_t offset);
115
116         std::string describe_parameter (Evoral::Parameter);
117         std::string state_node_name () const { return "lv2"; }
118
119         void print_parameter (uint32_t param,
120                               char*    buf,
121                               uint32_t len) const;
122
123         bool parameter_is_audio (uint32_t) const;
124         bool parameter_is_control (uint32_t) const;
125         bool parameter_is_event (uint32_t) const;
126         bool parameter_is_input (uint32_t) const;
127         bool parameter_is_output (uint32_t) const;
128         bool parameter_is_toggled (uint32_t) const;
129
130         uint32_t designated_bypass_port ();
131
132         boost::shared_ptr<ScalePoints>
133         get_scale_points(uint32_t port_index) const;
134
135         void set_insert_id(PBD::ID id);
136         void set_state_dir (const std::string& d = "");
137
138         int      set_state (const XMLNode& node, int version);
139         bool     save_preset (std::string uri);
140         void     remove_preset (std::string uri);
141         bool     load_preset (PresetRecord);
142         std::string current_preset () const;
143
144         bool has_editor () const;
145         bool has_message_output () const;
146
147         bool write_from_ui(uint32_t       index,
148                            uint32_t       protocol,
149                            uint32_t       size,
150                            const uint8_t* body);
151
152         typedef void UIMessageSink(void*       controller,
153                                    uint32_t    index,
154                                    uint32_t    size,
155                                    uint32_t    format,
156                                    const void* buffer);
157
158         void enable_ui_emission();
159         void emit_to_ui(void* controller, UIMessageSink sink);
160
161         Worker* worker() { return _worker; }
162
163         URIMap&       uri_map()       { return _uri_map; }
164         const URIMap& uri_map() const { return _uri_map; }
165
166         int work(uint32_t size, const void* data);
167         int work_response(uint32_t size, const void* data);
168
169         void                       set_property(uint32_t key, const Variant& value);
170         const PropertyDescriptors& get_supported_properties() const { return _property_descriptors; }
171         const ParameterDescriptor& get_property_descriptor(uint32_t id) const;
172         void                       announce_property_values();
173
174   private:
175         struct Impl;
176         Impl*         _impl;
177         void*         _module;
178         LV2_Feature** _features;
179         Worker*       _worker;
180         framecnt_t    _sample_rate;
181         float*        _control_data;
182         float*        _shadow_data;
183         float*        _defaults;
184         LV2_Evbuf**   _ev_buffers;
185         LV2_Evbuf**   _atom_ev_buffers;
186         float*        _bpm_control_port;  ///< Special input set by ardour
187         float*        _freewheel_control_port;  ///< Special input set by ardour
188         float*        _latency_control_port;  ///< Special output set by ardour
189         framepos_t    _next_cycle_start;  ///< Expected start frame of next run cycle
190         double        _next_cycle_speed;  ///< Expected start frame of next run cycle
191         PBD::ID       _insert_id;
192         std::string   _plugin_state_dir;
193         uint32_t      _patch_port_in_index;
194         uint32_t      _patch_port_out_index;
195         URIMap&       _uri_map;
196         bool          _no_sample_accurate_ctrl;
197         bool          _can_write_automation;
198         framecnt_t    _max_latency;
199         framecnt_t    _current_latency;
200
201         friend const void* lv2plugin_get_port_value(const char* port_symbol,
202                                                     void*       user_data,
203                                                     uint32_t*   size,
204                                                     uint32_t*   type);
205
206         typedef enum {
207                 PORT_INPUT    = 1,       ///< Input port
208                 PORT_OUTPUT   = 1 << 1,  ///< Output port
209                 PORT_AUDIO    = 1 << 2,  ///< Audio (buffer of float)
210                 PORT_CONTROL  = 1 << 3,  ///< Control (single float)
211                 PORT_EVENT    = 1 << 4,  ///< Old event API event port
212                 PORT_SEQUENCE = 1 << 5,  ///< New atom API event port
213                 PORT_MIDI     = 1 << 6,  ///< Event port understands MIDI
214                 PORT_POSITION = 1 << 7,  ///< Event port understands position
215                 PORT_PATCHMSG = 1 << 8,  ///< Event port supports patch:Message
216                 PORT_AUTOCTRL = 1 << 9,  ///< Event port supports auto:AutomationControl
217                 PORT_CTRLED   = 1 << 10, ///< Port prop auto:AutomationControlled (can be self controlled)
218                 PORT_CTRLER   = 1 << 11, ///< Port prop auto:AutomationController (can be self set)
219                 PORT_NOAUTO   = 1 << 12  ///< Port don't allow to automate
220         } PortFlag;
221
222         typedef unsigned PortFlags;
223
224         std::vector<PortFlags>         _port_flags;
225         std::vector<size_t>            _port_minimumSize;
226         std::map<std::string,uint32_t> _port_indices;
227
228         PropertyDescriptors _property_descriptors;
229
230         struct AutomationCtrl {
231                 AutomationCtrl (const AutomationCtrl &other)
232                         : ac (other.ac)
233                         , guard (other.guard)
234                 { }
235
236                 AutomationCtrl (boost::shared_ptr<ARDOUR::AutomationControl> c)
237                         : ac (c)
238                         , guard (false)
239                 { }
240                 boost::shared_ptr<ARDOUR::AutomationControl> ac;
241                 bool guard;
242         };
243
244         typedef boost::shared_ptr<AutomationCtrl> AutomationCtrlPtr;
245         typedef std::map<uint32_t, AutomationCtrlPtr> AutomationCtrlMap;
246         AutomationCtrlMap _ctrl_map;
247         AutomationCtrlPtr get_automation_control (uint32_t);
248
249         /// Message send to/from UI via ports
250         struct UIMessage {
251                 uint32_t index;
252                 uint32_t protocol;
253                 uint32_t size;
254         };
255
256         bool write_to_ui(uint32_t       index,
257                          uint32_t       protocol,
258                          uint32_t       size,
259                          const uint8_t* body);
260
261         bool write_to(RingBuffer<uint8_t>* dest,
262                       uint32_t             index,
263                       uint32_t             protocol,
264                       uint32_t             size,
265                       const uint8_t*       body);
266
267         // Created on demand so the space is only consumed if necessary
268         RingBuffer<uint8_t>* _to_ui;
269         RingBuffer<uint8_t>* _from_ui;
270
271 #ifdef LV2_EXTENDED
272         const LV2_Inline_Display_Interface* _display_interface;
273 #endif
274
275         typedef struct {
276                 const void* (*extension_data) (const char* uri);
277         } LV2_DataAccess;
278
279         LV2_DataAccess _data_access_extension_data;
280         LV2_Feature    _data_access_feature;
281         LV2_Feature    _instance_access_feature;
282         LV2_Feature    _make_path_feature;
283         LV2_Feature    _log_feature;
284         LV2_Feature    _work_schedule_feature;
285         LV2_Feature    _options_feature;
286         LV2_Feature    _def_state_feature;
287 #ifdef LV2_EXTENDED
288         LV2_Feature    _queue_draw_feature;
289 #endif
290
291         // Options passed to plugin
292         int32_t _seq_size;
293
294         mutable unsigned _state_version;
295
296         bool _was_activated;
297         bool _has_state_interface;
298
299         const std::string plugin_dir () const;
300         const std::string scratch_dir () const;
301         const std::string file_dir () const;
302         const std::string state_dir (unsigned num) const;
303
304         static char* lv2_state_make_path (void*       host_data,
305                                           const char* path);
306
307         void init (const void* c_plugin, framecnt_t rate);
308         void allocate_atom_event_buffers ();
309         void run (pframes_t nsamples);
310
311         void load_supported_properties(PropertyDescriptors& descs);
312
313 #ifdef LV2_EXTENDED
314         bool has_inline_display ();
315         Plugin::Display_Image_Surface* render_inline_display (uint32_t, uint32_t);
316 #endif
317
318         void latency_compute_run ();
319         std::string do_save_preset (std::string);
320         void do_remove_preset (std::string);
321         void find_presets ();
322         void add_state (XMLNode *) const;
323 };
324
325
326 class LIBARDOUR_API LV2PluginInfo : public PluginInfo , public boost::enable_shared_from_this<ARDOUR::LV2PluginInfo> {
327 public:
328         LV2PluginInfo (const char* plugin_uri);
329         ~LV2PluginInfo ();
330
331         static PluginInfoList* discover ();
332
333         PluginPtr load (Session& session);
334         std::vector<Plugin::PresetRecord> get_presets (bool user_only) const;
335         virtual bool in_category (const std::string &c) const;
336         virtual bool is_instrument() const;
337
338         char * _plugin_uri;
339 };
340
341 typedef boost::shared_ptr<LV2PluginInfo> LV2PluginInfoPtr;
342
343 } // namespace ARDOUR
344
345 #endif /* __ardour_lv2_plugin_h__ */