-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TimecodeInterval
: Added init(cmTime:)
and cmTime
property
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
Sources/TimecodeKit/TimecodeInterval/TimecodeInterval Rational CMTime.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// TimecodeInterval Rational CMTime.swift | ||
// TimecodeKit • https://github.com/orchetect/TimecodeKit | ||
// © 2022 Steffan Andrews • Licensed under MIT License | ||
// | ||
|
||
#if canImport(CoreMedia) | ||
|
||
import Foundation | ||
import CoreMedia | ||
|
||
@available(macOS 10.7, iOS 4.0, tvOS 9.0, watchOS 6.0, *) | ||
extension TimecodeInterval { | ||
/// Initialize from a time duration represented as a rational fraction. | ||
/// A negative fraction will produce a negative time interval. | ||
/// | ||
/// - Throws: ``ValidationError`` | ||
public init( | ||
_ cmTime: CMTime, | ||
at rate: TimecodeFrameRate, | ||
limit: Timecode.UpperLimit = ._24hours, | ||
base: Timecode.SubFramesBase = .default(), | ||
format: Timecode.StringFormat = .default() | ||
) throws { | ||
let fraction = Fraction(cmTime) | ||
try self.init(fraction, at: rate, limit: limit, base: base, format: format) | ||
} | ||
|
||
/// Returns the rational fraction for the timecode interval as `CMTime`. | ||
public var cmTime: CMTime { | ||
CMTime(rationalValue) | ||
} | ||
} | ||
|
||
#endif |
44 changes: 44 additions & 0 deletions
44
...deKit-Unit-Tests/Unit Tests/TimecodeInterval/TimecodeInterval Rational CMTime Tests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// | ||
// TimecodeInterval Rational CMTime Tests.swift | ||
// TimecodeKit • https://github.com/orchetect/TimecodeKit | ||
// © 2022 Steffan Andrews • Licensed under MIT License | ||
// | ||
|
||
#if shouldTestCurrentPlatform | ||
|
||
import XCTest | ||
@testable import TimecodeKit | ||
import CoreMedia | ||
|
||
class TimecodeInterval_Rational_CMTime_Tests: XCTestCase { | ||
override func setUp() { } | ||
override func tearDown() { } | ||
|
||
func testTimecodeInterval_init_cmTime() throws { | ||
let ti = try TimecodeInterval( | ||
CMTime(value: 60, timescale: 30), | ||
at: ._24 | ||
) | ||
|
||
XCTAssertEqual(ti.sign, .plus) | ||
|
||
XCTAssertEqual( | ||
ti.absoluteInterval, | ||
try TCC(s: 2).toTimecode(at: ._24) | ||
) | ||
} | ||
|
||
func testCMTime() throws { | ||
let ti = try TimecodeInterval( | ||
TCC(s: 2).toTimecode(at: ._24) | ||
) | ||
|
||
let cmTime = ti.cmTime | ||
|
||
XCTAssertEqual(cmTime.seconds.sign, .plus) | ||
XCTAssertEqual(cmTime.value, 2) | ||
XCTAssertEqual(cmTime.timescale, 1) | ||
} | ||
} | ||
|
||
#endif |