X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fsession.cc;h=fec12290932d8e489d0f6d4422b600ee6b4487cd;hb=6e94b1fb9f79c5bfcf22cd5b88619afa9bedb1b2;hp=c9fd336314d5a62788d99eb496c76ebe594efe2e;hpb=e9234c856a783ff3e585e1852852c72843d8ce7f;p=ardour.git diff --git a/libs/ardour/session.cc b/libs/ardour/session.cc index c9fd336314..fec1229093 100644 --- a/libs/ardour/session.cc +++ b/libs/ardour/session.cc @@ -73,6 +73,7 @@ #include "ardour/filename_extensions.h" #include "ardour/gain_control.h" #include "ardour/graph.h" +#include "ardour/luabindings.h" #include "ardour/midiport_manager.h" #include "ardour/scene_changer.h" #include "ardour/midi_patch_manager.h" @@ -106,6 +107,8 @@ #include "midi++/port.h" #include "midi++/mmc.h" +#include "LuaBridge/LuaBridge.h" + #include "i18n.h" #include @@ -122,6 +125,7 @@ using namespace PBD; bool Session::_disable_all_loaded_plugins = false; bool Session::_bypass_all_loaded_plugins = false; +guint Session::_name_id_counter = 0; PBD::Signal1 Session::AudioEngineSetupRequired; PBD::Signal1 Session::Dialog; @@ -201,8 +205,8 @@ Session::Session (AudioEngine &eng, , post_export_sync (false) , post_export_position (0) , _exporting (false) - , _export_started (false) , _export_rolling (false) + , _export_preroll (0) , _pre_export_mmc_enabled (false) , _name (snapshot_name) , _is_new (true) @@ -227,6 +231,9 @@ Session::Session (AudioEngine &eng, , pending_locate_flush (false) , pending_abort (false) , pending_auto_loop (false) + , _mempool ("Session", 1048576) + , lua (lua_newstate (&PBD::ReallocPool::lalloc, &_mempool)) + , _n_lua_scripts (0) , _butler (new Butler (*this)) , _post_transport_work (0) , cumulative_rf_motion (0) @@ -305,8 +312,12 @@ Session::Session (AudioEngine &eng, pthread_mutex_init (&_rt_emit_mutex, 0); pthread_cond_init (&_rt_emit_cond, 0); + init_name_id_counter (1); // reset for new sessions, start at 1 + pre_engine_init (fullpath); + setup_lua (); + if (_is_new) { Stateful::loading_state_version = CURRENT_SESSION_FILE_VERSION; @@ -461,6 +472,24 @@ Session::~Session () destroy (); } +unsigned int +Session::next_name_id () +{ + return g_atomic_int_add (&_name_id_counter, 1); +} + +unsigned int +Session::name_id_counter () +{ + return g_atomic_int_get (&_name_id_counter); +} + +void +Session::init_name_id_counter (guint n) +{ + g_atomic_int_set (&_name_id_counter, n); +} + int Session::ensure_engine (uint32_t desired_sample_rate) { @@ -590,8 +619,19 @@ Session::destroy () delete state_tree; state_tree = 0; - /* reset dynamic state version back to default */ + // unregister all lua functions, drop held references (if any) + (*_lua_cleanup)(); + lua.do_command ("Session = nil"); + delete _lua_run; + delete _lua_add; + delete _lua_del; + delete _lua_list; + delete _lua_save; + delete _lua_load; + delete _lua_cleanup; + lua.collect_garbage (); + /* reset dynamic state version back to default */ Stateful::loading_state_version = 0; _butler->drop_references (); @@ -2344,6 +2384,10 @@ Session::new_midi_track (const ChanCount& input, const ChanCount& output, boost: goto failed; } + if (Profile->get_mixbus ()) { + track->set_strict_io (true); + } + track->use_new_diskstream(); #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS @@ -2416,6 +2460,96 @@ Session::new_midi_track (const ChanCount& input, const ChanCount& output, boost: return ret; } +RouteList +Session::new_midi_route (RouteGroup* route_group, uint32_t how_many, string name_template, boost::shared_ptr instrument) +{ + string bus_name; + uint32_t bus_id = 0; + string port; + RouteList ret; + + bool const use_number = (how_many != 1) || name_template.empty () || name_template == _("Midi Bus"); + + while (how_many) { + if (!find_route_name (name_template.empty () ? _("Midi Bus") : name_template, ++bus_id, bus_name, use_number)) { + error << "cannot find name for new midi bus" << endmsg; + goto failure; + } + + try { + boost::shared_ptr bus (new Route (*this, bus_name, Route::Flag(0), DataType::AUDIO)); // XXX Editor::add_routes is not ready for ARDOUR::DataType::MIDI + + if (bus->init ()) { + goto failure; + } + + if (Profile->get_mixbus ()) { + bus->set_strict_io (true); + } + +#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS + // boost_debug_shared_ptr_mark_interesting (bus.get(), "Route"); +#endif + { + Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ()); + + if (bus->input()->ensure_io (ChanCount(DataType::MIDI, 1), false, this)) { + error << _("cannot configure new midi bus input") << endmsg; + goto failure; + } + + + if (bus->output()->ensure_io (ChanCount(DataType::MIDI, 1), false, this)) { + error << _("cannot configure new midi bus output") << endmsg; + goto failure; + } + } + + if (route_group) { + route_group->add (bus); + } + if (Config->get_remote_model() == UserOrdered) { + bus->set_remote_control_id (next_control_id()); + } + + ret.push_back (bus); + RouteAddedOrRemoved (true); /* EMIT SIGNAL */ + ARDOUR::GUIIdle (); + } + + catch (failed_constructor &err) { + error << _("Session: could not create new audio route.") << endmsg; + goto failure; + } + + catch (AudioEngine::PortRegistrationFailure& pfe) { + error << pfe.what() << endmsg; + goto failure; + } + + + --how_many; + } + + failure: + if (!ret.empty()) { + StateProtector sp (this); + add_routes (ret, false, false, false); + + if (instrument) { + for (RouteList::iterator r = ret.begin(); r != ret.end(); ++r) { + PluginPtr plugin = instrument->load (*this); + boost::shared_ptr p (new PluginInsert (*this, plugin)); + (*r)->add_processor (p, PreFader); + } + } + } + + return ret; + +} + + void Session::midi_output_change_handler (IOChange change, void * /*src*/, boost::weak_ptr wmt) { @@ -2883,6 +3017,11 @@ Session::new_audio_track (int input_channels, int output_channels, TrackMode mod goto failed; } + if (Profile->get_mixbus ()) { + track->set_strict_io (true); + } + + if (ARDOUR::Profile->get_trx ()) { // TRACKS considers it's not a USE CASE, it's // a piece of behavior of the session model: @@ -2992,6 +3131,10 @@ Session::new_audio_route (int input_channels, int output_channels, RouteGroup* r goto failure; } + if (Profile->get_mixbus ()) { + bus->set_strict_io (true); + } + #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS // boost_debug_shared_ptr_mark_interesting (bus.get(), "Route"); #endif @@ -3682,7 +3825,7 @@ Session::route_solo_changed (bool self_solo_change, Controllable::GroupControlDi */ RouteGroup* rg = route->route_group (); - const bool group_already_accounted_for = route->use_group (group_override, &RouteGroup::is_solo); + const bool group_already_accounted_for = (group_override == Controllable::ForGroup); if (delta == 1 && Config->get_exclusive_solo()) { @@ -4896,6 +5039,228 @@ Session::audition_playlist () queue_event (ev); } + +void +Session::register_lua_function ( + const std::string& name, + const std::string& script, + const LuaScriptParamList& args + ) +{ + Glib::Threads::Mutex::Lock lm (lua_lock); + + lua_State* L = lua.getState(); + + const std::string& bytecode = LuaScripting::get_factory_bytecode (script); + luabridge::LuaRef tbl_arg (luabridge::newTable(L)); + for (LuaScriptParamList::const_iterator i = args.begin(); i != args.end(); ++i) { + if ((*i)->optional && !(*i)->is_set) { continue; } + tbl_arg[(*i)->name] = (*i)->value; + } + (*_lua_add)(name, bytecode, tbl_arg); // throws luabridge::LuaException + set_dirty(); +} + +void +Session::unregister_lua_function (const std::string& name) +{ + Glib::Threads::Mutex::Lock lm (lua_lock); + (*_lua_del)(name); // throws luabridge::LuaException + lua.collect_garbage (); + set_dirty(); +} + +std::vector +Session::registered_lua_functions () +{ + Glib::Threads::Mutex::Lock lm (lua_lock); + std::vector rv; + + try { + luabridge::LuaRef list ((*_lua_list)()); + for (luabridge::Iterator i (list); !i.isNil (); ++i) { + if (!i.key ().isString ()) { assert(0); continue; } + rv.push_back (i.key ().cast ()); + } + } catch (luabridge::LuaException const& e) { } + return rv; +} + +#ifndef NDEBUG +static void _lua_print (std::string s) { + std::cout << "SessionLua: " << s << "\n"; +} +#endif + +void +Session::try_run_lua (pframes_t nframes) +{ + if (_n_lua_scripts == 0) return; + Glib::Threads::Mutex::Lock tm (lua_lock, Glib::Threads::TRY_LOCK); + if (tm.locked ()) { + try { (*_lua_run)(nframes); } catch (luabridge::LuaException const& e) { } + } +} + +void +Session::setup_lua () +{ +#ifndef NDEBUG + lua.Print.connect (&_lua_print); +#endif + lua.do_command ( + "function ArdourSession ()" + " local self = { scripts = {}, instances = {} }" + "" + " local remove = function (n)" + " self.scripts[n] = nil" + " self.instances[n] = nil" + " Session:scripts_changed()" // call back + " end" + "" + " local addinternal = function (n, f, a)" + " assert(type(n) == 'string', 'function-name must be string')" + " assert(type(f) == 'function', 'Given script is a not a function')" + " assert(type(a) == 'table' or type(a) == 'nil', 'Given argument is invalid')" + " assert(self.scripts[n] == nil, 'Callback \"'.. n ..'\" already exists.')" + " self.scripts[n] = { ['f'] = f, ['a'] = a }" + " local env = _ENV; env.f = nil env.io = nil env.os = nil env.loadfile = nil env.require = nil env.dofile = nil env.package = nil env.debug = nil" + " local env = { print = print, Session = Session, tostring = tostring, assert = assert, ipairs = ipairs, error = error, select = select, string = string, type = type, tonumber = tonumber, collectgarbage = collectgarbage, pairs = pairs, math = math, table = table, pcall = pcall }" + " self.instances[n] = load (string.dump(f, true), nil, nil, env)(a)" + " Session:scripts_changed()" // call back + " end" + "" + " local add = function (n, b, a)" + " assert(type(b) == 'string', 'ByteCode must be string')" + " load (b)()" // assigns f + " assert(type(f) == 'string', 'Assigned ByteCode must be string')" + " addinternal (n, load(f), a)" + " end" + "" + " local run = function (...)" + " for n, s in pairs (self.instances) do" + " local status, err = pcall (s, ...)" + " if not status then" + " print ('fn \"'.. n .. '\": ', err)" + " remove (n)" + " end" + " end" + " collectgarbage()" + " end" + "" + " local cleanup = function ()" + " self.scripts = nil" + " self.instances = nil" + " end" + "" + " local list = function ()" + " local rv = {}" + " for n, _ in pairs (self.scripts) do" + " rv[n] = true" + " end" + " return rv" + " end" + "" + " local function basic_serialize (o)" + " if type(o) == \"number\" then" + " return tostring(o)" + " else" + " return string.format(\"%q\", o)" + " end" + " end" + "" + " local function serialize (name, value)" + " local rv = name .. ' = '" + " collectgarbage()" + " if type(value) == \"number\" or type(value) == \"string\" or type(value) == \"nil\" then" + " return rv .. basic_serialize(value) .. ' '" + " elseif type(value) == \"table\" then" + " rv = rv .. '{} '" + " for k,v in pairs(value) do" + " local fieldname = string.format(\"%s[%s]\", name, basic_serialize(k))" + " rv = rv .. serialize(fieldname, v) .. ' '" + " collectgarbage()" // string concatenation allocates a new string :( + " end" + " return rv;" + " elseif type(value) == \"function\" then" + " return rv .. string.format(\"%q\", string.dump(value, true))" + " else" + " error('cannot save a ' .. type(value))" + " end" + " end" + "" + "" + " local save = function ()" + " return (serialize('scripts', self.scripts))" + " end" + "" + " local restore = function (state)" + " self.scripts = {}" + " load (state)()" + " for n, s in pairs (scripts) do" + " addinternal (n, load(s['f']), s['a'])" + " end" + " end" + "" + " return { run = run, add = add, remove = remove," + " list = list, restore = restore, save = save, cleanup = cleanup}" + " end" + " " + " sess = ArdourSession ()" + " ArdourSession = nil" + " " + "function ardour () end" + ); + + lua_State* L = lua.getState(); + + try { + luabridge::LuaRef lua_sess = luabridge::getGlobal (L, "sess"); + lua.do_command ("sess = nil"); // hide it. + lua.do_command ("collectgarbage()"); + + _lua_run = new luabridge::LuaRef(lua_sess["run"]); + _lua_add = new luabridge::LuaRef(lua_sess["add"]); + _lua_del = new luabridge::LuaRef(lua_sess["remove"]); + _lua_list = new luabridge::LuaRef(lua_sess["list"]); + _lua_save = new luabridge::LuaRef(lua_sess["save"]); + _lua_load = new luabridge::LuaRef(lua_sess["restore"]); + _lua_cleanup = new luabridge::LuaRef(lua_sess["cleanup"]); + } catch (luabridge::LuaException const& e) { + fatal << string_compose (_("programming error: %1"), + X_("Failed to setup Lua interpreter")) + << endmsg; + abort(); /*NOTREACHED*/ + } + + LuaBindings::stddef (L); + LuaBindings::common (L); + LuaBindings::dsp (L); + luabridge::push (L, this); + lua_setglobal (L, "Session"); +} + +void +Session::scripts_changed () +{ + assert (!lua_lock.trylock()); // must hold lua_lock + + try { + luabridge::LuaRef list ((*_lua_list)()); + int cnt = 0; + for (luabridge::Iterator i (list); !i.isNil (); ++i) { + if (!i.key ().isString ()) { assert(0); continue; } + ++cnt; + } + _n_lua_scripts = cnt; + } catch (luabridge::LuaException const& e) { + fatal << string_compose (_("programming error: %1"), + X_("Indexing Lua Session Scripts failed.")) + << endmsg; + abort(); /*NOTREACHED*/ + } +} + void Session::non_realtime_set_audition () { @@ -5261,6 +5626,7 @@ Session::unmark_aux_send_id (uint32_t id) void Session::unmark_return_id (uint32_t id) { + if (_state_of_the_state & Deletion) { return; } if (id < return_bitset.size()) { return_bitset[id] = false; } @@ -5570,6 +5936,7 @@ Session::write_one_track (Track& track, framepos_t start, framepos_t end, } unblock_processing (); + itt.done = true; return result; } @@ -5610,6 +5977,12 @@ Session::get_scratch_buffers (ChanCount count, bool silence) return ProcessThread::get_scratch_buffers (count, silence); } +BufferSet& +Session::get_noinplace_buffers (ChanCount count) +{ + return ProcessThread::get_noinplace_buffers (count); +} + BufferSet& Session::get_route_buffers (ChanCount count, bool silence) {