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