12
12
13
13
namespace opentime { namespace OPENTIME_VERSION {
14
14
15
+ // / @brief This enumeration provides options for drop frame timecode.
15
16
enum IsDropFrameRate : int
16
17
{
17
18
InferFromRate = -1 ,
18
19
ForceNo = 0 ,
19
20
ForceYes = 1 ,
20
21
};
21
22
23
+ // / @brief Returns the absolute value.
24
+ // /
25
+ // / \todo Document why this function is used instead of "std::fabs()".
22
26
constexpr double
23
27
fabs (double val) noexcept
24
28
{
@@ -31,77 +35,104 @@ fabs(double val) noexcept
31
35
return bits.f ;
32
36
}
33
37
38
+ // / @brief This class represents a measure of time defined by a value and rate.
34
39
class RationalTime
35
40
{
36
41
public:
42
+ // / @brief Construct a new time with an optional value and rate.
37
43
explicit constexpr RationalTime (double value = 0 , double rate = 1 ) noexcept
38
44
: _value{ value }
39
45
, _rate{ rate }
40
46
{}
41
47
48
+ // / @brief Returns true if the time is invalid.
49
+ // /
50
+ // / The time is considered invalid if the value or rate are a NaN value,
51
+ // / or if the rate is less than or equal to zero.
42
52
bool is_invalid_time () const noexcept
43
53
{
44
54
return (std::isnan (_rate) || std::isnan (_value)) ? true : (_rate <= 0 );
45
55
}
46
56
57
+ // / @brief Returns the time value.
47
58
constexpr double value () const noexcept { return _value; }
48
59
60
+ // / @brief Returns the time rate.
49
61
constexpr double rate () const noexcept { return _rate; }
50
62
63
+ // / @brief Returns the time converted to a new rate.
51
64
constexpr RationalTime rescaled_to (double new_rate) const noexcept
52
65
{
53
66
return RationalTime{ value_rescaled_to (new_rate), new_rate };
54
67
}
55
68
69
+ // / @brief Returns the time converted to a new rate.
56
70
constexpr RationalTime rescaled_to (RationalTime rt) const noexcept
57
71
{
58
72
return RationalTime{ value_rescaled_to (rt._rate ), rt._rate };
59
73
}
60
74
75
+ // / @brief Returns the time value converted to a new rate.
61
76
constexpr double value_rescaled_to (double new_rate) const noexcept
62
77
{
63
78
return new_rate == _rate ? _value : (_value * new_rate) / _rate;
64
79
}
65
80
81
+ // / @brief Returns the time value converted to a new rate.
66
82
constexpr double value_rescaled_to (RationalTime rt) const noexcept
67
83
{
68
84
return value_rescaled_to (rt._rate );
69
85
}
70
86
87
+ // / @brief Returns whether time is almost equal to another time.
88
+ // /
89
+ // / @param other The other time for comparison.
90
+ // / @param delta The tolerance used for the comparison.
71
91
constexpr bool
72
92
almost_equal (RationalTime other, double delta = 0 ) const noexcept
73
93
{
74
94
return fabs (value_rescaled_to (other._rate ) - other._value ) <= delta;
75
95
}
76
96
77
- // Return whether the value and rate are equal to another RationalTime.
78
- // This is different from the operator "==" that rescales the time before
79
- // comparison.
97
+ // / @brief Returns whether the value and rate are equal to another time.
98
+ // /
99
+ // / This is different from the operator "==" that rescales the time before
100
+ // / comparison.
80
101
constexpr bool strictly_equal (RationalTime other) const noexcept
81
102
{
82
103
return _value == other._value && _rate == other._rate ;
83
104
}
84
105
85
- // Return a RationalTime with the largest integer value not greater than
86
- // this value.
106
+ // / @brief Returns a time with the largest integer value not greater than
107
+ // / this value.
87
108
RationalTime floor () const
88
109
{
89
110
return RationalTime{ std::floor (_value), _rate };
90
111
}
91
112
92
- // Return a RationalTime with the smallest integer value not less than
93
- // this value.
113
+ // / @brief Returns a time with the smallest integer value not less than
114
+ // / this value.
94
115
RationalTime ceil () const
95
116
{
96
117
return RationalTime{ std::ceil (_value), _rate };
97
118
}
98
119
99
- // Return a RationalTime with the nearest integer value to this value.
120
+ // / @brief Returns a time with the nearest integer value to this value.
100
121
RationalTime round () const
101
122
{
102
123
return RationalTime{ std::round (_value), _rate };
103
124
}
104
125
126
+ // / @brief Compute the duration of samples from first to last (excluding
127
+ // / last).
128
+ // /
129
+ // / Note that this is not the same as distance.
130
+ // /
131
+ // / For example, the duration of a clip from frame 10 to frame 15 is 5
132
+ // / frames. The result will be in the rate of start time.
133
+ // /
134
+ // / @param start_time The start time.
135
+ // / @param end_time_exclusive The exclusive end time.
105
136
static constexpr RationalTime duration_from_start_end_time (
106
137
RationalTime start_time,
107
138
RationalTime end_time_exclusive) noexcept
@@ -116,6 +147,16 @@ class RationalTime
116
147
start_time._rate };
117
148
}
118
149
150
+ // / @brief Compute the duration of samples from first to last (including
151
+ // / last).
152
+ // /
153
+ // / Note that this is not the same as distance.
154
+ // /
155
+ // / For example, the duration of a clip from frame 10 to frame 15 is 6
156
+ // / frames. Result will be in the rate of start time.
157
+ // /
158
+ // / @param start_time The start time.
159
+ // / @param end_time_exclusive The inclusive end time.
119
160
static constexpr RationalTime duration_from_start_end_time_inclusive (
120
161
RationalTime start_time,
121
162
RationalTime end_time_inclusive) noexcept
@@ -130,80 +171,112 @@ class RationalTime
130
171
start_time._rate };
131
172
}
132
173
174
+ // / @brief Returns true if the rate is valid for use with timecode.
133
175
static bool is_valid_timecode_rate (double rate);
134
176
177
+ // / @brief Returns the first valid timecode rate that has the least
178
+ // / difference from rate.
135
179
static double nearest_valid_timecode_rate (double rate);
136
180
181
+ // / @brief Convert a frame number and rate into a time.
137
182
static constexpr RationalTime
138
183
from_frames (double frame, double rate) noexcept
139
184
{
140
185
return RationalTime{ double (int (frame)), rate };
141
186
}
142
187
188
+ // / @brief Convert a value in seconds and rate into a time.
143
189
static constexpr RationalTime
144
190
from_seconds (double seconds, double rate) noexcept
145
191
{
146
192
return RationalTime{ seconds, 1 }.rescaled_to (rate);
147
193
}
148
194
195
+ // / @brief Convert a value in seconds into a time.
149
196
static constexpr RationalTime from_seconds (double seconds) noexcept
150
197
{
151
198
return RationalTime{ seconds, 1 };
152
199
}
153
200
201
+ // / @brief Convert a timecode string ("HH:MM:SS;FRAME") into a time.
202
+ // /
203
+ // / @param timecode The timecode string.
204
+ // / @param rate The timecode rate.
205
+ // / @param error_status Optional error status.
154
206
static RationalTime from_timecode (
155
207
std::string const & timecode,
156
208
double rate,
157
209
ErrorStatus* error_status = nullptr );
158
210
159
- // parse a string in the form
160
- // hours:minutes:seconds
161
- // which may have a leading negative sign. seconds may have up to
162
- // microsecond precision.
211
+ // / @brief Parse a string in the form "hours:minutes:seconds".
212
+ // /
213
+ // / The string may have a leading negative sign.
214
+ // /
215
+ // / Seconds may have up to microsecond precision.
216
+ // /
217
+ // / @param time_string The time string.
218
+ // / @param rate The time rate.
219
+ // / @param error_status Optional error status.
163
220
static RationalTime from_time_string (
164
221
std::string const & time_string,
165
222
double rate,
166
223
ErrorStatus* error_status = nullptr );
167
224
225
+ // / @brief Returns the frame number based on the current rate.
168
226
constexpr int to_frames () const noexcept { return int (_value); }
169
227
228
+ // / @brief Returns the frame number based on the given rate.
170
229
constexpr int to_frames (double rate) const noexcept
171
230
{
172
231
return int (value_rescaled_to (rate));
173
232
}
174
233
234
+ // / @brief Returns the value in seconds.
175
235
constexpr double to_seconds () const noexcept
176
236
{
177
237
return value_rescaled_to (1 );
178
238
}
179
239
240
+ // / @brief Convert to timecode (e.g., "HH:MM:SS;FRAME").
241
+ // /
242
+ // / @param rate The timecode rate.
243
+ // / @param drop_frame Whether to use drop frame timecode.
244
+ // / @param error_status Optional error status.
180
245
std::string to_timecode (
181
246
double rate,
182
247
IsDropFrameRate drop_frame,
183
248
ErrorStatus* error_status = nullptr ) const ;
184
249
250
+ // / @brief Convert to timecode (e.g., "HH:MM:SS;FRAME").
185
251
std::string to_timecode (ErrorStatus* error_status = nullptr ) const
186
252
{
187
253
return to_timecode (_rate, IsDropFrameRate::InferFromRate, error_status);
188
254
}
189
255
256
+ // / @brief Convert to the nearest timecode (e.g., "HH:MM:SS;FRAME").
257
+ // /
258
+ // / @param rate The timecode rate.
259
+ // / @param drop_frame Whether to use drop frame timecode.
260
+ // / @param error_status Optional error status.
190
261
std::string to_nearest_timecode (
191
262
double rate,
192
263
IsDropFrameRate drop_frame,
193
264
ErrorStatus* error_status = nullptr ) const ;
194
265
266
+ // / @brief Convert to the nearest timecode (e.g., "HH:MM:SS;FRAME").
195
267
std::string to_nearest_timecode (ErrorStatus* error_status = nullptr ) const
196
268
{
197
269
return to_nearest_timecode (_rate, IsDropFrameRate::InferFromRate, error_status);
198
270
}
199
271
200
-
201
- // produce a string in the form
202
- // hours:minutes:seconds
203
- // which may have a leading negative sign. seconds may have up to
204
- // microsecond precision .
272
+ // / @brief Return a string in the form "hours:minutes:seconds".
273
+ // /
274
+ // / Seconds may have up to microsecond precision.
275
+ // /
276
+ // / @return The time string, which may have a leading negative sign .
205
277
std::string to_time_string () const ;
206
278
279
+ // / @brief Add a time to this time.
207
280
constexpr RationalTime const & operator +=(RationalTime other) noexcept
208
281
{
209
282
if (_rate < other._rate )
@@ -218,6 +291,7 @@ class RationalTime
218
291
return *this ;
219
292
}
220
293
294
+ // / @brief Subtract a time from this time.
221
295
constexpr RationalTime const & operator -=(RationalTime other) noexcept
222
296
{
223
297
if (_rate < other._rate )
@@ -232,6 +306,7 @@ class RationalTime
232
306
return *this ;
233
307
}
234
308
309
+ // / @brief Return the addition of two times.
235
310
friend constexpr RationalTime
236
311
operator +(RationalTime lhs, RationalTime rhs) noexcept
237
312
{
@@ -244,6 +319,7 @@ class RationalTime
244
319
lhs._rate };
245
320
}
246
321
322
+ // / @brief Return the subtraction of two times.
247
323
friend constexpr RationalTime
248
324
operator -(RationalTime lhs, RationalTime rhs) noexcept
249
325
{
@@ -256,39 +332,60 @@ class RationalTime
256
332
lhs._rate };
257
333
}
258
334
335
+ // / @brief Return the negative of this time.
259
336
friend constexpr RationalTime operator -(RationalTime lhs) noexcept
260
337
{
261
338
return RationalTime{ -lhs._value , lhs._rate };
262
339
}
263
340
341
+ // / @brief Return whether a time is greater than another time.
264
342
friend constexpr bool operator >(RationalTime lhs, RationalTime rhs) noexcept
265
343
{
266
344
return (lhs._value / lhs._rate ) > (rhs._value / rhs._rate );
267
345
}
268
346
347
+ // / @brief Return whether a time is greater or equal to another time.
269
348
friend constexpr bool
270
349
operator >=(RationalTime lhs, RationalTime rhs) noexcept
271
350
{
272
351
return (lhs._value / lhs._rate ) >= (rhs._value / rhs._rate );
273
352
}
274
353
354
+ // / @brief Return whether a time is less than another time.
275
355
friend constexpr bool operator <(RationalTime lhs, RationalTime rhs) noexcept
276
356
{
277
357
return !(lhs >= rhs);
278
358
}
279
359
360
+ // / @brief Return whether a time is less than or equal to another time.
280
361
friend constexpr bool
281
362
operator <=(RationalTime lhs, RationalTime rhs) noexcept
282
363
{
283
364
return !(lhs > rhs);
284
365
}
285
366
367
+ // / @brief Return whether two times are equal.
368
+ // /
369
+ // / Note that the right hand side time is rescaled to the rate of the
370
+ // / left hand side time. To compare two times without scaling, use
371
+ // / strictly_equal().
372
+ // /
373
+ // / @param lhs Left hand side time.
374
+ // / @param lhs Right hand side time.
286
375
friend constexpr bool
287
376
operator ==(RationalTime lhs, RationalTime rhs) noexcept
288
377
{
289
378
return lhs.value_rescaled_to (rhs._rate ) == rhs._value ;
290
379
}
291
380
381
+ // / @brief Return whether two times are not equal.
382
+ // /
383
+ // / Note that the right hand side time is rescaled to the rate of the
384
+ // / left hand side time. To compare two times without scaling, use
385
+ // / strictly_equal().
386
+ // /
387
+ // / @param lhs Left hand side time.
388
+ // / @param lhs Right hand side time.
292
389
friend constexpr bool
293
390
operator !=(RationalTime lhs, RationalTime rhs) noexcept
294
391
{
0 commit comments