video / src /mv /engine /lyrics.ts
Ethscriptions's picture
v8:远光照亮整块路面并闪白;喷射气体改为自然粒子气体;新增动感歌词(LRC / 打轴工具,随导出)
71ce0f9 verified
Raw History Blame Contribute Delete
5.64 kB
// Kinetic, beat-reactive lyrics. Lyrics are supplied by the user (LRC with
// timestamps, or plain lines stamped with the in-page timing tool) and are
// rendered into the frame, so they also appear in the exported MP4.
import type { AudioFrame } from './audio'
import { clamp, easeOutBack, hash } from './util'
export interface LyricLine {
t: number
end: number
text: string
}
let lines: LyricLine[] = []
let offset = 0
const KEY = 'mv-lyrics-lrc'
export function parseLrc(src: string): LyricLine[] {
const out: { t: number; text: string }[] = []
let off = 0
for (const raw of src.split(/\r?\n/)) {
const om = /^\[offset:([+-]?\d+)\]/i.exec(raw.trim())
if (om) off = parseInt(om[1], 10) / 1000
const tags = [...raw.matchAll(/\[(\d{1,2}):(\d{1,2}(?:\.\d{1,3})?)\]/g)]
if (!tags.length) continue
const text = raw.replace(/\[[^\]]*\]/g, '').trim()
if (!text) continue
for (const m of tags) out.push({ t: parseInt(m[1], 10) * 60 + parseFloat(m[2]) - off, text })
}
out.sort((a, b) => a.t - b.t)
return out.map((l, i) => {
const next = out[i + 1]?.t ?? l.t + 6
return { t: l.t, end: Math.min(next - 0.05, l.t + 7), text: l.text }
})
}
export function setLyrics(src: string, persist = true): number {
lines = parseLrc(src)
if (persist) {
try {
localStorage.setItem(KEY, src)
} catch {
/* private mode */
}
}
return lines.length
}
export function loadSavedLyrics(): string {
try {
const s = localStorage.getItem(KEY) ?? ''
if (s) lines = parseLrc(s)
return s
} catch {
return ''
}
}
export function setLyricOffset(sec: number) {
offset = sec
}
export function getLyricOffset() {
return offset
}
export function lyricCount() {
return lines.length
}
const FONT_STACK = '"MV Lyric", "MV Black", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif'
export function drawLyrics(ctx: CanvasRenderingContext2D, W: number, H: number, tIn: number, a: AudioFrame) {
if (!lines.length) return
const t = tIn - offset
const line = lines.find((l) => t >= l.t - 0.05 && t < l.end + 0.45)
if (!line) return
const chars = Array.from(line.text)
const n = chars.length
const dur = Math.max(0.6, line.end - line.t)
let size = H * 0.066
ctx.save()
ctx.font = `900 ${size}px ${FONT_STACK}`
let widths = chars.map((c) => ctx.measureText(c).width)
const spacing = () => size * 0.06
let total = widths.reduce((s, w) => s + w + spacing(), -spacing())
if (total > W * 0.88) {
size *= (W * 0.88) / total
ctx.font = `900 ${size}px ${FONT_STACK}`
widths = chars.map((c) => ctx.measureText(c).width)
total = widths.reduce((s, w) => s + w + spacing(), -spacing())
}
const baseY = H * 0.86
const pulse = 1 + 0.07 * a.kick
// readability band
const band = ctx.createLinearGradient(0, baseY - size * 1.3, 0, baseY + size * 0.9)
band.addColorStop(0, 'rgba(0,0,0,0)')
band.addColorStop(0.5, 'rgba(0,0,0,0.38)')
band.addColorStop(1, 'rgba(0,0,0,0)')
ctx.fillStyle = band
ctx.fillRect(0, baseY - size * 1.3, W, size * 2.2)
ctx.translate(W / 2, baseY)
ctx.scale(pulse, pulse)
ctx.textBaseline = 'middle'
ctx.textAlign = 'left'
const stagger = Math.min(0.09, (dur * 0.45) / Math.max(1, n))
const sung = clamp((t - line.t) / (dur * 0.92)) * n
let x = -total / 2
for (let i = 0; i < n; i++) {
const c = chars[i]
const w = widths[i]
const ti = line.t + i * stagger
const kIn = clamp((t - ti) / 0.24)
const kOut = clamp((t - line.end - i * 0.025) / 0.32)
if (kIn <= 0) {
x += w + spacing()
continue
}
const pop = easeOutBack(kIn)
const jitter = a.kick * size * 0.035
const jx = (hash(i * 3.1 + a.beat) - 0.5) * jitter
const jy = (hash(i * 7.7 + a.beat) - 0.5) * jitter - Math.sin(i * 0.9 + a.beatPos * Math.PI) * size * 0.05 * a.pulse
const y = (1 - pop) * size * 0.55 + jy - kOut * size * 0.9
const sc = (0.4 + 0.6 * pop) * (1 + kOut * 0.35)
const alpha = clamp(kIn * 1.4) * (1 - kOut)
const cx = x + w / 2 + jx
ctx.save()
ctx.translate(cx, y)
ctx.scale(sc, sc)
ctx.rotate((1 - pop) * (hash(i) - 0.5) * 0.6 + kOut * (hash(i * 2) - 0.5) * 0.8)
ctx.globalAlpha = alpha
// chromatic split on the kick
if (a.kick > 0.2) {
ctx.globalCompositeOperation = 'lighter'
ctx.fillStyle = `rgba(255,40,80,${0.55 * a.kick})`
ctx.fillText(c, -w / 2 - size * 0.05 * a.kick, 0)
ctx.fillStyle = `rgba(40,220,255,${0.55 * a.kick})`
ctx.fillText(c, -w / 2 + size * 0.05 * a.kick, 0)
ctx.globalCompositeOperation = 'source-over'
}
ctx.lineJoin = 'round'
ctx.strokeStyle = 'rgba(0,0,0,0.85)'
ctx.lineWidth = size * 0.14
ctx.strokeText(c, -w / 2, 0)
const done = i < Math.floor(sung)
const partial = i === Math.floor(sung) ? sung - i : 0
if (done || partial > 0) {
const g = ctx.createLinearGradient(0, -size / 2, 0, size / 2)
g.addColorStop(0, '#ffd27a')
g.addColorStop(1, '#ff2d55')
ctx.shadowColor = '#ff2d55'
ctx.shadowBlur = size * 0.35
ctx.fillStyle = done ? g : '#f4ece0'
ctx.fillText(c, -w / 2, 0)
if (partial > 0) {
ctx.save()
ctx.beginPath()
ctx.rect(-w / 2, -size, w * partial, size * 2)
ctx.clip()
ctx.fillStyle = g
ctx.fillText(c, -w / 2, 0)
ctx.restore()
}
} else {
ctx.shadowColor = 'rgba(255,255,255,0.6)'
ctx.shadowBlur = size * 0.2
ctx.fillStyle = '#f4ece0'
ctx.fillText(c, -w / 2, 0)
}
ctx.restore()
x += w + spacing()
}
ctx.restore()
}