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