13
0

Add method to query sections

This commit is contained in:
Robin Gareus 2023-08-12 02:30:43 +02:00
parent 6cf2659f8b
commit 46a916f0b4
Signed by: rgareus
GPG Key ID: A090BCE02CF57F04
4 changed files with 87 additions and 0 deletions

View File

@ -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

View File

@ -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<LocationPair> 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
{

View File

@ -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 <SessionObject> ("SessionObjectPtr")

View File

@ -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