summaryrefslogtreecommitdiff
path: root/libs/ardour/ardour/luaproc.h
blob: 8ac27095abf8bbad7c8e1edba3d2e57635b07377 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
    Copyright (C) 2016 Robin Gareus <robin@gareus.org>
    Copyright (C) 2006 Paul Davis

    This program is free software; you can redistribute it and/or modify it
    under the terms of the GNU General Public License as published by the Free
    Software Foundation; either version 2 of the License, or (at your option)
    any later version.

    This program is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    675 Mass Ave, Cambridge, MA 02139, USA.
*/

/* print runtime and garbage-collection timing statistics */
//#define WITH_LUAPROC_STATS

/* memory allocation system, default: ReallocPool */
//#define USE_TLSF // use TLSF instead of ReallocPool
//#define USE_MALLOC // or plain OS provided realloc (no mlock) -- if USE_TLSF isn't defined

#ifndef __ardour_luaproc_h__
#define __ardour_luaproc_h__

#include <set>
#include <vector>
#include <string>

#ifdef USE_TLSF
#  include "pbd/tlsf.h"
#else
#  include "pbd/reallocpool.h"
#endif

#include "pbd/stateful.h"

#include "ardour/types.h"
#include "ardour/plugin.h"
#include "ardour/luascripting.h"
#include "ardour/dsp_filter.h"
#include "ardour/lua_api.h"

#include "lua/luastate.h"

namespace luabridge {
	class LuaRef;
}

namespace ARDOUR {

class LIBARDOUR_API LuaProc : public ARDOUR::Plugin {
public:
	LuaProc (AudioEngine&, Session&, const std::string&);
	LuaProc (const LuaProc &);
	~LuaProc ();

	/* Plugin interface */

	std::string unique_id() const { return get_info()->unique_id; }
	const char* name()  const { return get_info()->name.c_str(); }
	const char* label() const { return get_info()->name.c_str(); }
	const char* maker() const { return get_info()->creator.c_str(); }

	uint32_t    parameter_count() const;
	float       default_value (uint32_t port);
	void        set_parameter (uint32_t port, float val);
	float       get_parameter (uint32_t port) const;
	int         get_parameter_descriptor (uint32_t which, ParameterDescriptor&) const;
	uint32_t    nth_parameter (uint32_t port, bool& ok) const;

	std::string get_docs () const { return _docs; }
	std::string get_parameter_docs (uint32_t) const;

	PluginOutputConfiguration possible_output () const { return _output_configs; }

	std::set<Evoral::Parameter> automatable() const;

	void activate () { }
	void deactivate () { }
	void cleanup () { }

	int set_block_size (pframes_t /*nframes*/) { return 0; }
	samplecnt_t  signal_latency() const { return 0; }

	int connect_and_run (BufferSet& bufs,
			samplepos_t start, samplepos_t end, double speed,
			ChanMapping in, ChanMapping out,
			pframes_t nframes, samplecnt_t offset);

	std::string describe_parameter (Evoral::Parameter);
	void        print_parameter (uint32_t, char*, uint32_t len) const;
	boost::shared_ptr<ScalePoints> get_scale_points(uint32_t port_index) const;

	bool parameter_is_audio (uint32_t) const { return false; }
	bool parameter_is_control (uint32_t) const { return true; }
	bool parameter_is_input (uint32_t) const;
	bool parameter_is_output (uint32_t) const;

	uint32_t designated_bypass_port () {
		return _designated_bypass_port;
	}

	std::string state_node_name() const { return "luaproc"; }
	void add_state (XMLNode *) const;
	int set_state (const XMLNode&, int version);
	int set_script_from_state (const XMLNode&);

	bool load_preset (PresetRecord);
	std::string do_save_preset (std::string);
	void do_remove_preset (std::string);

	bool has_editor() const { return false; }

	bool can_support_io_configuration (const ChanCount& in, ChanCount& out, ChanCount* imprecise);
	bool configure_io (ChanCount in, ChanCount out);

	ChanCount output_streams() const { return _configured_out; }
	ChanCount input_streams() const { return _configured_in; }

	bool has_inline_display () { return _lua_has_inline_display; }
	void setup_lua_inline_gui (LuaState *lua_gui);

	DSP::DspShm* instance_shm () { return &lshm; }
	LuaTableRef* instance_ref () { return &lref; }

private:
	void find_presets ();

	/* END Plugin interface */

public:
	void set_origin (std::string& path) { _origin = path; }

protected:
	const std::string& script() const { return _script; }
	const std::string& origin() const { return _origin; }

private:
#ifdef USE_TLSF
	PBD::TLSF _mempool;
#else
	PBD::ReallocPool _mempool;
#endif
	LuaState lua;
	luabridge::LuaRef * _lua_dsp;
	std::string _script;
	std::string _origin;
	std::string _docs;
	bool _lua_does_channelmapping;
	bool _lua_has_inline_display;

	void queue_draw () { QueueDraw(); /* EMIT SIGNAL */ }
	DSP::DspShm lshm;

	LuaTableRef lref;

	boost::weak_ptr<Route> route () const;

	void init ();
	bool load_script ();
	void lua_print (std::string s);

	std::string preset_name_to_uri (const std::string&) const;
	std::string presets_file () const;
	XMLTree* presets_tree () const;

	boost::shared_ptr<ScalePoints> parse_scale_points (luabridge::LuaRef*);

	std::vector<std::pair<bool, int> > _ctrl_params;
	std::map<int, ARDOUR::ParameterDescriptor> _param_desc;
	std::map<int, std::string> _param_doc;
	uint32_t _designated_bypass_port;

	float* _control_data;
	float* _shadow_data;

	ChanCount _configured_in;
	ChanCount _configured_out;

	bool      _configured;

	ChanCount _selected_in;
	ChanCount _selected_out;

	PluginOutputConfiguration _output_configs;

	bool _has_midi_input;
	bool _has_midi_output;

#ifdef WITH_LUAPROC_STATS
	int64_t _stats_avg[2];
	int64_t _stats_max[2];
	int64_t _stats_cnt;
#endif
};

class LIBARDOUR_API LuaPluginInfo : public PluginInfo
{
  public:
	LuaPluginInfo (LuaScriptInfoPtr lsi);
	~LuaPluginInfo () { };

	PluginPtr load (Session& session);
	std::vector<Plugin::PresetRecord> get_presets (bool user_only) const;

	bool reconfigurable_io() const { return true; }
};

typedef boost::shared_ptr<LuaPluginInfo> LuaPluginInfoPtr;

} // namespace ARDOUR

#endif // __ardour_luaproc_h__