From 80ffa58c8124899e7031c809b43a846f0b54ffe4 Mon Sep 17 00:00:00 2001 From: Mads Kiilerich Date: Wed, 5 Oct 2022 19:12:37 +0200 Subject: [PATCH] temporal: introduce Temporal::reset() with TempoMap initialization Ardour uses some global variables and singletons. These global variables can be initialized with a value prior to program execution (especially if they are const), but some of the static variables are modified, and it is crucial that they always are reset when switching to another session. To keep things simple and explicit and consistent, we thus introduce Temporal::reset() to reset TempoMap (and later on also the superclock rate). This is somewhat similar to Temporal::init(), which usually only is invoked once (on program start) to initialize singletons (such as the TempoMap). 9964f20c added TempoMap initialization to Session::create() ... but only when not using a template. This create method is mainly preparing the filesystem for a new session, and TempoMap initialization doesn't seem like a perfect fit for it. It also seemed odd that it only initialized TempoMap for clean new sessions, while existing sessions and templates initialized it elsewhere. Instead, invoke the TempoMap initialization early in the Session creation process. This might introduce an extra and unnecessary TempoMap initialization when loading an existing session or using a template, but that will be cheap and do no harm, while providing a guarantee that we always use the same default value. --- libs/ardour/session.cc | 2 ++ libs/ardour/session_state.cc | 5 ----- libs/temporal/enums.cc | 7 +++++++ libs/temporal/temporal/types.h | 2 ++ 4 files changed, 11 insertions(+), 5 deletions(-) diff --git a/libs/ardour/session.cc b/libs/ardour/session.cc index 30e24ebfc5..e0465bc927 100644 --- a/libs/ardour/session.cc +++ b/libs/ardour/session.cc @@ -361,6 +361,8 @@ Session::Session (AudioEngine &eng, _cue_events.reserve (1024); + Temporal::reset(); + pre_engine_init (fullpath); // sets _is_new setup_lua (); diff --git a/libs/ardour/session_state.cc b/libs/ardour/session_state.cc index 24357a9ece..216b8b3269 100644 --- a/libs/ardour/session_state.cc +++ b/libs/ardour/session_state.cc @@ -640,11 +640,6 @@ Session::create (const string& session_template, BusProfile const * bus_profile, << endmsg; return -1; } - - } else { - (void) TempoMap::write_copy(); /* we are going to throw away the return value and replace the map entirely */ - TempoMap::WritableSharedPtr new_map (new TempoMap (Tempo (120, 4), Meter (4, 4))); - TempoMap::update (new_map);; } /* set up Master Out and Monitor Out if necessary */ diff --git a/libs/temporal/enums.cc b/libs/temporal/enums.cc index b0e563cf2b..e61bbd7e42 100644 --- a/libs/temporal/enums.cc +++ b/libs/temporal/enums.cc @@ -76,6 +76,13 @@ void Temporal::init () } } +void Temporal::reset () +{ + (void) TempoMap::write_copy(); /* we are going to throw away the return value and replace the map entirely */ + TempoMap::WritableSharedPtr new_map (new TempoMap (Tempo (120, 4), Meter (4, 4))); + TempoMap::update (new_map);; +} + std::ostream& operator<< (std::ostream& o, Temporal::ratio_t const & r) { diff --git a/libs/temporal/temporal/types.h b/libs/temporal/temporal/types.h index ed7a88013a..df02b7fd78 100644 --- a/libs/temporal/temporal/types.h +++ b/libs/temporal/temporal/types.h @@ -30,8 +30,10 @@ namespace Temporal { #ifdef COMPILER_MSVC LIBTEMPORAL_API void init (); + LIBTEMPORAL_API void reset (); #else extern void init (); + extern void reset (); #endif /* Any position measured in audio samples.