Merge with trunk R2978.
[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
37 namespace ARDOUR {
38 class AudioEngine;
39 class Session;
40 struct LV2World;
41
42 class LV2Plugin : public ARDOUR::Plugin
43 {
44   public:
45         LV2Plugin (ARDOUR::AudioEngine&, ARDOUR::Session&, ARDOUR::LV2World&, SLV2Plugin plugin, nframes_t sample_rate);
46         LV2Plugin (const LV2Plugin &);
47         ~LV2Plugin ();
48
49         /* Plugin interface */
50         
51         std::string unique_id() const;
52         const char* label() const           { return slv2_value_as_string(_name); }
53         const char* name() const            { return slv2_value_as_string(_name); }
54         const char* maker() const           { return _author ? slv2_value_as_string(_author) : "Unknown"; }
55         uint32_t    parameter_count() const { return slv2_plugin_get_num_ports(_plugin); }
56         float       default_value (uint32_t port);
57         nframes_t   signal_latency() const;
58         void        set_parameter (uint32_t port, float val);
59         float       get_parameter (uint32_t port) const;
60         int         get_parameter_descriptor (uint32_t which, ParameterDescriptor&) const;
61         uint32_t    nth_parameter (uint32_t port, bool& ok) const;
62
63         SLV2Plugin slv2_plugin()         { return _plugin; }
64         SLV2Port   slv2_port(uint32_t i) { return slv2_plugin_get_port_by_index(_plugin, i); }
65         
66         std::set<Parameter> automatable() const;
67
68         void activate () { 
69                 if (!_was_activated) {
70                         slv2_instance_activate(_instance);
71                         _was_activated = true;
72                 }
73         }
74
75         void deactivate () {
76                 if (_was_activated) {
77                         slv2_instance_deactivate(_instance);
78                         _was_activated = false;
79                 }
80         }
81
82         void cleanup () {
83                 activate();
84                 deactivate();
85                 slv2_instance_free(_instance);
86                 _instance = NULL;
87         }
88
89         void set_block_size (nframes_t nframes) {}
90         
91         int         connect_and_run (BufferSet& bufs, uint32_t& in, uint32_t& out, nframes_t nframes, nframes_t offset);
92         std::string describe_parameter (Parameter);
93         std::string state_node_name() const { return "lv2"; }
94         void        print_parameter (uint32_t, char*, uint32_t len) const;
95
96         bool parameter_is_audio(uint32_t) const;
97         bool parameter_is_control(uint32_t) const;
98         bool parameter_is_input(uint32_t) const;
99         bool parameter_is_output(uint32_t) const;
100         bool parameter_is_toggled(uint32_t) const;
101
102         XMLNode& get_state();
103         int      set_state(const XMLNode& node);
104         bool     save_preset(std::string name);
105
106         bool has_editor() const { return false; }
107
108         int require_output_streams (uint32_t);
109         
110   private:
111         void*                    _module;
112         LV2World&                _world;
113         SLV2Plugin               _plugin;
114         SLV2Value                _name;
115         SLV2Value                _author;
116         SLV2Instance             _instance;
117         nframes_t                _sample_rate;
118         float*                   _control_data;
119         float*                   _shadow_data;
120         float*                   _defaults;
121         float*                   _latency_control_port;
122         bool                     _was_activated;
123         vector<bool>             _port_is_input;
124
125         void init (LV2World& world, SLV2Plugin plugin, nframes_t rate);
126         void run (nframes_t nsamples);
127         void latency_compute_run ();
128 };
129
130
131 /** The SLV2World, and various cached (as symbols, fast) URIs.
132  *
133  * This object represents everything ardour 'knows' about LV2
134  * (ie understood extensions/features/etc)
135  */
136 struct LV2World {
137         LV2World();
138         ~LV2World();
139
140         SLV2World world;
141         SLV2Value input_class;
142         SLV2Value output_class;
143         SLV2Value audio_class;
144         SLV2Value control_class;
145         SLV2Value event_class;
146         SLV2Value in_place_broken;
147         SLV2Value integer;
148         SLV2Value toggled;
149         SLV2Value srate;
150 };
151
152
153 class LV2PluginInfo : public PluginInfo {
154 public: 
155         LV2PluginInfo (void* slv2_world, void* slv2_plugin);;
156         ~LV2PluginInfo ();;
157         static PluginInfoList discover (void* slv2_world);
158
159         PluginPtr load (Session& session);
160
161         void* _lv2_world;
162         void* _slv2_plugin;
163 };
164
165 typedef boost::shared_ptr<LV2PluginInfo> LV2PluginInfoPtr;
166
167 } // namespace ARDOUR
168
169 #endif /* __ardour_lv2_plugin_h__ */