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.
This commit is contained in:
Artem Alimov 2021-03-06 23:16:40 +03:00
parent c294932142
commit 5add650871
3 changed files with 73 additions and 0 deletions

View File

@ -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<void,Location*> current_changed;

View File

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

View File

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