Spaces:
Paused
Paused
File size: 4,722 Bytes
f6f03fe c58db7d 71ce0f9 6855c38 f6f03fe c174bde 71ce0f9 f6f03fe c58db7d f6f03fe c174bde f6f03fe 71ce0f9 f6f03fe | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | 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<string, unknown>()
private warned = new Set<string>()
/** 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,
}
}
|