proper semantics for variable port-count
[ardour.git] / libs / ardour / ardour / luaproc.h
1 /*
2     Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3     Copyright (C) 2006 Paul Davis
4
5     This program is free software; you can redistribute it and/or modify it
6     under the terms of the GNU General Public License as published by the Free
7     Software Foundation; either version 2 of the License, or (at your option)
8     any later version.
9
10     This program is distributed in the hope that it will be useful, but WITHOUT
11     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13     for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifndef __ardour_luaproc_h__
21 #define __ardour_luaproc_h__
22
23 #include <set>
24 #include <vector>
25 #include <string>
26
27 #include "pbd/reallocpool.h"
28 #include "pbd/stateful.h"
29
30 #include "ardour/types.h"
31 #include "ardour/plugin.h"
32 #include "ardour/luascripting.h"
33 #include "ardour/dsp_filter.h"
34 #include "ardour/lua_api.h"
35
36 #include "lua/luastate.h"
37
38 namespace luabridge {
39         class LuaRef;
40 }
41
42 namespace ARDOUR {
43
44 class LIBARDOUR_API LuaProc : public ARDOUR::Plugin {
45 public:
46         LuaProc (AudioEngine&, Session&, const std::string&);
47         LuaProc (const LuaProc &);
48         ~LuaProc ();
49
50         /* Plugin interface */
51
52         std::string unique_id() const { return get_info()->unique_id; }
53         const char* name()  const { return get_info()->name.c_str(); }
54         const char* label() const { return get_info()->name.c_str(); }
55         const char* maker() const { return get_info()->creator.c_str(); }
56
57         uint32_t    parameter_count() const;
58         float       default_value (uint32_t port);
59         void        set_parameter (uint32_t port, float val);
60         float       get_parameter (uint32_t port) const;
61         int         get_parameter_descriptor (uint32_t which, ParameterDescriptor&) const;
62         uint32_t    nth_parameter (uint32_t port, bool& ok) const;
63
64         std::string get_docs () const { return _docs; }
65         std::string get_parameter_docs (uint32_t) const;
66
67         PluginOutputConfiguration possible_output () const { return _output_configs; }
68
69         std::set<Evoral::Parameter> automatable() const;
70
71         void activate () { }
72         void deactivate () { }
73         void cleanup () { }
74
75         int set_block_size (pframes_t /*nframes*/) { return 0; }
76         framecnt_t  signal_latency() const { return 0; }
77
78         int connect_and_run (BufferSet& bufs,
79                         framepos_t start, framepos_t end, double speed,
80                         ChanMapping in, ChanMapping out,
81                         pframes_t nframes, framecnt_t offset);
82
83         std::string describe_parameter (Evoral::Parameter);
84         void        print_parameter (uint32_t, char*, uint32_t len) const;
85         boost::shared_ptr<ScalePoints> get_scale_points(uint32_t port_index) const;
86
87         bool parameter_is_audio (uint32_t) const { return false; }
88         bool parameter_is_control (uint32_t) const { return true; }
89         bool parameter_is_input (uint32_t) const;
90         bool parameter_is_output (uint32_t) const;
91
92         uint32_t designated_bypass_port () {
93                 return _designated_bypass_port;
94         }
95
96         std::string state_node_name() const { return "luaproc"; }
97         void add_state (XMLNode *) const;
98         int set_state (const XMLNode&, int version);
99         int set_script_from_state (const XMLNode&);
100
101         bool load_preset (PresetRecord);
102         std::string do_save_preset (std::string);
103         void do_remove_preset (std::string);
104
105         bool has_editor() const { return false; }
106
107         bool can_support_io_configuration (const ChanCount& in, ChanCount& out, ChanCount* imprecise);
108         bool configure_io (ChanCount in, ChanCount out);
109
110         ChanCount output_streams() const { return _configured_out; }
111         ChanCount input_streams() const { return _configured_in; }
112
113         bool has_inline_display () { return _lua_has_inline_display; }
114         void setup_lua_inline_gui (LuaState *lua_gui);
115
116 private:
117         void find_presets ();
118
119         /* END Plugin interface */
120 protected:
121         const std::string& script() const { return _script; }
122
123 private:
124         PBD::ReallocPool _mempool;
125         LuaState lua;
126         luabridge::LuaRef * _lua_dsp;
127         std::string _script;
128         std::string _docs;
129         bool _lua_does_channelmapping;
130         bool _lua_has_inline_display;
131
132         void queue_draw () { QueueDraw(); /* EMIT SIGNAL */ }
133         DSP::DspShm* instance_shm () { return &lshm; }
134         DSP::DspShm lshm;
135
136         LuaTableRef* instance_ref () { return &lref; }
137         LuaTableRef lref;
138
139         void init ();
140         bool load_script ();
141         void lua_print (std::string s);
142
143         std::string preset_name_to_uri (const std::string&) const;
144         std::string presets_file () const;
145         XMLTree* presets_tree () const;
146
147         boost::shared_ptr<ScalePoints> parse_scale_points (luabridge::LuaRef*);
148
149         std::vector<std::pair<bool, int> > _ctrl_params;
150         std::map<int, ARDOUR::ParameterDescriptor> _param_desc;
151         std::map<int, std::string> _param_doc;
152         uint32_t _designated_bypass_port;
153
154         float* _control_data;
155         float* _shadow_data;
156
157         ChanCount _configured_in;
158         ChanCount _configured_out;
159
160         ChanCount _selected_in;
161         ChanCount _selected_out;
162
163         PluginOutputConfiguration _output_configs;
164
165         bool _has_midi_input;
166         bool _has_midi_output;
167
168 #ifdef WITH_LUAPROC_STATS
169         int64_t _stats_avg[2];
170         int64_t _stats_max[2];
171         int64_t _stats_cnt;
172 #endif
173 };
174
175 class LIBARDOUR_API LuaPluginInfo : public PluginInfo
176 {
177   public:
178         LuaPluginInfo (LuaScriptInfoPtr lsi);
179         ~LuaPluginInfo () { };
180
181         PluginPtr load (Session& session);
182         std::vector<Plugin::PresetRecord> get_presets (bool user_only) const;
183
184         bool in_category (const std::string &c) const {
185                 return (category == c);
186         }
187         bool is_instrument () const { return _is_instrument; }
188         bool reconfigurable_io() const { return true; }
189
190         bool _is_instrument;
191 };
192
193 typedef boost::shared_ptr<LuaPluginInfo> LuaPluginInfoPtr;
194
195 } // namespace ARDOUR
196
197 #endif // __ardour_luaproc_h__