Make sure we don't deactivate un-activated plugins or re-activate already activated...
[ardour.git] / libs / ardour / ardour / ladspa_plugin.h
1 /*
2     Copyright (C) 2000-2006 Paul Davis 
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __ardour_ladspa_plugin_h__
21 #define __ardour_ladspa_plugin_h__
22
23 #include <list>
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 <ardour/ladspa.h>
35 #include <ardour/plugin.h>
36 #include <ardour/ladspa_plugin.h>
37
38 using std::string;
39 using std::vector;
40 using std::list;
41
42 namespace ARDOUR {
43 class AudioEngine;
44 class Session;
45
46 class LadspaPlugin : public ARDOUR::Plugin
47 {
48   public:
49         LadspaPlugin (void *module, ARDOUR::AudioEngine&, ARDOUR::Session&, uint32_t index, nframes_t sample_rate);
50         LadspaPlugin (const LadspaPlugin &);
51         ~LadspaPlugin ();
52
53         /* Plugin interface */
54         
55         uint32_t unique_id() const                       { return descriptor->UniqueID; }
56         const char * label() const                       { return descriptor->Label; }
57         const char * name() const                        { return descriptor->Name; }
58         const char * maker() const                       { return descriptor->Maker; }
59         uint32_t parameter_count() const                 { return descriptor->PortCount; }
60         float default_value (uint32_t port);
61         nframes_t latency() const;
62         void set_parameter (uint32_t port, float val);
63         float get_parameter (uint32_t port) const;
64         int get_parameter_descriptor (uint32_t which, ParameterDescriptor&) const;
65         std::set<uint32_t> automatable() const;
66         uint32_t nth_parameter (uint32_t port, bool& ok) const;
67         void activate () { 
68                 if (was_activated)
69                         return;
70
71                 if (descriptor->activate) {
72                         descriptor->activate (handle);
73                 }
74                 was_activated = true;
75         }
76         void deactivate () {
77                 if (!was_activated)
78                         return;
79
80                 if (descriptor->deactivate) 
81                         descriptor->deactivate (handle);
82         }
83         void cleanup () {
84                 if (was_activated && descriptor->cleanup) {
85                         descriptor->cleanup (handle);
86                 }
87         }
88         void set_block_size (nframes_t nframes) {}
89         
90         int connect_and_run (vector<Sample*>& bufs, uint32_t maxbuf, int32_t& in, int32_t& out, nframes_t nframes, nframes_t offset);
91         string describe_parameter (uint32_t);
92         string state_node_name() const { return "ladspa"; }
93         void print_parameter (uint32_t, char*, uint32_t len) const;
94
95         bool parameter_is_audio(uint32_t) const;
96         bool parameter_is_control(uint32_t) const;
97         bool parameter_is_input(uint32_t) const;
98         bool parameter_is_output(uint32_t) const;
99         bool parameter_is_toggled(uint32_t) const;
100
101         XMLNode& get_state();
102         int set_state(const XMLNode& node);
103         bool save_preset(string name);
104
105         bool has_editor() const { return false; }
106
107         int require_output_streams (uint32_t);
108         
109         /* LADSPA extras */
110
111         LADSPA_Properties properties() const             { return descriptor->Properties; }
112         uint32_t index() const                      { return _index; }
113         const char * copyright() const                   { return descriptor->Copyright; }
114         LADSPA_PortDescriptor port_descriptor(uint32_t i) const { return descriptor->PortDescriptors[i]; }
115         const LADSPA_PortRangeHint * port_range_hints() const { return descriptor->PortRangeHints; }
116         const char * const * port_names() const          { return descriptor->PortNames; }
117         void set_gain (float gain) {
118                 descriptor->set_run_adding_gain (handle, gain);
119         }
120         void run_adding (uint32_t nsamples) {
121                 descriptor->run_adding (handle, nsamples);
122         }
123         void connect_port (uint32_t port, float *ptr) {
124                 descriptor->connect_port (handle, port, ptr);
125         }
126
127   private:
128         void                    *module;
129         const LADSPA_Descriptor *descriptor;
130         LADSPA_Handle            handle;
131         nframes_t           sample_rate;
132         LADSPA_Data             *control_data;
133         LADSPA_Data             *shadow_data;
134         LADSPA_Data             *latency_control_port;
135         uint32_t                _index;
136         bool                     was_activated;
137
138         void init (void *mod, uint32_t index, nframes_t rate);
139         void run (nframes_t nsamples);
140         void latency_compute_run ();
141 };
142
143 class LadspaPluginInfo : public PluginInfo {
144   public:       
145         LadspaPluginInfo () { };
146         ~LadspaPluginInfo () { };
147
148         PluginPtr load (Session& session);
149 };
150
151 typedef boost::shared_ptr<LadspaPluginInfo> LadspaPluginInfoPtr;
152
153 } // namespace ARDOUR
154
155 #endif /* __ardour_ladspa_plugin_h__ */