Skip to content

Commit 342092f

Browse files
committed
Timecode: Added base parameter to converted(to:) instance method
1 parent 06d243d commit 342092f

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

Sources/TimecodeKit/Timecode/Timecode Conversion.swift

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,29 +9,45 @@ extension Timecode {
99

1010
/// Return a new `Timecode` object converted to a new frame rate.
1111
///
12-
/// - If `preservingValues` is `false` (default): entire timecode is converted based on the equivalent real time value.
12+
/// - Warning: This conversion process may be lossy.
1313
///
14-
/// - If `preservingValues` is `true`: Return a new `Timecode` instance at the new frame rate preserving literal timecode values if
15-
/// possible.
16-
/// If any value is not expressible at the new frame rate, the entire timecode will be converted.
14+
/// - Parameters:
15+
/// - newFrameRate: New frame rate.
16+
/// If the new rate is identical to the current rate, the timecode will be returned unchanged.
1717
///
18-
/// - Note: this process may be lossy.
18+
/// - newSubFramesBase: New sub-frames base (optional).
19+
///
20+
/// If not specified, the current sub-frames base will be preserved.
21+
///
22+
/// - preservingValues: Preserve literal timecode component values. (Default: `false`)
23+
///
24+
/// When `true`, a new `Timecode` instance at the new frame rate is returned, preserving literal timecode values
25+
/// if possible. If any values are invalid at the new frame rate, an error is thrown.
26+
/// If any value is not expressible at the new frame rate, the entire timecode will be converted.
27+
/// When `false`, the timecode is converted to the new frame rate based on the equivalent real time value.
1928
///
2029
/// - Throws: ``ValidationError``
30+
///
31+
/// - Returns: A new `Timecode` instance converted to the new frame rate and sub-frames base.
2132
public func converted(
2233
to newFrameRate: TimecodeFrameRate,
34+
base newSubFramesBase: Timecode.SubFramesBase? = nil,
2335
preservingValues: Bool = false
2436
) throws -> Timecode {
25-
// just return self if frameRate is equal
26-
guard frameRate != newFrameRate else {
37+
let newSubFramesBase = newSubFramesBase ?? subFramesBase
38+
39+
// just return self if new parameters are equal to current parameters
40+
guard frameRate != newFrameRate ||
41+
subFramesBase != newSubFramesBase
42+
else {
2743
return self
2844
}
2945

3046
if preservingValues,
3147
let newTC = try? Timecode(
3248
.components(components),
3349
at: newFrameRate,
34-
base: subFramesBase,
50+
base: newSubFramesBase,
3551
limit: upperLimit
3652
)
3753
{
@@ -43,7 +59,7 @@ extension Timecode {
4359
return try Timecode(
4460
.realTime(seconds: realTimeValue),
4561
at: newFrameRate,
46-
base: subFramesBase,
62+
base: newSubFramesBase,
4763
limit: upperLimit
4864
)
4965
}

0 commit comments

Comments
 (0)