KAZIK/tools/generators/make_studio_v8_media.py

185 lines
7.1 KiB
Python

"""Phosphor scan, studio signature and stereo mix for the independent ident."""
from pathlib import Path
import math
import wave
import numpy as np
from PIL import Image, ImageDraw, ImageFont, ImageFilter
ROOT = Path(__file__).resolve().parents[2]
SOURCE = ROOT / "assets/cinematics/blender"
# Two traces share a diagonal cut, as on a spliced tape. The centres of the
# bends stay open even when the symbol is shown at game-menu scale.
TRACES = [
[
(0, 0),
(102, 0),
(85, 17),
(23, 17),
(23, 40),
(77, 40),
(61, 56),
(23, 56),
(23, 80),
(78, 80),
(59, 99),
(0, 99),
],
[
(131, 0),
(221, 0),
(203, 18),
(142, 18),
(121, 39),
(121, 72),
(131, 80),
(174, 80),
(190, 63),
(155, 63),
(171, 46),
(218, 46),
(218, 66),
(186, 99),
(120, 99),
(99, 82),
(99, 32),
],
]
def ease(a, b, t):
u = np.clip((t - a) / (b - a), 0, 1)
return u * u * (3 - 2 * u)
def signature():
# Lay out in 1280x720 units. Supersample the contours before emitting
# the native 1080p title so the long diagonal cuts remain antialiased.
scale = 3
w, h = 1280 * scale, 720 * scale
mask = Image.new("L", (w, h))
splice = Image.new("L", (w, h))
pen = ImageDraw.Draw(mask)
def contour(points):
return [(round((435 + x * 1.85) * scale), round((207 + y * 1.85) * scale)) for x, y in points]
for points in TRACES:
pen.polygon(contour(points), fill=255)
# Exposed red leader between the two letterforms, the same splice as the tape.
ImageDraw.Draw(splice).polygon(contour([(110, 0), (120, 0), (100, 20), (90, 20)]), fill=255)
font = ImageFont.truetype("C:/Windows/Fonts/bahnschrift.ttf", 55 * scale)
label = "EBLAN GAMES"
tracking = 8 * scale
widths = [font.getlength(c) for c in label]
start = (w - sum(widths) - tracking * (len(label) - 1)) / 2
for c, width in zip(label, widths):
pen.text((round(start), 443 * scale), c, font=font, fill=255, anchor="ls")
start += width + tracking
return tuple(
np.asarray(layer.resize((1920, 1080), Image.Resampling.LANCZOS), dtype=np.float32) / 255
for layer in (mask, splice)
)
def identity_frames():
mark, splice = signature()
h, w = mark.shape
yy, xx = np.mgrid[:h, :w]
xx = xx.astype(np.float32) / 1.5
yy = yy.astype(np.float32) / 1.5
tube = SOURCE / "screen-v8"
card = SOURCE / "signature-v8"
tube.mkdir(exist_ok=True)
card.mkdir(exist_ok=True)
Image.new("RGB", (w, h)).save(card / "signature_0332.png")
dark = Image.new("RGB", (768, 576), (1, 2, 2))
for f in range(1, 385):
if f < 264:
dark.save(tube / f"screen_{f:04}.png")
continue
# A cold cathode starts as a point, then a thin line. Vertical
# deflection opens only after horizontal sync has settled.
half_width = 12 + 465 * ease(264, 275, f)
half_height = 1.1 + 272 * ease(275, 296, f)
field = np.maximum(0, 1 - (np.abs(xx - 640) / half_width) ** 12)
beam = field * np.exp(-(((yy - 360) / half_height) ** 2) * 3)
beam *= max(0, 1 - ease(277, 318, f)) * (0.87 if f in (277, 279) else 1)
edge = 397 + 510 * ease(280, 330, f)
reveal = np.clip((edge - xx - (yy - 340) * 0.24) / 22, 0, 1)
trace = mark * reveal
heat = mark * np.exp(-(((xx + (yy - 340) * 0.24 - edge) / 28) ** 2)) * ease(280, 290, f)
scan = 1 - 0.23 * (1 - np.cos(yy * math.pi)) * (1 - ease(318, 342, f))
ink = trace * scan
rgb = np.stack(
(
ink * 210 + heat * 91 + beam * 105,
ink * 235 + heat * 91 + beam * 225,
ink * 196 + heat * 75 + beam * 157,
),
axis=-1,
)
rgb += splice[:, :, None] * reveal[:, :, None] * np.array([246, 118, 59], dtype=np.float32)
frame = Image.fromarray(np.uint8(np.clip(rgb, 0, 255)))
halo = frame.filter(ImageFilter.GaussianBlur(5))
optical = Image.fromarray(
np.uint8(np.minimum(255, np.asarray(frame, dtype=np.float32) + np.asarray(halo) * 0.22))
)
# CRT and clean slate share one drawing, with independent framing.
monitor = Image.new("RGB", (768, 576), (1, 2, 2))
monitor.paste(optical.resize((940, 529), Image.Resampling.LANCZOS), (-86, 24))
monitor.save(tube / f"screen_{f:04}.png")
if f >= 333:
optical.save(card / f"signature_{f:04}.png")
def soundtrack():
base = ROOT / "assets/audio/startup/studio-workshop-v7.wav"
with wave.open(str(base), "rb") as src:
rate = src.getframerate()
mix = np.frombuffer(src.readframes(src.getnframes()), "<i2").reshape(-1, 2).astype(float) / 32768
mix = np.pad(mix, ((0, rate * 4), (0, 0)))
t = np.arange(len(mix)) / rate
# Clear room for the visible CRT ignition and let the final note decay.
mix *= np.expand_dims(1 - ease(7.32, 7.55, t), 1)
rng = np.random.default_rng(815)
hiss = np.convolve(rng.normal(size=len(t)), np.hanning(18) / 8.5, mode="same")
winds = 0.041 * hiss * np.sin(np.pi * np.clip((t - 2.04) / 2.9, 0, 1)) ** 2
mix += np.stack((winds * 0.74, winds), axis=1)
# Six guide clicks follow the take-up hub teeth crossing the pawl.
for tick in (67, 76, 84, 92, 102, 119):
age = np.maximum(0, t - (tick - 1) / 24)
onset = t >= (tick - 1) / 24
transient = 0.055 * onset * np.exp(-145 * age) * (np.sin(2 * np.pi * 1520 * age) + hiss)
mix[:, 0] += transient * 0.63
mix[:, 1] += transient
# Each spring has its own attack and short metallic decay as its letter settles.
for i in range(5):
sec = (185 + 9 * i + 16 - 1) / 24
dt = np.maximum(0, t - sec)
tick = (t >= sec) * np.exp(-40 * dt) * (0.11 * np.sin(2 * np.pi * (570 + 62 * i) * dt) + 0.13 * hiss)
mix[:, 0] += tick * (1 - 0.11 * i)
mix[:, 1] += tick * (0.55 + 0.11 * i)
hum = 0.016 * np.sin(2 * np.pi * 52 * t) * ease(7.2, 7.9, t) * (1 - ease(13.3, 15.8, t))
mix += hum[:, None]
for sec, hz, gain, decay in (
(263 / 24, 113, 0.18, 4),
(274 / 24, 915, 0.042, 5),
(285 / 24, 221, 0.09, 1.5),
(309 / 24, 330, 0.078, 1.5),
(331 / 24, 442, 0.13, 1.5),
(335 / 24, 552, 0.055, 1.5),
):
dt = np.maximum(0, t - sec)
env = (t >= sec) * (1 - np.exp(-170 * dt)) * np.exp(-decay * dt)
tone = gain * env * (np.sin(2 * np.pi * hz * dt) + 0.14 * np.sin(4 * np.pi * hz * dt))
mix += tone[:, None]
mix *= np.clip((16 - t) / 0.36, 0, 1)[:, None]
with wave.open(str(base.with_name("studio-workshop-v8.wav")), "wb") as dst:
dst.setparams((2, 2, rate, 0, "NONE", "not compressed"))
dst.writeframes(np.int16(np.clip(mix, -0.94, 0.94) * 32767).tobytes())
if __name__ == "__main__":
soundtrack()
identity_frames()