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