Cache results of lilv_port_is_a(...) in various places.
[ardour.git] / libs / ardour / ardour / lv2_plugin.h
1
2 /*
3     Copyright (C) 2008-2011 Paul Davis
4     Author: David Robillard
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21
22 #ifndef __ardour_lv2_plugin_h__
23 #define __ardour_lv2_plugin_h__
24
25 #include <set>
26 #include <string>
27 #include <vector>
28
29 #include <dlfcn.h>
30
31 #include "pbd/stateful.h"
32
33 #include <jack/types.h>
34
35 #include "ardour/plugin.h"
36 #include "ardour/uri_map.h"
37
38 namespace ARDOUR {
39
40 class AudioEngine;
41 class Session;
42
43 class LV2Plugin : public ARDOUR::Plugin
44 {
45   public:
46         LV2Plugin (ARDOUR::AudioEngine& engine,
47                    ARDOUR::Session&     session,
48                    void*                c_plugin,
49                    framecnt_t           sample_rate);
50         LV2Plugin (const LV2Plugin &);
51         ~LV2Plugin ();
52
53         std::string unique_id () const;
54         const char* uri () const;
55         const char* label () const;
56         const char* name () const;
57         const char* maker () const;
58
59         uint32_t   num_ports () const;
60         uint32_t   parameter_count () const;
61         float      default_value (uint32_t port);
62         framecnt_t signal_latency () const;
63         void       set_parameter (uint32_t port, float val);
64         float      get_parameter (uint32_t port) const;
65         int        get_parameter_descriptor (uint32_t which, ParameterDescriptor&) const;
66         uint32_t   nth_parameter (uint32_t port, bool& ok) const;
67
68         const void* extension_data (const char* uri) const;
69
70         void* c_plugin();
71         void* c_ui();
72         void* c_ui_type();
73
74         bool is_external_ui () const;
75
76         const char* port_symbol (uint32_t port) const;
77
78         const LV2_Feature* const* features () { return _features; }
79
80         std::set<Evoral::Parameter> automatable () const;
81
82         void activate ();
83         void deactivate ();
84         void cleanup ();
85
86         int set_block_size (pframes_t /*nframes*/) { return 0; }
87
88         int connect_and_run (BufferSet& bufs,
89                              ChanMapping in, ChanMapping out,
90                              pframes_t nframes, framecnt_t offset);
91
92         std::string describe_parameter (Evoral::Parameter);
93         std::string state_node_name () const { return "lv2"; }
94
95         void print_parameter (uint32_t param,
96                               char*    buf,
97                               uint32_t len) const;
98
99         bool parameter_is_audio (uint32_t) const;
100         bool parameter_is_control (uint32_t) const;
101         bool parameter_is_midi (uint32_t) const;
102         bool parameter_is_input (uint32_t) const;
103         bool parameter_is_output (uint32_t) const;
104         bool parameter_is_toggled (uint32_t) const;
105
106         boost::shared_ptr<Plugin::ScalePoints>
107         get_scale_points(uint32_t port_index) const;
108
109         static uint32_t midi_event_type () { return _midi_event_type; }
110
111         void set_insert_info(const PluginInsert* insert);
112
113         int      set_state (const XMLNode& node, int version);
114         bool     save_preset (std::string uri);
115         void     remove_preset (std::string uri);
116         bool     load_preset (PresetRecord);
117         std::string current_preset () const;
118
119         bool has_editor () const;
120
121   private:
122         struct Impl;
123         Impl*             _impl;
124         void*             _module;
125         LV2_Feature**     _features;
126         framecnt_t        _sample_rate;
127         float*            _control_data;
128         float*            _shadow_data;
129         float*            _defaults;
130         float*            _latency_control_port;
131         bool              _was_activated;
132         bool              _has_state_interface;
133         std::vector<bool> _port_is_input;
134         std::vector<bool> _port_is_output;
135         std::vector<bool> _port_is_midi;
136         std::vector<bool> _port_is_audio;
137         std::vector<bool> _port_is_control;
138
139         std::map<std::string,uint32_t> _port_indices;
140
141         PBD::ID _insert_id;
142
143         typedef struct {
144                 const void* (*extension_data) (const char* uri);
145         } LV2_DataAccess;
146
147         LV2_DataAccess _data_access_extension_data;
148         LV2_Feature    _data_access_feature;
149         LV2_Feature    _instance_access_feature;
150         LV2_Feature    _map_path_feature;
151         LV2_Feature    _make_path_feature;
152
153         static URIMap   _uri_map;
154         static uint32_t _midi_event_type;
155         static uint32_t _state_path_type;
156
157         const std::string state_dir () const;
158
159         static int
160         lv2_state_store_callback (void*       handle,
161                                   uint32_t    key,
162                                   const void* value,
163                                   size_t      size,
164                                   uint32_t    type,
165                                   uint32_t    flags);
166
167         static const void*
168         lv2_state_retrieve_callback (void*     handle,
169                                      uint32_t  key,
170                                      size_t*   size,
171                                      uint32_t* type,
172                                      uint32_t* flags);
173
174         static char* lv2_state_abstract_path (void*       host_data,
175                                               const char* absolute_path);
176         static char* lv2_state_absolute_path (void*       host_data,
177                                               const char* abstract_path);
178         static char* lv2_state_make_path (void*       host_data,
179                                           const char* path);
180
181         void init (void* c_plugin, framecnt_t rate);
182         void run (pframes_t nsamples);
183
184         void latency_compute_run ();
185         std::string do_save_preset (std::string);
186         void do_remove_preset (std::string);
187         void find_presets ();
188         void add_state (XMLNode *) const;
189 };
190
191
192 class LV2PluginInfo : public PluginInfo {
193 public:
194         LV2PluginInfo (void* c_plugin);
195         ~LV2PluginInfo ();
196
197         static PluginInfoList* discover ();
198
199         PluginPtr load (Session& session);
200
201         void* _c_plugin;
202 };
203
204 typedef boost::shared_ptr<LV2PluginInfo> LV2PluginInfoPtr;
205
206 } // namespace ARDOUR
207
208 #endif /* __ardour_lv2_plugin_h__ */