// What the core answers the one player (`shell.player`, 5 Nov 2026; its // episodes and films, 6 Nov): what plays — a song (`now`), or a podcast // episode or a film (`playing`) — what plays after it, the settings, the face // worn and the words. The core keeps the queue, the listen rule, where a play // starts and the settings rows; the phone plays what it says and draws it. // Plain values, decoded as the core writes them. import CottonKit import CottonValues import Foundation /// One song as the core answers it: what the engine plays and the face says. public struct QueuedSong: Hashable, Sendable, Codable { public let id: String /// The song's audio, as the object's media key (`song.file`). public let file: String public var title: String? public var artist: String? /// The album's record, which Listening fronts; none for a song on no album. public var album: String? public var albumTitle: String? public var cover: String? public var duration: Double? /// The gain it plays at, in decibels: the core's ReplayGain. public var gain: Double enum CodingKeys: String, CodingKey { case id, file, title, artist, album, albumTitle = "album_title", cover, duration = "duration_s", gain = "gain_db" } public init(id: String, file: String, title: String? = nil, artist: String? = nil, album: String? = nil, albumTitle: String? = nil, cover: String? = nil, duration: Double? = nil, gain: Double = 0) { self.id = id self.file = file self.title = title self.artist = artist self.album = album self.albumTitle = albumTitle self.cover = cover self.duration = duration self.gain = gain } } /// A podcast episode or a film as the core answers it playing: what the /// instrument opens, where it starts and how often it reports, and what the /// lock screen and the marquee say. Every rule is the core's. public struct PlayerPlaying: Decodable, Hashable, Sendable { /// `episode` or `film`. public let kind: String /// `episode`, `film` or `film_episode`. public let table: String public let id: String public let title: String /// An episode's show. public let artist: String? /// The art's media key: a show's artwork, a film's poster, an episode's still. public let cover: String? /// A series' episode's marquee words ("S2 E5 · The Long Night"), and its /// series; an episode's show's record. public let caption: String? public let series: String? public let podcast: String? public let duration: Double /// What it plays from: a kept file (an episode's name in the podcast /// folder, a film's media key), a kept stream's bundle (its bookmark, /// base64), or an address to stream. public let file: String? public let bundle: String? public let stream: String? public let resumeAt: Double public let skip: Double public let saveEvery: Double /// Seconds a series' next episode is counted in before it plays itself. public let nextAfter: Double public let tracks: Tracks? /// A film's "more" menu, the core's: its title and its choices in order. public let more: More? /// A hidden title (HIDE FROM SCREEN): wherever the phone shows what plays outside the app (the lock screen, the /// system's now playing, a TV), it says "Cinemas" and shows no picture. The core decides; the phone follows. public let discreet: Bool? public struct More: Decodable, Hashable, Sendable { public let title: String public let choices: [Choice] public struct Choice: Decodable, Hashable, Sendable { /// `subtitle_timing`, `use_frame` or `original_picture`. public let id: String public let label: String /// `subtitles`: offered only for a film that carries subtitle tracks. public let needs: String? /// What is said when the frame could not be taken. public let failed: String? } } public struct Tracks: Decodable, Hashable, Sendable { public let subtitles: Bool /// None named is the phone's own language. public let subtitleLanguage: String? /// None named is the film's own audio. public let audioLanguage: String? /// The track the owner last chose in this film's player, over the /// cinema's settings: the core keeps it on the film's row. public let choice: SubtitleChoice? /// This film's subtitle timing, milliseconds; negative shows them earlier. public let offsetMs: Int /// The timing control's words, the core's. public let timing: Timing? public enum SubtitleChoice: Hashable, Sendable { case off case track(language: String, name: String) } public struct Timing: Decodable, Hashable, Sendable { public let title: String public let earlier: String public let later: String public let reset: String /// Where the timing stands: "In time", "0.3 s later". public let said: String } public init(subtitles: Bool, subtitleLanguage: String?, audioLanguage: String?, choice: SubtitleChoice? = nil, offsetMs: Int = 0, timing: Timing? = nil) { self.subtitles = subtitles self.subtitleLanguage = subtitleLanguage self.audioLanguage = audioLanguage self.choice = choice self.offsetMs = offsetMs self.timing = timing } public init(from decoder: Decoder) throws { let c = try decoder.container(keyedBy: CodingKeys.self) subtitles = try c.decode(Bool.self, forKey: .subtitles) subtitleLanguage = try c.decodeIfPresent(String.self, forKey: .subtitleLanguage) audioLanguage = try c.decodeIfPresent(String.self, forKey: .audioLanguage) if (try? c.decodeIfPresent(String.self, forKey: .choice)) == "off" { choice = .off } else if let chosen = try? c.decodeIfPresent(Chosen.self, forKey: .choice) { choice = .track(language: chosen.language, name: chosen.name ?? "") } else { choice = nil } offsetMs = (try? c.decodeIfPresent(Int.self, forKey: .offsetMs)) ?? 0 timing = try? c.decodeIfPresent(Timing.self, forKey: .timing) } private struct Chosen: Decodable { let language: String let name: String? } enum CodingKeys: String, CodingKey { case subtitles, subtitleLanguage = "subtitle_language", audioLanguage = "audio_language", choice, offsetMs = "offset_ms", timing } } enum CodingKeys: String, CodingKey { case kind, table, id, title, artist, cover, caption, series, podcast, duration = "duration_s", file, bundle, stream, resumeAt = "resume_at_s", skip = "skip_s", saveEvery = "save_every_s", nextAfter = "next_after_s", tracks, more, discreet } /// What the marquee says: an episode's caption, a film's title. var marquee: String { caption ?? title } } /// One episode or film after the one playing, as the queue says it. public struct PlayerFollowing: Decodable, Hashable, Sendable { public let table: String public let id: String public let title: String public let caption: String? } /// What a film does on the player's route, as the core answers `route` /// (cinemas-airplay, 18 Nov 2026): the TV plays it (`receiver`), from /// `stream` when its address changes for it, checked after `checkAfterS`; or /// the phone plays it and its picture goes to the TV (`phone`), `said` once. public struct PlayerRoute: Decodable, Hashable, Sendable { public enum To: String, Decodable, Hashable, Sendable { case receiver, phone } public let to: To public let stream: String? public let checkAfterS: Double? public let said: String? enum CodingKeys: String, CodingKey { case to, stream, checkAfterS = "check_after_s", said } } /// The player as the core answers it. public struct PlayerAnswer: Decodable, Hashable, Sendable { public struct At: Decodable, Hashable, Sendable { public let index: Int public let of: Int } public struct Settings: Decodable, Hashable, Sendable { public let crossfadeS: Double public let gapless: Bool public let replayGain: String public let speed: Double public let sleepMinutes: Double public let sleepUntil: String? public let shuffle: Bool enum CodingKeys: String, CodingKey { case crossfadeS = "crossfade_s", gapless, replayGain = "replay_gain", speed, sleepMinutes = "sleep_minutes", sleepUntil = "sleep_until", shuffle } } public struct Face: Decodable, Hashable, Sendable { public let id: String public let label: String } public struct Words: Decodable, Hashable, Sendable { public let nothing: String public let offline: String public let refused: String public let locked: String } /// The song playing. public let now: QueuedSong? /// The episode or the film playing. public let playing: PlayerPlaying? /// The songs after the song playing. public let queue: [QueuedSong] /// What plays after the episode or the film playing. public let following: [PlayerFollowing] public let at: At public let playedAfterS: Double public let settings: Settings public let face: String public let faces: [Face] public let words: Words public let refusal: String? /// What an act that worked says (a frame worn, the original back). public let said: String? /// Where the song stands, in seconds: where it was left, kept across a relaunch. public let positionS: Double? /// The song is held but not playing: kept from before, or its run over. A play loads it (`resume`). public let resting: Bool /// What the film playing does on the player's route (op `route`). public let route: PlayerRoute? enum CodingKeys: String, CodingKey { case now, queue, at, playedAfterS = "played_after_s", settings, face, faces, words, refusal, said, positionS = "position_s", resting, route } private struct Kind: Decodable { let kind: String } public init(from decoder: Decoder) throws { let fields = try decoder.container(keyedBy: CodingKeys.self) let kind = try fields.decodeIfPresent(Kind.self, forKey: .now)?.kind if kind == nil || kind == "song" { now = try fields.decodeIfPresent(QueuedSong.self, forKey: .now) playing = nil queue = try fields.decode([QueuedSong].self, forKey: .queue) following = [] } else { now = nil playing = try fields.decode(PlayerPlaying.self, forKey: .now) queue = [] following = try fields.decode([PlayerFollowing].self, forKey: .queue) } at = try fields.decode(At.self, forKey: .at) playedAfterS = try fields.decode(Double.self, forKey: .playedAfterS) settings = try fields.decode(Settings.self, forKey: .settings) face = try fields.decode(String.self, forKey: .face) faces = try fields.decode([Face].self, forKey: .faces) words = try fields.decode(Words.self, forKey: .words) refusal = try fields.decodeIfPresent(String.self, forKey: .refusal) said = try fields.decodeIfPresent(String.self, forKey: .said) positionS = try fields.decodeIfPresent(Double.self, forKey: .positionS) resting = try fields.decodeIfPresent(Bool.self, forKey: .resting) ?? false route = try fields.decodeIfPresent(PlayerRoute.self, forKey: .route) } /// When the sleep timer stops the music, while one runs. var sleepsAt: Date? { let format = ISO8601DateFormatter() format.formatOptions = [.withInternetDateTime, .withFractionalSeconds] return settings.sleepUntil.flatMap(format.date(from:)) } } /// What the instrument heard of one play, forward only (a scrub is no /// listening), and its reports to the core: a song's once, as the play ends; /// an episode's and a film's where it stands, while it plays and as it ends. struct PlayHeard: Equatable, Sendable { /// The record playing: its table and id. let table: String let id: String /// Seconds heard, scrubbing aside. private(set) var heard: Double = 0 /// Where it stood at the last tick. private var at: Double? /// The last report has gone: a play ends once. private(set) var reported = false init(table: String, id: String, from position: Double = 0) { self.table = table self.id = id at = position } /// The player stands at `position` while playing: a step forward no /// longer than `gap` seconds is heard; a longer one, or one back, is a /// scrub. mutating func tick(_ position: Double, gap: Double = 2) { if let at, position >= at, position - at <= gap { heard += position - at } at = position } /// Paused or sought: the next tick starts from there. mutating func jumped(to position: Double?) { at = position } /// Where the play stands, while it goes on (`listened` with no end). func standing(at position: Double, duration: Double?, device: String? = nil) -> RecordValue? { guard !reported, position.isFinite else { return nil } return .object(fields(at: position, duration: duration, device: device)) } /// The report the op leaving this play carries (`listened`); nil once sent. mutating func report(ended: String, at position: Double? = nil, duration: Double? = nil, device: String? = nil) -> RecordValue? { guard !reported else { return nil } reported = true var said = fields(at: position, duration: duration, device: device) said["ended"] = .text(ended) return .object(said) } private func fields(at position: Double?, duration: Double?, device: String?) -> [String: RecordValue] { var fields: [String: RecordValue] = ["table": .text(table), "id": .text(id), "seconds": .number(heard)] if let position, position.isFinite { fields["at_s"] = .number(max(0, position)) } if let duration, duration.isFinite, duration > 0 { fields["duration_s"] = .number(duration) } if let device { fields["device"] = .text(device) } return fields } }