fix "warning: this decimal constant is unsigned only in ISO C90"
[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 ("properties", &PBD::Stateful::properties)
243                 .addFunction ("clear_changes", &PBD::Stateful::clear_changes)
244                 .endClass ()
245
246                 .beginWSPtrClass <PBD::Stateful> ("StatefulPtr")
247                 .addFunction ("properties", &PBD::Stateful::properties)
248                 .addFunction ("clear_changes", &PBD::Stateful::clear_changes)
249                 .endClass ()
250
251                 .deriveClass <PBD::StatefulDestructible, PBD::Stateful> ("StatefulDestructible")
252                 .endClass ()
253
254                 .deriveWSPtrClass <PBD::StatefulDestructible, PBD::Stateful> ("StatefulDestructiblePtr")
255                 .endClass ()
256
257                 .deriveClass <Command, PBD::StatefulDestructible> ("Command")
258                 .addFunction ("set_name", &Command::set_name)
259                 .addFunction ("name", &Command::name)
260                 .endClass ()
261
262                 /* UndoTransaction::add_command() subscribes to DropReferences()
263                  * and deletes the object.
264                  *
265                  * This object cannot be constructed by lua because lua would manage lifetime
266                  * and delete the object leading to a double free.
267                  *
268                  * use Session::add_stateful_diff_command()
269                  * and Session::abort_reversible_command()
270                  */
271                 .deriveClass <PBD::StatefulDiffCommand, Command> ("StatefulDiffCommand")
272                 .addFunction ("undo", &PBD::StatefulDiffCommand::undo)
273                 .addFunction ("empty", &PBD::StatefulDiffCommand::empty)
274                 .endClass ()
275
276                 .deriveWSPtrClass <PBD::Controllable, PBD::StatefulDestructible> ("Controllable")
277                 .addFunction ("name", &PBD::Controllable::name)
278                 .addFunction ("get_value", &PBD::Controllable::get_value)
279                 .endClass ()
280
281                 .beginNamespace ("GroupControlDisposition")
282                 .addConst ("InverseGroup", PBD::Controllable::GroupControlDisposition(PBD::Controllable::InverseGroup))
283                 .addConst ("NoGroup", PBD::Controllable::GroupControlDisposition(PBD::Controllable::NoGroup))
284                 .addConst ("UseGroup", PBD::Controllable::GroupControlDisposition(PBD::Controllable::UseGroup))
285                 .endNamespace ()
286
287                 .endNamespace (); // PBD
288
289         luabridge::getGlobalNamespace (L)
290                 .beginNamespace ("Timecode")
291                 .beginClass <Timecode::BBT_Time> ("BBT_TIME")
292                 .addConstructor <void (*) (uint32_t, uint32_t, uint32_t)> ()
293                 .endClass ()
294                 .endNamespace ();
295
296         luabridge::getGlobalNamespace (L)
297
298                 .beginNamespace ("Evoral")
299                 .beginClass <Evoral::Parameter> ("Parameter")
300                 .addConstructor <void (*) (uint32_t, uint8_t, uint32_t)> ()
301                 .addFunction ("type", &Evoral::Parameter::type)
302                 .addFunction ("channel", &Evoral::Parameter::channel)
303                 .addFunction ("id", &Evoral::Parameter::id)
304                 .endClass ()
305
306                 .beginWSPtrClass <Evoral::ControlList> ("ControlList")
307                 .addFunction ("add", &Evoral::ControlList::add)
308                 .addFunction ("thin", &Evoral::ControlList::thin)
309                 .addFunction ("eval", &Evoral::ControlList::eval)
310                 .addRefFunction ("rt_safe_eval", &Evoral::ControlList::rt_safe_eval)
311                 .addFunction ("interpolation", &Evoral::ControlList::interpolation)
312                 .addFunction ("set_interpolation", &Evoral::ControlList::set_interpolation)
313                 .addFunction ("truncate_end", &Evoral::ControlList::truncate_end)
314                 .addFunction ("truncate_start", &Evoral::ControlList::truncate_start)
315                 .addFunction ("clear", (void (Evoral::ControlList::*)(double, double))&Evoral::ControlList::clear)
316                 .addFunction ("in_write_pass", &Evoral::ControlList::in_write_pass)
317                 .endClass ()
318
319                 .beginWSPtrClass <Evoral::ControlSet> ("ControlSet")
320                 .endClass ()
321
322                 .beginWSPtrClass <Evoral::Control> ("Control")
323                 .addFunction ("list", (boost::shared_ptr<Evoral::ControlList>(Evoral::Control::*)())&Evoral::Control::list)
324                 .endClass ()
325
326                 .beginClass <Evoral::ParameterDescriptor> ("ParameterDescriptor")
327                 .addVoidConstructor ()
328                 .addData ("lower", &Evoral::ParameterDescriptor::lower)
329                 .addData ("upper", &Evoral::ParameterDescriptor::upper)
330                 .addData ("normal", &Evoral::ParameterDescriptor::normal)
331                 .addData ("toggled", &Evoral::ParameterDescriptor::toggled)
332                 .endClass ()
333
334                 .beginClass <Evoral::Range<framepos_t> > ("Range")
335                 .addConstructor <void (*) (framepos_t, framepos_t)> ()
336                 .addData ("from", &Evoral::Range<framepos_t>::from)
337                 .addData ("to", &Evoral::Range<framepos_t>::to)
338                 .endClass ()
339
340                 /* libevoral enums */
341                 .beginNamespace ("InterpolationStyle")
342                 .addConst ("Discrete", Evoral::ControlList::InterpolationStyle(Evoral::ControlList::Discrete))
343                 .addConst ("Linear", Evoral::ControlList::InterpolationStyle(Evoral::ControlList::Linear))
344                 .addConst ("Curved", Evoral::ControlList::InterpolationStyle(Evoral::ControlList::Curved))
345                 .endNamespace ()
346
347                 .endNamespace () // Evoral
348
349                 .beginNamespace ("ARDOUR")
350
351                 .beginClass <InterThreadInfo> ("InterThreadInfo")
352                 .addVoidConstructor ()
353                 .addData ("done", const_cast<bool InterThreadInfo::*>(&InterThreadInfo::done))
354 #if 0 // currently unused, lua is single-threaded, no custom UIs.
355                 .addData ("cancel", (bool InterThreadInfo::*)&InterThreadInfo::cancel)
356 #endif
357                 .addData ("progress", const_cast<float InterThreadInfo::*>(&InterThreadInfo::progress))
358                 .endClass ()
359
360                 .beginClass <AudioRange> ("AudioRange")
361                 .addConstructor <void (*) (framepos_t, framepos_t, uint32_t)> ()
362                 .addFunction ("length", &AudioRange::length)
363                 .addFunction ("equal", &AudioRange::equal)
364                 .addData ("start", &AudioRange::start)
365                 .addData ("_end", &AudioRange::end) // XXX "end" is a lua reserved word
366                 .addData ("id", &AudioRange::id)
367                 .endClass ()
368
369                 .beginWSPtrClass <PluginInfo> ("PluginInfo")
370                 .addVoidConstructor ()
371                 .endClass ()
372
373                 .beginNamespace ("Route")
374                 .beginClass <Route::ProcessorStreams> ("ProcessorStreams")
375                 .addVoidConstructor ()
376                 .endClass ()
377                 .endNamespace ()
378
379                 .beginClass <ChanMapping> ("ChanMapping")
380                 .addVoidConstructor ()
381                 .addFunction ("get", static_cast<uint32_t(ChanMapping::*)(DataType, uint32_t) const>(&ChanMapping::get))
382                 .addFunction ("set", &ChanMapping::set)
383                 .addConst ("Invalid", 4294967295U) // UINT32_MAX
384                 .endClass ()
385
386                 .beginNamespace ("Properties")
387                 // templated class definitions
388                 .beginClass <PBD::PropertyDescriptor<bool> > ("BoolProperty").endClass ()
389                 .beginClass <PBD::PropertyDescriptor<float> > ("FloatProperty").endClass ()
390                 .beginClass <PBD::PropertyDescriptor<framepos_t> > ("FrameposProperty").endClass ()
391                 // actual references (TODO: also expose GQuark for std::set)
392                 //   ardour/region.h
393                 .addConst ("Start", &ARDOUR::Properties::start)
394                 .addConst ("Length", &ARDOUR::Properties::length)
395                 .addConst ("Position", &ARDOUR::Properties::position)
396                 .endNamespace ()
397
398                 .beginClass <PBD::PropertyChange> ("PropertyChange")
399                 // TODO add special handling (std::set<PropertyID>), PropertyID is a GQuark.
400                 // -> direct map to lua table  beginStdSet()A
401                 //
402                 // expand templated PropertyDescriptor<T>
403                 .addFunction ("containsBool", &PBD::PropertyChange::contains<bool>)
404                 .addFunction ("containsFloat", &PBD::PropertyChange::contains<float>)
405                 .addFunction ("containsFramePos", &PBD::PropertyChange::contains<framepos_t>)
406                 .endClass ()
407
408                 .beginClass <PBD::PropertyList> ("PropertyList")
409                 // is-a  std::map<PropertyID, PropertyBase*>
410                 .endClass ()
411
412                 .deriveClass <PBD::OwnedPropertyList, PBD::PropertyList> ("OwnedPropertyList")
413                 .endClass ()
414
415                 .beginWSPtrClass <AutomationList> ("AutomationList")
416                 .addCast<PBD::Stateful> ("to_stateful")
417                 .addCast<PBD::StatefulDestructible> ("to_statefuldestructible")
418                 .addCast<Evoral::ControlList> ("list")
419                 .addFunction ("get_state", &AutomationList::get_state)
420                 .addFunction ("memento_command", &AutomationList::memento_command)
421                 .addFunction ("touching", &AutomationList::touching)
422                 .addFunction ("writing", &AutomationList::writing)
423                 .addFunction ("touch_enabled", &AutomationList::touch_enabled)
424                 .endClass ()
425
426                 .deriveClass <Location, PBD::StatefulDestructible> ("Location")
427                 .addFunction ("locked", &Location::locked)
428                 .addFunction ("lock", &Location::lock)
429                 .addFunction ("start", &Location::start)
430                 .addFunction ("_end", &Location::end) // XXX "end" is a lua reserved word
431                 .addFunction ("length", &Location::length)
432                 .addFunction ("set_start", &Location::set_start)
433                 .addFunction ("set_end", &Location::set_end)
434                 .addFunction ("set_length", &Location::set)
435                 .addFunction ("move_to", &Location::move_to)
436                 .endClass ()
437
438                 .deriveClass <Locations, PBD::StatefulDestructible> ("Locations")
439                 .addFunction ("auto_loop_location", &Locations::auto_loop_location)
440                 .addFunction ("auto_punch_location", &Locations::auto_punch_location)
441                 .addFunction ("session_range_location", &Locations::session_range_location)
442                 .addFunction ("first_mark_after", &Locations::first_mark_after)
443                 .addFunction ("first_mark_after", &Locations::first_mark_after)
444                 .endClass ()
445
446                 .beginWSPtrClass <SessionObject> ("SessionObject")
447                 /* SessionObject is-a PBD::StatefulDestructible,
448                  * but multiple inheritance is not covered by luabridge,
449                  * we need explicit casts */
450                 .addCast<PBD::Stateful> ("to_stateful")
451                 .addCast<PBD::StatefulDestructible> ("to_statefuldestructible")
452                 .addFunction ("name", &SessionObject::name)
453                 .endClass ()
454
455                 .beginWSPtrClass <Port> ("Port")
456                 .addFunction ("name", &Port::name)
457                 .addFunction ("pretty_name", &Port::pretty_name)
458                 .addFunction ("receives_input", &Port::receives_input)
459                 .addFunction ("sends_output", &Port::sends_output)
460                 .addFunction ("connected", &Port::connected)
461                 .addFunction ("disconnect_all", &Port::disconnect_all)
462                 .addFunction ("connected_to_name", (bool (Port::*)(std::string const &)const)&Port::connected_to)
463                 .addFunction ("connect_by_name", (int (Port::*)(std::string const &))&Port::connect)
464                 .addFunction ("disconnect_by_name", (int (Port::*)(std::string const &))&Port::disconnect)
465                 .addFunction ("connected_to", (bool (Port::*)(Port*)const)&Port::connected_to)
466                 .addFunction ("connect", (int (Port::*)(Port*))&Port::connect)
467                 .addFunction ("disconnect", (int (Port::*)(Port*))&Port::disconnect)
468                 .endClass ()
469
470                 .deriveWSPtrClass <AudioPort, Port> ("AudioPort")
471                 .endClass ()
472
473                 .deriveWSPtrClass <MidiPort, Port> ("MidiPort")
474                 .addFunction ("input_active", &MidiPort::input_active)
475                 .addFunction ("set_input_active", &MidiPort::set_input_active)
476                 .endClass ()
477
478                 .beginWSPtrClass <PortSet> ("PortSet")
479                 .addFunction ("num_ports", (size_t (PortSet::*)(DataType)const)&PortSet::num_ports)
480                 .addFunction ("add", &PortSet::add)
481                 .addFunction ("remove", &PortSet::remove)
482                 .addFunction ("port", (boost::shared_ptr<Port> (PortSet::*)(DataType, size_t)const)&PortSet::port)
483                 .addFunction ("contains", &PortSet::contains)
484                 .addFunction ("clear", &PortSet::clear)
485                 .addFunction ("empty", &PortSet::empty)
486                 .endClass ()
487
488                 .deriveWSPtrClass <IO, SessionObject> ("IO")
489                 .addFunction ("active", &IO::active)
490                 .addFunction ("add_port", &IO::add_port)
491                 .addFunction ("remove_port", &IO::remove_port)
492                 .addFunction ("connect", &IO::connect)
493                 .addFunction ("disconnect", (int (IO::*)(boost::shared_ptr<Port>, std::string, void *))&IO::disconnect)
494                 .addFunction ("physically_connected", &IO::physically_connected)
495                 .addFunction ("has_port", &IO::has_port)
496                 .addFunction ("nth", &IO::nth)
497                 .addFunction ("audio", &IO::audio)
498                 .addFunction ("midi", &IO::midi)
499                 .addFunction ("port_by_name", &IO::nth)
500                 .addFunction ("n_ports", &IO::n_ports)
501                 .endClass ()
502
503                 .beginClass <RouteGroup> ("RouteGroup")
504                 // stub RouteGroup* is needed for new_audio_track()
505                 .endClass ()
506
507                 .deriveWSPtrClass <Route, SessionObject> ("Route")
508                 .addCast<Track> ("to_track")
509                 .addFunction ("set_name", &Route::set_name)
510                 .addFunction ("comment", &Route::comment)
511                 .addFunction ("active", &Route::active)
512                 .addFunction ("set_active", &Route::set_active)
513                 .addFunction ("nth_plugin", &Route::nth_plugin)
514                 .addFunction ("add_processor_by_index", &Route::add_processor_by_index)
515                 .addFunction ("remove_processor", &Route::remove_processor)
516                 .addFunction ("replace_processor", &Route::replace_processor)
517                 .addFunction ("n_inputs", &Route::n_inputs)
518                 .addFunction ("n_outputs", &Route::n_outputs)
519                 .addFunction ("set_comment", &Route::set_comment)
520                 .addFunction ("strict_io", &Route::strict_io)
521                 .addFunction ("set_strict_io", &Route::set_strict_io)
522                 .addFunction ("reset_plugin_insert", &Route::reset_plugin_insert)
523                 .addFunction ("customize_plugin_insert", &Route::customize_plugin_insert)
524                 .addFunction ("add_sidechain", &Route::add_sidechain)
525                 .addFunction ("remove_sidechain", &Route::remove_sidechain)
526                 .addFunction ("main_outs", &Route::main_outs)
527                 .addFunction ("muted", &Route::muted)
528                 .addFunction ("soloed", &Route::soloed)
529                 .addFunction ("amp", &Route::amp)
530                 .addFunction ("trim", &Route::trim)
531                 .endClass ()
532
533                 .deriveWSPtrClass <Playlist, SessionObject> ("Playlist")
534                 .addFunction ("region_by_id", &Playlist::region_by_id)
535                 .addFunction ("data_type", &Playlist::data_type)
536                 .addFunction ("n_regions", &Playlist::n_regions)
537                 //.addFunction ("get_extent", &Playlist::get_extent) // pair<framepos_t, framepos_t>
538                 //.addFunction ("region_list", &Playlist::region_list) // RegionListProperty&
539                 .addFunction ("add_region", &Playlist::add_region)
540                 .addFunction ("remove_region", &Playlist::remove_region)
541                 .addFunction ("regions_at", &Playlist::regions_at)
542                 .addFunction ("top_region_at", &Playlist::top_region_at)
543                 .addFunction ("top_unmuted_region_at", &Playlist::top_unmuted_region_at)
544                 .addFunction ("find_next_region", &Playlist::find_next_region)
545                 .addFunction ("find_next_region_boundary", &Playlist::find_next_region_boundary)
546                 .addFunction ("count_regions_at", &Playlist::count_regions_at)
547                 .addFunction ("regions_touched", &Playlist::regions_touched)
548                 .addFunction ("regions_with_start_within", &Playlist::regions_with_start_within)
549                 .addFunction ("regions_with_end_within", &Playlist::regions_with_end_within)
550                 .addFunction ("raise_region", &Playlist::raise_region)
551                 .addFunction ("lower_region", &Playlist::lower_region)
552                 .addFunction ("raise_region_to_top", &Playlist::raise_region_to_top)
553                 .addFunction ("lower_region_to_bottom", &Playlist::lower_region_to_bottom)
554                 .addFunction ("duplicate", (void (Playlist::*)(boost::shared_ptr<Region>, framepos_t, framecnt_t, float))&Playlist::duplicate)
555                 .addFunction ("duplicate_until", &Playlist::duplicate_until)
556                 .addFunction ("duplicate_range", &Playlist::duplicate_range)
557                 .addFunction ("combine", &Playlist::combine)
558                 .addFunction ("uncombine", &Playlist::uncombine)
559                 .addFunction ("split_region", &Playlist::split_region)
560                 .addFunction ("split", (void (Playlist::*)(framepos_t))&Playlist::split)
561                 .addFunction ("cut", (boost::shared_ptr<Playlist> (Playlist::*)(std::list<AudioRange>&, bool))&Playlist::cut)
562 #if 0
563                 .addFunction ("copy", &Playlist::copy)
564                 .addFunction ("paste", &Playlist::paste)
565 #endif
566                 .endClass ()
567
568                 .deriveWSPtrClass <Track, Route> ("Track")
569                 .addCast<AudioTrack> ("to_audio_track")
570                 .addCast<MidiTrack> ("to_midi_track")
571                 .addFunction ("set_name", &Track::set_name)
572                 .addFunction ("can_record", &Track::can_record)
573                 .addFunction ("record_enabled", &Track::record_enabled)
574                 .addFunction ("record_safe", &Track::record_safe)
575                 .addFunction ("set_record_enabled", &Track::set_record_enabled)
576                 .addFunction ("set_record_safe", &Track::set_record_safe)
577                 .addFunction ("bounceable", &Track::bounceable)
578                 .addFunction ("bounce", &Track::bounce)
579                 .addFunction ("bounce_range", &Track::bounce_range)
580                 .addFunction ("playlist", &Track::playlist)
581                 .endClass ()
582
583                 .deriveWSPtrClass <AudioTrack, Track> ("AudioTrack")
584                 .endClass ()
585
586                 .deriveWSPtrClass <MidiTrack, Track> ("MidiTrack")
587                 .endClass ()
588
589                 .deriveWSPtrClass <Region, SessionObject> ("Region")
590                 /* properties */
591                 .addFunction ("position", &Region::position)
592                 .addFunction ("start", &Region::start)
593                 .addFunction ("length", &Region::length)
594                 .addFunction ("layer", &Region::layer)
595                 .addFunction ("data_type", &Region::data_type)
596                 .addFunction ("stretch", &Region::stretch)
597                 .addFunction ("shift", &Region::shift)
598                 .addRefFunction ("sync_offset", &Region::sync_offset)
599                 .addFunction ("sync_position", &Region::sync_position)
600                 .addFunction ("hidden", &Region::hidden)
601                 .addFunction ("muted", &Region::muted)
602                 .addFunction ("opaque", &Region::opaque)
603                 .addFunction ("locked", &Region::locked)
604                 .addFunction ("position_locked", &Region::position_locked)
605                 .addFunction ("video_locked", &Region::video_locked)
606                 .addFunction ("automatic", &Region::automatic)
607                 .addFunction ("whole_file", &Region::whole_file)
608                 .addFunction ("captured", &Region::captured)
609                 .addFunction ("can_move", &Region::can_move)
610                 .addFunction ("sync_marked", &Region::sync_marked)
611                 .addFunction ("external", &Region::external)
612                 .addFunction ("import", &Region::import)
613                 .addFunction ("covers", &Region::covers)
614                 .addFunction ("at_natural_position", &Region::at_natural_position)
615                 .addFunction ("is_compound", &Region::is_compound)
616                 /* editing operations */
617                 .addFunction ("set_length", &Region::set_length)
618                 .addFunction ("set_start", &Region::set_start)
619                 .addFunction ("set_position", &Region::set_position)
620                 .addFunction ("set_initial_position", &Region::set_initial_position)
621                 .addFunction ("nudge_position", &Region::nudge_position)
622                 .addFunction ("move_to_natural_position", &Region::move_to_natural_position)
623                 .addFunction ("move_start", &Region::move_start)
624                 .addFunction ("trim_front", &Region::trim_front)
625                 .addFunction ("trim_end", &Region::trim_end)
626                 .addFunction ("trim_to", &Region::trim_to)
627                 .addFunction ("cut_front", &Region::cut_front)
628                 .addFunction ("cut_end", &Region::cut_end)
629                 .addFunction ("raise", &Region::raise)
630                 .addFunction ("lower", &Region::lower)
631                 .addFunction ("raise_to_top", &Region::raise_to_top)
632                 .addFunction ("lower_to_bottom", &Region::lower_to_bottom)
633                 .addFunction ("set_sync_position", &Region::set_sync_position)
634                 .addFunction ("clear_sync_position", &Region::clear_sync_position)
635                 .addFunction ("set_hidden", &Region::set_hidden)
636                 .addFunction ("set_muted", &Region::set_muted)
637                 .addFunction ("set_opaque", &Region::set_opaque)
638                 .addFunction ("set_locked", &Region::set_locked)
639                 .addFunction ("set_video_locked", &Region::set_video_locked)
640                 .addFunction ("set_position_locked", &Region::set_position_locked)
641                 .endClass ()
642
643                 .beginWSPtrClass <Source> ("Source")
644                 .endClass ()
645
646                 .beginClass <Plugin::PresetRecord> ("PresetRecord")
647                 .addData ("uri", &Plugin::PresetRecord::uri, false)
648                 .addData ("label", &Plugin::PresetRecord::label, false)
649                 .addData ("user", &Plugin::PresetRecord::user, false)
650                 .addData ("valid", &Plugin::PresetRecord::valid, false)
651                 .endClass ()
652
653                 .deriveWSPtrClass <Automatable, Evoral::ControlSet> ("Automatable")
654                 .addFunction ("automation_control", (boost::shared_ptr<AutomationControl>(Automatable::*)(const Evoral::Parameter&, bool))&Automatable::automation_control)
655                 .endClass ()
656
657                 .deriveClass <ParameterDescriptor, Evoral::ParameterDescriptor> ("ParameterDescriptor")
658                 .addVoidConstructor ()
659                 .addData ("label", &ParameterDescriptor::label)
660                 .addData ("logarithmic", &ParameterDescriptor::logarithmic)
661                 .endClass ()
662
663                 .deriveWSPtrClass <Processor, SessionObject> ("Processor")
664                 .addCast<Automatable> ("to_automatable")
665                 .addCast<PluginInsert> ("to_insert")
666                 .addCast<SideChain> ("to_sidechain")
667                 .addCast<IOProcessor> ("to_ioprocessor")
668                 .addFunction ("display_name", &Processor::display_name)
669                 .addFunction ("active", &Processor::active)
670                 .addFunction ("activate", &Processor::activate)
671                 .addFunction ("deactivate", &Processor::deactivate)
672                 .addFunction ("control", (boost::shared_ptr<Evoral::Control>(Evoral::ControlSet::*)(const Evoral::Parameter&, bool))&Evoral::ControlSet::control)
673                 .addFunction ("automation_control", (boost::shared_ptr<AutomationControl>(Automatable::*)(const Evoral::Parameter&, bool))&Automatable::automation_control)
674                 .endClass ()
675
676                 .deriveWSPtrClass <IOProcessor, Processor> ("IOProcessor")
677                 .addFunction ("natural_input_streams", &IOProcessor::natural_input_streams)
678                 .addFunction ("natural_output_streams", &IOProcessor::natural_output_streams)
679                 .addFunction ("input", (boost::shared_ptr<IO>(IOProcessor::*)())&IOProcessor::input)
680                 .addFunction ("output", (boost::shared_ptr<IO>(IOProcessor::*)())&IOProcessor::output)
681                 .endClass ()
682
683                 .deriveWSPtrClass <SideChain, IOProcessor> ("SideChain")
684                 .endClass ()
685
686                 .deriveWSPtrClass <Delivery, IOProcessor> ("Delivery")
687                 .endClass ()
688
689                 .deriveWSPtrClass <Plugin, PBD::StatefulDestructible> ("Plugin")
690                 .addFunction ("label", &Plugin::label)
691                 .addFunction ("name", &Plugin::name)
692                 .addFunction ("maker", &Plugin::maker)
693                 .addFunction ("parameter_count", &Plugin::parameter_count)
694                 .addRefFunction ("nth_parameter", &Plugin::nth_parameter)
695                 .addFunction ("preset_by_label", &Plugin::preset_by_label)
696                 .addFunction ("preset_by_uri", &Plugin::preset_by_uri)
697                 .addFunction ("load_preset", &Plugin::load_preset)
698                 .addFunction ("parameter_is_input", &Plugin::parameter_is_input)
699                 .addFunction ("get_docs", &Plugin::get_docs)
700                 .addFunction ("get_parameter_docs", &Plugin::get_parameter_docs)
701                 .addRefFunction ("get_parameter_descriptor", &Plugin::get_parameter_descriptor)
702                 .endClass ()
703
704                 .deriveWSPtrClass <PluginInsert, Processor> ("PluginInsert")
705                 .addFunction ("plugin", &PluginInsert::plugin)
706                 .addFunction ("activate", &PluginInsert::activate)
707                 .addFunction ("deactivate", &PluginInsert::deactivate)
708                 .addFunction ("strict_io_configured", &PluginInsert::strict_io_configured)
709                 .addFunction ("input_map", (ARDOUR::ChanMapping (PluginInsert::*)(uint32_t) const)&PluginInsert::input_map)
710                 .addFunction ("output_map", (ARDOUR::ChanMapping (PluginInsert::*)(uint32_t) const)&PluginInsert::output_map)
711                 .addFunction ("set_input_map", &PluginInsert::set_input_map)
712                 .addFunction ("set_output_map", &PluginInsert::set_output_map)
713                 .addFunction ("sidechain_input", &PluginInsert::sidechain_input)
714                 .endClass ()
715
716                 .deriveWSPtrClass <AutomationControl, PBD::Controllable> ("AutomationControl")
717                 .addCast<Evoral::Control> ("to_ctrl")
718                 .addFunction ("automation_state", &AutomationControl::automation_state)
719                 .addFunction ("automation_style", &AutomationControl::automation_style)
720                 .addFunction ("set_automation_state", &AutomationControl::set_automation_state)
721                 .addFunction ("set_automation_style", &AutomationControl::set_automation_style)
722                 .addFunction ("start_touch", &AutomationControl::start_touch)
723                 .addFunction ("stop_touch", &AutomationControl::stop_touch)
724                 .addFunction ("get_value", &AutomationControl::get_value)
725                 .addFunction ("set_value", &AutomationControl::set_value)
726                 .addFunction ("writable", &AutomationControl::writable)
727                 .addFunction ("alist", &AutomationControl::alist)
728                 .endClass ()
729
730                 .deriveWSPtrClass <GainControl, AutomationControl> ("GainControl")
731                 .endClass ()
732
733                 .deriveWSPtrClass <Amp, Processor> ("Amp")
734                 .addFunction ("gain_control", (boost::shared_ptr<GainControl>(Amp::*)())&Amp::gain_control)
735                 .endClass ()
736
737                 .deriveWSPtrClass <PluginInsert::PluginControl, AutomationControl> ("PluginControl")
738                 .endClass ()
739
740                 .deriveWSPtrClass <AudioSource, Source> ("AudioSource")
741                 .addFunction ("readable_length", &AudioSource::readable_length)
742                 .addFunction ("n_channels", &AudioSource::n_channels)
743                 .endClass ()
744
745                 // <std::list<boost::shared_ptr <AudioTrack> >
746                 .beginStdList <boost::shared_ptr<AudioTrack> > ("AudioTrackList")
747                 .endClass ()
748
749                 // std::list<boost::shared_ptr <MidiTrack> >
750                 .beginStdList <boost::shared_ptr<MidiTrack> > ("MidiTrackList")
751                 .endClass ()
752
753                 // RouteList == std::list<boost::shared_ptr<Route> >
754                 .beginConstStdList <boost::shared_ptr<Route> > ("RouteList")
755                 .endClass ()
756
757                 // boost::shared_ptr<RouteList>
758                 .beginPtrStdList <boost::shared_ptr<Route> > ("RouteListPtr")
759                 .endClass ()
760
761                 // typedef std::list<boost::weak_ptr <Route> > WeakRouteList
762                 .beginConstStdList <boost::weak_ptr<Route> > ("WeakRouteList")
763                 .endClass ()
764
765                 // std::list< boost::weak_ptr <AudioSource> >
766                 .beginConstStdList <boost::weak_ptr<AudioSource> > ("WeakAudioSourceList")
767                 .endClass ()
768
769                 // typedef std::list<boost::shared_ptr<Region> > RegionList
770                 .beginConstStdList <boost::shared_ptr<Region> > ("RegionList")
771                 .endClass ()
772
773                 // boost::shared_ptr <std::list<boost::shared_ptr<Region> > >
774                 .beginPtrStdList <boost::shared_ptr<Region> > ("RegionListPtr")
775                 .endClass ()
776
777                 //std::list<boost::shared_ptr<Port> > PortList;
778                 .beginConstStdList <boost::shared_ptr<Port> > ("PortList")
779                 .endClass ()
780
781                 // used by Playlist::cut/copy
782                 .beginConstStdList <AudioRange> ("AudioRangeList")
783                 .endClass ()
784
785                 .beginConstStdList <Location*> ("LocationList")
786                 .endClass ()
787
788 #if 0  // depends on Evoal:: Note, Beats see note_fixer.h
789         // typedef Evoral::Note<Evoral::Beats> Note;
790         // std::set< boost::weak_ptr<Note> >
791                 .beginStdSet <boost::weak_ptr<Note> > ("WeakNoteSet")
792                 .endClass ()
793 #endif
794
795         // std::list<boost::weak_ptr<Source> >
796                 .beginConstStdList <boost::weak_ptr<Source> > ("WeakSourceList")
797                 .endClass ()
798
799                 .beginClass <Tempo> ("Tempo")
800                 .addConstructor <void (*) (double, double)> ()
801                 .addFunction ("note_type", &Tempo::note_type)
802                 .addFunction ("beats_per_minute", &Tempo::beats_per_minute)
803                 .addFunction ("frames_per_beat", &Tempo::frames_per_beat)
804                 .endClass ()
805
806                 .beginClass <Meter> ("Meter")
807                 .addConstructor <void (*) (double, double)> ()
808                 .addFunction ("divisions_per_bar", &Meter::divisions_per_bar)
809                 .addFunction ("note_divisor", &Meter::note_divisor)
810                 .addFunction ("frames_per_bar", &Meter::frames_per_bar)
811                 .addFunction ("frames_per_grid", &Meter::frames_per_grid)
812                 .endClass ()
813
814                 .beginClass <TempoMap> ("TempoMap")
815                 .addFunction ("add_tempo", &TempoMap::add_tempo)
816                 .addFunction ("add_meter", &TempoMap::add_meter)
817                 .endClass ()
818
819                 .beginClass <ChanCount> ("ChanCount")
820                 .addConstructor <void (*) (DataType, uint32_t)> ()
821                 .addFunction ("get", &ChanCount::get)
822                 .addFunction ("set", &ChanCount::set)
823                 .addFunction ("n_audio", &ChanCount::n_audio)
824                 .addFunction ("n_midi", &ChanCount::n_midi)
825                 .addFunction ("n_total", &ChanCount::n_total)
826                 .addFunction ("reset", &ChanCount::reset)
827                 .endClass()
828
829                 .beginClass <DataType> ("DataType")
830                 .addConstructor <void (*) (std::string)> ()
831                 .addStaticCFunction ("null",  &LuaAPI::datatype_ctor_null) // "nil" is a lua reseved word
832                 .addStaticCFunction ("audio", &LuaAPI::datatype_ctor_audio)
833                 .addStaticCFunction ("midi",  &LuaAPI::datatype_ctor_midi)
834                 .addFunction ("to_string",  &DataType::to_string) // TODO Lua __tostring
835                 // TODO add uint32_t cast, add operator==  !=
836                 .endClass()
837
838                 /* libardour enums */
839                 .beginNamespace ("PluginType")
840                 .addConst ("AudioUnit", ARDOUR::PluginType(AudioUnit))
841                 .addConst ("LADSPA", ARDOUR::PluginType(LADSPA))
842                 .addConst ("LV2", ARDOUR::PluginType(LV2))
843                 .addConst ("Windows_VST", ARDOUR::PluginType(Windows_VST))
844                 .addConst ("LXVST", ARDOUR::PluginType(LXVST))
845                 .addConst ("Lua", ARDOUR::PluginType(Lua))
846                 .endNamespace ()
847
848                 .beginNamespace ("AutoStyle")
849                 .addConst ("Absolute", ARDOUR::AutoStyle(Absolute))
850                 .addConst ("Trim", ARDOUR::AutoStyle(Trim))
851                 .endNamespace ()
852
853                 .beginNamespace ("AutoState")
854                 .addConst ("Off", ARDOUR::AutoState(Off))
855                 .addConst ("Write", ARDOUR::AutoState(Write))
856                 .addConst ("Touch", ARDOUR::AutoState(Touch))
857                 .addConst ("Play", ARDOUR::AutoState(Play))
858                 .endNamespace ()
859
860                 .beginNamespace ("AutomationType")
861                 .addConst ("PluginAutomation", ARDOUR::AutomationType(PluginAutomation))
862                 .addConst ("PluginAutomation", ARDOUR::AutomationType(GainAutomation))
863                 .addConst ("PluginAutomation", ARDOUR::AutomationType(TrimAutomation))
864                 .endNamespace ()
865
866                 .beginNamespace ("SrcQuality")
867                 .addConst ("SrcBest", ARDOUR::SrcQuality(SrcBest))
868                 .endNamespace ()
869
870                 .beginNamespace ("PortFlags")
871                 .addConst ("IsInput", ARDOUR::PortFlags(IsInput))
872                 .addConst ("IsOutput", ARDOUR::PortFlags(IsOutput))
873                 .addConst ("IsPhysical", ARDOUR::PortFlags(IsPhysical))
874                 .addConst ("CanMonitor", ARDOUR::PortFlags(CanMonitor))
875                 .addConst ("IsTerminal", ARDOUR::PortFlags(IsTerminal))
876                 .endNamespace ()
877
878                 .beginNamespace ("PlaylistDisposition")
879                 .addConst ("CopyPlaylist", ARDOUR::PlaylistDisposition(CopyPlaylist))
880                 .addConst ("NewPlaylist", ARDOUR::PlaylistDisposition(NewPlaylist))
881                 .addConst ("SharePlaylist", ARDOUR::PlaylistDisposition(SharePlaylist))
882                 .endNamespace ()
883
884                 .beginNamespace ("RegionPoint")
885                 .addConst ("Start", ARDOUR::RegionPoint(Start))
886                 .addConst ("End", ARDOUR::RegionPoint(End))
887                 .addConst ("SyncPoint", ARDOUR::RegionPoint(SyncPoint))
888                 .endNamespace ()
889
890                 .beginNamespace ("TrackMode")
891                 .addConst ("Normal", ARDOUR::TrackMode(Start))
892                 .addConst ("NonLayered", ARDOUR::TrackMode(NonLayered))
893                 .addConst ("Destructive", ARDOUR::TrackMode(Destructive))
894                 .endNamespace ()
895                 .endNamespace ();
896
897         luabridge::getGlobalNamespace (L)
898                 .beginNamespace ("ARDOUR")
899                 .beginClass <AudioBackendInfo> ("AudioBackendInfo")
900                 .addData ("name", &AudioBackendInfo::name)
901                 .endClass()
902                 .beginStdVector <const AudioBackendInfo*> ("BackendVector").endClass ()
903
904                 .beginClass <AudioBackend::DeviceStatus> ("DeviceStatus")
905                 .addData ("name", &AudioBackend::DeviceStatus::name)
906                 .addData ("available", &AudioBackend::DeviceStatus::available)
907                 .endClass()
908                 .beginStdVector <AudioBackend::DeviceStatus> ("DeviceStatusVector").endClass ()
909
910                 .beginWSPtrClass <AudioBackend> ("AudioBackend")
911                 .addFunction ("info", &AudioBackend::info)
912                 .addFunction ("sample_rate", &AudioBackend::sample_rate)
913                 .addFunction ("buffer_size", &AudioBackend::buffer_size)
914                 .addFunction ("period_size", &AudioBackend::period_size)
915                 .addFunction ("input_channels", &AudioBackend::input_channels)
916                 .addFunction ("output_channels", &AudioBackend::output_channels)
917                 .addFunction ("dsp_load", &AudioBackend::dsp_load)
918
919                 .addFunction ("set_sample_rate", &AudioBackend::set_sample_rate)
920                 .addFunction ("set_buffer_size", &AudioBackend::set_buffer_size)
921                 .addFunction ("set_peridod_size", &AudioBackend::set_peridod_size)
922
923                 .addFunction ("enumerate_drivers", &AudioBackend::enumerate_drivers)
924                 .addFunction ("driver_name", &AudioBackend::driver_name)
925                 .addFunction ("set_driver", &AudioBackend::set_driver)
926
927                 .addFunction ("use_separate_input_and_output_devices", &AudioBackend::use_separate_input_and_output_devices)
928                 .addFunction ("enumerate_devices", &AudioBackend::enumerate_devices)
929                 .addFunction ("enumerate_input_devices", &AudioBackend::enumerate_input_devices)
930                 .addFunction ("enumerate_output_devices", &AudioBackend::enumerate_output_devices)
931                 .addFunction ("device_name", &AudioBackend::device_name)
932                 .addFunction ("input_device_name", &AudioBackend::input_device_name)
933                 .addFunction ("output_device_name", &AudioBackend::output_device_name)
934                 .addFunction ("set_device_name", &AudioBackend::set_device_name)
935                 .addFunction ("set_input_device_name", &AudioBackend::set_input_device_name)
936                 .addFunction ("set_output_device_name", &AudioBackend::set_output_device_name)
937                 .endClass()
938
939                 .beginClass <PortManager> ("PortManager")
940                 .addFunction ("port_engine", &PortManager::port_engine)
941                 .addFunction ("connected", &PortManager::connected)
942                 .addFunction ("connect", &PortManager::connect)
943                 .addFunction ("physically_connected", &PortManager::physically_connected)
944                 .addFunction ("disconnect", (int (PortManager::*)(const std::string&, const std::string&))&PortManager::disconnect)
945                 .addFunction ("disconnect_port", (int (PortManager::*)(boost::shared_ptr<Port>))&PortManager::disconnect)
946                 .addFunction ("get_port_by_name", &PortManager::get_port_by_name)
947                 .addFunction ("get_pretty_name_by_name", &PortManager::get_pretty_name_by_name)
948                 .addFunction ("port_is_physical", &PortManager::port_is_physical)
949                 .addFunction ("get_physical_outputs", &PortManager::get_physical_outputs)
950                 .addFunction ("get_physical_inputs", &PortManager::get_physical_inputs)
951                 .addFunction ("n_physical_outputs", &PortManager::n_physical_outputs)
952                 .addFunction ("n_physical_inputs", &PortManager::n_physical_inputs)
953                 .addRefFunction ("get_connections", &PortManager::get_connections)
954                 .addRefFunction ("get_ports", (int (PortManager::*)(DataType, PortManager::PortList&))&PortManager::get_ports)
955                 .addRefFunction ("get_backend_ports", (int (PortManager::*)(const std::string&, DataType, PortFlags, std::vector<std::string>&))&PortManager::get_ports)
956                 .endClass()
957
958                 .deriveClass <AudioEngine, PortManager> ("AudioEngine")
959                 .addFunction ("available_backends", &AudioEngine::available_backends)
960                 .addFunction ("current_backend_name", &AudioEngine::current_backend_name)
961                 .addFunction ("set_backend", &AudioEngine::set_backend)
962                 .addFunction ("setup_required", &AudioEngine::setup_required)
963                 .addFunction ("start", &AudioEngine::start)
964                 .addFunction ("stop", &AudioEngine::stop)
965                 .addFunction ("get_dsp_load", &AudioEngine::get_dsp_load)
966                 .addFunction ("set_device_name", &AudioEngine::set_device_name)
967                 .addFunction ("set_sample_rate", &AudioEngine::set_sample_rate)
968                 .addFunction ("set_buffer_size", &AudioEngine::set_buffer_size)
969                 .addFunction ("get_last_backend_error", &AudioEngine::get_last_backend_error)
970                 .endClass()
971                 .endNamespace ();
972
973         // basic representation of Session
974         // functions which can be used from realtime and non-realtime contexts
975         luabridge::getGlobalNamespace (L)
976                 .beginNamespace ("ARDOUR")
977                 .beginClass <Session> ("Session")
978                 .addFunction ("scripts_changed", &Session::scripts_changed) // used internally
979                 .addFunction ("transport_rolling", &Session::transport_rolling)
980                 .addFunction ("request_transport_speed", &Session::request_transport_speed)
981                 .addFunction ("transport_frame", &Session::transport_frame)
982                 .addFunction ("transport_speed", &Session::transport_speed)
983                 .addFunction ("frame_rate", &Session::frame_rate)
984                 .addFunction ("nominal_frame_rate", &Session::nominal_frame_rate)
985                 .addFunction ("frames_per_timecode_frame", &Session::frames_per_timecode_frame)
986                 .addFunction ("timecode_frames_per_hour", &Session::timecode_frames_per_hour)
987                 .addFunction ("timecode_frames_per_second", &Session::timecode_frames_per_second)
988                 .addFunction ("timecode_drop_frames", &Session::timecode_drop_frames)
989                 .addFunction ("request_locate", &Session::request_locate)
990                 .addFunction ("request_stop", &Session::request_stop)
991                 .addFunction ("last_transport_start", &Session::last_transport_start)
992                 .addFunction ("goto_start", &Session::goto_start)
993                 .addFunction ("goto_end", &Session::goto_end)
994                 .addFunction ("current_start_frame", &Session::current_start_frame)
995                 .addFunction ("current_end_frame", &Session::current_end_frame)
996                 .addFunction ("actively_recording", &Session::actively_recording)
997                 .addFunction ("new_audio_track", &Session::new_audio_track)
998                 .addFunction ("new_audio_route", &Session::new_audio_route)
999                 .addFunction ("new_midi_track", &Session::new_midi_track)
1000                 .addFunction ("new_midi_route", &Session::new_midi_route)
1001                 .addFunction ("get_routes", &Session::get_routes)
1002                 .addFunction ("get_tracks", &Session::get_tracks)
1003                 .addFunction ("name", &Session::name)
1004                 .addFunction ("path", &Session::path)
1005                 .addFunction ("record_status", &Session::record_status)
1006                 .addFunction ("route_by_id", &Session::route_by_id)
1007                 .addFunction ("route_by_name", &Session::route_by_name)
1008                 .addFunction ("route_by_remote_id", &Session::route_by_remote_id)
1009                 .addFunction ("track_by_diskstream_id", &Session::track_by_diskstream_id)
1010                 .addFunction ("source_by_id", &Session::source_by_id)
1011                 .addFunction ("controllable_by_id", &Session::controllable_by_id)
1012                 .addFunction ("processor_by_id", &Session::processor_by_id)
1013                 .addFunction ("snap_name", &Session::snap_name)
1014                 .addFunction ("monitor_out", &Session::monitor_out)
1015                 .addFunction ("master_out", &Session::master_out)
1016                 .addFunction ("tempo_map", (TempoMap& (Session::*)())&Session::tempo_map)
1017                 .addFunction ("locations", &Session::locations)
1018                 .addFunction ("begin_reversible_command", (void (Session::*)(const std::string&))&Session::begin_reversible_command)
1019                 .addFunction ("commit_reversible_command", &Session::commit_reversible_command)
1020                 .addFunction ("abort_reversible_command", &Session::abort_reversible_command)
1021                 .addFunction ("add_command", &Session::add_command)
1022                 .addFunction ("add_stateful_diff_command", &Session::add_stateful_diff_command)
1023                 .addFunction ("engine", (AudioEngine& (Session::*)())&Session::engine)
1024                 .endClass ()
1025
1026                 .beginClass <RegionFactory> ("RegionFactory")
1027                 .addStaticFunction ("region_by_id", &RegionFactory::region_by_id)
1028                 .endClass ()
1029
1030                 /* session enums */
1031                 .beginNamespace ("Session")
1032
1033                 .beginNamespace ("RecordState")
1034                 .addConst ("Disabled", ARDOUR::Session::RecordState(Session::Disabled))
1035                 .addConst ("Enabled", ARDOUR::Session::RecordState(Session::Enabled))
1036                 .addConst ("Recording", ARDOUR::Session::RecordState(Session::Recording))
1037                 .endNamespace ()
1038
1039                 .endNamespace () // END Session enums
1040
1041                 .beginNamespace ("LuaAPI")
1042                 .addFunction ("nil_proc", ARDOUR::LuaAPI::nil_processor)
1043                 .addFunction ("new_luaproc", ARDOUR::LuaAPI::new_luaproc)
1044                 .addFunction ("new_plugin_info", ARDOUR::LuaAPI::new_plugin_info)
1045                 .addFunction ("new_plugin", ARDOUR::LuaAPI::new_plugin)
1046                 .addFunction ("set_processor_param", ARDOUR::LuaAPI::set_processor_param)
1047                 .addFunction ("set_plugin_insert_param", ARDOUR::LuaAPI::set_plugin_insert_param)
1048                 .addCFunction ("plugin_automation", ARDOUR::LuaAPI::plugin_automation)
1049                 .addFunction ("usleep", Glib::usleep)
1050                 .endNamespace ()
1051
1052                 .endNamespace ();// END ARDOUR
1053 }
1054
1055 void
1056 LuaBindings::dsp (lua_State* L)
1057 {
1058         luabridge::getGlobalNamespace (L)
1059                 .beginNamespace ("ARDOUR")
1060
1061                 .beginClass <AudioBuffer> ("AudioBuffer")
1062                 .addEqualCheck ()
1063                 .addFunction ("data", (Sample*(AudioBuffer::*)(framecnt_t))&AudioBuffer::data)
1064                 .addFunction ("silence", &AudioBuffer::silence)
1065                 .addFunction ("apply_gain", &AudioBuffer::apply_gain)
1066                 .addFunction ("check_silence", &AudioBuffer::check_silence)
1067                 .addFunction ("read_from", (void (AudioBuffer::*)(const Sample*, framecnt_t, framecnt_t, framecnt_t))&AudioBuffer::check_silence)
1068                 .endClass()
1069
1070                 .beginClass <MidiBuffer> ("MidiBuffer")
1071                 .addEqualCheck ()
1072                 .addFunction ("silence", &MidiBuffer::silence)
1073                 .addFunction ("empty", &MidiBuffer::empty)
1074                 // TODO iterators..
1075                 .endClass()
1076
1077                 .beginClass <BufferSet> ("BufferSet")
1078                 .addEqualCheck ()
1079                 .addFunction ("get_audio", static_cast<AudioBuffer&(BufferSet::*)(size_t)>(&BufferSet::get_audio))
1080                 .addFunction ("count", static_cast<const ChanCount&(BufferSet::*)()const>(&BufferSet::count))
1081                 .endClass()
1082                 .endNamespace ();
1083
1084         luabridge::getGlobalNamespace (L)
1085                 .beginNamespace ("Evoral")
1086                 .beginClass <Evoral::Event<framepos_t> > ("Event")
1087                 .addFunction ("clear", &Evoral::Event<framepos_t>::clear)
1088                 .addFunction ("size", &Evoral::Event<framepos_t>::size)
1089                 .addFunction ("set_buffer", &Evoral::Event<framepos_t>::set_buffer)
1090                 .addFunction ("buffer", (uint8_t*(Evoral::Event<framepos_t>::*)())&Evoral::Event<framepos_t>::buffer)
1091                 .endClass ()
1092
1093                 .beginClass <Evoral::Beats> ("Beats")
1094                 .addFunction ("to_double", &Evoral::Beats::to_double)
1095                 .endClass ()
1096
1097                 .deriveClass <Evoral::MIDIEvent<framepos_t>, Evoral::Event<framepos_t> > ("MidiEvent")
1098                 // add Ctor?
1099                 .addFunction ("type", &Evoral::MIDIEvent<framepos_t>::type)
1100                 .addFunction ("channel", &Evoral::MIDIEvent<framepos_t>::channel)
1101                 .addFunction ("set_type", &Evoral::MIDIEvent<framepos_t>::type)
1102                 .addFunction ("set_channel", &Evoral::MIDIEvent<framepos_t>::channel)
1103                 .endClass ()
1104                 .endNamespace ();
1105
1106         // dsp releated session functions
1107         luabridge::getGlobalNamespace (L)
1108                 .beginNamespace ("ARDOUR")
1109                 .beginClass <Session> ("Session")
1110                 .addFunction ("get_scratch_buffers", &Session::get_scratch_buffers)
1111                 .addFunction ("get_silent_buffers", &Session::get_silent_buffers)
1112                 .endClass ()
1113                 .endNamespace ();
1114
1115         luabridge::getGlobalNamespace (L)
1116                 .beginNamespace ("ARDOUR")
1117                 .beginNamespace ("DSP")
1118                 .addFunction ("compute_peak", ARDOUR::compute_peak)
1119                 .addFunction ("find_peaks", ARDOUR::find_peaks)
1120                 .addFunction ("apply_gain_to_buffer", ARDOUR::apply_gain_to_buffer)
1121                 .addFunction ("mix_buffers_no_gain", ARDOUR::mix_buffers_no_gain)
1122                 .addFunction ("mix_buffers_with_gain", ARDOUR::mix_buffers_with_gain)
1123                 .addFunction ("copy_vector", ARDOUR::copy_vector)
1124                 .addFunction ("dB_to_coefficient", &dB_to_coefficient)
1125                 .addFunction ("fast_coefficient_to_dB", &fast_coefficient_to_dB)
1126                 .addFunction ("accurate_coefficient_to_dB", &accurate_coefficient_to_dB)
1127                 .addFunction ("memset", &DSP::memset)
1128                 .addFunction ("mmult", &DSP::mmult)
1129                 .addFunction ("log_meter", &DSP::log_meter)
1130                 .addFunction ("log_meter_coeff", &DSP::log_meter_coeff)
1131                 .addRefFunction ("peaks", &DSP::peaks)
1132
1133                 .beginClass <DSP::LowPass> ("LowPass")
1134                 .addConstructor <void (*) (double, float)> ()
1135                 .addFunction ("proc", &DSP::LowPass::proc)
1136                 .addFunction ("ctrl", &DSP::LowPass::ctrl)
1137                 .addFunction ("set_cutoff", &DSP::LowPass::set_cutoff)
1138                 .addFunction ("reset", &DSP::LowPass::reset)
1139                 .endClass ()
1140                 .beginClass <DSP::BiQuad> ("Biquad")
1141                 .addConstructor <void (*) (double)> ()
1142                 .addFunction ("run", &DSP::BiQuad::run)
1143                 .addFunction ("compute", &DSP::BiQuad::compute)
1144                 .addFunction ("reset", &DSP::BiQuad::reset)
1145                 .endClass ()
1146
1147                 /* DSP enums */
1148                 .beginNamespace ("BiQuadType")
1149                 .addConst ("LowPass", ARDOUR::DSP::BiQuad::LowPass)
1150                 .addConst ("HighPass", ARDOUR::DSP::BiQuad::HighPass)
1151                 .addConst ("BandPassSkirt", ARDOUR::DSP::BiQuad::BandPassSkirt)
1152                 .addConst ("BandPass0dB", ARDOUR::DSP::BiQuad::BandPass0dB)
1153                 .addConst ("Notch", ARDOUR::DSP::BiQuad::Notch)
1154                 .addConst ("AllPass", ARDOUR::DSP::BiQuad::AllPass)
1155                 .addConst ("Peaking", ARDOUR::DSP::BiQuad::Peaking)
1156                 .addConst ("LowShelf", ARDOUR::DSP::BiQuad::LowShelf)
1157                 .addConst ("HighShelf", ARDOUR::DSP::BiQuad::HighShelf)
1158                 .endNamespace ()
1159
1160                 .beginClass <DSP::DspShm> ("DspShm")
1161                 .addFunction ("allocate", &DSP::DspShm::allocate)
1162                 .addFunction ("clear", &DSP::DspShm::clear)
1163                 .addFunction ("to_float", &DSP::DspShm::to_float)
1164                 .addFunction ("to_int", &DSP::DspShm::to_int)
1165                 .addFunction ("atomic_set_int", &DSP::DspShm::atomic_set_int)
1166                 .addFunction ("atomic_get_int", &DSP::DspShm::atomic_get_int)
1167                 .endClass ()
1168
1169                 .endNamespace () // DSP
1170                 .endNamespace (); // ARDOUR
1171 }
1172
1173 void
1174 LuaBindings::session (lua_State* L)
1175 {
1176         // non-realtime session functions
1177         luabridge::getGlobalNamespace (L)
1178                 .beginNamespace ("ARDOUR")
1179                 .beginClass <Session> ("Session")
1180                 .addFunction ("save_state", &Session::save_state)
1181                 .addFunction ("set_dirty", &Session::set_dirty)
1182                 .addFunction ("unknown_processors", &Session::unknown_processors)
1183
1184                 .addFunction<RouteList (Session::*)(uint32_t, const std::string&, const std::string&, PlaylistDisposition)> ("new_route_from_template", &Session::new_route_from_template)
1185                 // TODO  session_add_audio_track  session_add_midi_track  session_add_mixed_track
1186                 //.addFunction ("new_midi_track", &Session::new_midi_track)
1187                 .endClass ()
1188
1189                 .endNamespace (); // ARDOUR
1190 }
1191
1192 void
1193 LuaBindings::osc (lua_State* L)
1194 {
1195         luabridge::getGlobalNamespace (L)
1196                 .beginNamespace ("ARDOUR")
1197                 .beginNamespace ("LuaOSC")
1198                 .beginClass<LuaOSC::Address> ("Address")
1199                 .addConstructor<void (*) (std::string)> ()
1200                 .addCFunction ("send", &LuaOSC::Address::send)
1201                 .endClass ()
1202                 .endNamespace ()
1203                 .endNamespace ();
1204 }
1205
1206 void
1207 LuaBindings::set_session (lua_State* L, Session *s)
1208 {
1209         /* LuaBridge uses unique keys to identify classes/c-types.
1210          *
1211          * Those keys are "generated" by using the memory-address of a static
1212          * variable, templated for every Class.
1213          * (see libs/lua/LuaBridge/detail/ClassInfo.h)
1214          *
1215          * When linking the final executable there must be exactly one static
1216          * function (static variable) for every templated class.
1217          * This works fine on OSX and Linux...
1218          *
1219          * Windows (mingw and MSVC) however expand the template differently for libardour
1220          * AND gtk2_ardour. We end up with two identical static functions
1221          * at different addresses!!
1222          *
1223          * The Solution: have gtk2_ardour never include LuaBridge headers directly
1224          * and always go via libardour function calls for classes that are registered
1225          * in libardour. (calling lua itself is fine,  calling c-functions in the GUI
1226          * which expand the template is not)
1227          *
1228          * (the actual cause: even static symbols in a .dll have no fixed address
1229          * and are mapped when loading the dll. static functions in .exe do have a fixed
1230          * address)
1231          *
1232          * libardour:
1233          *  0000000000000000 I __imp__ZZN9luabridge9ClassInfoIN6ARDOUR7SessionEE11getClassKeyEvE5value
1234          *  0000000000000000 I __nm__ZZN9luabridge9ClassInfoIN6ARDOUR7SessionEE11getClassKeyEvE5value
1235          *  0000000000000000 T _ZN9luabridge9ClassInfoIN6ARDOUR7SessionEE11getClassKeyEv
1236          *
1237          * ardour.exe
1238          *  000000000104f560 d .data$_ZZN9luabridge9ClassInfoIN6ARDOUR7SessionEE11getClassKeyEvE5value
1239          *  000000000104f560 D _ZZN9luabridge9ClassInfoIN6ARDOUR7SessionEE11getClassKeyEvE5value
1240          *  0000000000e9baf0 T _ZN9luabridge9ClassInfoIN6ARDOUR7SessionEE11getClassKeyEv
1241          *
1242          *
1243          */
1244         luabridge::push <Session *> (L, s);
1245         lua_setglobal (L, "Session");
1246
1247         if (s) {
1248                 // call lua function.
1249                 luabridge::LuaRef cb_ses = luabridge::getGlobal (L, "new_session");
1250                 if (cb_ses.type() == LUA_TFUNCTION) { cb_ses(s->name()); } // TODO args
1251         }
1252 }