File size: 5,637 Bytes
71ce0f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// 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()
}