Repository Public
1// SP Lecture Studio — LectureCore2// MediaTime: exact integer timeline time (nanoseconds). Every stream of a project is expressed on this timebase.3// Author: Simon-Pierre Boucher — contact@spboucher.ai4import Foundation56/// A point (or a length) on the project timeline, stored as an exact number of nanoseconds.7/// Integer storage keeps 90+ minute timelines free of floating point drift; conversions to CMTime use timescale 1e9.8public struct MediaTime: Hashable, Comparable, Sendable, Codable, CustomStringConvertible {9 public var ns: Int641011 public static let zero = MediaTime(ns: 0)12 public static let timescale: Int32 = 1_000_000_0001314 public init(ns: Int64) { self.ns = ns }15 public init(seconds: Double) { self.ns = Int64((seconds * 1e9).rounded()) }16 public init(milliseconds: Int64) { self.ns = milliseconds * 1_000_000 }17 /// Exact time of a sample position: `frames / sampleRate`, rounded to the nearest nanosecond.18 public init(frames: Int64, sampleRate: Double) {19 self.ns = Int64((Double(frames) / sampleRate * 1e9).rounded())20 }2122 public var seconds: Double { Double(ns) / 1e9 }23 public var milliseconds: Int64 { ns / 1_000_000 }24 public func frames(atSampleRate rate: Double) -> Int64 { Int64((Double(ns) / 1e9 * rate).rounded()) }2526 public static func < (a: MediaTime, b: MediaTime) -> Bool { a.ns < b.ns }27 public static func + (a: MediaTime, b: MediaTime) -> MediaTime { MediaTime(ns: a.ns + b.ns) }28 public static func - (a: MediaTime, b: MediaTime) -> MediaTime { MediaTime(ns: a.ns - b.ns) }29 public static func += (a: inout MediaTime, b: MediaTime) { a.ns += b.ns }30 public static func -= (a: inout MediaTime, b: MediaTime) { a.ns -= b.ns }3132 public init(from decoder: Decoder) throws { ns = try decoder.singleValueContainer().decode(Int64.self) }33 public func encode(to encoder: Encoder) throws { var c = encoder.singleValueContainer(); try c.encode(ns) }3435 public var description: String { MediaTime.clock(self) }3637 /// `H:MM:SS` or `M:SS` clock string (tenths optional).38 public static func clock(_ t: MediaTime, tenths: Bool = false) -> String {39 let total = max(0, t.ns)40 let s = total / 1_000_000_00041 let h = s / 3600, m = (s % 3600) / 60, sec = s % 6042 var out = h > 0 ? String(format: "%d:%02d:%02d", h, m, sec) : String(format: "%d:%02d", m, sec)43 if tenths { out += String(format: ".%d", (total % 1_000_000_000) / 100_000_000) }44 return out45 }46}4748/// Half-open range `[start, end)` on the timeline.49public struct TimeRange: Hashable, Sendable, Codable, CustomStringConvertible {50 public var start: MediaTime51 public var end: MediaTime5253 public init(start: MediaTime, end: MediaTime) {54 self.start = min(start, end); self.end = max(start, end)55 }56 public init(start: MediaTime, duration: MediaTime) { self.init(start: start, end: start + duration) }57 public init(seconds a: Double, _ b: Double) { self.init(start: MediaTime(seconds: a), end: MediaTime(seconds: b)) }5859 public var duration: MediaTime { end - start }60 public var isEmpty: Bool { end <= start }61 public func contains(_ t: MediaTime) -> Bool { t >= start && t < end }62 public func overlaps(_ o: TimeRange) -> Bool { start < o.end && o.start < end }63 public func intersection(_ o: TimeRange) -> TimeRange? {64 let s = max(start, o.start), e = min(end, o.end)65 return s < e ? TimeRange(start: s, end: e) : nil66 }67 public func clamped(to o: TimeRange) -> TimeRange {68 let s = min(max(start, o.start), o.end), e = min(max(end, o.start), o.end)69 return TimeRange(start: s, end: e)70 }71 public var description: String { "[\(MediaTime.clock(start, tenths: true)) – \(MediaTime.clock(end, tenths: true)))" }7273 /// Sorted, non overlapping union of the given ranges (touching ranges are merged).74 public static func merged(_ ranges: [TimeRange]) -> [TimeRange] {75 var out: [TimeRange] = []76 for r in ranges.filter({ !$0.isEmpty }).sorted(by: { $0.start < $1.start }) {77 if let last = out.last, r.start <= last.end {78 out[out.count - 1].end = max(last.end, r.end)79 } else { out.append(r) }80 }81 return out82 }83}84