2016-01-30 11:26:14 -05:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <iostream>
|
|
|
|
#include <string>
|
|
|
|
#include <list>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <glibmm.h>
|
|
|
|
|
|
|
|
#include "pbd/debug.h"
|
|
|
|
#include "pbd/event_loop.h"
|
|
|
|
#include "pbd/error.h"
|
|
|
|
#include "pbd/failed_constructor.h"
|
|
|
|
#include "pbd/pthread_utils.h"
|
|
|
|
#include "pbd/reallocpool.h"
|
|
|
|
#include "pbd/receiver.h"
|
|
|
|
#include "pbd/transmitter.h"
|
|
|
|
|
|
|
|
#include "ardour/ardour.h"
|
|
|
|
#include "ardour/audioengine.h"
|
|
|
|
#include "ardour/filename_extensions.h"
|
|
|
|
#include "ardour/filesystem_paths.h"
|
|
|
|
#include "ardour/luabindings.h"
|
|
|
|
#include "ardour/session.h"
|
|
|
|
#include "ardour/types.h"
|
|
|
|
#include "ardour/vst_types.h"
|
|
|
|
|
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
|
|
|
|
|
|
|
#include "lua/luastate.h"
|
|
|
|
#include "LuaBridge/LuaBridge.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace ARDOUR;
|
|
|
|
using namespace PBD;
|
|
|
|
|
|
|
|
static const char* localedir = LOCALEDIR;
|
|
|
|
static PBD::ScopedConnectionList engine_connections;
|
|
|
|
static PBD::ScopedConnectionList session_connections;
|
2016-04-28 15:38:09 -04:00
|
|
|
static Session* session = NULL;
|
|
|
|
static LuaState* lua;
|
|
|
|
static bool keep_running = true;
|
|
|
|
|
|
|
|
/* extern VST functions */
|
|
|
|
int vstfx_init (void*) { return 0; }
|
|
|
|
void vstfx_exit () {}
|
|
|
|
void vstfx_destroy_editor (VSTState*) {}
|
2016-01-30 11:26:14 -05:00
|
|
|
|
|
|
|
class LuaReceiver : public Receiver
|
|
|
|
{
|
|
|
|
protected:
|
|
|
|
void receive (Transmitter::Channel chn, const char * str)
|
|
|
|
{
|
|
|
|
const char *prefix = "";
|
|
|
|
|
|
|
|
switch (chn) {
|
|
|
|
case Transmitter::Error:
|
|
|
|
prefix = "[ERROR]: ";
|
|
|
|
break;
|
|
|
|
case Transmitter::Info:
|
|
|
|
/* ignore */
|
|
|
|
return;
|
|
|
|
case Transmitter::Warning:
|
|
|
|
prefix = "[WARNING]: ";
|
|
|
|
break;
|
|
|
|
case Transmitter::Fatal:
|
|
|
|
prefix = "[FATAL]: ";
|
|
|
|
break;
|
|
|
|
case Transmitter::Throw:
|
|
|
|
/* this isn't supposed to happen */
|
|
|
|
abort ();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* note: iostreams are already thread-safe: no external
|
|
|
|
lock required.
|
|
|
|
*/
|
|
|
|
|
|
|
|
std::cout << prefix << str << std::endl;
|
|
|
|
|
|
|
|
if (chn == Transmitter::Fatal) {
|
|
|
|
::exit (9);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MyEventLoop : public sigc::trackable, public EventLoop
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MyEventLoop (std::string const& name) : EventLoop (name) {
|
|
|
|
run_loop_thread = Glib::Threads::Thread::self ();
|
|
|
|
}
|
|
|
|
|
|
|
|
void call_slot (InvalidationRecord* ir, const boost::function<void()>& f) {
|
|
|
|
if (Glib::Threads::Thread::self () == run_loop_thread) {
|
2016-04-28 15:38:09 -04:00
|
|
|
cout << string_compose ("%1/%2 direct dispatch of call slot via functor @ %3, invalidation %4\n", event_loop_name(), pthread_name(), &f, ir);
|
2016-01-30 11:26:14 -05:00
|
|
|
f ();
|
|
|
|
} else {
|
2016-04-28 15:38:09 -04:00
|
|
|
cout << string_compose ("%1/%2 queue call-slot using functor @ %3, invalidation %4\n", event_loop_name(), pthread_name(), &f, ir);
|
2016-01-30 11:26:14 -05:00
|
|
|
assert (!ir);
|
|
|
|
f (); // XXX TODO, queue and process during run ()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void run () {
|
|
|
|
; // TODO process Events, if any
|
|
|
|
}
|
|
|
|
|
|
|
|
Glib::Threads::Mutex& slot_invalidation_mutex () { return request_buffer_map_lock; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Glib::Threads::Thread* run_loop_thread;
|
|
|
|
Glib::Threads::Mutex request_buffer_map_lock;
|
|
|
|
};
|
|
|
|
|
2016-04-28 15:38:09 -04:00
|
|
|
static MyEventLoop *event_loop = NULL;
|
|
|
|
|
|
|
|
/* ****************************************************************************/
|
|
|
|
/* internal helper fn and callbacks */
|
|
|
|
|
2016-01-30 11:26:14 -05:00
|
|
|
static void init ()
|
|
|
|
{
|
|
|
|
if (!ARDOUR::init (false, true, localedir)) {
|
|
|
|
cerr << "Ardour failed to initialize\n" << endl;
|
|
|
|
::exit (EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert (!event_loop);
|
|
|
|
event_loop = new MyEventLoop ("lua");
|
|
|
|
EventLoop::set_event_loop_for_thread (event_loop);
|
2016-04-23 18:25:34 -04:00
|
|
|
SessionEvent::create_per_thread_pool ("lua", 4096);
|
2016-01-30 11:26:14 -05:00
|
|
|
|
|
|
|
static LuaReceiver lua_receiver;
|
|
|
|
|
|
|
|
lua_receiver.listen_to (error);
|
|
|
|
lua_receiver.listen_to (info);
|
|
|
|
lua_receiver.listen_to (fatal);
|
|
|
|
lua_receiver.listen_to (warning);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_session (ARDOUR::Session *s)
|
|
|
|
{
|
2016-04-28 15:38:09 -04:00
|
|
|
session = s;
|
2016-01-30 11:26:14 -05:00
|
|
|
assert (lua);
|
|
|
|
lua_State* L = lua->getState ();
|
2016-04-28 15:38:09 -04:00
|
|
|
LuaBindings::set_session (L, session);
|
2016-01-30 11:26:14 -05:00
|
|
|
lua->collect_garbage (); // drop references
|
|
|
|
}
|
|
|
|
|
|
|
|
static void unset_session ()
|
|
|
|
{
|
|
|
|
session_connections.drop_connections ();
|
|
|
|
set_session (NULL);
|
|
|
|
}
|
|
|
|
|
2016-04-28 15:38:09 -04:00
|
|
|
static int prepare_engine ()
|
2016-04-23 18:25:34 -04:00
|
|
|
{
|
|
|
|
AudioEngine* engine = AudioEngine::instance ();
|
|
|
|
|
|
|
|
if (!engine->current_backend ()) {
|
|
|
|
if (!engine->set_backend ("None (Dummy)", "Unit-Test", "")) {
|
|
|
|
std::cerr << "Cannot create Audio/MIDI engine\n";
|
2016-04-28 15:38:09 -04:00
|
|
|
return -1;
|
2016-04-23 18:25:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!engine->current_backend ()) {
|
|
|
|
std::cerr << "Cannot create Audio/MIDI engine\n";
|
2016-04-28 15:38:09 -04:00
|
|
|
return -1;
|
2016-04-23 18:25:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (engine->running ()) {
|
|
|
|
engine->stop ();
|
|
|
|
}
|
2016-04-28 15:38:09 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int start_engine (uint32_t rate)
|
|
|
|
{
|
|
|
|
AudioEngine* engine = AudioEngine::instance ();
|
|
|
|
|
|
|
|
if (engine->set_sample_rate (rate)) {
|
|
|
|
std::cerr << "Cannot set session's samplerate.\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (engine->start () != 0) {
|
|
|
|
std::cerr << "Cannot start Audio/MIDI engine\n";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Session * _create_session (string dir, string state, uint32_t rate) // throws
|
|
|
|
{
|
|
|
|
if (prepare_engine ()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-23 18:25:34 -04:00
|
|
|
|
|
|
|
std::string s = Glib::build_filename (dir, state + statefile_suffix);
|
|
|
|
if (Glib::file_test (dir, Glib::FILE_TEST_EXISTS)) {
|
|
|
|
std::cerr << "Session already exists: " << s << "\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-28 15:38:09 -04:00
|
|
|
if (start_engine (rate)) {
|
2016-04-23 18:25:34 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-24 08:39:58 -04:00
|
|
|
// TODO add option/bindings for this
|
|
|
|
BusProfile bus_profile;
|
|
|
|
bus_profile.master_out_channels = 2;
|
|
|
|
|
2016-04-28 15:38:09 -04:00
|
|
|
AudioEngine* engine = AudioEngine::instance ();
|
2016-04-24 08:39:58 -04:00
|
|
|
Session* session = new Session (*engine, dir, state, &bus_profile);
|
2016-04-23 18:25:34 -04:00
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
2016-04-28 15:38:09 -04:00
|
|
|
static Session * _load_session (string dir, string state) // throws
|
2016-01-30 11:26:14 -05:00
|
|
|
{
|
2016-04-28 15:38:09 -04:00
|
|
|
if (prepare_engine ()) {
|
2016-01-30 11:26:14 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float sr;
|
|
|
|
SampleFormat sf;
|
2017-01-20 12:16:32 -05:00
|
|
|
std::string v;
|
2016-01-30 11:26:14 -05:00
|
|
|
std::string s = Glib::build_filename (dir, state + statefile_suffix);
|
|
|
|
if (!Glib::file_test (dir, Glib::FILE_TEST_EXISTS)) {
|
|
|
|
std::cerr << "Cannot find session: " << s << "\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-01-20 12:16:32 -05:00
|
|
|
if (Session::get_info_from_path (s, sr, sf, v) != 0) {
|
2016-01-30 11:26:14 -05:00
|
|
|
std::cerr << "Cannot get samplerate from session.\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-28 15:38:09 -04:00
|
|
|
if (start_engine (sr)) {
|
2016-01-30 11:26:14 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-28 15:38:09 -04:00
|
|
|
AudioEngine* engine = AudioEngine::instance ();
|
2016-01-30 11:26:14 -05:00
|
|
|
Session* session = new Session (*engine, dir, state);
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
2016-04-28 15:38:09 -04:00
|
|
|
/* ****************************************************************************/
|
|
|
|
/* lua bound functions */
|
|
|
|
|
2016-04-23 18:25:34 -04:00
|
|
|
static Session* create_session (string dir, string state, uint32_t rate)
|
|
|
|
{
|
|
|
|
Session* s = 0;
|
2016-04-28 15:38:09 -04:00
|
|
|
if (session) {
|
2016-04-23 18:25:34 -04:00
|
|
|
cerr << "Session already open" << "\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
s = _create_session (dir, state, rate);
|
|
|
|
} catch (failed_constructor& e) {
|
|
|
|
cerr << "failed_constructor: " << e.what () << "\n";
|
|
|
|
return 0;
|
|
|
|
} catch (AudioEngine::PortRegistrationFailure& e) {
|
|
|
|
cerr << "PortRegistrationFailure: " << e.what () << "\n";
|
|
|
|
return 0;
|
|
|
|
} catch (exception& e) {
|
|
|
|
cerr << "exception: " << e.what () << "\n";
|
|
|
|
return 0;
|
|
|
|
} catch (...) {
|
|
|
|
cerr << "unknown exception.\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
Glib::usleep (1000000); // allow signal propagation, callback/thread-pool setup
|
|
|
|
if (!s) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
set_session (s);
|
|
|
|
s->DropReferences.connect_same_thread (session_connections, &unset_session);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-01-30 11:26:14 -05:00
|
|
|
static Session* load_session (string dir, string state)
|
|
|
|
{
|
|
|
|
Session* s = 0;
|
2016-04-28 15:38:09 -04:00
|
|
|
if (session) {
|
2016-01-30 11:26:14 -05:00
|
|
|
cerr << "Session already open" << "\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
s = _load_session (dir, state);
|
|
|
|
} catch (failed_constructor& e) {
|
|
|
|
cerr << "failed_constructor: " << e.what () << "\n";
|
|
|
|
return 0;
|
|
|
|
} catch (AudioEngine::PortRegistrationFailure& e) {
|
|
|
|
cerr << "PortRegistrationFailure: " << e.what () << "\n";
|
|
|
|
return 0;
|
|
|
|
} catch (exception& e) {
|
|
|
|
cerr << "exception: " << e.what () << "\n";
|
|
|
|
return 0;
|
|
|
|
} catch (...) {
|
|
|
|
cerr << "unknown exception.\n";
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-23 18:25:34 -04:00
|
|
|
Glib::usleep (1000000); // allow signal propagation, callback/thread-pool setup
|
|
|
|
if (!s) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-01-30 11:26:14 -05:00
|
|
|
set_session (s);
|
|
|
|
s->DropReferences.connect_same_thread (session_connections, &unset_session);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-04-28 15:38:09 -04:00
|
|
|
static int set_debug_options (const char *opts)
|
|
|
|
{
|
|
|
|
return PBD::parse_debug_options (opts);
|
|
|
|
}
|
|
|
|
|
2016-01-30 11:26:14 -05:00
|
|
|
static void close_session ()
|
|
|
|
{
|
2016-04-28 15:38:09 -04:00
|
|
|
delete session;
|
|
|
|
assert (!session);
|
2016-01-30 11:26:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int close_session_lua (lua_State *L)
|
|
|
|
{
|
2016-04-28 15:38:09 -04:00
|
|
|
if (!session) {
|
2016-01-30 11:26:14 -05:00
|
|
|
cerr << "No open session" << "\n";
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
close_session ();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void delay (float d) {
|
|
|
|
if (d > 0) {
|
|
|
|
Glib::usleep (d * 1000000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 15:38:09 -04:00
|
|
|
static int do_quit (lua_State *L)
|
|
|
|
{
|
|
|
|
keep_running = false;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ****************************************************************************/
|
|
|
|
|
|
|
|
static void my_lua_print (std::string s) {
|
|
|
|
std::cout << s << "\n";
|
|
|
|
}
|
|
|
|
|
2016-01-30 11:26:14 -05:00
|
|
|
static void setup_lua ()
|
|
|
|
{
|
|
|
|
assert (!lua);
|
|
|
|
|
|
|
|
lua = new LuaState ();
|
|
|
|
lua->Print.connect (&my_lua_print);
|
|
|
|
lua_State* L = lua->getState ();
|
|
|
|
|
|
|
|
LuaBindings::stddef (L);
|
|
|
|
LuaBindings::common (L);
|
|
|
|
LuaBindings::session (L);
|
2016-03-19 11:55:51 -04:00
|
|
|
LuaBindings::osc (L);
|
2016-01-30 11:26:14 -05:00
|
|
|
|
|
|
|
luabridge::getGlobalNamespace (L)
|
|
|
|
.beginNamespace ("_G")
|
2016-04-23 18:25:34 -04:00
|
|
|
.addFunction ("create_session", &create_session)
|
2016-01-30 11:26:14 -05:00
|
|
|
.addFunction ("load_session", &load_session)
|
|
|
|
.addFunction ("close_session", &close_session)
|
|
|
|
.addFunction ("sleep", &delay)
|
2016-04-28 15:38:09 -04:00
|
|
|
.addFunction ("quit", &do_quit)
|
|
|
|
.addFunction ("set_debug_options", &set_debug_options)
|
2016-01-30 11:26:14 -05:00
|
|
|
.endNamespace ();
|
|
|
|
|
2016-04-28 15:38:09 -04:00
|
|
|
// add a Session::close() method
|
2016-01-30 11:26:14 -05:00
|
|
|
luabridge::getGlobalNamespace (L)
|
|
|
|
.beginNamespace ("ARDOUR")
|
|
|
|
.beginClass <Session> ("Session")
|
|
|
|
.addExtCFunction ("close", &close_session_lua)
|
|
|
|
.endClass ()
|
|
|
|
.endNamespace ();
|
|
|
|
|
|
|
|
// push instance to global namespace (C++ lifetime)
|
|
|
|
luabridge::push <AudioEngine *> (L, AudioEngine::create ());
|
|
|
|
lua_setglobal (L, "AudioEngine");
|
2016-04-28 15:38:09 -04:00
|
|
|
|
|
|
|
AudioEngine::instance ()->stop ();
|
2016-01-30 11:26:14 -05:00
|
|
|
}
|
|
|
|
|
2018-11-12 17:14:48 -05:00
|
|
|
static int
|
|
|
|
incomplete (lua_State* L, int status) {
|
|
|
|
if (status == LUA_ERRSYNTAX) {
|
|
|
|
size_t lmsg;
|
|
|
|
const char *msg = lua_tolstring (L, -1, &lmsg);
|
|
|
|
if (lmsg >= 5 && strcmp(msg + lmsg - 5, "<eof>") == 0) {
|
|
|
|
lua_pop(L, 1);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-30 11:26:14 -05:00
|
|
|
int main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
init ();
|
|
|
|
setup_lua ();
|
|
|
|
|
|
|
|
using_history ();
|
|
|
|
std::string histfile = Glib::build_filename (user_config_directory(), "/luahist");
|
|
|
|
|
2018-11-12 17:14:48 -05:00
|
|
|
rl_bind_key ('\t', rl_insert); // disable completion
|
2016-01-30 11:26:14 -05:00
|
|
|
read_history (histfile.c_str());
|
|
|
|
|
2016-04-28 15:38:09 -04:00
|
|
|
char *line = NULL;
|
|
|
|
while (keep_running && (line = readline ("> "))) {
|
2016-01-30 11:26:14 -05:00
|
|
|
event_loop->run();
|
|
|
|
if (!strcmp (line, "quit")) {
|
2016-04-28 15:38:09 -04:00
|
|
|
free (line); line = NULL;
|
2016-01-30 11:26:14 -05:00
|
|
|
break;
|
|
|
|
}
|
2016-04-28 15:38:09 -04:00
|
|
|
|
2016-01-30 11:26:14 -05:00
|
|
|
if (strlen (line) == 0) {
|
2016-04-28 15:38:09 -04:00
|
|
|
free (line); line = NULL;
|
2016-01-30 11:26:14 -05:00
|
|
|
continue;
|
|
|
|
}
|
2016-04-28 15:38:09 -04:00
|
|
|
|
2018-11-12 17:14:48 -05:00
|
|
|
do {
|
|
|
|
LuaState lt;
|
|
|
|
lua_State* L = lt.getState ();
|
|
|
|
int status = luaL_loadbuffer (L, line, strlen(line), "=stdin");
|
|
|
|
if (!incomplete (L, status)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
char *l2 = readline (">> ");
|
|
|
|
if (!l2) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (strlen (l2) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
line = (char*) realloc ((void*)line, (strlen(line) + strlen (l2) + 2) * sizeof(char));
|
|
|
|
strcat (line, "\n");
|
|
|
|
strcat (line, l2);
|
|
|
|
free (l2);
|
|
|
|
} while (1);
|
|
|
|
|
2016-04-28 15:38:09 -04:00
|
|
|
if (lua->do_command (line)) {
|
2018-11-12 17:14:48 -05:00
|
|
|
/* error */
|
|
|
|
free (line); line = NULL;
|
|
|
|
continue;
|
2016-01-30 11:26:14 -05:00
|
|
|
}
|
2016-04-28 15:38:09 -04:00
|
|
|
|
|
|
|
add_history (line);
|
2016-01-30 11:26:14 -05:00
|
|
|
event_loop->run();
|
2016-04-28 15:38:09 -04:00
|
|
|
free (line); line = NULL;
|
2016-01-30 11:26:14 -05:00
|
|
|
}
|
2016-04-24 08:41:07 -04:00
|
|
|
free (line);
|
2016-01-30 11:26:14 -05:00
|
|
|
printf ("\n");
|
|
|
|
|
2016-04-28 15:38:09 -04:00
|
|
|
if (session) {
|
2016-01-30 11:26:14 -05:00
|
|
|
close_session ();
|
|
|
|
}
|
|
|
|
|
|
|
|
engine_connections.drop_connections ();
|
|
|
|
|
|
|
|
delete lua;
|
|
|
|
lua = NULL;
|
|
|
|
|
|
|
|
write_history (histfile.c_str());
|
|
|
|
|
|
|
|
AudioEngine::instance ()->stop ();
|
|
|
|
AudioEngine::destroy ();
|
|
|
|
|
|
|
|
// cleanup
|
|
|
|
ARDOUR::cleanup ();
|
|
|
|
delete event_loop;
|
|
|
|
pthread_cancel_all ();
|
|
|
|
return 0;
|
|
|
|
}
|