Spaces:
Paused
Paused
File size: 3,097 Bytes
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 | 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)
}
|