another TLSF vs realloc-pool shootout
[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 //#define WITH_LUAPROC_STATS
21 //#define USE_TLSF
22
23 #ifndef __ardour_luaproc_h__
24 #define __ardour_luaproc_h__
25
26 #include <set>
27 #include <vector>
28 #include <string>
29
30 #ifdef USE_TLSF
31 #  include "pbd/tlsf.h"
32 #else
33 #  include "pbd/reallocpool.h"
34 #endif
35
36 #include "pbd/stateful.h"
37
38 #include "ardour/types.h"
39 #include "ardour/plugin.h"
40 #include "ardour/luascripting.h"
41 #include "ardour/dsp_filter.h"
42 #include "ardour/lua_api.h"
43
44 #include "lua/luastate.h"
45
46 namespace luabridge {
47         class LuaRef;
48 }
49
50 namespace ARDOUR {
51
52 class LIBARDOUR_API LuaProc : public ARDOUR::Plugin {
53 public:
54         LuaProc (AudioEngine&, Session&, const std::string&);
55         LuaProc (const LuaProc &);
56         ~LuaProc ();
57
58         /* Plugin interface */
59
60         std::string unique_id() const { return get_info()->unique_id; }
61         const char* name()  const { return get_info()->name.c_str(); }
62         const char* label() const { return get_info()->name.c_str(); }
63         const char* maker() const { return get_info()->creator.c_str(); }
64
65         uint32_t    parameter_count() const;
66         float       default_value (uint32_t port);
67         void        set_parameter (uint32_t port, float val);
68         float       get_parameter (uint32_t port) const;
69         int         get_parameter_descriptor (uint32_t which, ParameterDescriptor&) const;
70         uint32_t    nth_parameter (uint32_t port, bool& ok) const;
71
72         std::string get_docs () const { return _docs; }
73         std::string get_parameter_docs (uint32_t) const;
74
75         PluginOutputConfiguration possible_output () const { return _output_configs; }
76
77         std::set<Evoral::Parameter> automatable() const;
78
79         void activate () { }
80         void deactivate () { }
81         void cleanup () { }
82
83         int set_block_size (pframes_t /*nframes*/) { return 0; }
84         framecnt_t  signal_latency() const { return 0; }
85
86         int connect_and_run (BufferSet& bufs,
87                         framepos_t start, framepos_t end, double speed,
88                         ChanMapping in, ChanMapping out,
89                         pframes_t nframes, framecnt_t offset);
90
91         std::string describe_parameter (Evoral::Parameter);
92         void        print_parameter (uint32_t, char*, uint32_t len) const;
93         boost::shared_ptr<ScalePoints> get_scale_points(uint32_t port_index) const;
94
95         bool parameter_is_audio (uint32_t) const { return false; }
96         bool parameter_is_control (uint32_t) const { return true; }
97         bool parameter_is_input (uint32_t) const;
98         bool parameter_is_output (uint32_t) const;
99
100         uint32_t designated_bypass_port () {
101                 return _designated_bypass_port;
102         }
103
104         std::string state_node_name() const { return "luaproc"; }
105         void add_state (XMLNode *) const;
106         int set_state (const XMLNode&, int version);
107         int set_script_from_state (const XMLNode&);
108
109         bool load_preset (PresetRecord);
110         std::string do_save_preset (std::string);
111         void do_remove_preset (std::string);
112
113         bool has_editor() const { return false; }
114
115         bool can_support_io_configuration (const ChanCount& in, ChanCount& out, ChanCount* imprecise);
116         bool configure_io (ChanCount in, ChanCount out);
117
118         ChanCount output_streams() const { return _configured_out; }
119         ChanCount input_streams() const { return _configured_in; }
120
121         bool has_inline_display () { return _lua_has_inline_display; }
122         void setup_lua_inline_gui (LuaState *lua_gui);
123
124 private:
125         void find_presets ();
126
127         /* END Plugin interface */
128 protected:
129         const std::string& script() const { return _script; }
130
131 private:
132 #ifdef USE_TLSF
133         PBD::TLSF _mempool;
134 #else
135         PBD::ReallocPool _mempool;
136 #endif
137         LuaState lua;
138         luabridge::LuaRef * _lua_dsp;
139         std::string _script;
140         std::string _docs;
141         bool _lua_does_channelmapping;
142         bool _lua_has_inline_display;
143
144         void queue_draw () { QueueDraw(); /* EMIT SIGNAL */ }
145         DSP::DspShm* instance_shm () { return &lshm; }
146         DSP::DspShm lshm;
147
148         LuaTableRef* instance_ref () { return &lref; }
149         LuaTableRef lref;
150
151         void init ();
152         bool load_script ();
153         void lua_print (std::string s);
154
155         std::string preset_name_to_uri (const std::string&) const;
156         std::string presets_file () const;
157         XMLTree* presets_tree () const;
158
159         boost::shared_ptr<ScalePoints> parse_scale_points (luabridge::LuaRef*);
160
161         std::vector<std::pair<bool, int> > _ctrl_params;
162         std::map<int, ARDOUR::ParameterDescriptor> _param_desc;
163         std::map<int, std::string> _param_doc;
164         uint32_t _designated_bypass_port;
165
166         float* _control_data;
167         float* _shadow_data;
168
169         ChanCount _configured_in;
170         ChanCount _configured_out;
171
172         ChanCount _selected_in;
173         ChanCount _selected_out;
174
175         PluginOutputConfiguration _output_configs;
176
177         bool _has_midi_input;
178         bool _has_midi_output;
179
180 #ifdef WITH_LUAPROC_STATS
181         int64_t _stats_avg[2];
182         int64_t _stats_max[2];
183         int64_t _stats_cnt;
184 #endif
185 };
186
187 class LIBARDOUR_API LuaPluginInfo : public PluginInfo
188 {
189   public:
190         LuaPluginInfo (LuaScriptInfoPtr lsi);
191         ~LuaPluginInfo () { };
192
193         PluginPtr load (Session& session);
194         std::vector<Plugin::PresetRecord> get_presets (bool user_only) const;
195
196         bool in_category (const std::string &c) const {
197                 return (category == c);
198         }
199         bool is_instrument () const { return _is_instrument; }
200         bool reconfigurable_io() const { return true; }
201
202         bool _is_instrument;
203 };
204
205 typedef boost::shared_ptr<LuaPluginInfo> LuaPluginInfoPtr;
206
207 } // namespace ARDOUR
208
209 #endif // __ardour_luaproc_h__