video / src /mv /engine /audio.ts
Ethscriptions's picture
步骤3:渲染引擎 + 9 个场景 + WebGL 后期 + 播放器(横屏/竖屏)
f6f03fe verified
Raw History Blame Contribute Delete
3.1 kB
import feat from '../audio/features'
// Precomputed (offline) audio analysis of 黑街.mp3 at 60 Hz.
// Using precomputed envelopes makes the animation deterministic: the realtime
// preview and the frame-by-frame MP4 export see exactly the same values.
function decode(b64: string): Float32Array {
const s = atob(b64)
const out = new Float32Array(s.length)
for (let i = 0; i < s.length; i++) out[i] = s.charCodeAt(i) / 255
return out
}
const RATE = feat.fps
const LOW = decode(feat.low)
const MID = decode(feat.mid)
const HIGH = decode(feat.high)
const RMS = decode(feat.rms)
const KICK = decode(feat.kick)
export const DURATION = 144.3
export const BEAT = 60 / 105.47
export const BAR = BEAT * 4
export const DROP1 = 38.15
export const BREAK = 72.95
export const DROP2 = 109.436
function sample(arr: Float32Array, t: number): number {
const x = t * RATE
const i = Math.floor(x)
if (i < 0) return arr[0]
if (i >= arr.length - 1) return arr[arr.length - 1]
const f = x - i
return arr[i] * (1 - f) + arr[i + 1] * f
}
/** Kick envelope with fast attack, exponential release (looks punchier). */
function kickEnv(t: number): number {
const i = Math.floor(t * RATE)
let m = 0
for (let k = 0; k < 14; k++) {
const j = i - k
if (j < 0 || j >= KICK.length) continue
const v = KICK[j] * Math.exp(-k / 4)
if (v > m) m = v
}
return Math.min(1, m * 1.3)
}
export interface AudioFrame {
t: number
low: number
mid: number
high: number
rms: number
kick: number
/** continuous beat position (beats since anchor) */
beatPos: number
beat: number
beatPhase: number
bar: number
barPhase: number
/** 1 right on the beat, decays quickly */
pulse: number
/** true during Drop 1 / Drop 2 */
drop: boolean
/** 0..1 overall energy (smoothed) */
energy: number
}
export function beatPosAt(t: number): number {
const anchor = t < (BREAK + DROP2) / 2 ? DROP1 : DROP2
return (t - anchor) / BEAT
}
export function analyse(t: number): AudioFrame {
const beatPos = beatPosAt(t)
const beat = Math.floor(beatPos)
const beatPhase = beatPos - beat
const barPos = beatPos / 4
const bar = Math.floor(barPos)
const barPhase = barPos - bar
const drop = (t >= DROP1 && t < BREAK) || t >= DROP2
const low = sample(LOW, t)
const rms = sample(RMS, t)
let energy = 0
for (let k = 0; k < 6; k++) energy += sample(RMS, t - k * 0.08)
energy /= 6
return {
t,
low,
mid: sample(MID, t),
high: sample(HIGH, t),
rms,
kick: kickEnv(t),
beatPos,
beat,
beatPhase,
bar,
barPhase,
pulse: Math.exp(-beatPhase * 7),
drop,
energy,
}
}
/** Time (seconds) of a beat index relative to the anchor that governs time t. */
export function beatTime(t: number, beatIndex: number): number {
const anchor = t < (BREAK + DROP2) / 2 ? DROP1 : DROP2
return anchor + beatIndex * BEAT
}
export function bandAt(t: number, which: 'low' | 'mid' | 'high' | 'rms'): number {
const arr = which === 'low' ? LOW : which === 'mid' ? MID : which === 'high' ? HIGH : RMS
return sample(arr, t)
}