Skip to content

Commit

Permalink
Clips: Fixed trimAwayOverlap when supplying a range that doesn't over…
Browse files Browse the repository at this point in the history
…lap the clip
  • Loading branch information
drowaudio committed Jun 26, 2024
1 parent 4759877 commit 837f510
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 8 deletions.
5 changes: 5 additions & 0 deletions modules/tracktion_core/utilities/tracktion_Time.h
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,11 @@ inline juce::String& operator<< (juce::String& s, TimePosition p) { return s <<
inline juce::String& operator<< (juce::String& s, BeatDuration d) { return s << juce::String (d.inBeats()); }
inline juce::String& operator<< (juce::String& s, BeatPosition p) { return s << juce::String (p.inBeats()); }

inline std::ostream& operator<< (std::ostream& os, const TimeDuration& v) { os << v.inSeconds(); return os; }
inline std::ostream& operator<< (std::ostream& os, const TimePosition& v) { os << v.inSeconds(); return os; }
inline std::ostream& operator<< (std::ostream& os, const BeatDuration& v) { os << v.inBeats(); return os; }
inline std::ostream& operator<< (std::ostream& os, const BeatPosition& v) { os << v.inBeats(); return os; }

}} // namespace tracktion


Expand Down
5 changes: 4 additions & 1 deletion modules/tracktion_core/utilities/tracktion_TimeRange.h
Original file line number Diff line number Diff line change
Expand Up @@ -387,12 +387,15 @@ template<typename RangeType,
std::enable_if_t<std::is_same_v<TimeRange, RangeType>
|| std::is_same_v<BeatRange, RangeType>,
bool> = true>
std::string toString (RangeType range)
std::string to_string (RangeType range)
{
return std::to_string (toUnderlyingType (range.getStart())) + ", "
+ std::to_string (toUnderlyingType (range.getEnd()));
}

inline std::ostream& operator<< (std::ostream& os, const TimeRange& r) { os << to_string (r); return os; }
inline std::ostream& operator<< (std::ostream& os, const BeatRange& r) { os << to_string (r); return os; }

}} // namespace tracktion


Expand Down
63 changes: 56 additions & 7 deletions modules/tracktion_engine/model/clips/tracktion_Clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,13 +386,13 @@ void Clip::trimAwayOverlap (TimeRange r)
{
auto pos = getPosition();

if (r.getEnd() > pos.time.getStart())
{
if (r.getEnd() < pos.time.getEnd())
setStart (r.getEnd(), true, false);
else if (pos.time.getStart() < r.getStart())
setEnd (r.getStart(), true);
}
if (! pos.time.intersects (r))
return;

if (r.getEnd() < pos.time.getEnd())
setStart (r.getEnd(), true, false);
else if (pos.time.getStart() < r.getStart())
setEnd (r.getStart(), true);
}

void Clip::removeFromParent()
Expand Down Expand Up @@ -671,3 +671,52 @@ namespace details
}

}} // namespace tracktion { inline namespace engine

//==============================================================================
//==============================================================================
#if TRACKTION_UNIT_TESTS && ENGINE_UNIT_TESTS_CLIPS

#include "../../../3rd_party/doctest/tracktion_doctest.hpp"

namespace tracktion::inline engine
{

TEST_SUITE("tracktion_engine")
{
TEST_CASE("Trim away overlap")
{
auto& engine = *tracktion::engine::Engine::getEngines()[0];
auto edit = Edit::createSingleTrackEdit (engine);

auto clip = getAudioTracks (*edit)[0]->insertMIDIClip ({ 4_tp, 8_tp }, nullptr);
auto positionBeforeTrim = clip->getPosition();

SUBCASE ("Overlap at start")
{
clip->trimAwayOverlap ({ 2_tp, 6_tp });
CHECK (clip->getPosition().time == TimeRange (6_tp, 8_tp));
}

SUBCASE ("Overlap at end")
{
clip->trimAwayOverlap ({ 6_tp, 10_tp });
CHECK (clip->getPosition().time == TimeRange (4_tp, 6_tp));
}

SUBCASE ("No overlap at start")
{
clip->trimAwayOverlap ({ 2_tp, 4_tp });
CHECK (clip->getPosition().time == positionBeforeTrim.time);
}

SUBCASE ("No overlap at end")
{
clip->trimAwayOverlap ({ 10_tp, 12_tp });
CHECK (clip->getPosition().time == positionBeforeTrim.time);
}
}
}

} // namespace tracktion::inline engine

#endif

0 comments on commit 837f510

Please sign in to comment.