From 23121870704d5299e69d2e64d45a14a7895e786d Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Sun, 10 Aug 2025 20:25:53 -0600 Subject: [PATCH] sort-of-a-solution for local TempoMap context in EditingContext --- gtk2_ardour/cue_editor.cc | 1 - gtk2_ardour/debug.cc | 1 + gtk2_ardour/debug.h | 1 + gtk2_ardour/editing_context.cc | 33 +++++++++++++++++++++++++--- gtk2_ardour/editing_context.h | 36 ++++++++++++++----------------- gtk2_ardour/pianoroll.cc | 39 ++-------------------------------- 6 files changed, 50 insertions(+), 61 deletions(-) diff --git a/gtk2_ardour/cue_editor.cc b/gtk2_ardour/cue_editor.cc index a0f0c761de..e4a92d3e13 100644 --- a/gtk2_ardour/cue_editor.cc +++ b/gtk2_ardour/cue_editor.cc @@ -1516,7 +1516,6 @@ CueEditor::metric_get_bbt (std::vector& marks, sample tmap.reset (new Temporal::TempoMap (Temporal::Tempo (120, 4), Temporal::Meter (4, 4))); } - EditingContext::TempoMapScope tms (*this, tmap); Temporal::TempoMapPoints::const_iterator i; char buf[64]; diff --git a/gtk2_ardour/debug.cc b/gtk2_ardour/debug.cc index 5c9895ba9a..517aa4f199 100644 --- a/gtk2_ardour/debug.cc +++ b/gtk2_ardour/debug.cc @@ -32,3 +32,4 @@ PBD::DebugBits PBD::DEBUG::GUITiming = PBD::new_debug_bit ("guitiming"); PBD::DebugBits PBD::DEBUG::EngineControl = PBD::new_debug_bit ("enginecontrol"); PBD::DebugBits PBD::DEBUG::GuiStartup = PBD::new_debug_bit ("guistartup"); PBD::DebugBits PBD::DEBUG::TrackDrag = PBD::new_debug_bit ("trackdrag"); +PBD::DebugBits PBD::DEBUG::ScopedTempoMap = PBD::new_debug_bit ("scopedtempomap"); diff --git a/gtk2_ardour/debug.h b/gtk2_ardour/debug.h index 127b9dce3b..b767965998 100644 --- a/gtk2_ardour/debug.h +++ b/gtk2_ardour/debug.h @@ -33,6 +33,7 @@ namespace PBD { extern DebugBits EngineControl; extern DebugBits GuiStartup; extern DebugBits TrackDrag; + extern DebugBits ScopedTempoMap; } } diff --git a/gtk2_ardour/editing_context.cc b/gtk2_ardour/editing_context.cc index f119a877cc..9b0b98334f 100644 --- a/gtk2_ardour/editing_context.cc +++ b/gtk2_ardour/editing_context.cc @@ -37,6 +37,7 @@ #include "ardour_ui.h" #include "automation_line.h" #include "control_point.h" +#include "debug.h" #include "edit_note_dialog.h" #include "editing_context.h" #include "editing_convert.h" @@ -153,6 +154,7 @@ EditingContext::EditingContext (std::string const & name) , time_line_group (nullptr) , temporary_zoom_focus_change (false) , _dragging_playhead (false) + , local_tempo_map_depth (0) { using namespace Gtk::Menu_Helpers; @@ -1752,16 +1754,41 @@ EditingContext::snap_relative_time_to_relative_time (timepos_t const & origin, t } void -EditingContext::start_local_tempo_map (std::shared_ptr map) +EditingContext::start_local_tempo_map (std::shared_ptr map) { _local_tempo_map = map; + local_tempo_map_in (); + DEBUG_TRACE (DEBUG::ScopedTempoMap, string_compose ("%1: starting local tempo scope\n", editor_name())); } void EditingContext::end_local_tempo_map () { - _local_tempo_map.reset (); - Temporal::TempoMap::fetch (); + DEBUG_TRACE (DEBUG::ScopedTempoMap, string_compose ("%1: ending local tempo scope\n", editor_name())); + local_tempo_map_depth = 1; + local_tempo_map_out (); +} + +void +EditingContext::local_tempo_map_in () const +{ + if (local_tempo_map_depth++ == 0 ) { + DEBUG_TRACE (DEBUG::ScopedTempoMap, string_compose ("%1: in to local tempo %2\n", editor_name(), local_tempo_map_depth)); + if (_local_tempo_map) { + Temporal::TempoMap::set (_local_tempo_map); + _local_tempo_map.reset (); + } + } +} + +void +EditingContext::local_tempo_map_out () const +{ + DEBUG_TRACE (DEBUG::ScopedTempoMap, string_compose ("%1: out to local tempo %2\n", editor_name(), local_tempo_map_depth)); + if (local_tempo_map_depth && --local_tempo_map_depth == 0) { + DEBUG_TRACE (DEBUG::ScopedTempoMap, string_compose ("%1: done with local tempo, depth now %2\n", editor_name(), local_tempo_map_depth)); + Temporal::TempoMap::fetch (); /* get current global map into thread-local pointer */ + } } bool diff --git a/gtk2_ardour/editing_context.h b/gtk2_ardour/editing_context.h index f438993922..14942a864f 100644 --- a/gtk2_ardour/editing_context.h +++ b/gtk2_ardour/editing_context.h @@ -87,19 +87,6 @@ class EditingContext : public ARDOUR::SessionHandlePtr, public AxisViewProvider, Temporal::TimeDomain time_domain () const; - struct TempoMapScope { - TempoMapScope (EditingContext& context, std::shared_ptr map) - : ec (context) - { - ec.start_local_tempo_map (map); - ec.ensure_local_tempo_scope (); - } - ~TempoMapScope () { - ec.end_local_tempo_map (); - } - EditingContext& ec; - }; - DragManager* drags () const { return _drags; } @@ -665,9 +652,10 @@ class EditingContext : public ARDOUR::SessionHandlePtr, public AxisViewProvider, QuantizeDialog* quantize_dialog; friend struct TempoMapScope; - void set_local_tempo_map (std::shared_ptr); void start_local_tempo_map (std::shared_ptr); void end_local_tempo_map (); + void local_tempo_map_in () const; + void local_tempo_map_out () const; virtual bool button_press_handler (ArdourCanvas::Item*, GdkEvent*, ItemType) = 0; virtual bool button_press_handler_1 (ArdourCanvas::Item*, GdkEvent*, ItemType) = 0; @@ -834,12 +822,20 @@ class EditingContext : public ARDOUR::SessionHandlePtr, public AxisViewProvider, bool _dragging_playhead; mutable std::shared_ptr _local_tempo_map; - void ensure_local_tempo_scope () const { - if (_local_tempo_map) { - Temporal::TempoMap::set (_local_tempo_map); + mutable std::shared_ptr _pre_local_tempo_map; + mutable uint64_t local_tempo_map_depth; + + struct TempoMapScope { + TempoMapScope (EditingContext const & context) + : ec (context) + { + ec.local_tempo_map_in (); } - } + ~TempoMapScope () { + ec.local_tempo_map_out (); + } + EditingContext const & ec; + }; }; -#define EC_LOCAL_TEMPO_SCOPE ensure_local_tempo_scope () -#define EC_GIVEN_LOCAL_TEMPO_SCOPE(ec) ec.ensure_local_tempo_scope () +#define EC_LOCAL_TEMPO_SCOPE TempoMapScope __tms (*this); diff --git a/gtk2_ardour/pianoroll.cc b/gtk2_ardour/pianoroll.cc index 6f3fa256cb..7d395bee1b 100644 --- a/gtk2_ardour/pianoroll.cc +++ b/gtk2_ardour/pianoroll.cc @@ -1481,8 +1481,6 @@ Pianoroll::set_track (std::shared_ptr track) void Pianoroll::set_region (std::shared_ptr region) { - EC_LOCAL_TEMPO_SCOPE; - CueEditor::set_region (region); if (_visible_pending_region) { @@ -1505,42 +1503,9 @@ Pianoroll::set_region (std::shared_ptr region) r->DropReferences.connect (object_connections, invalidator (*this), std::bind (&Pianoroll::unset, this, false), gui_context()); r->PropertyChanged.connect (object_connections, invalidator (*this), std::bind (&Pianoroll::region_prop_change, this, _1), gui_context()); - bool provided = false; - std::shared_ptr map; - std::shared_ptr smf (std::dynamic_pointer_cast (r->midi_source())); + /* Compute zoom level to show entire source plus some margin if possible */ - if (smf) { - map = smf->tempo_map (provided); - } - - if (!provided) { - Temporal::TempoMap::SharedPtr tmap (Temporal::TempoMap::use()); - - if (with_transport_controls) { - /* clip editing, timeline irrelevant, sort of */ - - if (tmap->n_tempos() == 1 && tmap->n_meters() == 1) { - /* Single entry tempo map, use the values there */ - map.reset (new Temporal::TempoMap (tmap->tempo_at (timepos_t (0)), tmap->meter_at (timepos_t (0)))); - } else { - - map.reset (new Temporal::TempoMap (Temporal::Tempo (120, 4), Temporal::Meter (4, 4))); - } - - } else { - /* COPY MAIN SESSION TEMPO MAP? */ - Meter m (tmap->meter_at (r->source_position())); - Tempo t (tmap->tempo_at (r->source_position())); - - map.reset (new Temporal::TempoMap (t, m)); - } - } - - { - EditingContext::TempoMapScope tms (*this, map); - /* Compute zoom level to show entire source plus some margin if possible */ - zoom_to_show (timecnt_t (timepos_t (max_extents_scale() * max_zoom_extent ().second.samples()))); - } + zoom_to_show (timecnt_t (timepos_t (max_extents_scale() * max_zoom_extent ().second.samples()))); bg->display_region (*view);