what an oversight!
[ardour.git] / libs / ardour / luabindings.cc
1 /*
2     Copyright (C) 2016 Robin Gareus <robin@gareus.org>
3
4     This program is free software; you can redistribute it and/or modify it
5     under the terms of the GNU General Public License as published by the Free
6     Software Foundation; either version 2 of the License, or (at your option)
7     any later version.
8
9     This program is distributed in the hope that it will be useful, but WITHOUT
10     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12     for more details.
13
14     You should have received a copy of the GNU General Public License along
15     with this program; if not, write to the Free Software Foundation, Inc.,
16     675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <glibmm.h>
20
21 #include "timecode/bbt_time.h"
22 #include "pbd/stateful_diff_command.h"
23 #include "evoral/Control.hpp"
24 #include "evoral/ControlList.hpp"
25 #include "evoral/Range.hpp"
26
27 #include "ardour/amp.h"
28 #include "ardour/audioengine.h"
29 #include "ardour/audiosource.h"
30 #include "ardour/audio_backend.h"
31 #include "ardour/audio_buffer.h"
32 #include "ardour/audio_port.h"
33 #include "ardour/audio_track.h"
34 #include "ardour/buffer_set.h"
35 #include "ardour/chan_mapping.h"
36 #include "ardour/dB.h"
37 #include "ardour/dsp_filter.h"
38 #include "ardour/interthread_info.h"
39 #include "ardour/lua_api.h"
40 #include "ardour/luabindings.h"
41 #include "ardour/luaproc.h"
42 #include "ardour/meter.h"
43 #include "ardour/midi_track.h"
44 #include "ardour/midi_port.h"
45 #include "ardour/playlist.h"
46 #include "ardour/plugin.h"
47 #include "ardour/plugin_insert.h"
48 #include "ardour/port_manager.h"
49 #include "ardour/runtime_functions.h"
50 #include "ardour/region.h"
51 #include "ardour/region_factory.h"
52 #include "ardour/route_group.h"
53 #include "ardour/session.h"
54 #include "ardour/session_object.h"
55 #include "ardour/sidechain.h"
56 #include "ardour/track.h"
57 #include "ardour/tempo.h"
58
59 #include "LuaBridge/LuaBridge.h"
60
61 #ifdef PLATFORM_WINDOWS
62 /* luabridge uses addresses of static functions/variables to identify classes.
63  *
64  * Static symbols on windows (even identical symbols) are not
65  * mapped to the same address when mixing .dll + .exe.
66  * So we need a single point to define those static functions.
67  * (normally they're header-only in libs/lua/LuaBridge/detail/ClassInfo.h)
68  *
69  * Really!! A static function with a static variable in a library header
70  * should never ever be replicated, even if it is a template.
71  * But then again this is windows... what else can go wrong.
72  */
73
74 template <class T>
75 void const*
76 luabridge::ClassInfo<T>::getStaticKey ()
77 {
78         static char value;
79         return &value;
80 }
81
82 template <class T>
83 void const*
84 luabridge::ClassInfo<T>::getClassKey ()
85 {
86         static char value;
87         return &value;
88 }
89
90 template <class T>
91 void const*
92 luabridge::ClassInfo<T>::getConstKey ()
93 {
94         static char value;
95         return &value;
96 }
97
98 void*
99 luabridge::getIdentityKey ()
100 {
101   static char value;
102   return &value;
103 }
104
105 /* ...and this is the ugly part of it.
106  *
107  * We need to foward declare classes from gtk2_ardour
108  * end explicily list classes which are used by gtk2_ardour's bindings.
109  *
110  * This is needed because some of the GUI classes use objects from libardour
111  * as function parameters and the .exe would re-create symbols for libardour
112  * objects.
113  *
114  * Classes which don't use libardour symbols could be moved to
115  * gtk2_ardour/luainstance.cc, but keeping this here reduces code
116  * duplication and does not give the compiler a chance to even think
117  * about replicating the symbols.
118  */
119
120 #define CLASSKEYS(CLS) \
121         template void const* luabridge::ClassInfo< CLS >::getStaticKey(); \
122         template void const* luabridge::ClassInfo< CLS >::getClassKey();  \
123         template void const* luabridge::ClassInfo< CLS >::getConstKey();
124
125 #define CLASSINFO(CLS) \
126         class CLS; \
127         template void const* luabridge::ClassInfo< CLS >::getStaticKey(); \
128         template void const* luabridge::ClassInfo< CLS >::getClassKey();  \
129         template void const* luabridge::ClassInfo< CLS >::getConstKey();
130
131 CLASSINFO(MarkerSelection);
132 CLASSINFO(TrackSelection);
133 CLASSINFO(TrackViewList);
134 CLASSINFO(TimeSelection);
135 CLASSINFO(RegionSelection);
136 CLASSINFO(PublicEditor);
137 CLASSINFO(Selection);
138 CLASSINFO(ArdourMarker);
139
140 namespace Cairo {
141         class Context;
142 }
143 CLASSKEYS(Cairo::Context);
144 CLASSKEYS(std::vector<double>);
145 CLASSKEYS(std::list<ArdourMarker*>);
146 CLASSKEYS(std::bitset<47ul>); // LuaSignal::LAST_SIGNAL
147 CLASSKEYS(ArdourMarker*);
148 CLASSKEYS(ARDOUR::RouteGroup);
149 CLASSKEYS(ARDOUR::LuaProc);
150 CLASSKEYS(ARDOUR::DataType);
151 CLASSKEYS(ARDOUR::ChanCount);
152 CLASSKEYS(boost::shared_ptr<ARDOUR::Processor>);
153 CLASSKEYS(ARDOUR::ParameterDescriptor);
154 CLASSKEYS(boost::shared_ptr<ARDOUR::AutomationList>);
155 CLASSKEYS(boost::shared_ptr<Evoral::ControlList>);
156 CLASSKEYS(ARDOUR::LuaOSC::Address);
157 CLASSKEYS(ARDOUR::Session);
158 CLASSKEYS(ARDOUR::BufferSet);
159 CLASSKEYS(ARDOUR::ChanMapping);
160 CLASSKEYS(ARDOUR::DSP::DspShm);
161 CLASSKEYS(PBD::ID);
162 CLASSKEYS(ARDOUR::Location);
163 CLASSKEYS(ARDOUR::PluginInfo);
164 CLASSKEYS(PBD::PropertyChange);
165 CLASSKEYS(std::vector<std::string>);
166 CLASSKEYS(std::list<boost::shared_ptr<ARDOUR::Route> >);
167 CLASSKEYS(std::list<boost::shared_ptr<ARDOUR::Port> >);
168 CLASSKEYS(boost::shared_ptr<ARDOUR::PluginInfo>);
169 CLASSKEYS(boost::shared_ptr<ARDOUR::Region>);
170 CLASSKEYS(boost::weak_ptr<ARDOUR::Route>);
171 CLASSKEYS(std::list<boost::shared_ptr<ARDOUR::Region> >);
172 CLASSKEYS(std::list<ARDOUR::AudioRange>);
173 CLASSKEYS(Evoral::Beats);
174 CLASSKEYS(ARDOUR::PortManager);
175 CLASSKEYS(ARDOUR::AudioEngine);
176 CLASSKEYS(void);
177 CLASSKEYS(float);
178
179 #endif // end windows special case
180
181 /* Some notes on Lua bindings for libardour and friends
182  *
183  * - Prefer factory methods over Contructors whenever possible.
184  *   Don't expose the constructor method unless required.
185  *
186  *   e.g. Don't allow the script to construct a "Track" Object directly
187  *   but do allow to create a "BBT_TIME" object.
188  *
189  * - Do not dereference Shared or Weak Pointers. Pass the pointer to Lua.
190  * - Define Objects as boost:shared_ptr Object whenever possible.
191  *
192  *   Storing a boost::shared_ptr in a Lua-variable keeps the reference
193  *   until that variable is set to 'nil'.
194  *   (if the script were to keep a direct pointer to the object instance, the
195  *   behaviour is undefined if the actual object goes away)
196  *
197  *   Methods of the actual class are indirectly exposed,
198  *   boost::*_ptr get() and ::lock() is implicit when the class is exported
199  *   as LuaBridge's "WSPtrClass".
200  */
201
202 using namespace ARDOUR;
203
204 void
205 LuaBindings::stddef (lua_State* L)
206 {
207         // std::list<std::string>
208         luabridge::getGlobalNamespace (L)
209                 .beginNamespace ("C")
210                 .beginStdList <std::string> ("StringList")
211                 .endClass ()
212
213         // std::vector<std::string>
214                 .beginStdVector <std::string> ("StringVector")
215                 .endClass ()
216
217         // register float array (float*)
218                 .registerArray <float> ("FloatArray")
219
220         // register float array (int32_t*)
221                 .registerArray <int32_t> ("IntArray")
222
223         // TODO std::set
224                 .endNamespace ();
225 }
226
227 void
228 LuaBindings::common (lua_State* L)
229 {
230         luabridge::getGlobalNamespace (L)
231                 .beginNamespace ("PBD")
232                 .beginClass <PBD::ID> ("ID")
233                 .addConstructor <void (*) (std::string)> ()
234                 .addFunction ("to_s", &PBD::ID::to_s) // TODO special case LUA __tostring ?
235                 .endClass ()
236
237                 .beginClass <XMLNode> ("XMLNode")
238                 .addFunction ("name", &XMLNode::name)
239                 .endClass ()
240
241                 .beginClass <PBD::Stateful> ("Stateful")
242                 .addFunction ("id", &PBD::Stateful::id)
243                 .addFunction ("properties", &PBD::Stateful::properties)
244                 .addFunction ("clear_changes", &PBD::Stateful::clear_changes)
245                 .endClass ()
246
247                 .beginWSPtrClass <PBD::Stateful> ("StatefulPtr")
248                 .addFunction ("id", &PBD::Stateful::id)
249                 .addFunction ("properties", &PBD::Stateful::properties)
250                 .addFunction ("clear_changes", &PBD::Stateful::clear_changes)
251                 .endClass ()
252
253                 .deriveClass <PBD::StatefulDestructible, PBD::Stateful> ("StatefulDestructible")
254                 .endClass ()
255
256                 .deriveWSPtrClass <PBD::StatefulDestructible, PBD::Stateful> ("StatefulDestructiblePtr")
257                 .endClass ()
258
259                 .deriveClass <Command, PBD::StatefulDestructible> ("Command")
260                 .addFunction ("set_name", &Command::set_name)
261                 .addFunction ("name", &Command::name)
262                 .endClass ()
263
264                 /* UndoTransaction::add_command() subscribes to DropReferences()
265                  * and deletes the object.
266                  *
267                  * This object cannot be constructed by lua because lua would manage lifetime
268                  * and delete the object leading to a double free.
269                  *
270                  * use Session::add_stateful_diff_command()
271                  * and Session::abort_reversible_command()
272                  */
273                 .deriveClass <PBD::StatefulDiffCommand, Command> ("StatefulDiffCommand")
274                 .addFunction ("undo", &PBD::StatefulDiffCommand::undo)
275                 .addFunction ("empty", &PBD::StatefulDiffCommand::empty)
276                 .endClass ()
277
278                 .deriveWSPtrClass <PBD::Controllable, PBD::StatefulDestructible> ("Controllable")
279                 .addFunction ("name", &PBD::Controllable::name)
280                 .addFunction ("get_value", &PBD::Controllable::get_value)
281                 .endClass ()
282
283                 .beginNamespace ("GroupControlDisposition")
284                 .addConst ("InverseGroup", PBD::Controllable::GroupControlDisposition(PBD::Controllable::InverseGroup))
285                 .addConst ("NoGroup", PBD::Controllable::GroupControlDisposition(PBD::Controllable::NoGroup))
286                 .addConst ("UseGroup", PBD::Controllable::GroupControlDisposition(PBD::Controllable::UseGroup))
287                 .endNamespace ()
288
289                 .endNamespace (); // PBD
290
291         luabridge::getGlobalNamespace (L)
292                 .beginNamespace ("Timecode")
293                 .beginClass <Timecode::BBT_Time> ("BBT_TIME")
294                 .addConstructor <void (*) (uint32_t, uint32_t, uint32_t)> ()
295                 .endClass ()
296                 .endNamespace ();
297
298         luabridge::getGlobalNamespace (L)
299
300                 .beginNamespace ("Evoral")
301                 .beginClass <Evoral::Parameter> ("Parameter")
302                 .addConstructor <void (*) (uint32_t, uint8_t, uint32_t)> ()
303                 .addFunction ("type", &Evoral::Parameter::type)
304                 .addFunction ("channel", &Evoral::Parameter::channel)
305                 .addFunction ("id", &Evoral::Parameter::id)
306                 .endClass ()
307
308                 .beginWSPtrClass <Evoral::ControlList> ("ControlList")
309                 .addFunction ("add", &Evoral::ControlList::add)
310                 .addFunction ("thin", &Evoral::ControlList::thin)
311                 .addFunction ("eval", &Evoral::ControlList::eval)
312                 .addRefFunction ("rt_safe_eval", &Evoral::ControlList::rt_safe_eval)
313                 .addFunction ("interpolation", &Evoral::ControlList::interpolation)
314                 .addFunction ("set_interpolation", &Evoral::ControlList::set_interpolation)
315                 .addFunction ("truncate_end", &Evoral::ControlList::truncate_end)
316                 .addFunction ("truncate_start", &Evoral::ControlList::truncate_start)
317                 .addFunction ("clear", (void (Evoral::ControlList::*)(double, double))&Evoral::ControlList::clear)
318                 .addFunction ("in_write_pass", &Evoral::ControlList::in_write_pass)
319                 .endClass ()
320
321                 .beginWSPtrClass <Evoral::ControlSet> ("ControlSet")
322                 .endClass ()
323
324                 .beginWSPtrClass <Evoral::Control> ("Control")
325                 .addFunction ("list", (boost::shared_ptr<Evoral::ControlList>(Evoral::Control::*)())&Evoral::Control::list)
326                 .endClass ()
327
328                 .beginClass <Evoral::ParameterDescriptor> ("ParameterDescriptor")
329                 .addVoidConstructor ()
330                 .addData ("lower", &Evoral::ParameterDescriptor::lower)
331                 .addData ("upper", &Evoral::ParameterDescriptor::upper)
332                 .addData ("normal", &Evoral::ParameterDescriptor::normal)
333                 .addData ("toggled", &Evoral::ParameterDescriptor::toggled)
334                 .endClass ()
335
336                 .beginClass <Evoral::Range<framepos_t> > ("Range")
337                 .addConstructor <void (*) (framepos_t, framepos_t)> ()
338                 .addData ("from", &Evoral::Range<framepos_t>::from)
339                 .addData ("to", &Evoral::Range<framepos_t>::to)
340                 .endClass ()
341
342                 /* libevoral enums */
343                 .beginNamespace ("InterpolationStyle")
344                 .addConst ("Discrete", Evoral::ControlList::InterpolationStyle(Evoral::ControlList::Discrete))
345                 .addConst ("Linear", Evoral::ControlList::InterpolationStyle(Evoral::ControlList::Linear))
346                 .addConst ("Curved", Evoral::ControlList::InterpolationStyle(Evoral::ControlList::Curved))
347                 .endNamespace ()
348
349                 .endNamespace () // Evoral
350
351                 .beginNamespace ("ARDOUR")
352
353                 .beginClass <InterThreadInfo> ("InterThreadInfo")
354                 .addVoidConstructor ()
355                 .addData ("done", const_cast<bool InterThreadInfo::*>(&InterThreadInfo::done))
356 #if 0 // currently unused, lua is single-threaded, no custom UIs.
357                 .addData ("cancel", (bool InterThreadInfo::*)&InterThreadInfo::cancel)
358 #endif
359                 .addData ("progress", const_cast<float InterThreadInfo::*>(&InterThreadInfo::progress))
360                 .endClass ()
361
362                 .beginClass <AudioRange> ("AudioRange")
363                 .addConstructor <void (*) (framepos_t, framepos_t, uint32_t)> ()
364                 .addFunction ("length", &AudioRange::length)
365                 .addFunction ("equal", &AudioRange::equal)
366                 .addData ("start", &AudioRange::start)
367                 .addData ("_end", &AudioRange::end) // XXX "end" is a lua reserved word
368                 .addData ("id", &AudioRange::id)
369                 .endClass ()
370
371                 .beginWSPtrClass <PluginInfo> ("PluginInfo")
372                 .addVoidConstructor ()
373                 .endClass ()
374
375                 .beginNamespace ("Route")
376                 .beginClass <Route::ProcessorStreams> ("ProcessorStreams")
377                 .addVoidConstructor ()
378                 .endClass ()
379                 .endNamespace ()
380
381                 .beginClass <ChanMapping> ("ChanMapping")
382                 .addVoidConstructor ()
383                 .addFunction ("get", static_cast<uint32_t(ChanMapping::*)(DataType, uint32_t) const>(&ChanMapping::get))
384                 .addFunction ("set", &ChanMapping::set)
385                 .addConst ("Invalid", 4294967295U) // UINT32_MAX
386                 .endClass ()
387
388                 .beginNamespace ("Properties")
389                 // templated class definitions
390                 .beginClass <PBD::PropertyDescriptor<bool> > ("BoolProperty").endClass ()
391                 .beginClass <PBD::PropertyDescriptor<float> > ("FloatProperty").endClass ()
392                 .beginClass <PBD::PropertyDescriptor<framepos_t> > ("FrameposProperty").endClass ()
393                 // actual references (TODO: also expose GQuark for std::set)
394                 //   ardour/region.h
395                 .addConst ("Start", &ARDOUR::Properties::start)
396                 .addConst ("Length", &ARDOUR::Properties::length)
397                 .addConst ("Position", &ARDOUR::Properties::position)
398                 .endNamespace ()
399
400                 .beginClass <PBD::PropertyChange> ("PropertyChange")
401                 // TODO add special handling (std::set<PropertyID>), PropertyID is a GQuark.
402                 // -> direct map to lua table  beginStdSet()A
403                 //
404                 // expand templated PropertyDescriptor<T>
405                 .addFunction ("containsBool", &PBD::PropertyChange::contains<bool>)
406                 .addFunction ("containsFloat", &PBD::PropertyChange::contains<float>)
407                 .addFunction ("containsFramePos", &PBD::PropertyChange::contains<framepos_t>)
408                 .endClass ()
409
410                 .beginClass <PBD::PropertyList> ("PropertyList")
411                 // is-a  std::map<PropertyID, PropertyBase*>
412                 .endClass ()
413
414                 .deriveClass <PBD::OwnedPropertyList, PBD::PropertyList> ("OwnedPropertyList")
415                 .endClass ()
416
417                 .beginWSPtrClass <AutomationList> ("AutomationList")
418                 .addCast<PBD::Stateful> ("to_stateful")
419                 .addCast<PBD::StatefulDestructible> ("to_statefuldestructible")
420                 .addCast<Evoral::ControlList> ("list")
421                 .addFunction ("get_state", &AutomationList::get_state)
422                 .addFunction ("memento_command", &AutomationList::memento_command)
423                 .addFunction ("touching", &AutomationList::touching)
424                 .addFunction ("writing", &AutomationList::writing)
425                 .addFunction ("touch_enabled", &AutomationList::touch_enabled)
426                 .endClass ()
427
428                 .deriveClass <Location, PBD::StatefulDestructible> ("Location")
429                 .addFunction ("locked", &Location::locked)
430                 .addFunction ("lock", &Location::lock)
431                 .addFunction ("start", &Location::start)
432                 .addFunction ("_end", &Location::end) // XXX "end" is a lua reserved word
433                 .addFunction ("length", &Location::length)
434                 .addFunction ("set_start", &Location::set_start)
435                 .addFunction ("set_end", &Location::set_end)
436                 .addFunction ("set_length", &Location::set)
437                 .addFunction ("move_to", &Location::move_to)
438                 .endClass ()
439
440                 .deriveClass <Locations, PBD::StatefulDestructible> ("Locations")
441                 .addFunction ("auto_loop_location", &Locations::auto_loop_location)
442                 .addFunction ("auto_punch_location", &Locations::auto_punch_location)
443                 .addFunction ("session_range_location", &Locations::session_range_location)
444                 .addFunction ("first_mark_after", &Locations::first_mark_after)
445                 .addFunction ("first_mark_after", &Locations::first_mark_after)
446                 .endClass ()
447
448                 .beginWSPtrClass <SessionObject> ("SessionObject")
449                 /* SessionObject is-a PBD::StatefulDestructible,
450                  * but multiple inheritance is not covered by luabridge,
451                  * we need explicit casts */
452                 .addCast<PBD::Stateful> ("to_stateful")
453                 .addCast<PBD::StatefulDestructible> ("to_statefuldestructible")
454                 .addFunction ("name", &SessionObject::name)
455                 .endClass ()
456
457                 .beginWSPtrClass <Port> ("Port")
458                 .addFunction ("name", &Port::name)
459                 .addFunction ("pretty_name", &Port::pretty_name)
460                 .addFunction ("receives_input", &Port::receives_input)
461                 .addFunction ("sends_output", &Port::sends_output)
462                 .addFunction ("connected", &Port::connected)
463                 .addFunction ("disconnect_all", &Port::disconnect_all)
464                 .addFunction ("connected_to_name", (bool (Port::*)(std::string const &)const)&Port::connected_to)
465                 .addFunction ("connect_by_name", (int (Port::*)(std::string const &))&Port::connect)
466                 .addFunction ("disconnect_by_name", (int (Port::*)(std::string const &))&Port::disconnect)
467                 .addFunction ("connected_to", (bool (Port::*)(Port*)const)&Port::connected_to)
468                 .addFunction ("connect", (int (Port::*)(Port*))&Port::connect)
469                 .addFunction ("disconnect", (int (Port::*)(Port*))&Port::disconnect)
470                 .endClass ()
471
472                 .deriveWSPtrClass <AudioPort, Port> ("AudioPort")
473                 .endClass ()
474
475                 .deriveWSPtrClass <MidiPort, Port> ("MidiPort")
476                 .addFunction ("input_active", &MidiPort::input_active)
477                 .addFunction ("set_input_active", &MidiPort::set_input_active)
478                 .endClass ()
479
480                 .beginWSPtrClass <PortSet> ("PortSet")
481                 .addFunction ("num_ports", (size_t (PortSet::*)(DataType)const)&PortSet::num_ports)
482                 .addFunction ("add", &PortSet::add)
483                 .addFunction ("remove", &PortSet::remove)
484                 .addFunction ("port", (boost::shared_ptr<Port> (PortSet::*)(DataType, size_t)const)&PortSet::port)
485                 .addFunction ("contains", &PortSet::contains)
486                 .addFunction ("clear", &PortSet::clear)
487                 .addFunction ("empty", &PortSet::empty)
488                 .endClass ()
489
490                 .deriveWSPtrClass <IO, SessionObject> ("IO")
491                 .addFunction ("active", &IO::active)
492                 .addFunction ("add_port", &IO::add_port)
493                 .addFunction ("remove_port", &IO::remove_port)
494                 .addFunction ("connect", &IO::connect)
495                 .addFunction ("disconnect", (int (IO::*)(boost::shared_ptr<Port>, std::string, void *))&IO::disconnect)
496                 .addFunction ("physically_connected", &IO::physically_connected)
497                 .addFunction ("has_port", &IO::has_port)
498                 .addFunction ("nth", &IO::nth)
499                 .addFunction ("audio", &IO::audio)
500                 .addFunction ("midi", &IO::midi)
501                 .addFunction ("port_by_name", &IO::nth)
502                 .addFunction ("n_ports", &IO::n_ports)
503                 .endClass ()
504
505                 .beginClass <RouteGroup> ("RouteGroup")
506                 // stub RouteGroup* is needed for new_audio_track()
507                 .endClass ()
508
509                 .deriveWSPtrClass <Route, SessionObject> ("Route")
510                 .addCast<Track> ("to_track")
511                 .addFunction ("set_name", &Route::set_name)
512                 .addFunction ("comment", &Route::comment)
513                 .addFunction ("active", &Route::active)
514                 .addFunction ("set_active", &Route::set_active)
515                 .addFunction ("nth_plugin", &Route::nth_plugin)
516                 .addFunction ("add_processor_by_index", &Route::add_processor_by_index)
517                 .addFunction ("remove_processor", &Route::remove_processor)
518                 .addFunction ("replace_processor", &Route::replace_processor)
519                 .addFunction ("n_inputs", &Route::n_inputs)
520                 .addFunction ("n_outputs", &Route::n_outputs)
521                 .addFunction ("set_comment", &Route::set_comment)
522                 .addFunction ("strict_io", &Route::strict_io)
523                 .addFunction ("set_strict_io", &Route::set_strict_io)
524                 .addFunction ("reset_plugin_insert", &Route::reset_plugin_insert)
525                 .addFunction ("customize_plugin_insert", &Route::customize_plugin_insert)
526                 .addFunction ("add_sidechain", &Route::add_sidechain)
527                 .addFunction ("remove_sidechain", &Route::remove_sidechain)
528                 .addFunction ("main_outs", &Route::main_outs)
529                 .addFunction ("muted", &Route::muted)
530                 .addFunction ("soloed", &Route::soloed)
531                 .addFunction ("amp", &Route::amp)
532                 .addFunction ("trim", &Route::trim)
533                 .endClass ()
534
535                 .deriveWSPtrClass <Playlist, SessionObject> ("Playlist")
536                 .addFunction ("region_by_id", &Playlist::region_by_id)
537                 .addFunction ("data_type", &Playlist::data_type)
538                 .addFunction ("n_regions", &Playlist::n_regions)
539                 //.addFunction ("get_extent", &Playlist::get_extent) // pair<framepos_t, framepos_t>
540                 //.addFunction ("region_list", &Playlist::region_list) // RegionListProperty&
541                 .addFunction ("add_region", &Playlist::add_region)
542                 .addFunction ("remove_region", &Playlist::remove_region)
543                 .addFunction ("regions_at", &Playlist::regions_at)
544                 .addFunction ("top_region_at", &Playlist::top_region_at)
545                 .addFunction ("top_unmuted_region_at", &Playlist::top_unmuted_region_at)
546                 .addFunction ("find_next_region", &Playlist::find_next_region)
547                 .addFunction ("find_next_region_boundary", &Playlist::find_next_region_boundary)
548                 .addFunction ("count_regions_at", &Playlist::count_regions_at)
549                 .addFunction ("regions_touched", &Playlist::regions_touched)
550                 .addFunction ("regions_with_start_within", &Playlist::regions_with_start_within)
551                 .addFunction ("regions_with_end_within", &Playlist::regions_with_end_within)
552                 .addFunction ("raise_region", &Playlist::raise_region)
553                 .addFunction ("lower_region", &Playlist::lower_region)
554                 .addFunction ("raise_region_to_top", &Playlist::raise_region_to_top)
555                 .addFunction ("lower_region_to_bottom", &Playlist::lower_region_to_bottom)
556                 .addFunction ("duplicate", (void (Playlist::*)(boost::shared_ptr<Region>, framepos_t, framecnt_t, float))&Playlist::duplicate)
557                 .addFunction ("duplicate_until", &Playlist::duplicate_until)
558                 .addFunction ("duplicate_range", &Playlist::duplicate_range)
559                 .addFunction ("combine", &Playlist::combine)
560                 .addFunction ("uncombine", &Playlist::uncombine)
561                 .addFunction ("split_region", &Playlist::split_region)
562                 .addFunction ("split", (void (Playlist::*)(framepos_t))&Playlist::split)
563                 .addFunction ("cut", (boost::shared_ptr<Playlist> (Playlist::*)(std::list<AudioRange>&, bool))&Playlist::cut)
564 #if 0
565                 .addFunction ("copy", &Playlist::copy)
566                 .addFunction ("paste", &Playlist::paste)
567 #endif
568                 .endClass ()
569
570                 .deriveWSPtrClass <Track, Route> ("Track")
571                 .addCast<AudioTrack> ("to_audio_track")
572                 .addCast<MidiTrack> ("to_midi_track")
573                 .addFunction ("set_name", &Track::set_name)
574                 .addFunction ("can_record", &Track::can_record)
575                 .addFunction ("record_enabled", &Track::record_enabled)
576                 .addFunction ("record_safe", &Track::record_safe)
577                 .addFunction ("set_record_enabled", &Track::set_record_enabled)
578                 .addFunction ("set_record_safe", &Track::set_record_safe)
579                 .addFunction ("bounceable", &Track::bounceable)
580                 .addFunction ("bounce", &Track::bounce)
581                 .addFunction ("bounce_range", &Track::bounce_range)
582                 .addFunction ("playlist", &Track::playlist)
583                 .endClass ()
584
585                 .deriveWSPtrClass <AudioTrack, Track> ("AudioTrack")
586                 .endClass ()
587
588                 .deriveWSPtrClass <MidiTrack, Track> ("MidiTrack")
589                 .endClass ()
590
591                 .deriveWSPtrClass <Region, SessionObject> ("Region")
592                 /* properties */
593                 .addFunction ("position", &Region::position)
594                 .addFunction ("start", &Region::start)
595                 .addFunction ("length", &Region::length)
596                 .addFunction ("layer", &Region::layer)
597                 .addFunction ("data_type", &Region::data_type)
598                 .addFunction ("stretch", &Region::stretch)
599                 .addFunction ("shift", &Region::shift)
600                 .addRefFunction ("sync_offset", &Region::sync_offset)
601                 .addFunction ("sync_position", &Region::sync_position)
602                 .addFunction ("hidden", &Region::hidden)
603                 .addFunction ("muted", &Region::muted)
604                 .addFunction ("opaque", &Region::opaque)
605                 .addFunction ("locked", &Region::locked)
606                 .addFunction ("position_locked", &Region::position_locked)
607                 .addFunction ("video_locked", &Region::video_locked)
608                 .addFunction ("automatic", &Region::automatic)
609                 .addFunction ("whole_file", &Region::whole_file)
610                 .addFunction ("captured", &Region::captured)
611                 .addFunction ("can_move", &Region::can_move)
612                 .addFunction ("sync_marked", &Region::sync_marked)
613                 .addFunction ("external", &Region::external)
614                 .addFunction ("import", &Region::import)
615                 .addFunction ("covers", &Region::covers)
616                 .addFunction ("at_natural_position", &Region::at_natural_position)
617                 .addFunction ("is_compound", &Region::is_compound)
618                 /* editing operations */
619                 .addFunction ("set_length", &Region::set_length)
620                 .addFunction ("set_start", &Region::set_start)
621                 .addFunction ("set_position", &Region::set_position)
622                 .addFunction ("set_initial_position", &Region::set_initial_position)
623                 .addFunction ("nudge_position", &Region::nudge_position)
624                 .addFunction ("move_to_natural_position", &Region::move_to_natural_position)
625                 .addFunction ("move_start", &Region::move_start)
626                 .addFunction ("trim_front", &Region::trim_front)
627                 .addFunction ("trim_end", &Region::trim_end)
628                 .addFunction ("trim_to", &Region::trim_to)
629                 .addFunction ("cut_front", &Region::cut_front)
630                 .addFunction ("cut_end", &Region::cut_end)
631                 .addFunction ("raise", &Region::raise)
632                 .addFunction ("lower", &Region::lower)
633                 .addFunction ("raise_to_top", &Region::raise_to_top)
634                 .addFunction ("lower_to_bottom", &Region::lower_to_bottom)
635                 .addFunction ("set_sync_position", &Region::set_sync_position)
636                 .addFunction ("clear_sync_position", &Region::clear_sync_position)
637                 .addFunction ("set_hidden", &Region::set_hidden)
638                 .addFunction ("set_muted", &Region::set_muted)
639                 .addFunction ("set_opaque", &Region::set_opaque)
640                 .addFunction ("set_locked", &Region::set_locked)
641                 .addFunction ("set_video_locked", &Region::set_video_locked)
642                 .addFunction ("set_position_locked", &Region::set_position_locked)
643                 .endClass ()
644
645                 .beginWSPtrClass <Source> ("Source")
646                 .endClass ()
647
648                 .beginClass <Plugin::PresetRecord> ("PresetRecord")
649                 .addData ("uri", &Plugin::PresetRecord::uri, false)
650                 .addData ("label", &Plugin::PresetRecord::label, false)
651                 .addData ("user", &Plugin::PresetRecord::user, false)
652                 .addData ("valid", &Plugin::PresetRecord::valid, false)
653                 .endClass ()
654
655                 .deriveWSPtrClass <Automatable, Evoral::ControlSet> ("Automatable")
656                 .addFunction ("automation_control", (boost::shared_ptr<AutomationControl>(Automatable::*)(const Evoral::Parameter&, bool))&Automatable::automation_control)
657                 .endClass ()
658
659                 .deriveClass <ParameterDescriptor, Evoral::ParameterDescriptor> ("ParameterDescriptor")
660                 .addVoidConstructor ()
661                 .addData ("label", &ParameterDescriptor::label)
662                 .addData ("logarithmic", &ParameterDescriptor::logarithmic)
663                 .endClass ()
664
665                 .deriveWSPtrClass <Processor, SessionObject> ("Processor")
666                 .addCast<Automatable> ("to_automatable")
667                 .addCast<PluginInsert> ("to_insert")
668                 .addCast<SideChain> ("to_sidechain")
669                 .addCast<IOProcessor> ("to_ioprocessor")
670                 .addFunction ("display_name", &Processor::display_name)
671                 .addFunction ("active", &Processor::active)
672                 .addFunction ("activate", &Processor::activate)
673                 .addFunction ("deactivate", &Processor::deactivate)
674                 .addFunction ("control", (boost::shared_ptr<Evoral::Control>(Evoral::ControlSet::*)(const Evoral::Parameter&, bool))&Evoral::ControlSet::control)
675                 .addFunction ("automation_control", (boost::shared_ptr<AutomationControl>(Automatable::*)(const Evoral::Parameter&, bool))&Automatable::automation_control)
676                 .endClass ()
677
678                 .deriveWSPtrClass <IOProcessor, Processor> ("IOProcessor")
679                 .addFunction ("natural_input_streams", &IOProcessor::natural_input_streams)
680                 .addFunction ("natural_output_streams", &IOProcessor::natural_output_streams)
681                 .addFunction ("input", (boost::shared_ptr<IO>(IOProcessor::*)())&IOProcessor::input)
682                 .addFunction ("output", (boost::shared_ptr<IO>(IOProcessor::*)())&IOProcessor::output)
683                 .endClass ()
684
685                 .deriveWSPtrClass <SideChain, IOProcessor> ("SideChain")
686                 .endClass ()
687
688                 .deriveWSPtrClass <Delivery, IOProcessor> ("Delivery")
689                 .endClass ()
690
691                 .deriveWSPtrClass <Plugin, PBD::StatefulDestructible> ("Plugin")
692                 .addFunction ("label", &Plugin::label)
693                 .addFunction ("name", &Plugin::name)
694                 .addFunction ("maker", &Plugin::maker)
695                 .addFunction ("parameter_count", &Plugin::parameter_count)
696                 .addRefFunction ("nth_parameter", &Plugin::nth_parameter)
697                 .addFunction ("preset_by_label", &Plugin::preset_by_label)
698                 .addFunction ("preset_by_uri", &Plugin::preset_by_uri)
699                 .addFunction ("load_preset", &Plugin::load_preset)
700                 .addFunction ("parameter_is_input", &Plugin::parameter_is_input)
701                 .addFunction ("get_docs", &Plugin::get_docs)
702                 .addFunction ("get_parameter_docs", &Plugin::get_parameter_docs)
703                 .addRefFunction ("get_parameter_descriptor", &Plugin::get_parameter_descriptor)
704                 .endClass ()
705
706                 .deriveWSPtrClass <PluginInsert, Processor> ("PluginInsert")
707                 .addFunction ("plugin", &PluginInsert::plugin)
708                 .addFunction ("activate", &PluginInsert::activate)
709                 .addFunction ("deactivate", &PluginInsert::deactivate)
710                 .addFunction ("strict_io_configured", &PluginInsert::strict_io_configured)
711                 .addFunction ("input_map", (ARDOUR::ChanMapping (PluginInsert::*)(uint32_t) const)&PluginInsert::input_map)
712                 .addFunction ("output_map", (ARDOUR::ChanMapping (PluginInsert::*)(uint32_t) const)&PluginInsert::output_map)
713                 .addFunction ("set_input_map", &PluginInsert::set_input_map)
714                 .addFunction ("set_output_map", &PluginInsert::set_output_map)
715                 .addFunction ("sidechain_input", &PluginInsert::sidechain_input)
716                 .endClass ()
717
718                 .deriveWSPtrClass <AutomationControl, PBD::Controllable> ("AutomationControl")
719                 .addCast<Evoral::Control> ("to_ctrl")
720                 .addFunction ("automation_state", &AutomationControl::automation_state)
721                 .addFunction ("automation_style", &AutomationControl::automation_style)
722                 .addFunction ("set_automation_state", &AutomationControl::set_automation_state)
723                 .addFunction ("set_automation_style", &AutomationControl::set_automation_style)
724                 .addFunction ("start_touch", &AutomationControl::start_touch)
725                 .addFunction ("stop_touch", &AutomationControl::stop_touch)
726                 .addFunction ("get_value", &AutomationControl::get_value)
727                 .addFunction ("set_value", &AutomationControl::set_value)
728                 .addFunction ("writable", &AutomationControl::writable)
729                 .addFunction ("alist", &AutomationControl::alist)
730                 .endClass ()
731
732                 .deriveWSPtrClass <GainControl, AutomationControl> ("GainControl")
733                 .endClass ()
734
735                 .deriveWSPtrClass <Amp, Processor> ("Amp")
736                 .addFunction ("gain_control", (boost::shared_ptr<GainControl>(Amp::*)())&Amp::gain_control)
737                 .endClass ()
738
739                 .deriveWSPtrClass <PluginInsert::PluginControl, AutomationControl> ("PluginControl")
740                 .endClass ()
741
742                 .deriveWSPtrClass <AudioSource, Source> ("AudioSource")
743                 .addFunction ("readable_length", &AudioSource::readable_length)
744                 .addFunction ("n_channels", &AudioSource::n_channels)
745                 .endClass ()
746
747                 // <std::list<boost::shared_ptr <AudioTrack> >
748                 .beginStdList <boost::shared_ptr<AudioTrack> > ("AudioTrackList")
749                 .endClass ()
750
751                 // std::list<boost::shared_ptr <MidiTrack> >
752                 .beginStdList <boost::shared_ptr<MidiTrack> > ("MidiTrackList")
753                 .endClass ()
754
755                 // RouteList == std::list<boost::shared_ptr<Route> >
756                 .beginConstStdList <boost::shared_ptr<Route> > ("RouteList")
757                 .endClass ()
758
759                 // boost::shared_ptr<RouteList>
760                 .beginPtrStdList <boost::shared_ptr<Route> > ("RouteListPtr")
761                 .endClass ()
762
763                 // typedef std::list<boost::weak_ptr <Route> > WeakRouteList
764                 .beginConstStdList <boost::weak_ptr<Route> > ("WeakRouteList")
765                 .endClass ()
766
767                 // std::list< boost::weak_ptr <AudioSource> >
768                 .beginConstStdList <boost::weak_ptr<AudioSource> > ("WeakAudioSourceList")
769                 .endClass ()
770
771                 // typedef std::list<boost::shared_ptr<Region> > RegionList
772                 .beginConstStdList <boost::shared_ptr<Region> > ("RegionList")
773                 .endClass ()
774
775                 // boost::shared_ptr <std::list<boost::shared_ptr<Region> > >
776                 .beginPtrStdList <boost::shared_ptr<Region> > ("RegionListPtr")
777                 .endClass ()
778
779                 //std::list<boost::shared_ptr<Port> > PortList;
780                 .beginConstStdList <boost::shared_ptr<Port> > ("PortList")
781                 .endClass ()
782
783                 // used by Playlist::cut/copy
784                 .beginConstStdList <AudioRange> ("AudioRangeList")
785                 .endClass ()
786
787                 .beginConstStdList <Location*> ("LocationList")
788                 .endClass ()
789
790 #if 0  // depends on Evoal:: Note, Beats see note_fixer.h
791         // typedef Evoral::Note<Evoral::Beats> Note;
792         // std::set< boost::weak_ptr<Note> >
793                 .beginStdSet <boost::weak_ptr<Note> > ("WeakNoteSet")
794                 .endClass ()
795 #endif
796
797         // std::list<boost::weak_ptr<Source> >
798                 .beginConstStdList <boost::weak_ptr<Source> > ("WeakSourceList")
799                 .endClass ()
800
801                 .beginClass <Tempo> ("Tempo")
802                 .addConstructor <void (*) (double, double)> ()
803                 .addFunction ("note_type", &Tempo::note_type)
804                 .addFunction ("beats_per_minute", &Tempo::beats_per_minute)
805                 .addFunction ("frames_per_beat", &Tempo::frames_per_beat)
806                 .endClass ()
807
808                 .beginClass <Meter> ("Meter")
809                 .addConstructor <void (*) (double, double)> ()
810                 .addFunction ("divisions_per_bar", &Meter::divisions_per_bar)
811                 .addFunction ("note_divisor", &Meter::note_divisor)
812                 .addFunction ("frames_per_bar", &Meter::frames_per_bar)
813                 .addFunction ("frames_per_grid", &Meter::frames_per_grid)
814                 .endClass ()
815
816                 .beginClass <TempoMap> ("TempoMap")
817                 .addFunction ("add_tempo", &TempoMap::add_tempo)
818                 .addFunction ("add_meter", &TempoMap::add_meter)
819                 .endClass ()
820
821                 .beginClass <ChanCount> ("ChanCount")
822                 .addConstructor <void (*) (DataType, uint32_t)> ()
823                 .addFunction ("get", &ChanCount::get)
824                 .addFunction ("set", &ChanCount::set)
825                 .addFunction ("n_audio", &ChanCount::n_audio)
826                 .addFunction ("n_midi", &ChanCount::n_midi)
827                 .addFunction ("n_total", &ChanCount::n_total)
828                 .addFunction ("reset", &ChanCount::reset)
829                 .endClass()
830
831                 .beginClass <DataType> ("DataType")
832                 .addConstructor <void (*) (std::string)> ()
833                 .addStaticCFunction ("null",  &LuaAPI::datatype_ctor_null) // "nil" is a lua reseved word
834                 .addStaticCFunction ("audio", &LuaAPI::datatype_ctor_audio)
835                 .addStaticCFunction ("midi",  &LuaAPI::datatype_ctor_midi)
836                 .addFunction ("to_string",  &DataType::to_string) // TODO Lua __tostring
837                 // TODO add uint32_t cast, add operator==  !=
838                 .endClass()
839
840                 /* libardour enums */
841                 .beginNamespace ("PluginType")
842                 .addConst ("AudioUnit", ARDOUR::PluginType(AudioUnit))
843                 .addConst ("LADSPA", ARDOUR::PluginType(LADSPA))
844                 .addConst ("LV2", ARDOUR::PluginType(LV2))
845                 .addConst ("Windows_VST", ARDOUR::PluginType(Windows_VST))
846                 .addConst ("LXVST", ARDOUR::PluginType(LXVST))
847                 .addConst ("Lua", ARDOUR::PluginType(Lua))
848                 .endNamespace ()
849
850                 .beginNamespace ("AutoStyle")
851                 .addConst ("Absolute", ARDOUR::AutoStyle(Absolute))
852                 .addConst ("Trim", ARDOUR::AutoStyle(Trim))
853                 .endNamespace ()
854
855                 .beginNamespace ("AutoState")
856                 .addConst ("Off", ARDOUR::AutoState(Off))
857                 .addConst ("Write", ARDOUR::AutoState(Write))
858                 .addConst ("Touch", ARDOUR::AutoState(Touch))
859                 .addConst ("Play", ARDOUR::AutoState(Play))
860                 .endNamespace ()
861
862                 .beginNamespace ("AutomationType")
863                 .addConst ("PluginAutomation", ARDOUR::AutomationType(PluginAutomation))
864                 .addConst ("PluginAutomation", ARDOUR::AutomationType(GainAutomation))
865                 .addConst ("PluginAutomation", ARDOUR::AutomationType(TrimAutomation))
866                 .endNamespace ()
867
868                 .beginNamespace ("SrcQuality")
869                 .addConst ("SrcBest", ARDOUR::SrcQuality(SrcBest))
870                 .endNamespace ()
871
872                 .beginNamespace ("PortFlags")
873                 .addConst ("IsInput", ARDOUR::PortFlags(IsInput))
874                 .addConst ("IsOutput", ARDOUR::PortFlags(IsOutput))
875                 .addConst ("IsPhysical", ARDOUR::PortFlags(IsPhysical))
876                 .addConst ("CanMonitor", ARDOUR::PortFlags(CanMonitor))
877                 .addConst ("IsTerminal", ARDOUR::PortFlags(IsTerminal))
878                 .endNamespace ()
879
880                 .beginNamespace ("PlaylistDisposition")
881                 .addConst ("CopyPlaylist", ARDOUR::PlaylistDisposition(CopyPlaylist))
882                 .addConst ("NewPlaylist", ARDOUR::PlaylistDisposition(NewPlaylist))
883                 .addConst ("SharePlaylist", ARDOUR::PlaylistDisposition(SharePlaylist))
884                 .endNamespace ()
885
886                 .beginNamespace ("RegionPoint")
887                 .addConst ("Start", ARDOUR::RegionPoint(Start))
888                 .addConst ("End", ARDOUR::RegionPoint(End))
889                 .addConst ("SyncPoint", ARDOUR::RegionPoint(SyncPoint))
890                 .endNamespace ()
891
892                 .beginNamespace ("TrackMode")
893                 .addConst ("Normal", ARDOUR::TrackMode(Start))
894                 .addConst ("NonLayered", ARDOUR::TrackMode(NonLayered))
895                 .addConst ("Destructive", ARDOUR::TrackMode(Destructive))
896                 .endNamespace ()
897                 .endNamespace ();
898
899         luabridge::getGlobalNamespace (L)
900                 .beginNamespace ("ARDOUR")
901                 .beginClass <AudioBackendInfo> ("AudioBackendInfo")
902                 .addData ("name", &AudioBackendInfo::name)
903                 .endClass()
904                 .beginStdVector <const AudioBackendInfo*> ("BackendVector").endClass ()
905
906                 .beginClass <AudioBackend::DeviceStatus> ("DeviceStatus")
907                 .addData ("name", &AudioBackend::DeviceStatus::name)
908                 .addData ("available", &AudioBackend::DeviceStatus::available)
909                 .endClass()
910                 .beginStdVector <AudioBackend::DeviceStatus> ("DeviceStatusVector").endClass ()
911
912                 .beginWSPtrClass <AudioBackend> ("AudioBackend")
913                 .addFunction ("info", &AudioBackend::info)
914                 .addFunction ("sample_rate", &AudioBackend::sample_rate)
915                 .addFunction ("buffer_size", &AudioBackend::buffer_size)
916                 .addFunction ("period_size", &AudioBackend::period_size)
917                 .addFunction ("input_channels", &AudioBackend::input_channels)
918                 .addFunction ("output_channels", &AudioBackend::output_channels)
919                 .addFunction ("dsp_load", &AudioBackend::dsp_load)
920
921                 .addFunction ("set_sample_rate", &AudioBackend::set_sample_rate)
922                 .addFunction ("set_buffer_size", &AudioBackend::set_buffer_size)
923                 .addFunction ("set_peridod_size", &AudioBackend::set_peridod_size)
924
925                 .addFunction ("enumerate_drivers", &AudioBackend::enumerate_drivers)
926                 .addFunction ("driver_name", &AudioBackend::driver_name)
927                 .addFunction ("set_driver", &AudioBackend::set_driver)
928
929                 .addFunction ("use_separate_input_and_output_devices", &AudioBackend::use_separate_input_and_output_devices)
930                 .addFunction ("enumerate_devices", &AudioBackend::enumerate_devices)
931                 .addFunction ("enumerate_input_devices", &AudioBackend::enumerate_input_devices)
932                 .addFunction ("enumerate_output_devices", &AudioBackend::enumerate_output_devices)
933                 .addFunction ("device_name", &AudioBackend::device_name)
934                 .addFunction ("input_device_name", &AudioBackend::input_device_name)
935                 .addFunction ("output_device_name", &AudioBackend::output_device_name)
936                 .addFunction ("set_device_name", &AudioBackend::set_device_name)
937                 .addFunction ("set_input_device_name", &AudioBackend::set_input_device_name)
938                 .addFunction ("set_output_device_name", &AudioBackend::set_output_device_name)
939                 .endClass()
940
941                 .beginClass <PortManager> ("PortManager")
942                 .addFunction ("port_engine", &PortManager::port_engine)
943                 .addFunction ("connected", &PortManager::connected)
944                 .addFunction ("connect", &PortManager::connect)
945                 .addFunction ("physically_connected", &PortManager::physically_connected)
946                 .addFunction ("disconnect", (int (PortManager::*)(const std::string&, const std::string&))&PortManager::disconnect)
947                 .addFunction ("disconnect_port", (int (PortManager::*)(boost::shared_ptr<Port>))&PortManager::disconnect)
948                 .addFunction ("get_port_by_name", &PortManager::get_port_by_name)
949                 .addFunction ("get_pretty_name_by_name", &PortManager::get_pretty_name_by_name)
950                 .addFunction ("port_is_physical", &PortManager::port_is_physical)
951                 .addFunction ("get_physical_outputs", &PortManager::get_physical_outputs)
952                 .addFunction ("get_physical_inputs", &PortManager::get_physical_inputs)
953                 .addFunction ("n_physical_outputs", &PortManager::n_physical_outputs)
954                 .addFunction ("n_physical_inputs", &PortManager::n_physical_inputs)
955                 .addRefFunction ("get_connections", &PortManager::get_connections)
956                 .addRefFunction ("get_ports", (int (PortManager::*)(DataType, PortManager::PortList&))&PortManager::get_ports)
957                 .addRefFunction ("get_backend_ports", (int (PortManager::*)(const std::string&, DataType, PortFlags, std::vector<std::string>&))&PortManager::get_ports)
958                 .endClass()
959
960                 .deriveClass <AudioEngine, PortManager> ("AudioEngine")
961                 .addFunction ("available_backends", &AudioEngine::available_backends)
962                 .addFunction ("current_backend_name", &AudioEngine::current_backend_name)
963                 .addFunction ("set_backend", &AudioEngine::set_backend)
964                 .addFunction ("setup_required", &AudioEngine::setup_required)
965                 .addFunction ("start", &AudioEngine::start)
966                 .addFunction ("stop", &AudioEngine::stop)
967                 .addFunction ("get_dsp_load", &AudioEngine::get_dsp_load)
968                 .addFunction ("set_device_name", &AudioEngine::set_device_name)
969                 .addFunction ("set_sample_rate", &AudioEngine::set_sample_rate)
970                 .addFunction ("set_buffer_size", &AudioEngine::set_buffer_size)
971                 .addFunction ("get_last_backend_error", &AudioEngine::get_last_backend_error)
972                 .endClass()
973                 .endNamespace ();
974
975         // basic representation of Session
976         // functions which can be used from realtime and non-realtime contexts
977         luabridge::getGlobalNamespace (L)
978                 .beginNamespace ("ARDOUR")
979                 .beginClass <Session> ("Session")
980                 .addFunction ("scripts_changed", &Session::scripts_changed) // used internally
981                 .addFunction ("transport_rolling", &Session::transport_rolling)
982                 .addFunction ("request_transport_speed", &Session::request_transport_speed)
983                 .addFunction ("transport_frame", &Session::transport_frame)
984                 .addFunction ("transport_speed", &Session::transport_speed)
985                 .addFunction ("frame_rate", &Session::frame_rate)
986                 .addFunction ("nominal_frame_rate", &Session::nominal_frame_rate)
987                 .addFunction ("frames_per_timecode_frame", &Session::frames_per_timecode_frame)
988                 .addFunction ("timecode_frames_per_hour", &Session::timecode_frames_per_hour)
989                 .addFunction ("timecode_frames_per_second", &Session::timecode_frames_per_second)
990                 .addFunction ("timecode_drop_frames", &Session::timecode_drop_frames)
991                 .addFunction ("request_locate", &Session::request_locate)
992                 .addFunction ("request_stop", &Session::request_stop)
993                 .addFunction ("last_transport_start", &Session::last_transport_start)
994                 .addFunction ("goto_start", &Session::goto_start)
995                 .addFunction ("goto_end", &Session::goto_end)
996                 .addFunction ("current_start_frame", &Session::current_start_frame)
997                 .addFunction ("current_end_frame", &Session::current_end_frame)
998                 .addFunction ("actively_recording", &Session::actively_recording)
999                 .addFunction ("new_audio_track", &Session::new_audio_track)
1000                 .addFunction ("new_audio_route", &Session::new_audio_route)
1001                 .addFunction ("new_midi_track", &Session::new_midi_track)
1002                 .addFunction ("new_midi_route", &Session::new_midi_route)
1003                 .addFunction ("get_routes", &Session::get_routes)
1004                 .addFunction ("get_tracks", &Session::get_tracks)
1005                 .addFunction ("name", &Session::name)
1006                 .addFunction ("path", &Session::path)
1007                 .addFunction ("record_status", &Session::record_status)
1008                 .addFunction ("route_by_id", &Session::route_by_id)
1009                 .addFunction ("route_by_name", &Session::route_by_name)
1010                 .addFunction ("route_by_remote_id", &Session::route_by_remote_id)
1011                 .addFunction ("track_by_diskstream_id", &Session::track_by_diskstream_id)
1012                 .addFunction ("source_by_id", &Session::source_by_id)
1013                 .addFunction ("controllable_by_id", &Session::controllable_by_id)
1014                 .addFunction ("processor_by_id", &Session::processor_by_id)
1015                 .addFunction ("snap_name", &Session::snap_name)
1016                 .addFunction ("monitor_out", &Session::monitor_out)
1017                 .addFunction ("master_out", &Session::master_out)
1018                 .addFunction ("tempo_map", (TempoMap& (Session::*)())&Session::tempo_map)
1019                 .addFunction ("locations", &Session::locations)
1020                 .addFunction ("begin_reversible_command", (void (Session::*)(const std::string&))&Session::begin_reversible_command)
1021                 .addFunction ("commit_reversible_command", &Session::commit_reversible_command)
1022                 .addFunction ("abort_reversible_command", &Session::abort_reversible_command)
1023                 .addFunction ("add_command", &Session::add_command)
1024                 .addFunction ("add_stateful_diff_command", &Session::add_stateful_diff_command)
1025                 .addFunction ("engine", (AudioEngine& (Session::*)())&Session::engine)
1026                 .endClass ()
1027
1028                 .beginClass <RegionFactory> ("RegionFactory")
1029                 .addStaticFunction ("region_by_id", &RegionFactory::region_by_id)
1030                 .endClass ()
1031
1032                 /* session enums */
1033                 .beginNamespace ("Session")
1034
1035                 .beginNamespace ("RecordState")
1036                 .addConst ("Disabled", ARDOUR::Session::RecordState(Session::Disabled))
1037                 .addConst ("Enabled", ARDOUR::Session::RecordState(Session::Enabled))
1038                 .addConst ("Recording", ARDOUR::Session::RecordState(Session::Recording))
1039                 .endNamespace ()
1040
1041                 .endNamespace () // END Session enums
1042
1043                 .beginNamespace ("LuaAPI")
1044                 .addFunction ("nil_proc", ARDOUR::LuaAPI::nil_processor)
1045                 .addFunction ("new_luaproc", ARDOUR::LuaAPI::new_luaproc)
1046                 .addFunction ("new_plugin_info", ARDOUR::LuaAPI::new_plugin_info)
1047                 .addFunction ("new_plugin", ARDOUR::LuaAPI::new_plugin)
1048                 .addFunction ("set_processor_param", ARDOUR::LuaAPI::set_processor_param)
1049                 .addFunction ("set_plugin_insert_param", ARDOUR::LuaAPI::set_plugin_insert_param)
1050                 .addCFunction ("plugin_automation", ARDOUR::LuaAPI::plugin_automation)
1051                 .addFunction ("usleep", Glib::usleep)
1052                 .endNamespace ()
1053
1054                 .endNamespace ();// END ARDOUR
1055 }
1056
1057 void
1058 LuaBindings::dsp (lua_State* L)
1059 {
1060         luabridge::getGlobalNamespace (L)
1061                 .beginNamespace ("ARDOUR")
1062
1063                 .beginClass <AudioBuffer> ("AudioBuffer")
1064                 .addEqualCheck ()
1065                 .addFunction ("data", (Sample*(AudioBuffer::*)(framecnt_t))&AudioBuffer::data)
1066                 .addFunction ("silence", &AudioBuffer::silence)
1067                 .addFunction ("apply_gain", &AudioBuffer::apply_gain)
1068                 .addFunction ("check_silence", &AudioBuffer::check_silence)
1069                 .addFunction ("read_from", (void (AudioBuffer::*)(const Sample*, framecnt_t, framecnt_t, framecnt_t))&AudioBuffer::check_silence)
1070                 .endClass()
1071
1072                 .beginClass <MidiBuffer> ("MidiBuffer")
1073                 .addEqualCheck ()
1074                 .addFunction ("silence", &MidiBuffer::silence)
1075                 .addFunction ("empty", &MidiBuffer::empty)
1076                 // TODO iterators..
1077                 .endClass()
1078
1079                 .beginClass <BufferSet> ("BufferSet")
1080                 .addEqualCheck ()
1081                 .addFunction ("get_audio", static_cast<AudioBuffer&(BufferSet::*)(size_t)>(&BufferSet::get_audio))
1082                 .addFunction ("count", static_cast<const ChanCount&(BufferSet::*)()const>(&BufferSet::count))
1083                 .endClass()
1084                 .endNamespace ();
1085
1086         luabridge::getGlobalNamespace (L)
1087                 .beginNamespace ("Evoral")
1088                 .beginClass <Evoral::Event<framepos_t> > ("Event")
1089                 .addFunction ("clear", &Evoral::Event<framepos_t>::clear)
1090                 .addFunction ("size", &Evoral::Event<framepos_t>::size)
1091                 .addFunction ("set_buffer", &Evoral::Event<framepos_t>::set_buffer)
1092                 .addFunction ("buffer", (uint8_t*(Evoral::Event<framepos_t>::*)())&Evoral::Event<framepos_t>::buffer)
1093                 .endClass ()
1094
1095                 .beginClass <Evoral::Beats> ("Beats")
1096                 .addFunction ("to_double", &Evoral::Beats::to_double)
1097                 .endClass ()
1098
1099                 .deriveClass <Evoral::MIDIEvent<framepos_t>, Evoral::Event<framepos_t> > ("MidiEvent")
1100                 // add Ctor?
1101                 .addFunction ("type", &Evoral::MIDIEvent<framepos_t>::type)
1102                 .addFunction ("channel", &Evoral::MIDIEvent<framepos_t>::channel)
1103                 .addFunction ("set_type", &Evoral::MIDIEvent<framepos_t>::type)
1104                 .addFunction ("set_channel", &Evoral::MIDIEvent<framepos_t>::channel)
1105                 .endClass ()
1106                 .endNamespace ();
1107
1108         // dsp releated session functions
1109         luabridge::getGlobalNamespace (L)
1110                 .beginNamespace ("ARDOUR")
1111                 .beginClass <Session> ("Session")
1112                 .addFunction ("get_scratch_buffers", &Session::get_scratch_buffers)
1113                 .addFunction ("get_silent_buffers", &Session::get_silent_buffers)
1114                 .endClass ()
1115                 .endNamespace ();
1116
1117         luabridge::getGlobalNamespace (L)
1118                 .beginNamespace ("ARDOUR")
1119                 .beginNamespace ("DSP")
1120                 .addFunction ("compute_peak", ARDOUR::compute_peak)
1121                 .addFunction ("find_peaks", ARDOUR::find_peaks)
1122                 .addFunction ("apply_gain_to_buffer", ARDOUR::apply_gain_to_buffer)
1123                 .addFunction ("mix_buffers_no_gain", ARDOUR::mix_buffers_no_gain)
1124                 .addFunction ("mix_buffers_with_gain", ARDOUR::mix_buffers_with_gain)
1125                 .addFunction ("copy_vector", ARDOUR::copy_vector)
1126                 .addFunction ("dB_to_coefficient", &dB_to_coefficient)
1127                 .addFunction ("fast_coefficient_to_dB", &fast_coefficient_to_dB)
1128                 .addFunction ("accurate_coefficient_to_dB", &accurate_coefficient_to_dB)
1129                 .addFunction ("memset", &DSP::memset)
1130                 .addFunction ("mmult", &DSP::mmult)
1131                 .addFunction ("log_meter", &DSP::log_meter)
1132                 .addFunction ("log_meter_coeff", &DSP::log_meter_coeff)
1133                 .addRefFunction ("peaks", &DSP::peaks)
1134
1135                 .beginClass <DSP::LowPass> ("LowPass")
1136                 .addConstructor <void (*) (double, float)> ()
1137                 .addFunction ("proc", &DSP::LowPass::proc)
1138                 .addFunction ("ctrl", &DSP::LowPass::ctrl)
1139                 .addFunction ("set_cutoff", &DSP::LowPass::set_cutoff)
1140                 .addFunction ("reset", &DSP::LowPass::reset)
1141                 .endClass ()
1142                 .beginClass <DSP::BiQuad> ("Biquad")
1143                 .addConstructor <void (*) (double)> ()
1144                 .addFunction ("run", &DSP::BiQuad::run)
1145                 .addFunction ("compute", &DSP::BiQuad::compute)
1146                 .addFunction ("reset", &DSP::BiQuad::reset)
1147                 .endClass ()
1148
1149                 /* DSP enums */
1150                 .beginNamespace ("BiQuadType")
1151                 .addConst ("LowPass", ARDOUR::DSP::BiQuad::LowPass)
1152                 .addConst ("HighPass", ARDOUR::DSP::BiQuad::HighPass)
1153                 .addConst ("BandPassSkirt", ARDOUR::DSP::BiQuad::BandPassSkirt)
1154                 .addConst ("BandPass0dB", ARDOUR::DSP::BiQuad::BandPass0dB)
1155                 .addConst ("Notch", ARDOUR::DSP::BiQuad::Notch)
1156                 .addConst ("AllPass", ARDOUR::DSP::BiQuad::AllPass)
1157                 .addConst ("Peaking", ARDOUR::DSP::BiQuad::Peaking)
1158                 .addConst ("LowShelf", ARDOUR::DSP::BiQuad::LowShelf)
1159                 .addConst ("HighShelf", ARDOUR::DSP::BiQuad::HighShelf)
1160                 .endNamespace ()
1161
1162                 .beginClass <DSP::DspShm> ("DspShm")
1163                 .addFunction ("allocate", &DSP::DspShm::allocate)
1164                 .addFunction ("clear", &DSP::DspShm::clear)
1165                 .addFunction ("to_float", &DSP::DspShm::to_float)
1166                 .addFunction ("to_int", &DSP::DspShm::to_int)
1167                 .addFunction ("atomic_set_int", &DSP::DspShm::atomic_set_int)
1168                 .addFunction ("atomic_get_int", &DSP::DspShm::atomic_get_int)
1169                 .endClass ()
1170
1171                 .endNamespace () // DSP
1172                 .endNamespace (); // ARDOUR
1173 }
1174
1175 void
1176 LuaBindings::session (lua_State* L)
1177 {
1178         // non-realtime session functions
1179         luabridge::getGlobalNamespace (L)
1180                 .beginNamespace ("ARDOUR")
1181                 .beginClass <Session> ("Session")
1182                 .addFunction ("save_state", &Session::save_state)
1183                 .addFunction ("set_dirty", &Session::set_dirty)
1184                 .addFunction ("unknown_processors", &Session::unknown_processors)
1185
1186                 .addFunction<RouteList (Session::*)(uint32_t, const std::string&, const std::string&, PlaylistDisposition)> ("new_route_from_template", &Session::new_route_from_template)
1187                 // TODO  session_add_audio_track  session_add_midi_track  session_add_mixed_track
1188                 //.addFunction ("new_midi_track", &Session::new_midi_track)
1189                 .endClass ()
1190
1191                 .endNamespace (); // ARDOUR
1192 }
1193
1194 void
1195 LuaBindings::osc (lua_State* L)
1196 {
1197         luabridge::getGlobalNamespace (L)
1198                 .beginNamespace ("ARDOUR")
1199                 .beginNamespace ("LuaOSC")
1200                 .beginClass<LuaOSC::Address> ("Address")
1201                 .addConstructor<void (*) (std::string)> ()
1202                 .addCFunction ("send", &LuaOSC::Address::send)
1203                 .endClass ()
1204                 .endNamespace ()
1205                 .endNamespace ();
1206 }
1207
1208 void
1209 LuaBindings::set_session (lua_State* L, Session *s)
1210 {
1211         /* LuaBridge uses unique keys to identify classes/c-types.
1212          *
1213          * Those keys are "generated" by using the memory-address of a static
1214          * variable, templated for every Class.
1215          * (see libs/lua/LuaBridge/detail/ClassInfo.h)
1216          *
1217          * When linking the final executable there must be exactly one static
1218          * function (static variable) for every templated class.
1219          * This works fine on OSX and Linux...
1220          *
1221          * Windows (mingw and MSVC) however expand the template differently for libardour
1222          * AND gtk2_ardour. We end up with two identical static functions
1223          * at different addresses!!
1224          *
1225          * The Solution: have gtk2_ardour never include LuaBridge headers directly
1226          * and always go via libardour function calls for classes that are registered
1227          * in libardour. (calling lua itself is fine,  calling c-functions in the GUI
1228          * which expand the template is not)
1229          *
1230          * (the actual cause: even static symbols in a .dll have no fixed address
1231          * and are mapped when loading the dll. static functions in .exe do have a fixed
1232          * address)
1233          *
1234          * libardour:
1235          *  0000000000000000 I __imp__ZZN9luabridge9ClassInfoIN6ARDOUR7SessionEE11getClassKeyEvE5value
1236          *  0000000000000000 I __nm__ZZN9luabridge9ClassInfoIN6ARDOUR7SessionEE11getClassKeyEvE5value
1237          *  0000000000000000 T _ZN9luabridge9ClassInfoIN6ARDOUR7SessionEE11getClassKeyEv
1238          *
1239          * ardour.exe
1240          *  000000000104f560 d .data$_ZZN9luabridge9ClassInfoIN6ARDOUR7SessionEE11getClassKeyEvE5value
1241          *  000000000104f560 D _ZZN9luabridge9ClassInfoIN6ARDOUR7SessionEE11getClassKeyEvE5value
1242          *  0000000000e9baf0 T _ZN9luabridge9ClassInfoIN6ARDOUR7SessionEE11getClassKeyEv
1243          *
1244          *
1245          */
1246         luabridge::push <Session *> (L, s);
1247         lua_setglobal (L, "Session");
1248
1249         if (s) {
1250                 // call lua function.
1251                 luabridge::LuaRef cb_ses = luabridge::getGlobal (L, "new_session");
1252                 if (cb_ses.type() == LUA_TFUNCTION) { cb_ses(s->name()); } // TODO args
1253         }
1254 }