tracktion-engine 3.0-10-g034fdde4aa5
Tracktion Engine — High level data model for audio applications

« « « Anklang Documentation
Loading...
Searching...
No Matches
tracktion_EditTimeRange.h
Go to the documentation of this file.
1 /*
2 ,--. ,--. ,--. ,--.
3 ,-' '-.,--.--.,--,--.,---.| |,-.,-' '-.`--' ,---. ,--,--, Copyright 2024
4 '-. .-'| .--' ,-. | .--'| /'-. .-',--.| .-. || \ Tracktion Software
5 | | | | \ '-' \ `--.| \ \ | | | |' '-' '| || | Corporation
6 `---' `--' `--`--'`---'`--'`--' `---' `--' `---' `--''--' www.tracktion.com
7
8 Tracktion Engine uses a GPL/commercial licence - see LICENCE.md for details.
9*/
10
11#pragma once
12
13namespace tracktion { inline namespace engine { namespace legacy
14{
15
17{
18 EditTimeRange() = default;
19 EditTimeRange (const EditTimeRange&) = default;
20 EditTimeRange& operator= (const EditTimeRange&) = default;
21
22 EditTimeRange (double start, double end);
24
26 static EditTimeRange between (double time1, double time2);
27
29 static EditTimeRange withStartAndLength (double time1, double length);
30
32 static EditTimeRange emptyRange (double start);
33
34 double start = 0;
35 double end = 0;
36
37 double getStart() const { return start; }
38 double getEnd() const { return end; }
39 double getLength() const { return end - start; }
40 double getCentre() const { return (start + end) * 0.5; }
41 double clipValue (double value) const { return juce::jlimit (start, end, value); }
42
43 bool isEmpty() const { return end <= start; }
44
45 bool operator== (const EditTimeRange& other) const { return start == other.start && end == other.end; }
46 bool operator!= (const EditTimeRange& other) const { return ! operator== (other); }
47
48 bool overlaps (const EditTimeRange& other) const { return other.start < end && start < other.end; }
49 bool contains (const EditTimeRange& other) const { return other.start >= start && other.end <= end; }
50 bool contains (double time) const { return time >= start && time < end; }
51 bool containsInclusive (double time) const { return time >= start && time <= end; }
52
53 EditTimeRange getUnionWith (EditTimeRange other) const;
54 EditTimeRange getIntersectionWith (EditTimeRange other) const;
55 EditTimeRange rescaled (double anchorTime, double factor) const;
56 EditTimeRange constrainRange (EditTimeRange rangeToConstrain) const;
57 EditTimeRange expanded (double amount) const;
58 EditTimeRange reduced (double amount) const;
59 EditTimeRange movedToStartAt (double newStart) const;
60 EditTimeRange movedToEndAt (double newEnd) const;
61 EditTimeRange withStart (double newStart) const;
62 EditTimeRange withEnd (double newEnd) const;
63 EditTimeRange withLength (double newLength) const;
64
65 EditTimeRange operator+ (double amount) const;
66 EditTimeRange operator- (double amount) const { return operator+ (-amount); }
67};
68
69//==============================================================================
70// _ _ _ _
71// __| | ___ | |_ __ _ (_)| | ___
72// / _` | / _ \| __| / _` || || |/ __|
73// | (_| || __/| |_ | (_| || || |\__ \ _ _ _
74// \__,_| \___| \__| \__,_||_||_||___/(_)(_)(_)
75//
76// Code beyond this point is implementation detail...
77//
78//==============================================================================
79
80inline EditTimeRange::EditTimeRange (double s, double e) : start (s), end (e)
81{
82 jassert (s <= e);
83}
84
85inline EditTimeRange::EditTimeRange (juce::Range<double> timeRange)
86 : EditTimeRange (timeRange.getStart(), timeRange.getEnd())
87{
88}
89
90inline EditTimeRange EditTimeRange::between (double time1, double time2)
91{
92 return time1 <= time2 ? EditTimeRange (time1, time2)
93 : EditTimeRange (time2, time1);
94}
95
96inline EditTimeRange EditTimeRange::withStartAndLength (double startValue, double length)
97{
98 jassert (length >= 0.0);
99 return juce::Range<double>::withStartAndLength (startValue, length);
100}
101
103{
104 return EditTimeRange (start, start);
105}
106
107inline EditTimeRange EditTimeRange::getUnionWith (EditTimeRange other) const
108{
109 return { std::min (start, other.start),
110 std::max (end, other.end) };
111}
112
113inline EditTimeRange EditTimeRange::getIntersectionWith (EditTimeRange other) const
114{
115 auto newStart = std::max (start, other.start);
116 return { newStart, std::max (newStart, std::min (end, other.end)) };
117}
118
119inline EditTimeRange EditTimeRange::constrainRange (EditTimeRange rangeToConstrain) const
120{
121 auto otherLen = rangeToConstrain.getLength();
122
123 return getLength() <= otherLen
124 ? *this
125 : rangeToConstrain.movedToStartAt (juce::jlimit (start, end - otherLen,
126 rangeToConstrain.getStart()));
127}
128
129inline EditTimeRange EditTimeRange::rescaled (double anchorTime, double factor) const
130{
131 jassert (factor > 0);
132 return { anchorTime + (start - anchorTime) * factor,
133 anchorTime + (end - anchorTime) * factor };
134}
135
136inline EditTimeRange EditTimeRange::expanded (double amount) const
137{
138 jassert (amount >= 0);
139 return { start - amount, end + amount };
140}
141
142inline EditTimeRange EditTimeRange::reduced (double amount) const
143{
144 jassert (amount >= 0);
145 amount = std::min (amount, getLength() / 2.0);
146 return { start + amount, end - amount };
147}
148
149inline EditTimeRange EditTimeRange::movedToStartAt (double newStart) const
150{
151 return { newStart, end + (newStart - start) };
152}
153
154inline EditTimeRange EditTimeRange::movedToEndAt (double newEnd) const
155{
156 return { start + (newEnd - end), newEnd };
157}
158
159inline EditTimeRange EditTimeRange::withStart (double newStart) const
160{
161 jassert (newStart <= end);
162 return { newStart, std::max (end, newStart) };
163}
164
165inline EditTimeRange EditTimeRange::withEnd (double newEnd) const
166{
167 jassert (newEnd >= start);
168 return { std::min (start, newEnd), newEnd };
169}
170
171inline EditTimeRange EditTimeRange::withLength (double newLength) const
172{
173 jassert (newLength >= 0);
174 return { start, start + std::max (0.0, newLength) };
175}
176
177inline EditTimeRange EditTimeRange::operator+ (double amount) const
178{
179 return { start + amount, end + amount };
180}
181
182} // namespace legacy
183
185inline TimeRange toTimeRange (legacy::EditTimeRange r)
186{
187 return { TimePosition::fromSeconds (r.getStart()), TimePosition::fromSeconds (r.getEnd()) };
188}
189
191inline legacy::EditTimeRange toEditTimeRange (TimeRange r)
192{
193 return { r.getStart().inSeconds(), r.getEnd().inSeconds() };
194}
195
196}} // namespace tracktion { inline namespace engine
static Range withStartAndLength(const ValueType startValue, const ValueType length) noexcept
#define jassert(expression)
T max(T... args)
T min(T... args)
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
TimeRange toTimeRange(legacy::EditTimeRange r)
@temporary
legacy::EditTimeRange toEditTimeRange(TimeRange r)
@temporary
Represents a time range in an Edit stored as either time or beats.
static EditTimeRange emptyRange(double start)
Returns a range with the specified start position and a length of zero.
static EditTimeRange between(double time1, double time2)
Returns the range that lies between two positions (in either order).
static EditTimeRange withStartAndLength(double time1, double length)
Returns a range with a given start and length.
time