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