import { DROP1, DROP2, DURATION, analyse, type AudioFrame } from './audio' import { Post, type PostParams } from './post' import { PULSE } from './draw' import { drawLyrics } from './lyrics' import { CUTS, sceneAt } from './timeline' import type { SceneCtx } from './types' import { clamp, hash, noise1, smoothstep } from './util' /** * One renderer = one output canvas. renderFrame(t) is a pure function of t, * shared by the live preview, the cover generator and the MP4 exporter. */ export class Renderer { readonly out: HTMLCanvasElement private scene: HTMLCanvasElement private sctx: CanvasRenderingContext2D private scratch: HTMLCanvasElement private post: Post | null private out2d: CanvasRenderingContext2D | null = null private cache = new Map() private warned = new Set() /** draw the user's lyrics into the frame (off for covers) */ lyrics = true W = 0 H = 0 constructor(out?: HTMLCanvasElement) { this.out = out ?? document.createElement('canvas') this.scene = document.createElement('canvas') this.sctx = this.scene.getContext('2d', { alpha: false })! this.scratch = document.createElement('canvas') this.post = Post.create(this.out) if (!this.post) this.out2d = this.out.getContext('2d') } get webgl() { return !!this.post } setSize(W: number, H: number) { W = Math.max(2, Math.round(W / 2) * 2) H = Math.max(2, Math.round(H / 2) * 2) if (W === this.W && H === this.H) return this.W = W this.H = H for (const c of [this.out, this.scene, this.scratch]) { c.width = W c.height = H } } render(tIn: number) { const t = clamp(Number.isFinite(tIn) ? tIn : 0, 0, DURATION) const { W, H } = this const a = analyse(t) const sc = sceneAt(t) PULSE.low = a.low PULSE.mid = a.mid PULSE.high = a.high PULSE.kick = a.kick const ctx = this.sctx ctx.setTransform(1, 0, 0, 1, 0, 0) ctx.globalAlpha = 1 ctx.globalCompositeOperation = 'source-over' ctx.shadowBlur = 0 ctx.shadowColor = 'transparent' ctx.filter = 'none' const s: SceneCtx = { ctx, W, H, P: H > W, u: Math.min(W, H) / 1000, t, lt: t - sc.start, dur: sc.end - sc.start, a, scratch: this.scratch, cache: this.cache, } ctx.save() try { sc.fn(s) } catch (e) { // never let a single drawing glitch break playback / export const key = sc.id + ':' + String(e) if (!this.warned.has(key)) console.warn('scene error', sc.id, t.toFixed(2), String(e), (e as Error)?.stack?.split('\n')[1] ?? '') this.warned.add(key) } ctx.restore() ctx.setTransform(1, 0, 0, 1, 0, 0) ctx.globalAlpha = 1 ctx.shadowBlur = 0 if (this.lyrics) { try { drawLyrics(ctx, W, H, t, a) } catch (e) { console.warn('lyrics error', e) } } const pp = postParams(t, a) if (this.post) { this.post.render(this.scene, pp) } else if (this.out2d) { this.out2d.globalAlpha = 1 this.out2d.drawImage(this.scene, 0, 0) if (pp.flash > 0.01) { this.out2d.fillStyle = `rgba(255,255,255,${pp.flash})` this.out2d.fillRect(0, 0, W, H) } if (pp.black > 0) { this.out2d.fillStyle = `rgba(0,0,0,${pp.black})` this.out2d.fillRect(0, 0, W, H) } } } } export function postParams(t: number, a: AudioFrame): PostParams { let flash = 0 let glitch = 0 for (const c of CUTS) { const d = t - c if (d >= 0 && d < 0.6) { const big = Math.abs(c - DROP1) < 0.01 || Math.abs(c - DROP2) < 0.01 flash = Math.max(flash, Math.exp(-d * (big ? 5 : 11)) * (big ? 1 : 0.55)) if (d < 0.14) glitch = Math.max(glitch, big ? 0.9 : 0.5) } if (d < 0 && d > -0.06) glitch = Math.max(glitch, 0.35) } // white-out build into Drop 1 if (t < DROP1 && t > DROP1 - 0.7) flash = Math.max(flash, smoothstep(DROP1 - 0.7, DROP1, t) * 0.9) if (a.drop && hash(a.beat * 5.1) > 0.78 && a.beatPhase < 0.16) glitch = Math.max(glitch, 0.65) // tension glitches right before Drop 2 if (t > DROP2 - 2.3 && t < DROP2) glitch = Math.max(glitch, hash(Math.floor(t * 14)) > 0.6 ? 0.45 : 0) const k = a.drop ? a.kick : 0 return { time: t, flash: clamp(flash), flashColor: [1, 0.97, 0.94], aberr: 0.0012 + (a.drop ? k * 0.006 : a.high * 0.0014), glitch, zoom: 1 + k * 0.04, shake: [noise1(t * 37, 1) * 0.005 * k, noise1(t * 41, 2) * 0.005 * k], bloom: 0.85 + k * 0.35, grain: 0.065, vignette: 0.55, scan: 0.05, black: t >= 144.22 ? 1 : 1 - smoothstep(0, 0.8, t), seed: Math.floor(t * 24) % 97, } }