convert codebase to use Temporal for various time types
[ardour.git] / libs / ardour / ardour / lua_api.h
1 /*
2  * Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19 #ifndef _ardour_lua_api_h_
20 #define _ardour_lua_api_h_
21
22 #include <string>
23 #include <lo/lo.h>
24 #include <boost/shared_ptr.hpp>
25 #include <vamp-hostsdk/Plugin.h>
26
27 #include "evoral/Note.hpp"
28
29 #include "ardour/libardour_visibility.h"
30
31 #include "ardour/midi_model.h"
32 #include "ardour/processor.h"
33 #include "ardour/session.h"
34
35 namespace ARDOUR {
36         class Readable;
37 }
38
39 namespace ARDOUR { namespace LuaAPI {
40
41         /** convenience constructor for DataType::NIL with managed lifetime
42          * @returns DataType::NIL
43          */
44         int datatype_ctor_null (lua_State *lua);
45         /** convenience constructor for DataType::AUDIO with managed lifetime
46          * @returns DataType::AUDIO
47          */
48         int datatype_ctor_audio (lua_State *L);
49         /** convenience constructor for DataType::MIDI with managed lifetime
50          * @returns DataType::MIDI
51          */
52         int datatype_ctor_midi (lua_State *L);
53
54         /** Create a null processor shared pointer
55          *
56          * This is useful for Track:bounce() to indicate no processing.
57          */
58         boost::shared_ptr<ARDOUR::Processor> nil_processor ();
59
60         /** create a new Lua Processor (Plugin)
61          *
62          * @param s Session Handle
63          * @param p Identifier or Name of the Processor
64          * @returns Processor object (may be nil)
65          */
66         boost::shared_ptr<ARDOUR::Processor> new_luaproc (ARDOUR::Session *s, const std::string& p);
67
68         /** search a Plugin
69          *
70          * @param id Plugin Name, ID or URI
71          * @param type Plugin Type
72          * @returns PluginInfo or nil if not found
73          */
74         boost::shared_ptr<ARDOUR::PluginInfo> new_plugin_info (const std::string& id, ARDOUR::PluginType type);
75
76         /** create a new Plugin Instance
77          *
78          * @param s Session Handle
79          * @param id Plugin Name, ID or URI
80          * @param type Plugin Type
81          * @returns Processor or nil
82          */
83         boost::shared_ptr<ARDOUR::Processor> new_plugin (ARDOUR::Session *s, const std::string& id, ARDOUR::PluginType type, const std::string& preset = "");
84
85         /** set a plugin control-input parameter value
86          *
87          * @param proc Plugin-Processor
88          * @param which control-input to set (starting at 0)
89          * @param value value to set
90          * @returns true on success, false on error or out-of-bounds value
91          */
92         bool set_processor_param (boost::shared_ptr<ARDOUR::Processor> proc, uint32_t which, float val);
93
94         /** get a plugin control parameter value
95          *
96          * @param proc Plugin-Processor
97          * @param which control port to set (starting at 0, including ports of type input and output))
98          * @param ok boolean variable contains true or false after call returned. to be checked by caller before using value.
99          * @returns value
100          */
101         float get_processor_param (boost::shared_ptr<Processor> proc, uint32_t which, bool &ok);
102
103         /** reset a processor to its default values (only works for plugins )
104          *
105          * This is a wrapper which looks up the Processor by plugin-insert.
106          *
107          * @param proc Plugin-Insert
108          * @returns true on success, false when the processor is not a plugin
109          */
110         bool reset_processor_to_default (boost::shared_ptr<Processor> proc);
111
112         /** set a plugin control-input parameter value
113          *
114          * This is a wrapper around set_processor_param which looks up the Processor by plugin-insert.
115          *
116          * @param proc Plugin-Insert
117          * @param which control-input to set (starting at 0)
118          * @param value value to set
119          * @returns true on success, false on error or out-of-bounds value
120          */
121         bool set_plugin_insert_param (boost::shared_ptr<ARDOUR::PluginInsert> pi, uint32_t which, float val);
122
123         /** get a plugin control parameter value
124          *
125          * @param proc Plugin-Insert
126          * @param which control port to query (starting at 0, including ports of type input and output)
127          * @param ok boolean variable contains true or false after call returned. to be checked by caller before using value.
128          * @returns value
129          */
130         float get_plugin_insert_param (boost::shared_ptr<ARDOUR::PluginInsert> pi, uint32_t which, bool &ok);
131
132         /**
133          * A convenience function to get a Automation Lists and ParamaterDescriptor
134          * for a given plugin control.
135          *
136          * This is equivalent to the following lua code
137          * @code
138          * function (processor, param_id)
139          *  local plugininsert = processor:to_insert ()
140          *  local plugin = plugininsert:plugin(0)
141          *  local _, t = plugin:get_parameter_descriptor(param_id, ARDOUR.ParameterDescriptor ())
142          *  local ctrl = Evoral.Parameter (ARDOUR.AutomationType.PluginAutomation, 0, param_id)
143          *  local ac = pi:automation_control (ctrl, false)
144          *  local acl = ac:alist()
145          *  return ac:alist(), ac:to_ctrl():list(), t[2]
146          * end
147          * @endcode
148          *
149          * Example usage: get the third input parameter of first plugin on the given route
150          * (Ardour starts counting at zero).
151          * @code
152          * local al, cl, pd = ARDOUR.LuaAPI.plugin_automation (route:nth_plugin (0), 3)
153          * @endcode
154          * @returns 3 parameters: AutomationList, ControlList, ParamaterDescriptor
155          */
156         int plugin_automation (lua_State *lua);
157
158         /**
159          * A convenience function for colorspace HSL to RGB conversion.
160          * All ranges are 0..1
161          *
162          * Example:
163          * @code
164          * local r, g, b, a = ARDOUR.LuaAPI.hsla_to_rgba (hue, saturation, luminosity, alpha)
165          * @endcode
166          * @returns 4 parameters: red, green, blue, alpha (in range 0..1)
167          */
168         int hsla_to_rgba (lua_State *lua);
169
170         /**
171          * A convenience function to expand RGBA parameters from an integer
172          *
173          * convert a Canvas::Color (uint32_t 0xRRGGBBAA) into
174          * double RGBA values which can be passed as parameters to
175          * Cairo::Context::set_source_rgba
176          *
177          * Example:
178          * @code
179          * local r, g, b, a = ARDOUR.LuaAPI.color_to_rgba (0x88aa44ff)
180          * cairo_ctx:set_source_rgba (ARDOUR.LuaAPI.color_to_rgba (0x11336699)
181          * @endcode
182          * @returns 4 parameters: red, green, blue, alpha (in range 0..1)
183          */
184         int color_to_rgba (lua_State *lua);
185
186         /**
187          * Creates a filename from a series of elements using the correct separator for filenames.
188          *
189          * No attempt is made to force the resulting filename to be an absolute path.
190          * If the first element is a relative path, the result will be a relative path.
191          */
192         int build_filename (lua_State *lua);
193
194         /**
195          * Generic conversion from audio sample count to timecode.
196          * (TimecodeType, sample-rate, sample-pos)
197          */
198         int sample_to_timecode (lua_State *L);
199
200         /**
201          * Generic conversion from timecode to audio sample count.
202          * (TimecodeType, sample-rate, hh, mm, ss, ff)
203          */
204         int timecode_to_sample (lua_State *L);
205
206         /**
207          * Use current session settings to convert
208          * audio-sample count into hh, mm, ss, ff
209          * timecode (this include session pull up/down).
210          */
211         int sample_to_timecode_lua (lua_State *L);
212
213         /**
214          * Use current session settings to convert
215          * timecode (hh, mm, ss, ff) to audio-sample
216          * count (this include session pull up/down).
217          */
218         int timecode_to_sample_lua (lua_State *L);
219
220         class Vamp {
221         /** Vamp Plugin Interface
222          *
223          * Vamp is an audio processing plugin system for plugins that extract descriptive information
224          * from audio data - typically referred to as audio analysis plugins or audio feature
225          * extraction plugins.
226          *
227          * This interface allows to load a plugins and directly access it using the Vamp Plugin API.
228          *
229          * A convenience method is provided to analyze Ardour::Readable objects (Regions).
230          */
231                 public:
232                         Vamp (const std::string&, float sample_rate);
233                         ~Vamp ();
234
235                         /** Search for all available available Vamp plugins.
236                          * @returns list of plugin-keys
237                          */
238                         static std::vector<std::string> list_plugins ();
239
240                         ::Vamp::Plugin* plugin () { return _plugin; }
241
242                         /** high-level abstraction to process a single channel of the given Readable.
243                          *
244                          * If the plugin is not yet initialized, initialize() is called.
245                          *
246                          * if @cb is not nil, it is called with the immediate
247                          * Vamp::Plugin::Features on every process call.
248                          *
249                          * @param r readable
250                          * @param channel channel to process
251                          * @param fn lua callback function
252                          * @return 0 on success
253                          */
254                         int analyze (boost::shared_ptr<ARDOUR::Readable> r, uint32_t channel, luabridge::LuaRef fn);
255
256                         /** call plugin():reset() and clear intialization flag */
257                         void reset ();
258
259                         /** initialize the plugin for use with analyze().
260                          *
261                          * This is equivalent to plugin():initialise (1, ssiz, bsiz)
262                          * and prepares a plugin for analyze.
263                          * (by preferred step and block sizes are used. if the plugin
264                          * does not specify them or they're larger than 8K, both are set to 1024)
265                          *
266                          * Manual initialization is only required to set plugin-parameters
267                          * which depend on prior initialization of the plugin.
268                          *
269                          * @code
270                          * vamp:reset ()
271                          * vamp:initialize ()
272                          * vamp:plugin():setParameter (0, 1.5, nil)
273                          * vamp:analyze (r, 0)
274                          * @endcode
275                          */
276                         bool initialize ();
277
278                         bool initialized () const { return _initialized; }
279
280                         /** process given array of audio-samples.
281                          *
282                          * This is a lua-binding for vamp:plugin():process ()
283                          *
284                          * @param d audio-data, the vector must match the configured channel count
285                          *    and hold a complete buffer for every channel as set during
286                          *    plugin():initialise()
287                          * @param rt timestamp matching the provided buffer.
288                          * @returns features extracted from that data (if the plugin is causal)
289                          */
290                         ::Vamp::Plugin::FeatureSet process (const std::vector<float*>& d, ::Vamp::RealTime rt);
291
292                 private:
293                         ::Vamp::Plugin* _plugin;
294                         float           _sample_rate;
295                         samplecnt_t      _bufsize;
296                         samplecnt_t      _stepsize;
297                         bool            _initialized;
298
299         };
300
301         boost::shared_ptr<Evoral::Note<Temporal::Beats> >
302                 new_noteptr (uint8_t, Temporal::Beats, Temporal::Beats, uint8_t, uint8_t);
303
304         std::list<boost::shared_ptr< Evoral::Note<Temporal::Beats> > >
305                 note_list (boost::shared_ptr<ARDOUR::MidiModel>);
306
307 } } /* namespace */
308
309 namespace ARDOUR { namespace LuaOSC {
310         /** OSC transmitter
311          *
312          * A Class to send OSC messages.
313          */
314         class Address {
315                 /*
316                  * OSC is kinda special, lo_address is a void* and lo_send() has varags
317                  * and typed arguments which makes it hard to bind, even lo_cpp.
318                  */
319                 public:
320                         /** Construct a new OSC transmitter object
321                          * @param uri the destination uri e.g. "osc.udp://localhost:7890"
322                          */
323                         Address (std::string uri) {
324                                 _addr = lo_address_new_from_url (uri.c_str());
325                         }
326
327                         ~Address () { if (_addr) { lo_address_free (_addr); } }
328                         /** Transmit an OSC message
329                          *
330                          * Path (string) and type (string) must always be given.
331                          * The number of following args must match the type.
332                          * Supported types are:
333                          *
334                          *  'i': integer (lua number)
335                          *
336                          *  'f': float (lua number)
337                          *
338                          *  'd': double (lua number)
339                          *
340                          *  'h': 64bit integer (lua number)
341                          *
342                          *  's': string (lua string)
343                          *
344                          *  'c': character (lua string)
345                          *
346                          *  'T': boolean (lua bool) -- this is not implicily True, a lua true/false must be given
347                          *
348                          *  'F': boolean (lua bool) -- this is not implicily False, a lua true/false must be given
349                          *
350                          * @param lua: lua arguments: path, types, ...
351                          * @returns boolean true if successful, false on error.
352                          */
353                         int send (lua_State *lua);
354                 private:
355                         lo_address _addr;
356         };
357
358 }
359
360 class LuaTableRef {
361         public:
362                 LuaTableRef ();
363                 ~LuaTableRef ();
364
365                 int get (lua_State* L);
366                 int set (lua_State* L);
367
368         private:
369                 struct LuaTableEntry {
370                         LuaTableEntry (int kt, int vt)
371                                 : keytype (kt)
372                                 , valuetype (vt)
373                         { }
374
375                         int keytype;
376                         std::string k_s;
377                         unsigned int k_n;
378
379                         int valuetype;
380                         // LUA_TUSERDATA
381                         const void* c;
382                         void* p;
383                         // LUA_TBOOLEAN
384                         bool b;
385                         // LUA_TSTRING:
386                         std::string s;
387                         // LUA_TNUMBER:
388                         double n;
389                 };
390
391                 std::vector<LuaTableEntry> _data;
392
393                 static void* findclasskey (lua_State *L, const void* key);
394                 template<typename T>
395                 static void assign (luabridge::LuaRef* rv, T key, const LuaTableEntry& s);
396 };
397
398 } /* namespace */
399
400 #endif // _ardour_lua_api_h_