From 5add650871591abe466a686e6cb9ccb60d3bd755 Mon Sep 17 00:00:00 2001 From: Artem Alimov Date: Sat, 6 Mar 2021 23:16:40 +0300 Subject: [PATCH] Add lua methods to control range locations Method Session:locations():range_starts_at(pos, slop, incl) to search range by start point with some inaccuracy delta. Similar to mark_at(pos, slop) Method Session:locations():add_range(start, end) to create new range and get it for later changes. --- libs/ardour/ardour/location.h | 20 ++++++++++++++ libs/ardour/location.cc | 51 +++++++++++++++++++++++++++++++++++ libs/ardour/luabindings.cc | 2 ++ 3 files changed, 73 insertions(+) diff --git a/libs/ardour/ardour/location.h b/libs/ardour/ardour/location.h index 9c1edaa119..2ccc8cc486 100644 --- a/libs/ardour/ardour/location.h +++ b/libs/ardour/ardour/location.h @@ -189,6 +189,16 @@ public: LocationList list () { return locations; } void add (Location *, bool make_current = false); + + /** Add new range to the collection + * + * @param start start position + * @param end end position + * + * @return New location object + */ + Location* add_range (samplepos_t start, samplepos_t end); + void remove (Location *); void clear (); void clear_markers (); @@ -218,6 +228,16 @@ public: void marks_either_side (samplepos_t const, samplepos_t &, samplepos_t &) const; + /** Return range with closest start pos to the where argument + * + * @param pos point to compare with start pos + * @param slop area around point to search for start pos + * @param incl (optional) look only for ranges that includes 'where' point + * + * @return Location object or nil + */ + Location* range_starts_at(samplepos_t pos, samplecnt_t slop = 0, bool incl = false) const; + void find_all_between (samplepos_t start, samplepos_t, LocationList&, Location::Flags); PBD::Signal1 current_changed; diff --git a/libs/ardour/location.cc b/libs/ardour/location.cc index 3f6126938f..23e6945be9 100644 --- a/libs/ardour/location.cc +++ b/libs/ardour/location.cc @@ -1017,6 +1017,18 @@ Locations::add (Location *loc, bool make_current) } } +Location* +Locations::add_range(samplepos_t start, samplepos_t end) +{ + string name; + next_available_name(name, _("range")); + + Location* loc = new Location(_session, start, end, name, Location::IsRangeMarker); + add(loc, false); + + return loc; +} + void Locations::remove (Location *loc) { @@ -1484,3 +1496,42 @@ Locations::find_all_between (samplepos_t start, samplepos_t end, LocationList& l } } } + +Location * +Locations::range_starts_at(samplepos_t pos, samplecnt_t slop, bool incl) const +{ + Glib::Threads::Mutex::Lock lm(lock); + Location *closest = 0; + sampleoffset_t mindelta = max_samplepos; + sampleoffset_t delta; + + for (LocationList::const_iterator i = locations.begin(); i != locations.end(); ++i) + { + + if ((*i)->is_range_marker()) + { + if (incl && (pos < (*i)->start() || pos > (*i)->end())) { + continue; + } + + delta = std::abs(pos - (*i)->start()); + + if (delta == 0) + { + return *i; + } + + if (delta > slop) { + continue; + } + + if (delta < mindelta) + { + closest = *i; + mindelta = delta; + } + } + } + + return closest; +} diff --git a/libs/ardour/luabindings.cc b/libs/ardour/luabindings.cc index f466eb7d48..466e650e26 100644 --- a/libs/ardour/luabindings.cc +++ b/libs/ardour/luabindings.cc @@ -886,6 +886,8 @@ LuaBindings::common (lua_State* L) .addFunction ("first_mark_before", &Locations::first_mark_before) .addFunction ("first_mark_at", &Locations::mark_at) .addFunction ("mark_at", &Locations::mark_at) + .addFunction ("range_starts_at", &Locations::range_starts_at) + .addFunction ("add_range", &Locations::add_range) .addFunction ("remove", &Locations::remove) .addRefFunction ("marks_either_side", &Locations::marks_either_side) .addRefFunction ("find_all_between", &Locations::find_all_between)