Clean up LV2 code in preparation for atom support.
[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
30 namespace ARDOUR {
31
32 class AudioEngine;
33 class Session;
34
35 class LV2Plugin : public ARDOUR::Plugin
36 {
37   public:
38         LV2Plugin (ARDOUR::AudioEngine& engine,
39                    ARDOUR::Session&     session,
40                    void*                c_plugin,
41                    framecnt_t           sample_rate);
42         LV2Plugin (const LV2Plugin &);
43         ~LV2Plugin ();
44
45         std::string unique_id () const;
46         const char* uri () const;
47         const char* label () const;
48         const char* name () const;
49         const char* maker () const;
50
51         uint32_t   num_ports () const;
52         uint32_t   parameter_count () const;
53         float      default_value (uint32_t port);
54         framecnt_t signal_latency () const;
55         void       set_parameter (uint32_t port, float val);
56         float      get_parameter (uint32_t port) const;
57         int        get_parameter_descriptor (uint32_t which, ParameterDescriptor&) const;
58         uint32_t   nth_parameter (uint32_t port, bool& ok) const;
59
60         const void* extension_data (const char* uri) const;
61
62         void* c_plugin();
63         void* c_ui();
64         void* c_ui_type();
65
66         bool is_external_ui () const;
67
68         const char* port_symbol (uint32_t port) const;
69
70         const LV2_Feature* const* features () { return _features; }
71
72         std::set<Evoral::Parameter> automatable () const;
73
74         void activate ();
75         void deactivate ();
76         void cleanup ();
77
78         int set_block_size (pframes_t /*nframes*/) { return 0; }
79
80         int connect_and_run (BufferSet& bufs,
81                              ChanMapping in, ChanMapping out,
82                              pframes_t nframes, framecnt_t offset);
83
84         std::string describe_parameter (Evoral::Parameter);
85         std::string state_node_name () const { return "lv2"; }
86
87         void print_parameter (uint32_t param,
88                               char*    buf,
89                               uint32_t len) const;
90
91         bool parameter_is_audio (uint32_t) const;
92         bool parameter_is_control (uint32_t) const;
93         bool parameter_is_event (uint32_t) const;
94         bool parameter_is_input (uint32_t) const;
95         bool parameter_is_output (uint32_t) const;
96         bool parameter_is_toggled (uint32_t) const;
97
98         boost::shared_ptr<Plugin::ScalePoints>
99         get_scale_points(uint32_t port_index) const;
100
101         static uint32_t midi_event_type () { return _midi_event_type; }
102
103         void set_insert_info(const PluginInsert* insert);
104
105         int      set_state (const XMLNode& node, int version);
106         bool     save_preset (std::string uri);
107         void     remove_preset (std::string uri);
108         bool     load_preset (PresetRecord);
109         std::string current_preset () const;
110
111         bool has_editor () const;
112
113   private:
114         struct Impl;
115         Impl*         _impl;
116         void*         _module;
117         LV2_Feature** _features;
118         framecnt_t    _sample_rate;
119         float*        _control_data;
120         float*        _shadow_data;
121         float*        _defaults;
122         float*        _latency_control_port;
123         PBD::ID       _insert_id;
124
125         typedef enum {
126                 PORT_INPUT   = 1,
127                 PORT_OUTPUT  = 1 << 1,
128                 PORT_EVENT   = 1 << 2,
129                 PORT_AUDIO   = 1 << 3,
130                 PORT_CONTROL = 1 << 4
131         } PortFlag;
132
133         typedef unsigned PortFlags;
134
135         std::vector<PortFlags>         _port_flags;
136         std::map<std::string,uint32_t> _port_indices;
137
138         typedef struct {
139                 const void* (*extension_data) (const char* uri);
140         } LV2_DataAccess;
141
142         LV2_DataAccess _data_access_extension_data;
143         LV2_Feature    _data_access_feature;
144         LV2_Feature    _instance_access_feature;
145         LV2_Feature    _make_path_feature;
146
147         mutable unsigned _state_version;
148
149         bool _was_activated;
150         bool _has_state_interface;
151
152         static URIMap   _uri_map;
153         static uint32_t _midi_event_type;
154         static uint32_t _state_path_type;
155
156         const std::string plugin_dir () const;
157         const std::string scratch_dir () const;
158         const std::string file_dir () const;
159         const std::string state_dir (unsigned num) const;
160
161         static char* lv2_state_make_path (void*       host_data,
162                                           const char* path);
163
164         void init (void* c_plugin, framecnt_t rate);
165         void run (pframes_t nsamples);
166
167         void latency_compute_run ();
168         std::string do_save_preset (std::string);
169         void do_remove_preset (std::string);
170         void find_presets ();
171         void add_state (XMLNode *) const;
172 };
173
174
175 class LV2PluginInfo : public PluginInfo {
176 public:
177         LV2PluginInfo (void* c_plugin);
178         ~LV2PluginInfo ();
179
180         static PluginInfoList* discover ();
181
182         PluginPtr load (Session& session);
183
184         void* _c_plugin;
185 };
186
187 typedef boost::shared_ptr<LV2PluginInfo> LV2PluginInfoPtr;
188
189 } // namespace ARDOUR
190
191 #endif /* __ardour_lv2_plugin_h__ */