From db8b054543649e5942a5e4c0c7e034255f635f0d Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Fri, 2 Oct 2020 21:25:57 -0600 Subject: [PATCH] add required methods for region-relative time conversion --- libs/ardour/ardour/region.h | 29 +++++++++++++++++++++ libs/ardour/region.cc | 51 ++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/libs/ardour/ardour/region.h b/libs/ardour/ardour/region.h index 378db8c5c7..e22d7c17b4 100644 --- a/libs/ardour/ardour/region.h +++ b/libs/ardour/ardour/region.h @@ -278,6 +278,35 @@ public: void set_video_locked (bool yn); void set_position_locked (bool yn); + Temporal::timepos_t region_beats_to_absolute_time(Temporal::Beats beats) const; + /** Convert a timestamp in beats into timepos_t (both relative to region position) */ + Temporal::timepos_t region_beats_to_region_time (Temporal::Beats beats) const { + return timepos_t (nt_position().distance (region_beats_to_absolute_time (beats))); + } + /** Convert a timestamp in beats relative to region position into beats relative to source start */ + Temporal::Beats region_beats_to_source_beats (Temporal::Beats beats) const { + return nt_position().distance (region_beats_to_absolute_time (beats)).beats (); + } + /** Convert a distance within a region to beats relative to region position */ + Temporal::Beats region_distance_to_region_beats (Temporal::timecnt_t const &) const; + + /** Convert a timestamp in beats measured from source start into absolute beats */ + Temporal::Beats source_beats_to_absolute_beats(Temporal::Beats beats) const; + + /** Convert a timestamp in beats measured from source start into absolute samples */ + Temporal::timepos_t source_beats_to_absolute_time(Temporal::Beats beats) const; + + /** Convert a timestamp in beats measured from source start into region-relative samples */ + Temporal::timepos_t source_beats_to_region_time(Temporal::Beats beats) const { + return timepos_t (nt_position().distance (source_beats_to_absolute_time (beats))); + } + /** Convert a timestamp in absolute time to beats measured from source start*/ + Temporal::Beats absolute_time_to_source_beats(Temporal::timepos_t const &) const; + + Temporal::Beats absolute_time_to_region_beats (Temporal::timepos_t const & b) const { + return b.distance (nt_position()).beats (); + } + int apply (Filter &, Progress* progress = 0); boost::shared_ptr playlist () const { return _playlist.lock(); } diff --git a/libs/ardour/region.cc b/libs/ardour/region.cc index 877655a3c7..25b9fa9164 100644 --- a/libs/ardour/region.cc +++ b/libs/ardour/region.cc @@ -1973,6 +1973,56 @@ Region::latest_possible_sample () const return position_sample() + (minlen - start_sample()) - 1; } +Temporal::TimeDomain +Region::position_time_domain() const +{ + return _position.val().time_domain(); +} + +timepos_t +Region::nt_end() const +{ + return _position.val() + _length.val(); +} + +Temporal::Beats +Region::region_distance_to_region_beats (timecnt_t const & region_relative_offset) const +{ + return timecnt_t (region_relative_offset, nt_position()).beats (); +} + +Temporal::Beats +Region::source_beats_to_absolute_beats (Temporal::Beats beats) const +{ + return source_position().beats() + beats; +} + +Temporal::timepos_t +Region::region_beats_to_absolute_time (Temporal::Beats beats) const +{ + /* beats is an additional offset to the start point of the region, from + the effective start of the source on the timeline. + */ + return source_position() + nt_start () + beats; +} + +Temporal::timepos_t +Region::source_beats_to_absolute_time (Temporal::Beats beats) const +{ + /* return the time corresponding to `beats' relative to the start of + the source. The start of the source is an implied position given by + region->position - region->start + */ + return source_position() + beats; +} + +Temporal::Beats +Region::absolute_time_to_source_beats(timepos_t const & time) const +{ + const timepos_t s (source_position()); + return time.earlier (timecnt_t (s, s)).beats(); +} + timepos_t Region::source_position () const { @@ -1990,4 +2040,3 @@ Region::region_relative_position (timepos_t const & p) const { return p.earlier (_position.val()); } -