From 46a916f0b42de0b2631f1640b6a8cd05fed10cb1 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Sat, 12 Aug 2023 02:30:43 +0200 Subject: [PATCH] Add method to query sections --- libs/ardour/ardour/location.h | 2 ++ libs/ardour/location.cc | 66 +++++++++++++++++++++++++++++++++++ libs/ardour/luabindings.cc | 1 + share/scripts/s_sections.lua | 18 ++++++++++ 4 files changed, 87 insertions(+) create mode 100644 share/scripts/s_sections.lua diff --git a/libs/ardour/ardour/location.h b/libs/ardour/ardour/location.h index 75eca45970..acfbc2cc79 100644 --- a/libs/ardour/ardour/location.h +++ b/libs/ardour/ardour/location.h @@ -298,6 +298,8 @@ public: timepos_t first_mark_before (timepos_t const &, bool include_special_ranges = false); timepos_t first_mark_after (timepos_t const &, bool include_special_ranges = false); + Location* next_section (Location*, timepos_t&, timepos_t&) const; + void marks_either_side (timepos_t const &, timepos_t &, timepos_t &) const; /** Return range with closest start pos to the where argument diff --git a/libs/ardour/location.cc b/libs/ardour/location.cc index afee4afbe9..ae4014ac40 100644 --- a/libs/ardour/location.cc +++ b/libs/ardour/location.cc @@ -1568,6 +1568,72 @@ Locations::marks_either_side (timepos_t const & pos, timepos_t& before, timepos_ before = *i; } +Location* +Locations::next_section (Location* l, timepos_t& start, timepos_t& end) const +{ + vector locs; + Location* session_range = NULL; + { + Glib::Threads::RWLock::ReaderLock lm (_lock); + + for (auto const& i: locations) { + if (i->is_session_range ()) { + session_range = i; + } else if (i->is_section ()) { + locs.push_back (make_pair (i->start(), i)); + } + } + } + + LocationStartEarlierComparison cmp; + sort (locs.begin(), locs.end(), cmp); + + if (session_range) { + if (locs.empty()) { + //locs.push_back (make_pair (session_range->start (), session_range)); + locs.push_back (make_pair (session_range->end (), (ARDOUR::Location*)NULL)); + } else { + if (locs.back().second->start () < session_range->end ()) { + locs.push_back (make_pair (session_range->end (), (ARDOUR::Location*)NULL)); + } + if (locs.front().second->start () > session_range->start ()) { + //locs.insert (locs.begin (), make_pair (session_range->start (), session_range)); + } + } + } + + if (locs.size () < 2) { + return NULL; + } + + /* special case fist element */ + if (!l) { + l = locs[0].second; + start = locs[0].first; + end = locs[1].first; + return l; + } + + Location* rv = NULL; + bool found = false; + + for (auto const& i: locs) { + if (rv && found) { + end = i.first; + return rv; + } + else if (found) { + start = i.first; + rv = i.second; + } + else if (i.second == l) { + found = true; + } + } + + return NULL; +} + Location* Locations::session_range_location () const { diff --git a/libs/ardour/luabindings.cc b/libs/ardour/luabindings.cc index 3edc9ff299..32aebd83a9 100644 --- a/libs/ardour/luabindings.cc +++ b/libs/ardour/luabindings.cc @@ -1229,6 +1229,7 @@ LuaBindings::common (lua_State* L) .addFunction ("remove", &Locations::remove) .addRefFunction ("marks_either_side", &Locations::marks_either_side) .addRefFunction ("find_all_between", &Locations::find_all_between) + .addRefFunction ("next_section", &Locations::next_section) .endClass () .beginWSPtrClass ("SessionObjectPtr") diff --git a/share/scripts/s_sections.lua b/share/scripts/s_sections.lua new file mode 100644 index 0000000000..e7a84508e2 --- /dev/null +++ b/share/scripts/s_sections.lua @@ -0,0 +1,18 @@ +ardour { ["type"] = "Snippet", name = "List Sections" } +function factory () return function () + + local s = Temporal.timepos_t(0) + local e = Temporal.timepos_t(0) + local loc = Session:locations () + + local l = nil + local cnt = 0 + repeat + l, rv = loc:next_section (l, s, e) + if l ~= nil then + print (l:name (), rv[2], rv[3]); + end + cnt = cnt + 1 + until (l == nil or cnt > 10) + +end end