Implement most recent LV2 persist extension.
[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
21 #ifndef __ardour_lv2_plugin_h__
22 #define __ardour_lv2_plugin_h__
23
24 #include <set>
25 #include <string>
26 #include <vector>
27
28 #include <dlfcn.h>
29
30 #include "pbd/stateful.h"
31
32 #include <jack/types.h>
33 #include <slv2/slv2.h>
34
35 #include "ardour/plugin.h"
36 #include "ardour/uri_map.h"
37
38 namespace ARDOUR {
39
40 class AudioEngine;
41 class LV2World;
42 class Session;
43
44 class LV2Plugin : public ARDOUR::Plugin
45 {
46   public:
47         LV2Plugin (ARDOUR::AudioEngine& engine,
48                    ARDOUR::Session&     session,
49                    ARDOUR::LV2World&    world,
50                    SLV2Plugin           plugin,
51                    framecnt_t           sample_rate);
52         LV2Plugin (const LV2Plugin &);
53         ~LV2Plugin ();
54
55         std::string unique_id () const;
56
57         const char* label () const { return slv2_value_as_string(_name); }
58         const char* name () const  { return slv2_value_as_string(_name); }
59         const char* maker () const {
60                 return _author ? slv2_value_as_string (_author) : "Unknown";
61         }
62
63         uint32_t   parameter_count () const { return slv2_plugin_get_num_ports(_plugin); }
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         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) {
72                 return _instance->lv2_descriptor->extension_data (uri);
73         }
74
75         SLV2Plugin slv2_plugin ()         { return _plugin; }
76         SLV2UI     slv2_ui ()             { return _ui; }
77         SLV2Port   slv2_port (uint32_t i) { return slv2_plugin_get_port_by_index (_plugin, i); }
78         bool       is_external_ui () const;
79
80         const char* port_symbol (uint32_t port) const;
81
82         const LV2_Feature* const* features () { return _features; }
83
84         std::set<Evoral::Parameter> automatable () const;
85
86         void activate ();
87         void deactivate ();
88         void cleanup ();
89
90         int set_block_size (pframes_t /*nframes*/) { return 0; }
91
92         int connect_and_run (BufferSet& bufs,
93                         ChanMapping in, ChanMapping out,
94                         pframes_t nframes, framecnt_t offset);
95
96         std::string describe_parameter (Evoral::Parameter);
97         std::string state_node_name () const { return "lv2"; }
98
99         void print_parameter (uint32_t param,
100                               char*    buf,
101                               uint32_t len) const;
102
103         bool parameter_is_audio (uint32_t) const;
104         bool parameter_is_control (uint32_t) const;
105         bool parameter_is_midi (uint32_t) const;
106         bool parameter_is_input (uint32_t) const;
107         bool parameter_is_output (uint32_t) const;
108         bool parameter_is_toggled (uint32_t) const;
109
110         static uint32_t midi_event_type () { return _midi_event_type; }
111
112         int      set_state (const XMLNode& node, int version);
113         bool     save_preset (std::string uri);
114         void     remove_preset (std::string uri);
115         bool     load_preset (PresetRecord);
116         std::string current_preset () const;
117
118         bool has_editor () const;
119
120   private:
121         void*             _module;
122         LV2World&         _world;
123         LV2_Feature**     _features;
124         SLV2Plugin        _plugin;
125         SLV2UI            _ui;
126         SLV2Value         _name;
127         SLV2Value         _author;
128         SLV2Instance      _instance;
129         framecnt_t        _sample_rate;
130         float*            _control_data;
131         float*            _shadow_data;
132         float*            _defaults;
133         float*            _latency_control_port;
134         bool              _was_activated;
135         bool              _supports_persist;
136         std::vector<bool> _port_is_input;
137
138         std::map<std::string,uint32_t> _port_indices;
139
140         typedef struct {
141                 const void* (*extension_data) (const char* uri);
142         } LV2_DataAccess;
143
144         LV2_DataAccess _data_access_extension_data;
145         LV2_Feature    _data_access_feature;
146         LV2_Feature    _instance_access_feature;
147         LV2_Feature    _persist_feature;
148
149         static URIMap   _uri_map;
150         static uint32_t _midi_event_type;
151
152         static int lv2_persist_store_callback (void*       callback_data,
153                                                uint32_t    key,
154                                                const void* value,
155                                                size_t      size,
156                                                uint32_t    type,
157                                                bool        pod);
158
159         static const void* lv2_persist_retrieve_callback (void*     callback_data,
160                                                           uint32_t  key,
161                                                           size_t*   size,
162                                                           uint32_t* type,
163                                                           bool*     pod);
164
165         void init (LV2World& world, SLV2Plugin plugin, framecnt_t rate);
166         void run (pframes_t nsamples);
167
168         void latency_compute_run ();
169         std::string do_save_preset (std::string);
170         void do_remove_preset (std::string);
171         void find_presets ();
172         void add_state (XMLNode *) const;
173 };
174
175
176 /** The SLV2World, and various cached (as symbols, fast) URIs.
177  *
178  * This object represents everything ardour 'knows' about LV2
179  * (ie understood extensions/features/etc)
180  */
181 class LV2World {
182 public:
183         LV2World ();
184         ~LV2World ();
185
186         SLV2World world;
187         SLV2Value input_class; ///< Input port
188         SLV2Value output_class; ///< Output port
189         SLV2Value audio_class; ///< Audio port
190         SLV2Value control_class; ///< Control port
191         SLV2Value event_class; ///< Event port
192         SLV2Value midi_class; ///< MIDI event
193         SLV2Value in_place_broken;
194         SLV2Value integer;
195         SLV2Value toggled;
196         SLV2Value srate;
197         SLV2Value gtk_gui;
198         SLV2Value external_gui;
199         SLV2Value logarithmic;
200 };
201
202
203 class LV2PluginInfo : public PluginInfo {
204 public:
205         LV2PluginInfo (void* slv2_world, void* slv2_plugin);
206         ~LV2PluginInfo ();
207         static PluginInfoList* discover (void* slv2_world);
208
209         PluginPtr load (Session& session);
210
211         void* _lv2_world;
212         void* _slv2_plugin;
213 };
214
215 typedef boost::shared_ptr<LV2PluginInfo> LV2PluginInfoPtr;
216
217 } // namespace ARDOUR
218
219 #endif /* __ardour_lv2_plugin_h__ */