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