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