# SPDX-License-Identifier: MIT """Output for people: a labelled palette, PNG frames and WAV audio (frontend side). The board produces colour-register values (hue << 4 | luma), never RGB (docs/start-here.md "Output"). Colours differ per console and per television ([AHRM] 6.3 "A word on colors"), so a palette is a frontend choice and says where it comes from. """ from __future__ import annotations import math import struct import wave import zlib from pathlib import Path def ntsc_palette() -> list[tuple[int, int, int]]: """A computed NTSC palette, labelled: YIQ with hue 1 at the colour burst's "gold" phase and 24 degree steps per hue, luma in 16 equal steps, hue 0 grey ([AHRM] 6.3 describes the encoding; the numbers here are a choice, not a measurement). HYPOTHESIS for every number (33-degree offset, 24-degree step, saturation 0.22, linear luma): a frontend palette, outside the specification; a capture of a console's composite output would replace it.""" colours = [] for value in range(256): hue, luma = value >> 4, value & 0x0F y = luma / 15.0 if hue == 0: i = q = 0.0 else: angle = math.radians(180.0 - 24.0 * (hue - 1) + 33.0) sat = 0.22 i, q = sat * math.cos(angle), sat * math.sin(angle) r = y + 0.956 * i + 0.621 * q g = y - 0.272 * i - 0.647 * q b = y - 1.106 * i + 1.703 * q colours.append(tuple(max(0, min(255, round(c * 255))) for c in (r, g, b))) return colours PALETTE_LABEL = "computed NTSC YIQ, 24-degree hue steps (a frontend choice; see media.py)" def write_png( path: str | Path, frame: bytes, width: int = 456, height: int = 262, palette: list[tuple[int, int, int]] | None = None, ) -> None: """Write a frame of colour-register values as an RGB PNG (pixels doubled vertically).""" palette = palette or ntsc_palette() lut = [bytes(c) for c in palette] rows = bytearray() for y in range(height): line = b"".join(lut[v] for v in frame[y * width : (y + 1) * width]) rows += b"\x00" + line + b"\x00" + line def chunk(kind: bytes, data: bytes) -> bytes: return ( struct.pack(">I", len(data)) + kind + data + struct.pack(">I", zlib.crc32(kind + data)) ) header = struct.pack(">IIBBBBB", width, height * 2, 8, 2, 0, 0, 0) png = ( b"\x89PNG\r\n\x1a\n" + chunk(b"IHDR", header) + chunk(b"IDAT", zlib.compress(bytes(rows), 6)) ) Path(path).write_bytes(png + chunk(b"IEND", b"")) class AudioTap: """Samples POKEY's mix and GTIA's speaker bit (CONSOL bit 3) into 16-bit mono. pokey-python's ``mix()`` is a labelled fit of the chip's output curve; the speaker bit is added as a fixed step ([BUCH]: CONSOL bit 3 is the speaker). The level is read every ``step`` machine cycles and averaged over each output sample: a box filter, not a model of the console's audio circuit. HYPOTHESIS: the 0.8 / 0.2 weights of POKEY and the speaker (a scope capture of the console's audio output with each source alone would set them); the speaker bit itself is DOC ([BUCH]). Gameplay envelopes against Atari800 (OBSERVED): r = 0.73-0.92 on pokey-python 6ed1ffa (validation.md milestone 4). """ def __init__(self, board, rate: int = 44100, step: int = 8) -> None: self.board = board self.rate = rate self.step = step self.per_sample = board.machine.cycle_hz / rate self.samples = bytearray() self._acc = 0.0 self._n = 0 self._next = 0 self._edge = self.per_sample self._since = 0.0 def poll(self) -> None: """Call once per machine cycle (the board does, when a tap is attached).""" self._next -= 1 if self._next > 0: return self._next = self.step board = self.board level = board.pokey.mix() * 0.8 if board.gtia.consol_out & 8: level += 0.2 self._acc += level self._n += 1 self._since += self.step if self._since >= self._edge: self._since -= self._edge value = self._acc / self._n if self._n else 0.0 self._acc = 0.0 self._n = 0 # Silence (mix 0, speaker off) is sample 0; full scale is 1.0. sample = max(-32768, min(32767, int(value * 32767))) self.samples += struct.pack(" None: with wave.open(str(path), "wb") as out: out.setnchannels(1) out.setsampwidth(2) out.setframerate(self.rate) out.writeframes(bytes(self.samples))