Add "Description" pane to generic plugin UI for LV2 plugins with documentation (rdfs...
[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
27 #include "ardour/plugin.h"
28 #include "ardour/uri_map.h"
29 #include "ardour/worker.h"
30 #include "pbd/ringbuffer.h"
31
32 namespace ARDOUR {
33
34 class AudioEngine;
35 class Session;
36
37 class LV2Plugin : public ARDOUR::Plugin, public ARDOUR::Workee
38 {
39   public:
40         LV2Plugin (ARDOUR::AudioEngine& engine,
41                    ARDOUR::Session&     session,
42                    void*                c_plugin,
43                    framecnt_t           sample_rate);
44         LV2Plugin (const LV2Plugin &);
45         ~LV2Plugin ();
46
47         std::string unique_id () const;
48         const char* uri () const;
49         const char* label () const;
50         const char* name () const;
51         const char* maker () const;
52
53         uint32_t    num_ports () const;
54         uint32_t    parameter_count () const;
55         float       default_value (uint32_t port);
56         framecnt_t  signal_latency () const;
57         void        set_parameter (uint32_t port, float val);
58         float       get_parameter (uint32_t port) const;
59         std::string get_docs() const;
60         std::string get_parameter_docs(uint32_t which) const;
61         int         get_parameter_descriptor (uint32_t which, ParameterDescriptor&) const;
62         uint32_t    nth_parameter (uint32_t port, bool& ok) const;
63
64         const void* extension_data (const char* uri) const;
65
66         void* c_plugin();
67         void* c_ui();
68         void* c_ui_type();
69
70         bool is_external_ui () const;
71         bool ui_is_resizable () const;
72
73         const char* port_symbol (uint32_t port) const;
74         uint32_t    port_index (const char* symbol) const;
75
76         const LV2_Feature* const* features () { return _features; }
77
78         std::set<Evoral::Parameter> automatable () const;
79
80         void activate ();
81         void deactivate ();
82         void cleanup ();
83
84         int set_block_size (pframes_t /*nframes*/) { return 0; }
85
86         int connect_and_run (BufferSet& bufs,
87                              ChanMapping in, ChanMapping out,
88                              pframes_t nframes, framecnt_t offset);
89
90         std::string describe_parameter (Evoral::Parameter);
91         std::string state_node_name () const { return "lv2"; }
92
93         void print_parameter (uint32_t param,
94                               char*    buf,
95                               uint32_t len) const;
96
97         bool parameter_is_audio (uint32_t) const;
98         bool parameter_is_control (uint32_t) const;
99         bool parameter_is_event (uint32_t) const;
100         bool parameter_is_input (uint32_t) const;
101         bool parameter_is_output (uint32_t) const;
102         bool parameter_is_toggled (uint32_t) const;
103
104         boost::shared_ptr<Plugin::ScalePoints>
105         get_scale_points(uint32_t port_index) const;
106
107         /// Return the URID of midi:MidiEvent
108         static uint32_t midi_event_type (bool event_api) {
109                 return event_api ? _midi_event_type_ev : _midi_event_type;
110         }
111
112         void set_insert_info(const PluginInsert* insert);
113
114         int      set_state (const XMLNode& node, int version);
115         bool     save_preset (std::string uri);
116         void     remove_preset (std::string uri);
117         bool     load_preset (PresetRecord);
118         std::string current_preset () const;
119
120         bool has_editor () const;
121         bool has_message_output () const;
122
123         uint32_t atom_eventTransfer() const;
124
125         void write_from_ui(uint32_t index, uint32_t protocol, uint32_t size, uint8_t* body);
126
127         typedef void UIMessageSink(void*       controller,
128                                    uint32_t    index,
129                                    uint32_t    size,
130                                    uint32_t    format,
131                                    const void* buffer);
132
133         void enable_ui_emmission();
134         void emit_to_ui(void* controller, UIMessageSink sink);
135
136         Worker* worker() { return _worker; }
137
138         int work(uint32_t size, const void* data);
139         int work_response(uint32_t size, const void* data);
140
141         static URIMap _uri_map;
142
143         static uint32_t _midi_event_type_ev;
144         static uint32_t _midi_event_type;
145         static uint32_t _chunk_type;
146         static uint32_t _sequence_type;
147         static uint32_t _event_transfer_type;
148         static uint32_t _path_type;
149
150   private:
151         struct Impl;
152         Impl*         _impl;
153         void*         _module;
154         LV2_Feature** _features;
155         Worker*       _worker;
156         framecnt_t    _sample_rate;
157         float*        _control_data;
158         float*        _shadow_data;
159         float*        _defaults;
160         LV2_Evbuf**   _ev_buffers;
161         float*        _bpm_control_port;  ///< Special input set by ardour
162         float*        _freewheel_control_port;  ///< Special input set by ardour
163         float*        _latency_control_port;  ///< Special output set by ardour
164         PBD::ID       _insert_id;
165
166         typedef enum {
167                 PORT_INPUT   = 1,
168                 PORT_OUTPUT  = 1 << 1,
169                 PORT_AUDIO   = 1 << 2,
170                 PORT_CONTROL = 1 << 3,
171                 PORT_EVENT   = 1 << 4,
172                 PORT_MESSAGE = 1 << 5
173         } PortFlag;
174
175         typedef unsigned PortFlags;
176
177         std::vector<PortFlags>         _port_flags;
178         std::map<std::string,uint32_t> _port_indices;
179
180         /// Message send to/from UI via ports
181         struct UIMessage {
182                 uint32_t index;
183                 uint32_t protocol;
184                 uint32_t size;
185         };
186
187         void write_to_ui(uint32_t index,
188                          uint32_t protocol,
189                          uint32_t size,
190                          uint8_t* body);
191
192         void write_to(RingBuffer<uint8_t>* dest,
193                       uint32_t             index,
194                       uint32_t             protocol,
195                       uint32_t             size,
196                       uint8_t*             body);
197
198         // Created on demand so the space is only consumed if necessary
199         RingBuffer<uint8_t>* _to_ui;
200         RingBuffer<uint8_t>* _from_ui;
201
202         typedef struct {
203                 const void* (*extension_data) (const char* uri);
204         } LV2_DataAccess;
205
206         LV2_DataAccess _data_access_extension_data;
207         LV2_Feature    _data_access_feature;
208         LV2_Feature    _instance_access_feature;
209         LV2_Feature    _make_path_feature;
210         LV2_Feature    _work_schedule_feature;
211
212         mutable unsigned _state_version;
213
214         bool _was_activated;
215         bool _has_state_interface;
216
217         const std::string plugin_dir () const;
218         const std::string scratch_dir () const;
219         const std::string file_dir () const;
220         const std::string state_dir (unsigned num) const;
221
222         static char* lv2_state_make_path (void*       host_data,
223                                           const char* path);
224
225         void init (void* c_plugin, framecnt_t rate);
226         void run (pframes_t nsamples);
227
228         void latency_compute_run ();
229         std::string do_save_preset (std::string);
230         void do_remove_preset (std::string);
231         void find_presets ();
232         void add_state (XMLNode *) const;
233 };
234
235
236 class LV2PluginInfo : public PluginInfo {
237 public:
238         LV2PluginInfo (void* c_plugin);
239         ~LV2PluginInfo ();
240
241         static PluginInfoList* discover ();
242
243         PluginPtr load (Session& session);
244
245         void* _c_plugin;
246 };
247
248 typedef boost::shared_ptr<LV2PluginInfo> LV2PluginInfoPtr;
249
250 } // namespace ARDOUR
251
252 #endif /* __ardour_lv2_plugin_h__ */