X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fsession.cc;h=4097335f5b811138e035e9f654f89cdc4523fec3;hb=7fc3b0c34c552d7be862897bd0aaa542453e9973;hp=d668fb46e768cf32707ddde67be8785a0c9fb72b;hpb=e7181c0cc49094d56eb1b9c320a1dc3bb34e50cd;p=ardour.git diff --git a/libs/ardour/session.cc b/libs/ardour/session.cc index d668fb46e7..4097335f5b 100644 --- a/libs/ardour/session.cc +++ b/libs/ardour/session.cc @@ -36,12 +36,12 @@ #include #include "pbd/basename.h" -#include "pbd/boost_debug.h" #include "pbd/convert.h" #include "pbd/convert.h" #include "pbd/error.h" #include "pbd/file_utils.h" #include "pbd/md5.h" +#include "pbd/pthread_utils.h" #include "pbd/search_path.h" #include "pbd/stacktrace.h" #include "pbd/stl_delete.h" @@ -58,6 +58,7 @@ #include "ardour/audioengine.h" #include "ardour/audiofilesource.h" #include "ardour/auditioner.h" +#include "ardour/boost_debug.h" #include "ardour/buffer_manager.h" #include "ardour/buffer_set.h" #include "ardour/bundle.h" @@ -100,6 +101,7 @@ #include "ardour/source_factory.h" #include "ardour/speakers.h" #include "ardour/tempo.h" +#include "ardour/ticker.h" #include "ardour/track.h" #include "ardour/user_bundle.h" #include "ardour/utils.h" @@ -125,11 +127,13 @@ 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; PBD::Signal0 Session::AskAboutPendingState; PBD::Signal2 Session::AskAboutSampleRateMismatch; +PBD::Signal2 Session::NotifyAboutSampleRateMismatch; PBD::Signal0 Session::SendFeedback; PBD::Signal3 Session::MissingFile; @@ -165,8 +169,8 @@ Session::Session (AudioEngine &eng, , _bounce_processing_active (false) , waiting_for_sync_offset (false) , _base_frame_rate (0) - , _current_frame_rate (0) , _nominal_frame_rate (0) + , _current_frame_rate (0) , transport_sub_state (0) , _record_status (Disabled) , _transport_frame (0) @@ -241,6 +245,7 @@ Session::Session (AudioEngine &eng, , _ignore_skips_updates (false) , _rt_thread_active (false) , _rt_emit_pending (false) + , _ac_thread_active (false) , step_speed (0) , outbound_mtc_timecode_frame (0) , next_quarter_frame_to_send (-1) @@ -302,6 +307,7 @@ Session::Session (AudioEngine &eng, , _speakers (new Speakers) , _order_hint (-1) , ignore_route_processor_changes (false) + , midi_clock (0) , _scene_changer (0) , _midi_ports (0) , _mmc (0) @@ -311,6 +317,11 @@ Session::Session (AudioEngine &eng, pthread_mutex_init (&_rt_emit_mutex, 0); pthread_cond_init (&_rt_emit_cond, 0); + pthread_mutex_init (&_auto_connect_mutex, 0); + pthread_cond_init (&_auto_connect_cond, 0); + + init_name_id_counter (1); // reset for new sessions, start at 1 + pre_engine_init (fullpath); setup_lua (); @@ -322,7 +333,7 @@ Session::Session (AudioEngine &eng, #ifdef USE_TRACKS_CODE_FEATURES sr = EngineStateController::instance()->get_current_sample_rate(); #endif - if (ensure_engine (sr)) { + if (ensure_engine (sr, true)) { destroy (); throw SessionException (_("Cannot connect to audio/midi engine")); } @@ -370,13 +381,14 @@ Session::Session (AudioEngine &eng, */ if (state_tree) { - const XMLProperty* prop; - if ((prop = state_tree->root()->property (X_("sample-rate"))) != 0) { + XMLProperty const * prop; + XMLNode const * root (state_tree->root()); + if ((prop = root->property (X_("sample-rate"))) != 0) { sr = atoi (prop->value()); } } - if (ensure_engine (sr)) { + if (ensure_engine (sr, false)) { destroy (); throw SessionException (_("Cannot connect to audio/midi engine")); } @@ -404,6 +416,7 @@ Session::Session (AudioEngine &eng, EndTimeChanged.connect_same_thread (*this, boost::bind (&Session::end_time_changed, this, _1)); emit_thread_start (); + auto_connect_thread_start (); /* hook us up to the engine since we are now completely constructed */ @@ -469,8 +482,26 @@ 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) +Session::ensure_engine (uint32_t desired_sample_rate, bool isnew) { if (_engine.current_backend() == 0) { /* backend is unknown ... */ @@ -478,6 +509,8 @@ Session::ensure_engine (uint32_t desired_sample_rate) if (r.get_value_or (-1) != 0) { return -1; } + } else if (!isnew && _engine.running() && _engine.sample_rate () == desired_sample_rate) { + /* keep engine */ } else if (_engine.setup_required()) { /* backend is known, but setup is needed */ boost::optional r = AudioEngineSetupRequired (desired_sample_rate); @@ -490,8 +523,7 @@ Session::ensure_engine (uint32_t desired_sample_rate) } } - /* at this point the engine should be running - */ + /* at this point the engine should be running */ if (!_engine.running()) { return -1; @@ -529,6 +561,7 @@ Session::immediately_post_engine () } try { + LocaleGuard lg; BootMessage (_("Set up LTC")); setup_ltc (); BootMessage (_("Set up Click")); @@ -563,8 +596,12 @@ Session::destroy () _state_of_the_state = StateOfTheState (CannotSave|Deletion); + /* stop autoconnecting */ + auto_connect_thread_terminate (); + /* disconnect from any and all signals that we are connected to */ + Port::PortSignalDrop (); /* EMIT SIGNAL */ drop_connections (); /* shutdown control surface protocols while we still have ports @@ -697,6 +734,9 @@ Session::destroy () pthread_cond_destroy (&_rt_emit_cond); pthread_mutex_destroy (&_rt_emit_mutex); + pthread_cond_destroy (&_auto_connect_cond); + pthread_mutex_destroy (&_auto_connect_mutex); + delete _scene_changer; _scene_changer = 0; delete midi_control_ui; midi_control_ui = 0; @@ -704,13 +744,48 @@ Session::destroy () delete _midi_ports; _midi_ports = 0; delete _locations; _locations = 0; + delete midi_clock; delete _tempo_map; + /* clear event queue, the session is gone, nobody is interested in + * those anymore, but they do leak memory if not removed + */ + while (!immediate_events.empty ()) { + Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ()); + SessionEvent *ev = immediate_events.front (); + DEBUG_TRACE (DEBUG::SessionEvents, string_compose ("Drop event: %1\n", enum_2_string (ev->type))); + immediate_events.pop_front (); + bool remove = true; + bool del = true; + switch (ev->type) { + case SessionEvent::AutoLoop: + case SessionEvent::AutoLoopDeclick: + case SessionEvent::Skip: + case SessionEvent::PunchIn: + case SessionEvent::PunchOut: + case SessionEvent::StopOnce: + case SessionEvent::RangeStop: + case SessionEvent::RangeLocate: + remove = false; + del = false; + break; + case SessionEvent::RealTimeOperation: + process_rtop (ev); + del = false; + default: + break; + } + if (remove) { + del = del && !_remove_event (ev); + } + if (del) { + delete ev; + } + } + DEBUG_TRACE (DEBUG::Destruction, "Session::destroy() done\n"); -#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS - boost_debug_list_ptrs (); -#endif + BOOST_SHOW_POINTERS (); } void @@ -1042,11 +1117,16 @@ Session::remove_monitor_section () } remove_route (_monitor_out); + if (_state_of_the_state & Deletion) { + return; + } + auto_connect_master_bus (); if (auditioner) { auditioner->connect (); } + Config->ParameterChanged ("use-monitor-bus"); } @@ -1065,9 +1145,8 @@ Session::add_monitor_section () return; } -#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS - // boost_debug_shared_ptr_mark_interesting (r.get(), "Route"); -#endif + BOOST_MARK_ROUTE(r); + try { Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ()); r->input()->ensure_io (_master_out->output()->n_ports(), false, this); @@ -1118,7 +1197,7 @@ Session::add_monitor_section () /* if monitor section is not connected, connect it to physical outs */ - if (Config->get_auto_connect_standard_busses() && !_monitor_out->output()->connected ()) { + if ((Config->get_auto_connect_standard_busses () || Profile->get_mixbus ()) && !_monitor_out->output()->connected ()) { if (!Config->get_monitor_bus_preferred_bundle().empty()) { @@ -2003,7 +2082,12 @@ Session::set_frame_rate (framecnt_t frames_per_second) here. */ - _base_frame_rate = frames_per_second; + if (_base_frame_rate == 0) { + _base_frame_rate = frames_per_second; + } + else if (_base_frame_rate != frames_per_second && frames_per_second != _nominal_frame_rate) { + NotifyAboutSampleRateMismatch (_base_frame_rate, frames_per_second); + } _nominal_frame_rate = frames_per_second; sync_time_vars(); @@ -2126,16 +2210,18 @@ Session::resort_routes () } #ifndef NDEBUG - boost::shared_ptr rl = routes.reader (); - for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) { - DEBUG_TRACE (DEBUG::Graph, string_compose ("%1 fed by ...\n", (*i)->name())); + if (DEBUG_ENABLED(DEBUG::Graph)) { + boost::shared_ptr rl = routes.reader (); + for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) { + DEBUG_TRACE (DEBUG::Graph, string_compose ("%1 fed by ...\n", (*i)->name())); - const Route::FedBy& fb ((*i)->fed_by()); + const Route::FedBy& fb ((*i)->fed_by()); - for (Route::FedBy::const_iterator f = fb.begin(); f != fb.end(); ++f) { - boost::shared_ptr sf = f->r.lock(); - if (sf) { - DEBUG_TRACE (DEBUG::Graph, string_compose ("\t%1 (sends only ? %2)\n", sf->name(), f->sends_only)); + for (Route::FedBy::const_iterator f = fb.begin(); f != fb.end(); ++f) { + boost::shared_ptr sf = f->r.lock(); + if (sf) { + DEBUG_TRACE (DEBUG::Graph, string_compose ("\t%1 (sends only ? %2)\n", sf->name(), f->sends_only)); + } } } } @@ -2337,7 +2423,7 @@ Session::default_track_name_pattern (DataType t) */ list > Session::new_midi_track (const ChanCount& input, const ChanCount& output, boost::shared_ptr instrument, - TrackMode mode, RouteGroup* route_group, uint32_t how_many, string name_template) + TrackMode mode, RouteGroup* route_group, uint32_t how_many, string name_template, Plugin::PresetRecord* pset) { string track_name; uint32_t track_id = 0; @@ -2363,11 +2449,14 @@ 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 - // boost_debug_shared_ptr_mark_interesting (track.get(), "Track"); -#endif + BOOST_MARK_TRACK (track); + { Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ()); if (track->input()->ensure_io (input, false, this)) { @@ -2425,6 +2514,9 @@ Session::new_midi_track (const ChanCount& input, const ChanCount& output, boost: if (instrument) { for (RouteList::iterator r = new_routes.begin(); r != new_routes.end(); ++r) { PluginPtr plugin = instrument->load (*this); + if (pset) { + plugin->load_preset (*pset); + } boost::shared_ptr p (new PluginInsert (*this, plugin)); (*r)->add_processor (p, PreFader); @@ -2436,7 +2528,7 @@ Session::new_midi_track (const ChanCount& input, const ChanCount& output, boost: } RouteList -Session::new_midi_route (RouteGroup* route_group, uint32_t how_many, string name_template, boost::shared_ptr instrument) +Session::new_midi_route (RouteGroup* route_group, uint32_t how_many, string name_template, boost::shared_ptr instrument, Plugin::PresetRecord* pset) { string bus_name; uint32_t bus_id = 0; @@ -2458,9 +2550,12 @@ Session::new_midi_route (RouteGroup* route_group, uint32_t how_many, string name goto failure; } -#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS - // boost_debug_shared_ptr_mark_interesting (bus.get(), "Route"); -#endif + if (Profile->get_mixbus ()) { + bus->set_strict_io (true); + } + + BOOST_MARK_ROUTE(bus); + { Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ()); @@ -2510,6 +2605,9 @@ Session::new_midi_route (RouteGroup* route_group, uint32_t how_many, string name if (instrument) { for (RouteList::iterator r = ret.begin(); r != ret.end(); ++r) { PluginPtr plugin = instrument->load (*this); + if (pset) { + plugin->load_preset (*pset); + } boost::shared_ptr p (new PluginInsert (*this, plugin)); (*r)->add_processor (p, PreFader); } @@ -2524,151 +2622,26 @@ Session::new_midi_route (RouteGroup* route_group, uint32_t how_many, string name void Session::midi_output_change_handler (IOChange change, void * /*src*/, boost::weak_ptr wmt) { - boost::shared_ptr midi_track (wmt.lock()); - - if (!midi_track) { - return; - } - - if ((change.type & IOChange::ConfigurationChanged) && Config->get_output_auto_connect() != ManualConnect) { + boost::shared_ptr midi_track (wmt.lock()); - if (change.after.n_audio() <= change.before.n_audio()) { - return; - } - - /* new audio ports: make sure the audio goes somewhere useful, - unless the user has no-auto-connect selected. - - The existing ChanCounts don't matter for this call as they are only - to do with matching input and output indices, and we are only changing - outputs here. - */ - - ChanCount dummy; - - auto_connect_route (midi_track, dummy, dummy, false, false, ChanCount(), change.before); - } -} - -/** @param connect_inputs true to connect inputs as well as outputs, false to connect just outputs. - * @param input_start Where to start from when auto-connecting inputs; e.g. if this is 0, auto-connect starting from input 0. - * @param output_start As \a input_start, but for outputs. - */ -void -Session::auto_connect_route (boost::shared_ptr route, ChanCount& existing_inputs, ChanCount& existing_outputs, - bool with_lock, bool connect_inputs, ChanCount input_start, ChanCount output_start) -{ - if (!IO::connecting_legal) { + if (!midi_track) { return; } - Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock (), Glib::Threads::NOT_LOCK); - - if (with_lock) { - lm.acquire (); - } - - /* If both inputs and outputs are auto-connected to physical ports, - use the max of input and output offsets to ensure auto-connected - port numbers always match up (e.g. the first audio input and the - first audio output of the route will have the same physical - port number). Otherwise just use the lowest input or output - offset possible. - */ - - DEBUG_TRACE (DEBUG::Graph, - string_compose("Auto-connect: existing in = %1 out = %2\n", - existing_inputs, existing_outputs)); - - const bool in_out_physical = - (Config->get_input_auto_connect() & AutoConnectPhysical) - && (Config->get_output_auto_connect() & AutoConnectPhysical) - && connect_inputs; - - const ChanCount in_offset = in_out_physical - ? ChanCount::max(existing_inputs, existing_outputs) - : existing_inputs; - - const ChanCount out_offset = in_out_physical - ? ChanCount::max(existing_inputs, existing_outputs) - : existing_outputs; - - for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) { - vector physinputs; - vector physoutputs; - - _engine.get_physical_outputs (*t, physoutputs); - _engine.get_physical_inputs (*t, physinputs); - - if (!physinputs.empty() && connect_inputs) { - uint32_t nphysical_in = physinputs.size(); - - DEBUG_TRACE (DEBUG::Graph, - string_compose("There are %1 physical inputs of type %2\n", - nphysical_in, *t)); - - for (uint32_t i = input_start.get(*t); i < route->n_inputs().get(*t) && i < nphysical_in; ++i) { - string port; - - if (Config->get_input_auto_connect() & AutoConnectPhysical) { - DEBUG_TRACE (DEBUG::Graph, - string_compose("Get index %1 + %2 % %3 = %4\n", - in_offset.get(*t), i, nphysical_in, - (in_offset.get(*t) + i) % nphysical_in)); - port = physinputs[(in_offset.get(*t) + i) % nphysical_in]; - } - - DEBUG_TRACE (DEBUG::Graph, - string_compose("Connect route %1 IN to %2\n", - route->name(), port)); - - if (!port.empty() && route->input()->connect (route->input()->ports().port(*t, i), port, this)) { - break; - } + if ((change.type & IOChange::ConfigurationChanged) && Config->get_output_auto_connect() != ManualConnect) { - ChanCount one_added (*t, 1); - existing_inputs += one_added; - } + if (change.after.n_audio() <= change.before.n_audio()) { + return; } - if (!physoutputs.empty()) { - uint32_t nphysical_out = physoutputs.size(); - for (uint32_t i = output_start.get(*t); i < route->n_outputs().get(*t); ++i) { - string port; - - /* Waves Tracks: - * do not create new connections if we reached the limit of physical outputs - * in Multi Out mode - */ - - if (!(Config->get_output_auto_connect() & AutoConnectMaster) && - ARDOUR::Profile->get_trx () && - existing_outputs.get(*t) == nphysical_out ) { - break; - } - - if ((*t) == DataType::MIDI && (Config->get_output_auto_connect() & AutoConnectPhysical)) { - port = physoutputs[(out_offset.get(*t) + i) % nphysical_out]; - } else if ((*t) == DataType::AUDIO && (Config->get_output_auto_connect() & AutoConnectMaster)) { - /* master bus is audio only */ - if (_master_out && _master_out->n_inputs().get(*t) > 0) { - port = _master_out->input()->ports().port(*t, - i % _master_out->input()->n_ports().get(*t))->name(); - } - } - - DEBUG_TRACE (DEBUG::Graph, - string_compose("Connect route %1 OUT to %2\n", - route->name(), port)); - - if (!port.empty() && route->output()->connect (route->output()->ports().port(*t, i), port, this)) { - break; - } - - ChanCount one_added (*t, 1); - existing_outputs += one_added; - } - } + /* new audio ports: make sure the audio goes somewhere useful, + * unless the user has no-auto-connect selected. + * + * The existing ChanCounts don't matter for this call as they are only + * to do with matching input and output indices, and we are only changing + * outputs here. + */ + auto_connect_route (midi_track, false, ChanCount(), change.before); } } @@ -2830,8 +2803,6 @@ Session::reconnect_existing_routes (bool withLock, bool reconnect_master, bool r } } } - - //auto_connect_route (*rIter, inputs, outputs, false, reconnectIputs); } _master_out->output()->disconnect (this); @@ -2988,6 +2959,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: @@ -3004,9 +2980,8 @@ Session::new_audio_track (int input_channels, int output_channels, TrackMode mod track->use_new_diskstream(); -#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS - // boost_debug_shared_ptr_mark_interesting (track.get(), "Track"); -#endif + BOOST_MARK_TRACK (track); + { Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ()); @@ -3097,9 +3072,12 @@ Session::new_audio_route (int input_channels, int output_channels, RouteGroup* r goto failure; } -#ifdef BOOST_SP_ENABLE_DEBUG_HOOKS - // boost_debug_shared_ptr_mark_interesting (bus.get(), "Route"); -#endif + if (Profile->get_mixbus ()) { + bus->set_strict_io (true); + } + + BOOST_MARK_ROUTE(bus); + { Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ()); @@ -3243,10 +3221,48 @@ Session::new_route_from_template (uint32_t how_many, XMLNode& node, const std::s XMLNodeList children = node_copy.children (); for (XMLNodeList::iterator i = children.begin(); i != children.end(); ++i) { if ((*i)->name() == X_("Processor")) { - XMLProperty* role = (*i)->property (X_("role")); + /* ForceIDRegeneration does not catch the following */ + XMLProperty const * role = (*i)->property (X_("role")); + XMLProperty const * type = (*i)->property (X_("type")); + if (role && role->value() == X_("Aux")) { + /* check if the target bus exists. + * we should not save aux-sends in templates. + */ + XMLProperty const * target = (*i)->property (X_("target")); + if (!target) { + (*i)->add_property ("type", "dangling-aux-send"); + continue; + } + boost::shared_ptr r = route_by_id (target->value()); + if (!r || boost::dynamic_pointer_cast(r)) { + (*i)->add_property ("type", "dangling-aux-send"); + continue; + } + } if (role && role->value() == X_("Listen")) { (*i)->remove_property (X_("bitslot")); } + else if (role && (role->value() == X_("Send") || role->value() == X_("Aux"))) { + char buf[32]; + Delivery::Role xrole; + uint32_t bitslot = 0; + xrole = Delivery::Role (string_2_enum (role->value(), xrole)); + std::string name = Send::name_and_id_new_send(*this, xrole, bitslot, false); + snprintf (buf, sizeof (buf), "%" PRIu32, bitslot); + (*i)->remove_property (X_("bitslot")); + (*i)->remove_property (X_("name")); + (*i)->add_property ("bitslot", buf); + (*i)->add_property ("name", name); + } + else if (type && type->value() == X_("return")) { + // Return::set_state() generates a new one + (*i)->remove_property (X_("bitslot")); + } + else if (type && type->value() == X_("port")) { + // PortInsert::set_state() handles the bitslot + (*i)->remove_property (X_("bitslot")); + (*i)->add_property ("ignore-name", "1"); + } } } @@ -3354,16 +3370,17 @@ Session::add_routes (RouteList& new_routes, bool input_auto_connect, bool output void Session::add_routes_inner (RouteList& new_routes, bool input_auto_connect, bool output_auto_connect) { - ChanCount existing_inputs; - ChanCount existing_outputs; + ChanCount existing_inputs; + ChanCount existing_outputs; uint32_t order = next_control_id(); + if (_order_hint > -1) { order = _order_hint; _order_hint = -1; } - count_existing_track_channels (existing_inputs, existing_outputs); + count_existing_track_channels (existing_inputs, existing_outputs); { RCUWriter writer (routes); @@ -3371,10 +3388,10 @@ Session::add_routes_inner (RouteList& new_routes, bool input_auto_connect, bool r->insert (r->end(), new_routes.begin(), new_routes.end()); /* if there is no control out and we're not in the middle of loading, - resort the graph here. if there is a control out, we will resort - toward the end of this method. if we are in the middle of loading, - we will resort when done. - */ + * resort the graph here. if there is a control out, we will resort + * toward the end of this method. if we are in the middle of loading, + * we will resort when done. + */ if (!_monitor_out && IO::connecting_legal) { resort_routes_using (r); @@ -3410,19 +3427,20 @@ Session::add_routes_inner (RouteList& new_routes, bool input_auto_connect, bool boost::shared_ptr mt = boost::dynamic_pointer_cast (tr); if (mt) { mt->StepEditStatusChange.connect_same_thread (*this, boost::bind (&Session::step_edit_status_change, this, _1)); - mt->output()->changed.connect_same_thread (*this, boost::bind (&Session::midi_output_change_handler, this, _1, _2, boost::weak_ptr(mt))); + mt->output()->changed.connect_same_thread (*this, boost::bind (&Session::midi_output_change_handler, this, _1, _2, boost::weak_ptr(mt))); } } - if (input_auto_connect || output_auto_connect) { - auto_connect_route (r, existing_inputs, existing_outputs, true, input_auto_connect); + auto_connect_route (r, input_auto_connect, ChanCount (), ChanCount (), existing_inputs, existing_outputs); + existing_inputs += r->n_inputs(); + existing_outputs += r->n_outputs(); } /* order keys are a GUI responsibility but we need to set up - reasonable defaults because they also affect the remote control - ID in most situations. - */ + reasonable defaults because they also affect the remote control + ID in most situations. + */ if (!r->has_order_key ()) { if (r->is_auditioner()) { @@ -3445,7 +3463,7 @@ Session::add_routes_inner (RouteList& new_routes, bool input_auto_connect, bool if ((*x)->is_monitor()) { /* relax */ } else if ((*x)->is_master()) { - /* relax */ + /* relax */ } else { (*x)->enable_monitor_send (); } @@ -3539,7 +3557,6 @@ Session::add_internal_send (boost::shared_ptr dest, boost::shared_ptr routes_to_remove) { @@ -3625,7 +3642,7 @@ Session::remove_routes (boost::shared_ptr routes_to_remove) resort_routes (); #endif - if (_process_graph) { + if (_process_graph && !(_state_of_the_state & Deletion)) { _process_graph->clear_other_chain (); } @@ -3642,6 +3659,10 @@ Session::remove_routes (boost::shared_ptr routes_to_remove) (*iter)->drop_references (); } + if (_state_of_the_state & Deletion) { + return; + } + Route::RemoteControlIDChange(); /* EMIT SIGNAL */ /* save the new state of the world */ @@ -5087,7 +5108,7 @@ Session::setup_lua () " 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 }" + " local env = { print = print, 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, Session = Session, PBD = PBD, Timecode = Timecode, Evoral = Evoral, C = C, ARDOUR = ARDOUR }" " self.instances[n] = load (string.dump(f, true), nil, nil, env)(a)" " Session:scripts_changed()" // call back " end" @@ -5447,7 +5468,7 @@ Session::next_insert_id () /* this doesn't really loop forever. just think about it */ while (true) { - for (boost::dynamic_bitset::size_type n = 0; n < insert_bitset.size(); ++n) { + for (boost::dynamic_bitset::size_type n = 1; n < insert_bitset.size(); ++n) { if (!insert_bitset[n]) { insert_bitset[n] = true; return n; @@ -5467,7 +5488,7 @@ Session::next_send_id () /* this doesn't really loop forever. just think about it */ while (true) { - for (boost::dynamic_bitset::size_type n = 0; n < send_bitset.size(); ++n) { + for (boost::dynamic_bitset::size_type n = 1; n < send_bitset.size(); ++n) { if (!send_bitset[n]) { send_bitset[n] = true; return n; @@ -5487,7 +5508,7 @@ Session::next_aux_send_id () /* this doesn't really loop forever. just think about it */ while (true) { - for (boost::dynamic_bitset::size_type n = 0; n < aux_send_bitset.size(); ++n) { + for (boost::dynamic_bitset::size_type n = 1; n < aux_send_bitset.size(); ++n) { if (!aux_send_bitset[n]) { aux_send_bitset[n] = true; return n; @@ -5507,7 +5528,7 @@ Session::next_return_id () /* this doesn't really loop forever. just think about it */ while (true) { - for (boost::dynamic_bitset::size_type n = 0; n < return_bitset.size(); ++n) { + for (boost::dynamic_bitset::size_type n = 1; n < return_bitset.size(); ++n) { if (!return_bitset[n]) { return_bitset[n] = true; return n; @@ -5938,6 +5959,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) { @@ -6375,9 +6402,10 @@ Session::unknown_processors () const void Session::update_latency (bool playback) { + DEBUG_TRACE (DEBUG::Latency, string_compose ("JACK latency callback: %1\n", (playback ? "PLAYBACK" : "CAPTURE"))); - if ((_state_of_the_state & (InitialConnecting|Deletion)) || _adding_routes_in_progress) { + if ((_state_of_the_state & (InitialConnecting|Deletion)) || _adding_routes_in_progress || _route_deletion_in_progress) { return; } @@ -6738,3 +6766,191 @@ Session::clear_object_selection () follow_playhead_priority (); #endif } + +void +Session::auto_connect_route (boost::shared_ptr route, bool connect_inputs, + const ChanCount& input_start, + const ChanCount& output_start, + const ChanCount& input_offset, + const ChanCount& output_offset) +{ + Glib::Threads::Mutex::Lock lx (_auto_connect_queue_lock); + _auto_connect_queue.push (AutoConnectRequest (route, connect_inputs, + input_start, output_start, + input_offset, output_offset)); + + if (pthread_mutex_trylock (&_auto_connect_mutex) == 0) { + pthread_cond_signal (&_auto_connect_cond); + pthread_mutex_unlock (&_auto_connect_mutex); + } +} + +void +Session::auto_connect (const AutoConnectRequest& ar) +{ + boost::shared_ptr route = ar.route.lock(); + + if (!route) { return; } + + if (!IO::connecting_legal) { + return; + } + + /* If both inputs and outputs are auto-connected to physical ports, + * use the max of input and output offsets to ensure auto-connected + * port numbers always match up (e.g. the first audio input and the + * first audio output of the route will have the same physical + * port number). Otherwise just use the lowest input or output + * offset possible. + */ + + const bool in_out_physical = + (Config->get_input_auto_connect() & AutoConnectPhysical) + && (Config->get_output_auto_connect() & AutoConnectPhysical) + && ar.connect_inputs; + + const ChanCount in_offset = in_out_physical + ? ChanCount::max(ar.input_offset, ar.output_offset) + : ar.input_offset; + + const ChanCount out_offset = in_out_physical + ? ChanCount::max(ar.input_offset, ar.output_offset) + : ar.output_offset; + + for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) { + vector physinputs; + vector physoutputs; + + _engine.get_physical_outputs (*t, physoutputs); + _engine.get_physical_inputs (*t, physinputs); + + if (!physinputs.empty() && ar.connect_inputs) { + uint32_t nphysical_in = physinputs.size(); + + for (uint32_t i = ar.input_start.get(*t); i < route->n_inputs().get(*t) && i < nphysical_in; ++i) { + string port; + + if (Config->get_input_auto_connect() & AutoConnectPhysical) { + port = physinputs[(in_offset.get(*t) + i) % nphysical_in]; + } + + if (!port.empty() && route->input()->connect (route->input()->ports().port(*t, i), port, this)) { + break; + } + } + } + + if (!physoutputs.empty()) { + uint32_t nphysical_out = physoutputs.size(); + for (uint32_t i = ar.output_start.get(*t); i < route->n_outputs().get(*t); ++i) { + string port; + + /* Waves Tracks: + * do not create new connections if we reached the limit of physical outputs + * in Multi Out mode + */ + if (!(Config->get_output_auto_connect() & AutoConnectMaster) && + ARDOUR::Profile->get_trx () && + ar.output_offset.get(*t) == nphysical_out ) { + break; + } + + if ((*t) == DataType::MIDI && (Config->get_output_auto_connect() & AutoConnectPhysical)) { + port = physoutputs[(out_offset.get(*t) + i) % nphysical_out]; + } else if ((*t) == DataType::AUDIO && (Config->get_output_auto_connect() & AutoConnectMaster)) { + /* master bus is audio only */ + if (_master_out && _master_out->n_inputs().get(*t) > 0) { + port = _master_out->input()->ports().port(*t, + i % _master_out->input()->n_ports().get(*t))->name(); + } + } + + if (!port.empty() && route->output()->connect (route->output()->ports().port(*t, i), port, this)) { + break; + } + } + } + } +} + +void +Session::auto_connect_thread_start () +{ + if (_ac_thread_active) { + return; + } + + while (!_auto_connect_queue.empty ()) { + _auto_connect_queue.pop (); + } + + _ac_thread_active = true; + if (pthread_create (&_auto_connect_thread, NULL, auto_connect_thread, this)) { + _ac_thread_active = false; + } +} + +void +Session::auto_connect_thread_terminate () +{ + if (!_ac_thread_active) { + return; + } + _ac_thread_active = false; + + { + Glib::Threads::Mutex::Lock lx (_auto_connect_queue_lock); + while (!_auto_connect_queue.empty ()) { + _auto_connect_queue.pop (); + } + } + + if (pthread_mutex_lock (&_auto_connect_mutex) == 0) { + pthread_cond_signal (&_auto_connect_cond); + pthread_mutex_unlock (&_auto_connect_mutex); + } + + void *status; + pthread_join (_auto_connect_thread, &status); +} + +void * +Session::auto_connect_thread (void *arg) +{ + Session *s = static_cast(arg); + s->auto_connect_thread_run (); + pthread_exit (0); + return 0; +} + +void +Session::auto_connect_thread_run () +{ + pthread_set_name (X_("autoconnect")); + SessionEvent::create_per_thread_pool (X_("autoconnect"), 1024); + PBD::notify_event_loops_about_thread_creation (pthread_self(), X_("autoconnect"), 1024); + pthread_mutex_lock (&_auto_connect_mutex); + while (_ac_thread_active) { + + if (!_auto_connect_queue.empty ()) { + // Why would we need the process lock ?? + // A: if ports are added while we're connecting, the backend's iterator may be invalidated: + // graph_order_callback() -> resort_routes() -> direct_feeds_according_to_reality () -> backend::connected_to() + // All ardour-internal backends use a std::vector xxxAudioBackend::find_port() + // We have control over those, but what does jack do? + Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ()); + + Glib::Threads::Mutex::Lock lx (_auto_connect_queue_lock); + while (!_auto_connect_queue.empty ()) { + const AutoConnectRequest ar (_auto_connect_queue.front()); + _auto_connect_queue.pop (); + lx.release (); + auto_connect (ar); + lx.acquire (); + } + } + + pthread_cond_wait (&_auto_connect_cond, &_auto_connect_mutex); + } + pthread_mutex_unlock (&_auto_connect_mutex); +}