From a36ddb72ddb825f4bcfb72e1ebcfe35c7d40f9e0 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Mon, 3 Jul 2023 00:13:20 +0200 Subject: [PATCH] Correctly set sample-rate of plugins when loading a different session Directly loading a new session (Session > Recent) stops the engine when the sample-rate mismatches. All is fine. When closing a session (Session > Close), the engine is kept running. Loading a different session with different sample-rate shows the "SR mismatch" dialog. Reconfiguring the engine then does not call `Session::immediately_post_engine` again. --- libs/ardour/session_state.cc | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/libs/ardour/session_state.cc b/libs/ardour/session_state.cc index c404731b85..985c5a4fbb 100644 --- a/libs/ardour/session_state.cc +++ b/libs/ardour/session_state.cc @@ -1704,8 +1704,7 @@ Session::set_state (const XMLNode& node, int version) if (node.get_property (X_("sample-rate"), _base_sample_rate)) { - /* required to convert positions during session load */ - Temporal::set_sample_rate (_base_sample_rate); + bool reconfigured = false; while (!AudioEngine::instance()->running () || _base_sample_rate != AudioEngine::instance()->sample_rate ()) { boost::optional r = AskAboutSampleRateMismatch (_base_sample_rate, _current_sample_rate); @@ -1715,7 +1714,7 @@ Session::set_state (const XMLNode& node, int version) break; } else if (rv == -1 && AudioEngine::instance()->running ()) { /* retry */ - set_block_size (_engine.samples_per_cycle()); + reconfigured = true; continue; } else { if (AudioEngine::instance()->running ()) { @@ -1728,14 +1727,32 @@ Session::set_state (const XMLNode& node, int version) } } - if (_base_sample_rate != _engine.sample_rate ()) { + if (reconfigured) { + set_block_size (_engine.samples_per_cycle()); + } + + if (_base_sample_rate != _engine.sample_rate () || reconfigured) { + /* set _current_sample_rate early on. It is used when instantiating plugins. + * This also calls Temporal::set_sample_rate, which is required to + * to convert positions during session load. + */ set_sample_rate (_base_sample_rate); /* post_engine_init() calls initialize_latencies() * which sets up resampling. However by that time all session * ports will exist and would be need to be reinitialized. */ setup_engine_resampling (); + } else { + /* required to convert positions during session load */ + Temporal::set_sample_rate (_base_sample_rate); + /* all other initialization is correctly done by + * Session::immediately_post_engine + */ } + + } else { + error << _("Session: XML state has no sample-rate ") << endmsg; + goto out; } /* need the tempo map setup ASAP */