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