prototype [LV2]patch-change support for generic plugin UIs.
[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 /*nframes*/) { return 0; }
100
101         int connect_and_run (BufferSet& bufs,
102                              ChanMapping in, ChanMapping out,
103                              pframes_t nframes, framecnt_t offset);
104
105         std::string describe_parameter (Evoral::Parameter);
106         std::string state_node_name () const { return "lv2"; }
107
108         void print_parameter (uint32_t param,
109                               char*    buf,
110                               uint32_t len) const;
111
112         bool parameter_is_audio (uint32_t) const;
113         bool parameter_is_control (uint32_t) const;
114         bool parameter_is_event (uint32_t) const;
115         bool parameter_is_input (uint32_t) const;
116         bool parameter_is_output (uint32_t) const;
117         bool parameter_is_toggled (uint32_t) const;
118
119         boost::shared_ptr<Plugin::ScalePoints>
120         get_scale_points(uint32_t port_index) const;
121
122         void set_insert_info(const PluginInsert* insert);
123
124         int      set_state (const XMLNode& node, int version);
125         bool     save_preset (std::string uri);
126         void     remove_preset (std::string uri);
127         bool     load_preset (PresetRecord);
128         std::string current_preset () const;
129
130         bool has_editor () const;
131         bool has_message_output () const;
132
133         bool write_from_ui(uint32_t       index,
134                            uint32_t       protocol,
135                            uint32_t       size,
136                            const uint8_t* body);
137
138         typedef void UIMessageSink(void*       controller,
139                                    uint32_t    index,
140                                    uint32_t    size,
141                                    uint32_t    format,
142                                    const void* buffer);
143
144         void enable_ui_emmission();
145         void emit_to_ui(void* controller, UIMessageSink sink);
146
147         Worker* worker() { return _worker; }
148
149         uint32_t     patch_count() const { return _patch_count; }
150         const char * patch_uri(const uint32_t p) const { if (p < _patch_count) return _patch_value_uri[p]; else return NULL; }
151         const char * patch_key(const uint32_t p) const { if (p < _patch_count) return _patch_value_key[p]; else return NULL; }
152         const char * patch_val(const uint32_t p) const { if (p < _patch_count) return _patch_value_cur[p]; else return NULL; }
153         bool         patch_set(const uint32_t p, const char * val);
154         PBD::Signal1<void,const uint32_t> PatchChanged;
155
156         int work(uint32_t size, const void* data);
157         int work_response(uint32_t size, const void* data);
158
159         static URIMap _uri_map;
160
161         struct URIDs {
162                 uint32_t atom_Chunk;
163                 uint32_t atom_Path;
164                 uint32_t atom_Sequence;
165                 uint32_t atom_eventTransfer;
166                 uint32_t atom_URID;
167                 uint32_t atom_Blank;
168                 uint32_t log_Error;
169                 uint32_t log_Note;
170                 uint32_t log_Warning;
171                 uint32_t midi_MidiEvent;
172                 uint32_t time_Position;
173                 uint32_t time_bar;
174                 uint32_t time_barBeat;
175                 uint32_t time_beatUnit;
176                 uint32_t time_beatsPerBar;
177                 uint32_t time_beatsPerMinute;
178                 uint32_t time_frame;
179                 uint32_t time_speed;
180                 uint32_t patch_Set;
181                 uint32_t patch_property;
182                 uint32_t patch_value;
183         };
184
185         static URIDs urids;
186
187   private:
188         struct Impl;
189         Impl*         _impl;
190         void*         _module;
191         LV2_Feature** _features;
192         Worker*       _worker;
193         framecnt_t    _sample_rate;
194         float*        _control_data;
195         float*        _shadow_data;
196         float*        _defaults;
197         LV2_Evbuf**   _ev_buffers;
198         LV2_Evbuf**   _atom_ev_buffers;
199         float*        _bpm_control_port;  ///< Special input set by ardour
200         float*        _freewheel_control_port;  ///< Special input set by ardour
201         float*        _latency_control_port;  ///< Special output set by ardour
202         framepos_t    _next_cycle_start;  ///< Expected start frame of next run cycle
203         double        _next_cycle_speed;  ///< Expected start frame of next run cycle
204         PBD::ID       _insert_id;
205
206         uint32_t      _patch_count;
207         char **       _patch_value_uri;
208         char **       _patch_value_key;
209         char          (*_patch_value_cur)[PATH_MAX]; ///< current value
210         char          (*_patch_value_set)[PATH_MAX]; ///< new value to set
211         Glib::Threads::Mutex _patch_set_lock;
212
213         friend const void* lv2plugin_get_port_value(const char* port_symbol,
214                                                     void*       user_data,
215                                                     uint32_t*   size,
216                                                     uint32_t*   type);
217
218         typedef enum {
219                 PORT_INPUT    = 1,       ///< Input port
220                 PORT_OUTPUT   = 1 << 1,  ///< Output port
221                 PORT_AUDIO    = 1 << 2,  ///< Audio (buffer of float)
222                 PORT_CONTROL  = 1 << 3,  ///< Control (single float)
223                 PORT_EVENT    = 1 << 4,  ///< Old event API event port
224                 PORT_SEQUENCE = 1 << 5,  ///< New atom API event port
225                 PORT_MIDI     = 1 << 6,  ///< Event port understands MIDI
226                 PORT_POSITION = 1 << 7,  ///< Event port understands position
227                 PORT_PATCHMSG = 1 << 8   ///< Event port supports patch:Message
228         } PortFlag;
229
230         typedef unsigned PortFlags;
231
232         std::vector<PortFlags>         _port_flags;
233         std::vector<size_t>            _port_minimumSize;
234         std::map<std::string,uint32_t> _port_indices;
235
236         /// Message send to/from UI via ports
237         struct UIMessage {
238                 uint32_t index;
239                 uint32_t protocol;
240                 uint32_t size;
241         };
242
243         bool write_to_ui(uint32_t       index,
244                          uint32_t       protocol,
245                          uint32_t       size,
246                          const uint8_t* body);
247
248         bool write_to(RingBuffer<uint8_t>* dest,
249                       uint32_t             index,
250                       uint32_t             protocol,
251                       uint32_t             size,
252                       const uint8_t*       body);
253
254         // Created on demand so the space is only consumed if necessary
255         RingBuffer<uint8_t>* _to_ui;
256         RingBuffer<uint8_t>* _from_ui;
257
258         typedef struct {
259                 const void* (*extension_data) (const char* uri);
260         } LV2_DataAccess;
261
262         LV2_DataAccess _data_access_extension_data;
263         LV2_Feature    _data_access_feature;
264         LV2_Feature    _instance_access_feature;
265         LV2_Feature    _make_path_feature;
266         LV2_Feature    _log_feature;
267         LV2_Feature    _work_schedule_feature;
268         LV2_Feature    _options_feature;
269         LV2_Feature    _def_state_feature;
270
271         // Options passed to plugin
272         int32_t _block_length;
273         int32_t _seq_size;
274
275         mutable unsigned _state_version;
276
277         bool _was_activated;
278         bool _has_state_interface;
279
280         const std::string plugin_dir () const;
281         const std::string scratch_dir () const;
282         const std::string file_dir () const;
283         const std::string state_dir (unsigned num) const;
284
285         static char* lv2_state_make_path (void*       host_data,
286                                           const char* path);
287
288         void init (const void* c_plugin, framecnt_t rate);
289         void allocate_atom_event_buffers ();
290         void run (pframes_t nsamples);
291
292         void latency_compute_run ();
293         std::string do_save_preset (std::string);
294         void do_remove_preset (std::string);
295         void find_presets ();
296         void add_state (XMLNode *) const;
297 };
298
299
300 class LIBARDOUR_API LV2PluginInfo : public PluginInfo , public boost::enable_shared_from_this<ARDOUR::LV2PluginInfo> {
301 public:
302         LV2PluginInfo (const char* plugin_uri);
303         ~LV2PluginInfo ();
304
305         static PluginInfoList* discover ();
306
307         PluginPtr load (Session& session);
308
309         char * _plugin_uri;
310 };
311
312 typedef boost::shared_ptr<LV2PluginInfo> LV2PluginInfoPtr;
313
314 } // namespace ARDOUR
315
316 #endif /* __ardour_lv2_plugin_h__ */