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