Skip to content

Commit

Permalink
TimecodeInterval: Added init(cmTime:) and cmTime property
Browse files Browse the repository at this point in the history
  • Loading branch information
orchetect committed Jan 3, 2023
1 parent 21214b3 commit 0533640
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
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
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

0 comments on commit 0533640

Please sign in to comment.