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