Skip to content

Instantly share code, notes, and snippets.

@rydermackay
Last active August 29, 2015 14:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rydermackay/d25cc060bcfdde7ffe21 to your computer and use it in GitHub Desktop.
Save rydermackay/d25cc060bcfdde7ffe21 to your computer and use it in GitHub Desktop.
CoreMedia could use some Swift love…
// Tempus fugit
import CoreMedia
let defaultTimeScale = Int32(kCMTimeMaxTimescale)
extension CMTime {
static var invalid: CMTime { return kCMTimeInvalid }
static var indefinite: CMTime { return kCMTimeIndefinite }
static var positiveInifinity: CMTime { return kCMTimePositiveInfinity }
static var negativeInfinity: CMTime { return kCMTimeNegativeInfinity }
static var zero: CMTime { return kCMTimeZero }
init(value: Int64, timeScale: Int32, epoch: Int64 = 0) {
self = CMTimeMakeWithEpoch(value, timeScale, epoch)
}
init(_ seconds: Double, timeScale: Int32 = defaultTimeScale) {
self = CMTimeMakeWithSeconds(seconds, timeScale)
}
var seconds: Double { return CMTimeGetSeconds(self) }
var absoluteTime: CMTime { return CMTimeAbsoluteValue(self) }
func show() { CMTimeShow(self) }
}
extension CMTime: IntegerLiteralConvertible {
public static func convertFromIntegerLiteral(value: IntegerLiteralType) -> CMTime {
return CMTime(Double(value))
}
}
extension CMTime: FloatLiteralConvertible {
public static func convertFromFloatLiteral(value: FloatLiteralType) -> CMTime {
return CMTime(value)
}
}
func +(lhs: CMTime, rhs: CMTime) -> CMTime { return CMTimeAdd(lhs, rhs) }
func -(lhs: CMTime, rhs: CMTime) -> CMTime { return CMTimeSubtract(lhs, rhs) }
func +=(inout lhs: CMTime, rhs: CMTime) -> CMTime { lhs = CMTimeAdd(lhs, rhs); return lhs }
func -=(inout lhs: CMTime, rhs: CMTime) -> CMTime { lhs = CMTimeSubtract(lhs, rhs); return lhs }
func *(lhs: CMTime, rhs: Double) -> CMTime { return CMTimeMultiplyByFloat64(lhs, rhs) }
func *(lhs: Double, rhs: CMTime) -> CMTime { return CMTimeMultiplyByFloat64(rhs, lhs) }
prefix func -(rhs: CMTime) -> CMTime { return kCMTimeZero - rhs }
extension CMTimeRange {
static var zero: CMTimeRange { return kCMTimeRangeZero }
static var invalid: CMTimeRange { return kCMTimeRangeInvalid }
init(start: CMTime, duration: CMTime) {
self = CMTimeRangeMake(start, duration)
}
init(start: CMTime, end: CMTime) {
self = CMTimeRangeFromTimeToTime(start, end)
}
var end: CMTime { return CMTimeRangeGetEnd(self) }
func union(range: CMTimeRange) -> CMTimeRange { return CMTimeRangeGetUnion(self, range) }
func intersection(range: CMTimeRange) -> CMTimeRange { return CMTimeRangeGetIntersection(self, range) }
func contains(#time: CMTime) -> Boolean { return CMTimeRangeContainsTime(self, time) }
func contains(#timeRange: CMTimeRange) -> Boolean { return CMTimeRangeContainsTimeRange(self, timeRange) }
func show() { CMTimeRangeShow(self) }
}
let range = CMTimeRange(start: 0, duration: 2)
range.contains(time: 1.99)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment