// 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.importCottonKitimportCottonValuesimportFoundation/// One song as the core answers it: what the engine plays and the face says.publicstructQueuedSong: Hashable, Sendable, Codable {publiclet id: String/// The song's audio, as the object's media key (`song.file`).publiclet file: Stringpublicvar title: String?publicvar artist: String?/// The album's record, which Listening fronts; none for a song on no album.publicvar album: String?publicvar albumTitle: String?publicvar cover: String?publicvar duration: Double?/// The gain it plays at, in decibels: the core's ReplayGain.publicvar gain: DoubleenumCodingKeys: String, CodingKey {case id, file, title, artist, album, albumTitle = "album_title", cover, duration = "duration_s", gain = "gain_db" }publicinit(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 = idself.file = fileself.title = titleself.artist = artistself.album = albumself.albumTitle = albumTitleself.cover = coverself.duration = durationself.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.publicstructPlayerPlaying: Decodable, Hashable, Sendable {/// `episode` or `film`.publiclet kind: String/// `episode`, `film` or `film_episode`.publiclet table: Stringpubliclet id: Stringpubliclet title: String/// An episode's show.publiclet artist: String?/// The art's media key: a show's artwork, a film's poster, an episode's still.publiclet cover: String?/// A series' episode's marquee words ("S2 E5 · The Long Night"), and its/// series; an episode's show's record.publiclet caption: String?publiclet series: String?publiclet podcast: String?publiclet 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.publiclet file: String?publiclet bundle: String?publiclet stream: String?publiclet resumeAt: Doublepubliclet skip: Doublepubliclet saveEvery: Double/// Seconds a series' next episode is counted in before it plays itself.publiclet nextAfter: Doublepubliclet tracks: Tracks?/// A film's "more" menu, the core's: its title and its choices in order.publiclet 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.publiclet discreet: Bool?publicstructMore: Decodable, Hashable, Sendable {publiclet title: Stringpubliclet choices: [Choice]publicstructChoice: Decodable, Hashable, Sendable {/// `subtitle_timing`, `use_frame` or `original_picture`.publiclet id: Stringpubliclet label: String/// `subtitles`: offered only for a film that carries subtitle tracks.publiclet needs: String?/// What is said when the frame could not be taken.publiclet failed: String? } }publicstructTracks: Decodable, Hashable, Sendable {publiclet subtitles: Bool/// None named is the phone's own language.publiclet subtitleLanguage: String?/// None named is the film's own audio.publiclet 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.publiclet choice: SubtitleChoice?/// This film's subtitle timing, milliseconds; negative shows them earlier.publiclet offsetMs: Int/// The timing control's words, the core's.publiclet timing: Timing?publicenumSubtitleChoice: Hashable, Sendable {case offcase track(language: String, name: String) }publicstructTiming: Decodable, Hashable, Sendable {publiclet title: Stringpubliclet earlier: Stringpubliclet later: Stringpubliclet reset: String/// Where the timing stands: "In time", "0.3 s later".publiclet said: String }publicinit(subtitles: Bool, subtitleLanguage: String?, audioLanguage: String?, choice: SubtitleChoice? = nil, offsetMs: Int = 0, timing: Timing? = nil) {self.subtitles = subtitlesself.subtitleLanguage = subtitleLanguageself.audioLanguage = audioLanguageself.choice = choiceself.offsetMs = offsetMsself.timing = timing }publicinit(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 } elseiflet 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) }privatestructChosen: Decodable {let language: Stringlet name: String? }enumCodingKeys: String, CodingKey {case subtitles, subtitleLanguage = "subtitle_language", audioLanguage = "audio_language", choice, offsetMs = "offset_ms", timing } }enumCodingKeys: 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.publicstructPlayerFollowing: Decodable, Hashable, Sendable {publiclet table: Stringpubliclet id: Stringpubliclet title: Stringpubliclet 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.publicstructPlayerRoute: Decodable, Hashable, Sendable {publicenumTo: String, Decodable, Hashable, Sendable { case receiver, phone }publiclet to: Topubliclet stream: String?publiclet checkAfterS: Double?publiclet said: String?enumCodingKeys: String, CodingKey {case to, stream, checkAfterS = "check_after_s", said }}/// The player as the core answers it.publicstructPlayerAnswer: Decodable, Hashable, Sendable {publicstructAt: Decodable, Hashable, Sendable {publiclet index: Intpubliclet of: Int }publicstructSettings: Decodable, Hashable, Sendable {publiclet crossfadeS: Doublepubliclet gapless: Boolpubliclet replayGain: Stringpubliclet speed: Doublepubliclet sleepMinutes: Doublepubliclet sleepUntil: String?publiclet shuffle: BoolenumCodingKeys: String, CodingKey {case crossfadeS = "crossfade_s", gapless, replayGain = "replay_gain", speed, sleepMinutes = "sleep_minutes", sleepUntil = "sleep_until", shuffle } }publicstructFace: Decodable, Hashable, Sendable {publiclet id: Stringpubliclet label: String }publicstructWords: Decodable, Hashable, Sendable {publiclet nothing: Stringpubliclet offline: Stringpubliclet refused: Stringpubliclet locked: String }/// The song playing.publiclet now: QueuedSong?/// The episode or the film playing.publiclet playing: PlayerPlaying?/// The songs after the song playing.publiclet queue: [QueuedSong]/// What plays after the episode or the film playing.publiclet following: [PlayerFollowing]publiclet at: Atpubliclet playedAfterS: Doublepubliclet settings: Settingspubliclet face: Stringpubliclet faces: [Face]publiclet words: Wordspubliclet refusal: String?/// What an act that worked says (a frame worn, the original back).publiclet said: String?/// Where the song stands, in seconds: where it was left, kept across a relaunch.publiclet positionS: Double?/// The song is held but not playing: kept from before, or its run over. A play loads it (`resume`).publiclet resting: Bool/// What the film playing does on the player's route (op `route`).publiclet route: PlayerRoute?enumCodingKeys: String, CodingKey {case now, queue, at, playedAfterS = "played_after_s", settings, face, faces, words, refusal, said, positionS = "position_s", resting, route }privatestructKind: Decodable { let kind: String }publicinit(from decoder: Decoder) throws {let fields = try decoder.container(keyedBy: CodingKeys.self)let kind = try fields.decodeIfPresent(Kind.self, forKey: .now)?.kindif 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.structPlayHeard: Equatable, Sendable {/// The record playing: its table and id.let table: Stringlet id: String/// Seconds heard, scrubbing aside.private(set) var heard: Double = 0/// Where it stood at the last tick.privatevar at: Double?/// The last report has gone: a play ends once.private(set) var reported = falseinit(table: String, id: String, from position: Double = 0) {self.table = tableself.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.mutatingfunc tick(_ position: Double, gap: Double = 2) {iflet at, position >= at, position - at <= gap { heard += position - at } at = position }/// Paused or sought: the next tick starts from there.mutatingfunc 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 { returnnil }return .object(fields(at: position, duration: duration, device: device)) }/// The report the op leaving this play carries (`listened`); nil once sent.mutatingfunc report(ended: String, at position: Double? = nil, duration: Double? = nil, device: String? = nil) -> RecordValue? {guard !reported else { returnnil } reported = truevar said = fields(at: position, duration: duration, device: device) said["ended"] = .text(ended)return .object(said) }privatefunc fields(at position: Double?, duration: Double?, device: String?) -> [String: RecordValue] {var fields: [String: RecordValue] = ["table": .text(table), "id": .text(id), "seconds": .number(heard)]iflet position, position.isFinite { fields["at_s"] = .number(max(0, position)) }iflet duration, duration.isFinite, duration > 0 { fields["duration_s"] = .number(duration) }iflet device { fields["device"] = .text(device) }return fields }}