a few strategic LocaleGuards..
[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                 .addVoidConstructor ()
650                 .addData ("uri", &Plugin::PresetRecord::uri, false)
651                 .addData ("label", &Plugin::PresetRecord::label, false)
652                 .addData ("user", &Plugin::PresetRecord::user, false)
653                 .addData ("valid", &Plugin::PresetRecord::valid, false)
654                 .endClass ()
655
656                 .deriveWSPtrClass <Automatable, Evoral::ControlSet> ("Automatable")
657                 .addFunction ("automation_control", (boost::shared_ptr<AutomationControl>(Automatable::*)(const Evoral::Parameter&, bool))&Automatable::automation_control)
658                 .endClass ()
659
660                 .deriveClass <ParameterDescriptor, Evoral::ParameterDescriptor> ("ParameterDescriptor")
661                 .addVoidConstructor ()
662                 .addData ("label", &ParameterDescriptor::label)
663                 .addData ("logarithmic", &ParameterDescriptor::logarithmic)
664                 .endClass ()
665
666                 .deriveWSPtrClass <Processor, SessionObject> ("Processor")
667                 .addCast<Automatable> ("to_automatable")
668                 .addCast<PluginInsert> ("to_insert")
669                 .addCast<SideChain> ("to_sidechain")
670                 .addCast<IOProcessor> ("to_ioprocessor")
671                 .addFunction ("display_name", &Processor::display_name)
672                 .addFunction ("active", &Processor::active)
673                 .addFunction ("activate", &Processor::activate)
674                 .addFunction ("deactivate", &Processor::deactivate)
675                 .addFunction ("control", (boost::shared_ptr<Evoral::Control>(Evoral::ControlSet::*)(const Evoral::Parameter&, bool))&Evoral::ControlSet::control)
676                 .addFunction ("automation_control", (boost::shared_ptr<AutomationControl>(Automatable::*)(const Evoral::Parameter&, bool))&Automatable::automation_control)
677                 .endClass ()
678
679                 .deriveWSPtrClass <IOProcessor, Processor> ("IOProcessor")
680                 .addFunction ("natural_input_streams", &IOProcessor::natural_input_streams)
681                 .addFunction ("natural_output_streams", &IOProcessor::natural_output_streams)
682                 .addFunction ("input", (boost::shared_ptr<IO>(IOProcessor::*)())&IOProcessor::input)
683                 .addFunction ("output", (boost::shared_ptr<IO>(IOProcessor::*)())&IOProcessor::output)
684                 .endClass ()
685
686                 .deriveWSPtrClass <SideChain, IOProcessor> ("SideChain")
687                 .endClass ()
688
689                 .deriveWSPtrClass <Delivery, IOProcessor> ("Delivery")
690                 .endClass ()
691
692                 .deriveWSPtrClass <Plugin, PBD::StatefulDestructible> ("Plugin")
693                 .addFunction ("label", &Plugin::label)
694                 .addFunction ("name", &Plugin::name)
695                 .addFunction ("maker", &Plugin::maker)
696                 .addFunction ("parameter_count", &Plugin::parameter_count)
697                 .addRefFunction ("nth_parameter", &Plugin::nth_parameter)
698                 .addFunction ("preset_by_label", &Plugin::preset_by_label)
699                 .addFunction ("preset_by_uri", &Plugin::preset_by_uri)
700                 .addFunction ("load_preset", &Plugin::load_preset)
701                 .addFunction ("parameter_is_input", &Plugin::parameter_is_input)
702                 .addFunction ("get_docs", &Plugin::get_docs)
703                 .addFunction ("get_parameter_docs", &Plugin::get_parameter_docs)
704                 .addRefFunction ("get_parameter_descriptor", &Plugin::get_parameter_descriptor)
705                 .endClass ()
706
707                 .deriveWSPtrClass <PluginInsert, Processor> ("PluginInsert")
708                 .addFunction ("plugin", &PluginInsert::plugin)
709                 .addFunction ("activate", &PluginInsert::activate)
710                 .addFunction ("deactivate", &PluginInsert::deactivate)
711                 .addFunction ("strict_io_configured", &PluginInsert::strict_io_configured)
712                 .addFunction ("input_map", (ARDOUR::ChanMapping (PluginInsert::*)(uint32_t) const)&PluginInsert::input_map)
713                 .addFunction ("output_map", (ARDOUR::ChanMapping (PluginInsert::*)(uint32_t) const)&PluginInsert::output_map)
714                 .addFunction ("set_input_map", &PluginInsert::set_input_map)
715                 .addFunction ("set_output_map", &PluginInsert::set_output_map)
716                 .addFunction ("sidechain_input", &PluginInsert::sidechain_input)
717                 .endClass ()
718
719                 .deriveWSPtrClass <AutomationControl, PBD::Controllable> ("AutomationControl")
720                 .addCast<Evoral::Control> ("to_ctrl")
721                 .addFunction ("automation_state", &AutomationControl::automation_state)
722                 .addFunction ("automation_style", &AutomationControl::automation_style)
723                 .addFunction ("set_automation_state", &AutomationControl::set_automation_state)
724                 .addFunction ("set_automation_style", &AutomationControl::set_automation_style)
725                 .addFunction ("start_touch", &AutomationControl::start_touch)
726                 .addFunction ("stop_touch", &AutomationControl::stop_touch)
727                 .addFunction ("get_value", &AutomationControl::get_value)
728                 .addFunction ("set_value", &AutomationControl::set_value)
729                 .addFunction ("writable", &AutomationControl::writable)
730                 .addFunction ("alist", &AutomationControl::alist)
731                 .endClass ()
732
733                 .deriveWSPtrClass <GainControl, AutomationControl> ("GainControl")
734                 .endClass ()
735
736                 .deriveWSPtrClass <Amp, Processor> ("Amp")
737                 .addFunction ("gain_control", (boost::shared_ptr<GainControl>(Amp::*)())&Amp::gain_control)
738                 .endClass ()
739
740                 .deriveWSPtrClass <PluginInsert::PluginControl, AutomationControl> ("PluginControl")
741                 .endClass ()
742
743                 .deriveWSPtrClass <AudioSource, Source> ("AudioSource")
744                 .addFunction ("readable_length", &AudioSource::readable_length)
745                 .addFunction ("n_channels", &AudioSource::n_channels)
746                 .endClass ()
747
748                 // <std::list<boost::shared_ptr <AudioTrack> >
749                 .beginStdList <boost::shared_ptr<AudioTrack> > ("AudioTrackList")
750                 .endClass ()
751
752                 // std::list<boost::shared_ptr <MidiTrack> >
753                 .beginStdList <boost::shared_ptr<MidiTrack> > ("MidiTrackList")
754                 .endClass ()
755
756                 // RouteList == std::list<boost::shared_ptr<Route> >
757                 .beginConstStdList <boost::shared_ptr<Route> > ("RouteList")
758                 .endClass ()
759
760                 // boost::shared_ptr<RouteList>
761                 .beginPtrStdList <boost::shared_ptr<Route> > ("RouteListPtr")
762                 .endClass ()
763
764                 // typedef std::list<boost::weak_ptr <Route> > WeakRouteList
765                 .beginConstStdList <boost::weak_ptr<Route> > ("WeakRouteList")
766                 .endClass ()
767
768                 // std::list< boost::weak_ptr <AudioSource> >
769                 .beginConstStdList <boost::weak_ptr<AudioSource> > ("WeakAudioSourceList")
770                 .endClass ()
771
772                 // typedef std::list<boost::shared_ptr<Region> > RegionList
773                 .beginConstStdList <boost::shared_ptr<Region> > ("RegionList")
774                 .endClass ()
775
776                 // boost::shared_ptr <std::list<boost::shared_ptr<Region> > >
777                 .beginPtrStdList <boost::shared_ptr<Region> > ("RegionListPtr")
778                 .endClass ()
779
780                 //std::list<boost::shared_ptr<Port> > PortList;
781                 .beginConstStdList <boost::shared_ptr<Port> > ("PortList")
782                 .endClass ()
783
784                 // used by Playlist::cut/copy
785                 .beginConstStdList <AudioRange> ("AudioRangeList")
786                 .endClass ()
787
788                 .beginConstStdList <Location*> ("LocationList")
789                 .endClass ()
790
791 #if 0  // depends on Evoal:: Note, Beats see note_fixer.h
792         // typedef Evoral::Note<Evoral::Beats> Note;
793         // std::set< boost::weak_ptr<Note> >
794                 .beginStdSet <boost::weak_ptr<Note> > ("WeakNoteSet")
795                 .endClass ()
796 #endif
797
798         // std::list<boost::weak_ptr<Source> >
799                 .beginConstStdList <boost::weak_ptr<Source> > ("WeakSourceList")
800                 .endClass ()
801
802                 .beginClass <Tempo> ("Tempo")
803                 .addConstructor <void (*) (double, double)> ()
804                 .addFunction ("note_type", &Tempo::note_type)
805                 .addFunction ("beats_per_minute", &Tempo::beats_per_minute)
806                 .addFunction ("frames_per_beat", &Tempo::frames_per_beat)
807                 .endClass ()
808
809                 .beginClass <Meter> ("Meter")
810                 .addConstructor <void (*) (double, double)> ()
811                 .addFunction ("divisions_per_bar", &Meter::divisions_per_bar)
812                 .addFunction ("note_divisor", &Meter::note_divisor)
813                 .addFunction ("frames_per_bar", &Meter::frames_per_bar)
814                 .addFunction ("frames_per_grid", &Meter::frames_per_grid)
815                 .endClass ()
816
817                 .beginClass <TempoMap> ("TempoMap")
818                 .addFunction ("add_tempo", &TempoMap::add_tempo)
819                 .addFunction ("add_meter", &TempoMap::add_meter)
820                 .endClass ()
821
822                 .beginClass <ChanCount> ("ChanCount")
823                 .addConstructor <void (*) (DataType, uint32_t)> ()
824                 .addFunction ("get", &ChanCount::get)
825                 .addFunction ("set", &ChanCount::set)
826                 .addFunction ("n_audio", &ChanCount::n_audio)
827                 .addFunction ("n_midi", &ChanCount::n_midi)
828                 .addFunction ("n_total", &ChanCount::n_total)
829                 .addFunction ("reset", &ChanCount::reset)
830                 .endClass()
831
832                 .beginClass <DataType> ("DataType")
833                 .addConstructor <void (*) (std::string)> ()
834                 .addStaticCFunction ("null",  &LuaAPI::datatype_ctor_null) // "nil" is a lua reseved word
835                 .addStaticCFunction ("audio", &LuaAPI::datatype_ctor_audio)
836                 .addStaticCFunction ("midi",  &LuaAPI::datatype_ctor_midi)
837                 .addFunction ("to_string",  &DataType::to_string) // TODO Lua __tostring
838                 // TODO add uint32_t cast, add operator==  !=
839                 .endClass()
840
841                 /* libardour enums */
842                 .beginNamespace ("PluginType")
843                 .addConst ("AudioUnit", ARDOUR::PluginType(AudioUnit))
844                 .addConst ("LADSPA", ARDOUR::PluginType(LADSPA))
845                 .addConst ("LV2", ARDOUR::PluginType(LV2))
846                 .addConst ("Windows_VST", ARDOUR::PluginType(Windows_VST))
847                 .addConst ("LXVST", ARDOUR::PluginType(LXVST))
848                 .addConst ("Lua", ARDOUR::PluginType(Lua))
849                 .endNamespace ()
850
851                 .beginNamespace ("AutoStyle")
852                 .addConst ("Absolute", ARDOUR::AutoStyle(Absolute))
853                 .addConst ("Trim", ARDOUR::AutoStyle(Trim))
854                 .endNamespace ()
855
856                 .beginNamespace ("AutoState")
857                 .addConst ("Off", ARDOUR::AutoState(Off))
858                 .addConst ("Write", ARDOUR::AutoState(Write))
859                 .addConst ("Touch", ARDOUR::AutoState(Touch))
860                 .addConst ("Play", ARDOUR::AutoState(Play))
861                 .endNamespace ()
862
863                 .beginNamespace ("AutomationType")
864                 .addConst ("PluginAutomation", ARDOUR::AutomationType(PluginAutomation))
865                 .addConst ("PluginAutomation", ARDOUR::AutomationType(GainAutomation))
866                 .addConst ("PluginAutomation", ARDOUR::AutomationType(TrimAutomation))
867                 .endNamespace ()
868
869                 .beginNamespace ("SrcQuality")
870                 .addConst ("SrcBest", ARDOUR::SrcQuality(SrcBest))
871                 .endNamespace ()
872
873                 .beginNamespace ("PortFlags")
874                 .addConst ("IsInput", ARDOUR::PortFlags(IsInput))
875                 .addConst ("IsOutput", ARDOUR::PortFlags(IsOutput))
876                 .addConst ("IsPhysical", ARDOUR::PortFlags(IsPhysical))
877                 .addConst ("CanMonitor", ARDOUR::PortFlags(CanMonitor))
878                 .addConst ("IsTerminal", ARDOUR::PortFlags(IsTerminal))
879                 .endNamespace ()
880
881                 .beginNamespace ("PlaylistDisposition")
882                 .addConst ("CopyPlaylist", ARDOUR::PlaylistDisposition(CopyPlaylist))
883                 .addConst ("NewPlaylist", ARDOUR::PlaylistDisposition(NewPlaylist))
884                 .addConst ("SharePlaylist", ARDOUR::PlaylistDisposition(SharePlaylist))
885                 .endNamespace ()
886
887                 .beginNamespace ("RegionPoint")
888                 .addConst ("Start", ARDOUR::RegionPoint(Start))
889                 .addConst ("End", ARDOUR::RegionPoint(End))
890                 .addConst ("SyncPoint", ARDOUR::RegionPoint(SyncPoint))
891                 .endNamespace ()
892
893                 .beginNamespace ("TrackMode")
894                 .addConst ("Normal", ARDOUR::TrackMode(Start))
895                 .addConst ("NonLayered", ARDOUR::TrackMode(NonLayered))
896                 .addConst ("Destructive", ARDOUR::TrackMode(Destructive))
897                 .endNamespace ()
898                 .endNamespace ();
899
900         luabridge::getGlobalNamespace (L)
901                 .beginNamespace ("ARDOUR")
902                 .beginClass <AudioBackendInfo> ("AudioBackendInfo")
903                 .addData ("name", &AudioBackendInfo::name)
904                 .endClass()
905                 .beginStdVector <const AudioBackendInfo*> ("BackendVector").endClass ()
906
907                 .beginClass <AudioBackend::DeviceStatus> ("DeviceStatus")
908                 .addData ("name", &AudioBackend::DeviceStatus::name)
909                 .addData ("available", &AudioBackend::DeviceStatus::available)
910                 .endClass()
911                 .beginStdVector <AudioBackend::DeviceStatus> ("DeviceStatusVector").endClass ()
912
913                 .beginWSPtrClass <AudioBackend> ("AudioBackend")
914                 .addFunction ("info", &AudioBackend::info)
915                 .addFunction ("sample_rate", &AudioBackend::sample_rate)
916                 .addFunction ("buffer_size", &AudioBackend::buffer_size)
917                 .addFunction ("period_size", &AudioBackend::period_size)
918                 .addFunction ("input_channels", &AudioBackend::input_channels)
919                 .addFunction ("output_channels", &AudioBackend::output_channels)
920                 .addFunction ("dsp_load", &AudioBackend::dsp_load)
921
922                 .addFunction ("set_sample_rate", &AudioBackend::set_sample_rate)
923                 .addFunction ("set_buffer_size", &AudioBackend::set_buffer_size)
924                 .addFunction ("set_peridod_size", &AudioBackend::set_peridod_size)
925
926                 .addFunction ("enumerate_drivers", &AudioBackend::enumerate_drivers)
927                 .addFunction ("driver_name", &AudioBackend::driver_name)
928                 .addFunction ("set_driver", &AudioBackend::set_driver)
929
930                 .addFunction ("use_separate_input_and_output_devices", &AudioBackend::use_separate_input_and_output_devices)
931                 .addFunction ("enumerate_devices", &AudioBackend::enumerate_devices)
932                 .addFunction ("enumerate_input_devices", &AudioBackend::enumerate_input_devices)
933                 .addFunction ("enumerate_output_devices", &AudioBackend::enumerate_output_devices)
934                 .addFunction ("device_name", &AudioBackend::device_name)
935                 .addFunction ("input_device_name", &AudioBackend::input_device_name)
936                 .addFunction ("output_device_name", &AudioBackend::output_device_name)
937                 .addFunction ("set_device_name", &AudioBackend::set_device_name)
938                 .addFunction ("set_input_device_name", &AudioBackend::set_input_device_name)
939                 .addFunction ("set_output_device_name", &AudioBackend::set_output_device_name)
940                 .endClass()
941
942                 .beginClass <PortManager> ("PortManager")
943                 .addFunction ("port_engine", &PortManager::port_engine)
944                 .addFunction ("connected", &PortManager::connected)
945                 .addFunction ("connect", &PortManager::connect)
946                 .addFunction ("physically_connected", &PortManager::physically_connected)
947                 .addFunction ("disconnect", (int (PortManager::*)(const std::string&, const std::string&))&PortManager::disconnect)
948                 .addFunction ("disconnect_port", (int (PortManager::*)(boost::shared_ptr<Port>))&PortManager::disconnect)
949                 .addFunction ("get_port_by_name", &PortManager::get_port_by_name)
950                 .addFunction ("get_pretty_name_by_name", &PortManager::get_pretty_name_by_name)
951                 .addFunction ("port_is_physical", &PortManager::port_is_physical)
952                 .addFunction ("get_physical_outputs", &PortManager::get_physical_outputs)
953                 .addFunction ("get_physical_inputs", &PortManager::get_physical_inputs)
954                 .addFunction ("n_physical_outputs", &PortManager::n_physical_outputs)
955                 .addFunction ("n_physical_inputs", &PortManager::n_physical_inputs)
956                 .addRefFunction ("get_connections", &PortManager::get_connections)
957                 .addRefFunction ("get_ports", (int (PortManager::*)(DataType, PortManager::PortList&))&PortManager::get_ports)
958                 .addRefFunction ("get_backend_ports", (int (PortManager::*)(const std::string&, DataType, PortFlags, std::vector<std::string>&))&PortManager::get_ports)
959                 .endClass()
960
961                 .deriveClass <AudioEngine, PortManager> ("AudioEngine")
962                 .addFunction ("available_backends", &AudioEngine::available_backends)
963                 .addFunction ("current_backend_name", &AudioEngine::current_backend_name)
964                 .addFunction ("set_backend", &AudioEngine::set_backend)
965                 .addFunction ("setup_required", &AudioEngine::setup_required)
966                 .addFunction ("start", &AudioEngine::start)
967                 .addFunction ("stop", &AudioEngine::stop)
968                 .addFunction ("get_dsp_load", &AudioEngine::get_dsp_load)
969                 .addFunction ("set_device_name", &AudioEngine::set_device_name)
970                 .addFunction ("set_sample_rate", &AudioEngine::set_sample_rate)
971                 .addFunction ("set_buffer_size", &AudioEngine::set_buffer_size)
972                 .addFunction ("get_last_backend_error", &AudioEngine::get_last_backend_error)
973                 .endClass()
974                 .endNamespace ();
975
976         // basic representation of Session
977         // functions which can be used from realtime and non-realtime contexts
978         luabridge::getGlobalNamespace (L)
979                 .beginNamespace ("ARDOUR")
980                 .beginClass <Session> ("Session")
981                 .addFunction ("scripts_changed", &Session::scripts_changed) // used internally
982                 .addFunction ("transport_rolling", &Session::transport_rolling)
983                 .addFunction ("request_transport_speed", &Session::request_transport_speed)
984                 .addFunction ("transport_frame", &Session::transport_frame)
985                 .addFunction ("transport_speed", &Session::transport_speed)
986                 .addFunction ("frame_rate", &Session::frame_rate)
987                 .addFunction ("nominal_frame_rate", &Session::nominal_frame_rate)
988                 .addFunction ("frames_per_timecode_frame", &Session::frames_per_timecode_frame)
989                 .addFunction ("timecode_frames_per_hour", &Session::timecode_frames_per_hour)
990                 .addFunction ("timecode_frames_per_second", &Session::timecode_frames_per_second)
991                 .addFunction ("timecode_drop_frames", &Session::timecode_drop_frames)
992                 .addFunction ("request_locate", &Session::request_locate)
993                 .addFunction ("request_stop", &Session::request_stop)
994                 .addFunction ("last_transport_start", &Session::last_transport_start)
995                 .addFunction ("goto_start", &Session::goto_start)
996                 .addFunction ("goto_end", &Session::goto_end)
997                 .addFunction ("current_start_frame", &Session::current_start_frame)
998                 .addFunction ("current_end_frame", &Session::current_end_frame)
999                 .addFunction ("actively_recording", &Session::actively_recording)
1000                 .addFunction ("new_audio_track", &Session::new_audio_track)
1001                 .addFunction ("new_audio_route", &Session::new_audio_route)
1002                 .addFunction ("new_midi_track", &Session::new_midi_track)
1003                 .addFunction ("new_midi_route", &Session::new_midi_route)
1004                 .addFunction ("get_routes", &Session::get_routes)
1005                 .addFunction ("get_tracks", &Session::get_tracks)
1006                 .addFunction ("name", &Session::name)
1007                 .addFunction ("path", &Session::path)
1008                 .addFunction ("record_status", &Session::record_status)
1009                 .addFunction ("route_by_id", &Session::route_by_id)
1010                 .addFunction ("route_by_name", &Session::route_by_name)
1011                 .addFunction ("route_by_remote_id", &Session::route_by_remote_id)
1012                 .addFunction ("track_by_diskstream_id", &Session::track_by_diskstream_id)
1013                 .addFunction ("source_by_id", &Session::source_by_id)
1014                 .addFunction ("controllable_by_id", &Session::controllable_by_id)
1015                 .addFunction ("processor_by_id", &Session::processor_by_id)
1016                 .addFunction ("snap_name", &Session::snap_name)
1017                 .addFunction ("monitor_out", &Session::monitor_out)
1018                 .addFunction ("master_out", &Session::master_out)
1019                 .addFunction ("tempo_map", (TempoMap& (Session::*)())&Session::tempo_map)
1020                 .addFunction ("locations", &Session::locations)
1021                 .addFunction ("begin_reversible_command", (void (Session::*)(const std::string&))&Session::begin_reversible_command)
1022                 .addFunction ("commit_reversible_command", &Session::commit_reversible_command)
1023                 .addFunction ("abort_reversible_command", &Session::abort_reversible_command)
1024                 .addFunction ("add_command", &Session::add_command)
1025                 .addFunction ("add_stateful_diff_command", &Session::add_stateful_diff_command)
1026                 .addFunction ("engine", (AudioEngine& (Session::*)())&Session::engine)
1027                 .endClass ()
1028
1029                 .beginClass <RegionFactory> ("RegionFactory")
1030                 .addStaticFunction ("region_by_id", &RegionFactory::region_by_id)
1031                 .endClass ()
1032
1033                 /* session enums */
1034                 .beginNamespace ("Session")
1035
1036                 .beginNamespace ("RecordState")
1037                 .addConst ("Disabled", ARDOUR::Session::RecordState(Session::Disabled))
1038                 .addConst ("Enabled", ARDOUR::Session::RecordState(Session::Enabled))
1039                 .addConst ("Recording", ARDOUR::Session::RecordState(Session::Recording))
1040                 .endNamespace ()
1041
1042                 .endNamespace () // END Session enums
1043
1044                 .beginNamespace ("LuaAPI")
1045                 .addFunction ("nil_proc", ARDOUR::LuaAPI::nil_processor)
1046                 .addFunction ("new_luaproc", ARDOUR::LuaAPI::new_luaproc)
1047                 .addFunction ("new_plugin_info", ARDOUR::LuaAPI::new_plugin_info)
1048                 .addFunction ("new_plugin", ARDOUR::LuaAPI::new_plugin)
1049                 .addFunction ("set_processor_param", ARDOUR::LuaAPI::set_processor_param)
1050                 .addFunction ("set_plugin_insert_param", ARDOUR::LuaAPI::set_plugin_insert_param)
1051                 .addCFunction ("plugin_automation", ARDOUR::LuaAPI::plugin_automation)
1052                 .addFunction ("usleep", Glib::usleep)
1053                 .endNamespace ()
1054
1055                 .endNamespace ();// END ARDOUR
1056 }
1057
1058 void
1059 LuaBindings::dsp (lua_State* L)
1060 {
1061         luabridge::getGlobalNamespace (L)
1062                 .beginNamespace ("ARDOUR")
1063
1064                 .beginClass <AudioBuffer> ("AudioBuffer")
1065                 .addEqualCheck ()
1066                 .addFunction ("data", (Sample*(AudioBuffer::*)(framecnt_t))&AudioBuffer::data)
1067                 .addFunction ("silence", &AudioBuffer::silence)
1068                 .addFunction ("apply_gain", &AudioBuffer::apply_gain)
1069                 .addFunction ("check_silence", &AudioBuffer::check_silence)
1070                 .addFunction ("read_from", (void (AudioBuffer::*)(const Sample*, framecnt_t, framecnt_t, framecnt_t))&AudioBuffer::check_silence)
1071                 .endClass()
1072
1073                 .beginClass <MidiBuffer> ("MidiBuffer")
1074                 .addEqualCheck ()
1075                 .addFunction ("silence", &MidiBuffer::silence)
1076                 .addFunction ("empty", &MidiBuffer::empty)
1077                 // TODO iterators..
1078                 .endClass()
1079
1080                 .beginClass <BufferSet> ("BufferSet")
1081                 .addEqualCheck ()
1082                 .addFunction ("get_audio", static_cast<AudioBuffer&(BufferSet::*)(size_t)>(&BufferSet::get_audio))
1083                 .addFunction ("count", static_cast<const ChanCount&(BufferSet::*)()const>(&BufferSet::count))
1084                 .endClass()
1085                 .endNamespace ();
1086
1087         luabridge::getGlobalNamespace (L)
1088                 .beginNamespace ("Evoral")
1089                 .beginClass <Evoral::Event<framepos_t> > ("Event")
1090                 .addFunction ("clear", &Evoral::Event<framepos_t>::clear)
1091                 .addFunction ("size", &Evoral::Event<framepos_t>::size)
1092                 .addFunction ("set_buffer", &Evoral::Event<framepos_t>::set_buffer)
1093                 .addFunction ("buffer", (uint8_t*(Evoral::Event<framepos_t>::*)())&Evoral::Event<framepos_t>::buffer)
1094                 .endClass ()
1095
1096                 .beginClass <Evoral::Beats> ("Beats")
1097                 .addFunction ("to_double", &Evoral::Beats::to_double)
1098                 .endClass ()
1099
1100                 .deriveClass <Evoral::MIDIEvent<framepos_t>, Evoral::Event<framepos_t> > ("MidiEvent")
1101                 // add Ctor?
1102                 .addFunction ("type", &Evoral::MIDIEvent<framepos_t>::type)
1103                 .addFunction ("channel", &Evoral::MIDIEvent<framepos_t>::channel)
1104                 .addFunction ("set_type", &Evoral::MIDIEvent<framepos_t>::type)
1105                 .addFunction ("set_channel", &Evoral::MIDIEvent<framepos_t>::channel)
1106                 .endClass ()
1107                 .endNamespace ();
1108
1109         // dsp releated session functions
1110         luabridge::getGlobalNamespace (L)
1111                 .beginNamespace ("ARDOUR")
1112                 .beginClass <Session> ("Session")
1113                 .addFunction ("get_scratch_buffers", &Session::get_scratch_buffers)
1114                 .addFunction ("get_silent_buffers", &Session::get_silent_buffers)
1115                 .endClass ()
1116                 .endNamespace ();
1117
1118         luabridge::getGlobalNamespace (L)
1119                 .beginNamespace ("ARDOUR")
1120                 .beginNamespace ("DSP")
1121                 .addFunction ("compute_peak", ARDOUR::compute_peak)
1122                 .addFunction ("find_peaks", ARDOUR::find_peaks)
1123                 .addFunction ("apply_gain_to_buffer", ARDOUR::apply_gain_to_buffer)
1124                 .addFunction ("mix_buffers_no_gain", ARDOUR::mix_buffers_no_gain)
1125                 .addFunction ("mix_buffers_with_gain", ARDOUR::mix_buffers_with_gain)
1126                 .addFunction ("copy_vector", ARDOUR::copy_vector)
1127                 .addFunction ("dB_to_coefficient", &dB_to_coefficient)
1128                 .addFunction ("fast_coefficient_to_dB", &fast_coefficient_to_dB)
1129                 .addFunction ("accurate_coefficient_to_dB", &accurate_coefficient_to_dB)
1130                 .addFunction ("memset", &DSP::memset)
1131                 .addFunction ("mmult", &DSP::mmult)
1132                 .addFunction ("log_meter", &DSP::log_meter)
1133                 .addFunction ("log_meter_coeff", &DSP::log_meter_coeff)
1134                 .addRefFunction ("peaks", &DSP::peaks)
1135
1136                 .beginClass <DSP::LowPass> ("LowPass")
1137                 .addConstructor <void (*) (double, float)> ()
1138                 .addFunction ("proc", &DSP::LowPass::proc)
1139                 .addFunction ("ctrl", &DSP::LowPass::ctrl)
1140                 .addFunction ("set_cutoff", &DSP::LowPass::set_cutoff)
1141                 .addFunction ("reset", &DSP::LowPass::reset)
1142                 .endClass ()
1143                 .beginClass <DSP::BiQuad> ("Biquad")
1144                 .addConstructor <void (*) (double)> ()
1145                 .addFunction ("run", &DSP::BiQuad::run)
1146                 .addFunction ("compute", &DSP::BiQuad::compute)
1147                 .addFunction ("reset", &DSP::BiQuad::reset)
1148                 .endClass ()
1149
1150                 /* DSP enums */
1151                 .beginNamespace ("BiQuadType")
1152                 .addConst ("LowPass", ARDOUR::DSP::BiQuad::LowPass)
1153                 .addConst ("HighPass", ARDOUR::DSP::BiQuad::HighPass)
1154                 .addConst ("BandPassSkirt", ARDOUR::DSP::BiQuad::BandPassSkirt)
1155                 .addConst ("BandPass0dB", ARDOUR::DSP::BiQuad::BandPass0dB)
1156                 .addConst ("Notch", ARDOUR::DSP::BiQuad::Notch)
1157                 .addConst ("AllPass", ARDOUR::DSP::BiQuad::AllPass)
1158                 .addConst ("Peaking", ARDOUR::DSP::BiQuad::Peaking)
1159                 .addConst ("LowShelf", ARDOUR::DSP::BiQuad::LowShelf)
1160                 .addConst ("HighShelf", ARDOUR::DSP::BiQuad::HighShelf)
1161                 .endNamespace ()
1162
1163                 .beginClass <DSP::DspShm> ("DspShm")
1164                 .addFunction ("allocate", &DSP::DspShm::allocate)
1165                 .addFunction ("clear", &DSP::DspShm::clear)
1166                 .addFunction ("to_float", &DSP::DspShm::to_float)
1167                 .addFunction ("to_int", &DSP::DspShm::to_int)
1168                 .addFunction ("atomic_set_int", &DSP::DspShm::atomic_set_int)
1169                 .addFunction ("atomic_get_int", &DSP::DspShm::atomic_get_int)
1170                 .endClass ()
1171
1172                 .endNamespace () // DSP
1173                 .endNamespace (); // ARDOUR
1174 }
1175
1176 void
1177 LuaBindings::session (lua_State* L)
1178 {
1179         // non-realtime session functions
1180         luabridge::getGlobalNamespace (L)
1181                 .beginNamespace ("ARDOUR")
1182                 .beginClass <Session> ("Session")
1183                 .addFunction ("save_state", &Session::save_state)
1184                 .addFunction ("set_dirty", &Session::set_dirty)
1185                 .addFunction ("unknown_processors", &Session::unknown_processors)
1186
1187                 .addFunction<RouteList (Session::*)(uint32_t, const std::string&, const std::string&, PlaylistDisposition)> ("new_route_from_template", &Session::new_route_from_template)
1188                 // TODO  session_add_audio_track  session_add_midi_track  session_add_mixed_track
1189                 //.addFunction ("new_midi_track", &Session::new_midi_track)
1190                 .endClass ()
1191
1192                 .endNamespace (); // ARDOUR
1193 }
1194
1195 void
1196 LuaBindings::osc (lua_State* L)
1197 {
1198         luabridge::getGlobalNamespace (L)
1199                 .beginNamespace ("ARDOUR")
1200                 .beginNamespace ("LuaOSC")
1201                 .beginClass<LuaOSC::Address> ("Address")
1202                 .addConstructor<void (*) (std::string)> ()
1203                 .addCFunction ("send", &LuaOSC::Address::send)
1204                 .endClass ()
1205                 .endNamespace ()
1206                 .endNamespace ();
1207 }
1208
1209 void
1210 LuaBindings::set_session (lua_State* L, Session *s)
1211 {
1212         /* LuaBridge uses unique keys to identify classes/c-types.
1213          *
1214          * Those keys are "generated" by using the memory-address of a static
1215          * variable, templated for every Class.
1216          * (see libs/lua/LuaBridge/detail/ClassInfo.h)
1217          *
1218          * When linking the final executable there must be exactly one static
1219          * function (static variable) for every templated class.
1220          * This works fine on OSX and Linux...
1221          *
1222          * Windows (mingw and MSVC) however expand the template differently for libardour
1223          * AND gtk2_ardour. We end up with two identical static functions
1224          * at different addresses!!
1225          *
1226          * The Solution: have gtk2_ardour never include LuaBridge headers directly
1227          * and always go via libardour function calls for classes that are registered
1228          * in libardour. (calling lua itself is fine,  calling c-functions in the GUI
1229          * which expand the template is not)
1230          *
1231          * (the actual cause: even static symbols in a .dll have no fixed address
1232          * and are mapped when loading the dll. static functions in .exe do have a fixed
1233          * address)
1234          *
1235          * libardour:
1236          *  0000000000000000 I __imp__ZZN9luabridge9ClassInfoIN6ARDOUR7SessionEE11getClassKeyEvE5value
1237          *  0000000000000000 I __nm__ZZN9luabridge9ClassInfoIN6ARDOUR7SessionEE11getClassKeyEvE5value
1238          *  0000000000000000 T _ZN9luabridge9ClassInfoIN6ARDOUR7SessionEE11getClassKeyEv
1239          *
1240          * ardour.exe
1241          *  000000000104f560 d .data$_ZZN9luabridge9ClassInfoIN6ARDOUR7SessionEE11getClassKeyEvE5value
1242          *  000000000104f560 D _ZZN9luabridge9ClassInfoIN6ARDOUR7SessionEE11getClassKeyEvE5value
1243          *  0000000000e9baf0 T _ZN9luabridge9ClassInfoIN6ARDOUR7SessionEE11getClassKeyEv
1244          *
1245          *
1246          */
1247         luabridge::push <Session *> (L, s);
1248         lua_setglobal (L, "Session");
1249
1250         if (s) {
1251                 // call lua function.
1252                 luabridge::LuaRef cb_ses = luabridge::getGlobal (L, "new_session");
1253                 if (cb_ses.type() == LUA_TFUNCTION) { cb_ses(s->name()); } // TODO args
1254         }
1255 }