SPB Git

Repository Public MIT

GlassPlaybackMath.swift 5.7 KB 121 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : SP GlassKit7// File             : Sources/SPGlassMedia/Media/GlassPlaybackMath.swift8// Module           : SPGlassMedia9// Repository       : https://git.spboucher.ai/sp-glasskit10// Project Website  : https://glass.spboucher.ai11// Created          : 2026-12-0212// Last Modified    : 2026-12-0213//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : MIT33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import Foundation42import SPGlassCore4344/// Pure time arithmetic and formatting shared by the media components.45public enum GlassPlaybackMath {46    /// A usable duration in seconds: negative, NaN and infinite values become 0 (unknown).47    public static func sanitizedDuration(_ duration: TimeInterval) -> TimeInterval {48        duration.isFinite && duration > 0 ? duration : 049    }5051    /// Clamps a seek target to `0...duration`. NaN and negative targets become 0; an unknown duration gives 0.52    public static func clampedSeek(_ time: TimeInterval, duration: TimeInterval) -> TimeInterval {53        let limit = sanitizedDuration(duration)54        guard !time.isNaN else { return 0 }55        return min(max(time, 0), limit)56    }5758    /// The position reached by skipping `interval` seconds (negative to go back) from `time`.59    public static func skip(from time: TimeInterval, by interval: TimeInterval, duration: TimeInterval) -> TimeInterval {60        clampedSeek(clampedSeek(time, duration: duration) + (interval.isFinite ? interval : 0), duration: duration)61    }6263    /// Progress through the media, 0 to 1. An unknown duration gives 0.64    public static func fraction(currentTime: TimeInterval, duration: TimeInterval) -> Double {65        let limit = sanitizedDuration(duration)66        guard limit > 0 else { return 0 }67        return clampedSeek(currentTime, duration: limit) / limit68    }6970    /// Seconds left before the end, never negative.71    public static func remaining(currentTime: TimeInterval, duration: TimeInterval) -> TimeInterval {72        let limit = sanitizedDuration(duration)73        return limit - clampedSeek(currentTime, duration: limit)74    }7576    /// Formats seconds as `m:ss`, or `h:mm:ss` from one hour, with the `Duration` time format style.77    public static func formatted(_ seconds: TimeInterval, locale: Locale = .autoupdatingCurrent) -> String {78        let whole = seconds.isFinite ? max(seconds, 0).rounded(.down) : 079        let pattern: Duration.TimeFormatStyle.Pattern = whole >= 3600 ? .hourMinuteSecond : .minuteSecond80        return Duration.seconds(whole).formatted(Duration.TimeFormatStyle(pattern: pattern, locale: locale))81    }8283    /// Formats the time left with a leading minus sign, as transport bars do (`-1:05`).84    public static func formattedRemaining(currentTime: TimeInterval, duration: TimeInterval, locale: Locale = .autoupdatingCurrent) -> String {85        "\u{2212}" + formatted(remaining(currentTime: currentTime, duration: duration).rounded(.up), locale: locale)86    }8788    /// The spoken position, for example "1:05 of 3:20".89    public static func spokenPosition(currentTime: TimeInterval, duration: TimeInterval, locale: Locale = .autoupdatingCurrent) -> String {90        GlassL10n.string(.timeOfDuration, formatted(clampedSeek(currentTime, duration: duration), locale: locale), formatted(sanitizedDuration(duration), locale: locale))91    }92}9394/// What an audio session interruption means for a player.95public enum GlassAudioInterruptionEvent: Equatable, Sendable {96    /// Another audio source took over (phone call, alarm).97    case began98    /// The interruption ended; `shouldResume` is the system's hint.99    case ended(shouldResume: Bool)100}101102/// What the player does in response to an interruption.103public enum GlassAudioInterruptionAction: Equatable, Sendable {104    case pause, resume, none105}106107/// The interruption rule of ``GlassAVAudioPlayback``, separated so that it can be tested without audio.108public enum GlassAudioInterruptionPolicy {109    /// - Parameters:110    ///   - wasPlayingWhenInterrupted: whether playback was running when the interruption began.111    ///   - resumesAfterInterruption: the host's choice; playback never resumes by itself when false.112    public static func action(for event: GlassAudioInterruptionEvent, wasPlayingWhenInterrupted: Bool, resumesAfterInterruption: Bool) -> GlassAudioInterruptionAction {113        switch event {114        case .began:115            return .pause116        case .ended(let shouldResume):117            return shouldResume && wasPlayingWhenInterrupted && resumesAfterInterruption ? .resume : .none118        }119    }120}121
Files main
Menu

Theme