LV2 external UI patch from nedko via 2.X and a couple of .rej's
[ardour.git] / libs / ardour / ardour / lv2_plugin.h
1 /*
2     Copyright (C) 2008 Paul Davis
3     Author: Dave 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 <vector>
26 #include <string>
27 #include <dlfcn.h>
28
29 #include <sigc++/signal.h>
30
31 #include "pbd/stateful.h" 
32
33 #include <jack/types.h>
34 #include <slv2/slv2.h>
35 #include "ardour/plugin.h"
36 #include "ardour/uri_map.h"
37
38 namespace ARDOUR {
39 class AudioEngine;
40 class Session;
41 struct LV2World;
42
43 class LV2Plugin : public ARDOUR::Plugin
44 {
45   public:
46         LV2Plugin (ARDOUR::AudioEngine&, ARDOUR::Session&, ARDOUR::LV2World&, SLV2Plugin plugin, nframes_t sample_rate);
47         LV2Plugin (const LV2Plugin &);
48         ~LV2Plugin ();
49
50         /* Plugin interface */
51         
52         std::string unique_id() const;
53         const char* label() const           { return slv2_value_as_string(_name); }
54         const char* name() const            { return slv2_value_as_string(_name); }
55         const char* maker() const           { return _author ? slv2_value_as_string(_author) : "Unknown"; }
56         uint32_t    parameter_count() const { return slv2_plugin_get_num_ports(_plugin); }
57         float       default_value (uint32_t port);
58         nframes_t   signal_latency() const;
59         void        set_parameter (uint32_t port, float val);
60         float       get_parameter (uint32_t port) const;
61         int         get_parameter_descriptor (uint32_t which, ParameterDescriptor&) const;
62         uint32_t    nth_parameter (uint32_t port, bool& ok) const;
63
64         const void* extension_data(const char* uri) { return _instance->lv2_descriptor->extension_data(uri); }
65
66         SLV2Plugin slv2_plugin()         { return _plugin; }
67         SLV2UI     slv2_ui()             { return _ui; }
68         bool       is_external_ui() const;
69         SLV2Port   slv2_port(uint32_t i) { return slv2_plugin_get_port_by_index(_plugin, i); }
70
71         const char* port_symbol(uint32_t port);
72         
73         const LV2_Feature* const* features() { return _features; }
74
75         std::set<Evoral::Parameter> automatable() const;
76
77         void activate () { 
78                 if (!_was_activated) {
79                         slv2_instance_activate(_instance);
80                         _was_activated = true;
81                 }
82         }
83
84         void deactivate () {
85                 if (_was_activated) {
86                         slv2_instance_deactivate(_instance);
87                         _was_activated = false;
88                 }
89         }
90
91         void cleanup () {
92                 activate();
93                 deactivate();
94                 slv2_instance_free(_instance);
95                 _instance = NULL;
96         }
97
98         void set_block_size (nframes_t /*nframes*/) {}
99         
100         int connect_and_run (BufferSet& bufs,
101                         ChanMapping in, ChanMapping out,
102                         nframes_t nframes, nframes_t offset);
103
104         std::string describe_parameter (Evoral::Parameter);
105         std::string state_node_name() const { return "lv2"; }
106         void        print_parameter (uint32_t, char*, 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_midi(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         static uint32_t midi_event_type() { return _midi_event_type; }
116
117         XMLNode& get_state();
118         int      set_state(const XMLNode& node);
119         bool     save_preset(std::string uri);
120         bool     load_preset(const std::string uri);
121         virtual std::vector<Plugin::PresetRecord> get_presets();
122
123         bool has_editor() const;
124
125   private:
126         void*                    _module;
127         LV2World&                _world;
128         LV2_Feature**            _features;
129         SLV2Plugin               _plugin;
130         SLV2UI                   _ui;
131         SLV2Value                _name;
132         SLV2Value                _author;
133         SLV2Instance             _instance;
134         nframes_t                _sample_rate;
135         float*                   _control_data;
136         float*                   _shadow_data;
137         float*                   _defaults;
138         float*                   _latency_control_port;
139         bool                     _was_activated;
140         std::vector<bool>        _port_is_input;
141         std::map<std::string,uint32_t> _port_indices;
142
143         typedef struct { const void* (*extension_data)(const char* uri); } LV2_DataAccess;
144         LV2_DataAccess _data_access_extension_data;
145         LV2_Feature _data_access_feature;
146         LV2_Feature _instance_access_feature;
147
148         static URIMap   _uri_map;
149         static uint32_t _midi_event_type;
150
151         void init (LV2World& world, SLV2Plugin plugin, nframes_t rate);
152         void run (nframes_t nsamples);
153         void latency_compute_run ();
154 };
155
156
157 /** The SLV2World, and various cached (as symbols, fast) URIs.
158  *
159  * This object represents everything ardour 'knows' about LV2
160  * (ie understood extensions/features/etc)
161  */
162 struct LV2World {
163         LV2World();
164         ~LV2World();
165
166         SLV2World world;
167         SLV2Value input_class; ///< Input port
168         SLV2Value output_class; ///< Output port
169         SLV2Value audio_class; ///< Audio port
170         SLV2Value control_class; ///< Control port
171         SLV2Value event_class; ///< Event port
172         SLV2Value midi_class; ///< MIDI event
173         SLV2Value in_place_broken;
174         SLV2Value integer;
175         SLV2Value toggled;
176         SLV2Value srate;
177         SLV2Value gtk_gui;
178         SLV2Value external_gui;
179 };
180
181
182 class LV2PluginInfo : public PluginInfo {
183 public: 
184         LV2PluginInfo (void* slv2_world, void* slv2_plugin);;
185         ~LV2PluginInfo ();;
186         static PluginInfoList discover (void* slv2_world);
187
188         PluginPtr load (Session& session);
189
190         void* _lv2_world;
191         void* _slv2_plugin;
192 };
193
194 typedef boost::shared_ptr<LV2PluginInfo> LV2PluginInfoPtr;
195
196 } // namespace ARDOUR
197
198 #endif /* __ardour_lv2_plugin_h__ */