Clean up plugin preset handling a bit.
[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 <set>
24 #include <vector>
25 #include <string>
26 #include <dlfcn.h>
27
28
29 #include "pbd/stateful.h"
30
31 #include <jack/types.h>
32 #include "ardour/ladspa.h"
33 #include "ardour/plugin.h"
34
35 namespace ARDOUR {
36 class AudioEngine;
37 class Session;
38
39 class LadspaPlugin : public ARDOUR::Plugin
40 {
41   public:
42         LadspaPlugin (void *module, ARDOUR::AudioEngine&, ARDOUR::Session&, uint32_t index, framecnt_t sample_rate);
43         LadspaPlugin (const LadspaPlugin &);
44         ~LadspaPlugin ();
45
46         /* Plugin interface */
47
48         std::string unique_id() const;
49         const char* label() const           { return _descriptor->Label; }
50         const char* name() const            { return _descriptor->Name; }
51         const char* maker() const           { return _descriptor->Maker; }
52         uint32_t    parameter_count() const { return _descriptor->PortCount; }
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         std::set<Evoral::Parameter> automatable() const;
61
62         void activate () {
63                 if (!_was_activated && _descriptor->activate)
64                         _descriptor->activate (_handle);
65
66                 _was_activated = true;
67         }
68
69         void deactivate () {
70                 if (_was_activated && _descriptor->deactivate)
71                         _descriptor->deactivate (_handle);
72
73                 _was_activated = false;
74         }
75
76         void cleanup () {
77                 activate();
78                 deactivate();
79
80                 if (_descriptor->cleanup)
81                         _descriptor->cleanup (_handle);
82         }
83
84         int set_block_size (pframes_t /*nframes*/) { return 0; }
85
86         int connect_and_run (BufferSet& bufs,
87                         ChanMapping in, ChanMapping out,
88                         pframes_t nframes, framecnt_t offset);
89
90         std::string describe_parameter (Evoral::Parameter);
91         std::string state_node_name() const { return "ladspa"; }
92         void        print_parameter (uint32_t, char*, uint32_t len) const;
93
94         bool parameter_is_audio(uint32_t) const;
95         bool parameter_is_control(uint32_t) const;
96         bool parameter_is_input(uint32_t) const;
97         bool parameter_is_output(uint32_t) const;
98         bool parameter_is_toggled(uint32_t) const;
99
100         int      set_state (const XMLNode&, int version);
101
102         bool load_preset (PresetRecord);
103
104         bool has_editor() const { return false; }
105
106         int require_output_streams (uint32_t);
107
108         /* LADSPA extras */
109
110         LADSPA_Properties           properties() const                { return _descriptor->Properties; }
111         uint32_t                    index() const                     { return _index; }
112         const char *                copyright() const                 { return _descriptor->Copyright; }
113         LADSPA_PortDescriptor       port_descriptor(uint32_t i) const { return _descriptor->PortDescriptors[i]; }
114         const LADSPA_PortRangeHint* port_range_hints() const          { return _descriptor->PortRangeHints; }
115         const char * const *        port_names() const                { return _descriptor->PortNames; }
116
117         void set_gain (float gain)                    { _descriptor->set_run_adding_gain (_handle, gain); }
118         void run_adding (uint32_t nsamples)           { _descriptor->run_adding (_handle, nsamples); }
119         void connect_port (uint32_t port, float *ptr) { _descriptor->connect_port (_handle, port, ptr); }
120
121   private:
122         void*                    _module;
123         const LADSPA_Descriptor* _descriptor;
124         LADSPA_Handle            _handle;
125         framecnt_t               _sample_rate;
126         LADSPA_Data*             _control_data;
127         LADSPA_Data*             _shadow_data;
128         LADSPA_Data*             _latency_control_port;
129         uint32_t                 _index;
130         bool                     _was_activated;
131
132         void find_presets ();
133         
134         void init (void *mod, uint32_t index, framecnt_t rate);
135         void run_in_place (pframes_t nsamples);
136         void latency_compute_run ();
137         int set_state_2X (const XMLNode&, int version);
138         std::string do_save_preset (std::string name);
139         void do_remove_preset (std::string name);
140         std::string preset_envvar () const;
141         std::string preset_source (std::string) const;
142         bool write_preset_file (std::string);
143         void add_state (XMLNode *) const;
144 };
145
146 class LadspaPluginInfo : public PluginInfo {
147   public:
148         LadspaPluginInfo ();
149         ~LadspaPluginInfo () { };
150
151         PluginPtr load (Session& session);
152 };
153
154 typedef boost::shared_ptr<LadspaPluginInfo> LadspaPluginInfoPtr;
155
156 } // namespace ARDOUR
157
158 #endif /* __ardour_ladspa_plugin_h__ */