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