Spaces:
Running
Running
Download dengtaishan-src/src/video/audio.ts from Ethscriptions/mx: direct link, hf CLI and curl.
- Browser
- Download file 27 kB
-
https://huggingface.co/spaces/Ethscriptions/mx/resolve/main/dengtaishan-src/src/video/audio.ts
- Command line
-
hf download hf://spaces/Ethscriptions/mx/dengtaishan-src/src/video/audio.ts
-
curl -L -o audio.ts https://huggingface.co/spaces/Ethscriptions/mx/resolve/main/dengtaishan-src/src/video/audio.ts
27 kB
| // 声音引擎:旁白 + 程序化配乐(古琴拨弦/笙垫/编钟/鼓/风雪)+ 音效 | |
| // 采用分段离线渲染(每段 30 秒,含 16 秒预卷),预览与导出共用,结果逐样本一致。 | |
| import { TL, LINE, ALL_LINES, DURATION } from './timeline' | |
| import { wordTime } from './draw' | |
| import { DRUM_BEATS } from './scenes/shot3' | |
| import { mulberry32, clamp, smooth, inv } from './util' | |
| export const SR = 44100 | |
| export const SEG = 30 | |
| const PRE = 16 | |
| export const TOTAL_SAMPLES = Math.ceil(DURATION * SR) | |
| export const SEG_COUNT = Math.ceil(TOTAL_SAMPLES / (SEG * SR)) | |
| // ---------------- 旁白 ---------------- | |
| const voices: Record<string, AudioBuffer> = {} | |
| let voiceLoading: Promise<void> | null = null | |
| export function loadVoices(onProgress?: (p: number) => void) { | |
| if (voiceLoading) return voiceLoading | |
| const base = import.meta.env.BASE_URL || './' | |
| const dec = new OfflineAudioContext(1, 1, 24000) | |
| let done = 0 | |
| // 分批(最多 6 路并发)+ 失败退避重试,避免静态托管限流(429)导致整段旁白加载失败 | |
| const fetchOne = async (id: string) => { | |
| let err: unknown | |
| for (let i = 0; i < 6; i++) { | |
| try { | |
| const r = await fetch(`${base}voice/${id}.mp3`) | |
| if (!r.ok) throw new Error(`HTTP ${r.status}`) | |
| return await dec.decodeAudioData(await r.arrayBuffer()) | |
| } catch (e) { | |
| err = e | |
| await new Promise((res) => setTimeout(res, 400 * 2 ** i + Math.random() * 300)) | |
| } | |
| } | |
| throw err | |
| } | |
| const queue = ALL_LINES.filter((l) => !voices[l.id]).map((l) => l.id) | |
| done = ALL_LINES.length - queue.length | |
| const worker = async () => { | |
| while (queue.length) { | |
| const id = queue.shift()! | |
| voices[id] = await fetchOne(id) | |
| done++ | |
| onProgress?.(done / ALL_LINES.length) | |
| } | |
| } | |
| voiceLoading = Promise.all(Array.from({ length: 6 }, worker)) | |
| .then(() => undefined) | |
| .catch((e) => { | |
| voiceLoading = null | |
| throw e | |
| }) | |
| return voiceLoading | |
| } | |
| // ---------------- 乐音素材 ---------------- | |
| const ksCache = new Map<number, AudioBuffer>() | |
| function ksBuffer(freq: number) { | |
| const key = Math.round(freq * 10) | |
| let b = ksCache.get(key) | |
| if (b) return b | |
| const len = Math.floor(SR * 4) | |
| b = new AudioBuffer({ length: len, sampleRate: SR, numberOfChannels: 1 }) | |
| const d = b.getChannelData(0) | |
| const N = Math.max(2, Math.round(SR / freq)) | |
| const rnd = mulberry32(key) | |
| const buf = new Float32Array(N) | |
| let prev = 0 | |
| for (let i = 0; i < N; i++) { | |
| const v = rnd() * 2 - 1 | |
| prev = prev * 0.55 + v * 0.45 // 拨弦初始噪声低通 → 更温润 | |
| buf[i] = prev | |
| } | |
| let idx = 0 | |
| const decay = 0.9965 - Math.min(0.004, freq / 400000) | |
| let last = 0 | |
| for (let i = 0; i < len; i++) { | |
| const cur = buf[idx] | |
| const nxt = buf[(idx + 1) % N] | |
| const v = decay * 0.5 * (cur + nxt) | |
| buf[idx] = v | |
| // 琴体共鸣:轻微低通 | |
| last = last * 0.3 + cur * 0.7 | |
| d[i] = last | |
| idx = (idx + 1) % N | |
| } | |
| // 起音包络,避免爆音 | |
| for (let i = 0; i < 64; i++) d[i] *= i / 64 | |
| ksCache.set(key, b) | |
| return b | |
| } | |
| let noiseBuf: AudioBuffer | null = null | |
| function noise() { | |
| if (noiseBuf) return noiseBuf | |
| const len = SR * 6 | |
| noiseBuf = new AudioBuffer({ length: len, sampleRate: SR, numberOfChannels: 2 }) | |
| for (let c = 0; c < 2; c++) { | |
| const d = noiseBuf.getChannelData(c) | |
| const r = mulberry32(31 + c) | |
| let b0 = 0 | |
| let b1 = 0 | |
| let b2 = 0 | |
| for (let i = 0; i < len; i++) { | |
| const w = r() * 2 - 1 | |
| b0 = 0.99765 * b0 + w * 0.099 | |
| b1 = 0.963 * b1 + w * 0.2965 | |
| b2 = 0.57 * b2 + w * 1.0527 | |
| d[i] = (b0 + b1 + b2 + w * 0.1848) * 0.2 | |
| } | |
| } | |
| return noiseBuf | |
| } | |
| let irBuf: AudioBuffer | null = null | |
| function ir() { | |
| if (irBuf) return irBuf | |
| const len = Math.floor(SR * 3.6) | |
| irBuf = new AudioBuffer({ length: len, sampleRate: SR, numberOfChannels: 2 }) | |
| for (let c = 0; c < 2; c++) { | |
| const d = irBuf.getChannelData(c) | |
| const r = mulberry32(77 + c) | |
| let lp = 0 | |
| for (let i = 0; i < len; i++) { | |
| const t = i / SR | |
| const k = 0.35 + 0.6 * (t / 3.6) | |
| lp = lp * k + (r() * 2 - 1) * (1 - k) | |
| d[i] = lp * Math.pow(1 - t / 3.6, 2.2) * (t < 0.01 ? t / 0.01 : 1) | |
| } | |
| } | |
| return irBuf | |
| } | |
| // ---------------- 图 ---------------- | |
| interface G { | |
| ctx: OfflineAudioContext | |
| music: GainNode | |
| sfx: GainNode | |
| voice: GainNode | |
| verb: GainNode | |
| rnd: () => number | |
| } | |
| const midi = (m: number) => 440 * Math.pow(2, (m - 69) / 12) | |
| const PENTA = [0, 2, 4, 7, 9] // 宫 商 角 徵 羽(D 宫) | |
| const D = 62 | |
| const pentaNote = (deg: number) => { | |
| const o = Math.floor(deg / 5) | |
| return D + o * 12 + PENTA[((deg % 5) + 5) % 5] | |
| } | |
| function env(g: GainNode, at: number, a: number, s: number, r: number, peak: number) { | |
| g.gain.setValueAtTime(0, at) | |
| g.gain.linearRampToValueAtTime(peak, at + a) | |
| g.gain.setValueAtTime(peak, at + a + s) | |
| g.gain.linearRampToValueAtTime(0, at + a + s + r) | |
| } | |
| function pluck(G: G, at: number, m: number, vel: number, slide = 0, pan = 0, verb = 0.35) { | |
| const { ctx } = G | |
| const src = new AudioBufferSourceNode(ctx, { buffer: ksBuffer(midi(m)) }) | |
| if (slide) { | |
| src.detune.setValueAtTime(slide, at) | |
| src.detune.setTargetAtTime(0, at + 0.03, 0.07) | |
| } | |
| // 吟猱:轻微颤音 | |
| const lfo = new OscillatorNode(ctx, { frequency: 5.2 }) | |
| const lg = new GainNode(ctx, { gain: 6 }) | |
| lfo.connect(lg).connect(src.detune) | |
| const g = new GainNode(ctx, { gain: vel }) | |
| g.gain.setValueAtTime(vel, at) | |
| g.gain.setTargetAtTime(0, at + 2.2, 0.5) | |
| const p = new StereoPannerNode(ctx, { pan }) | |
| src.connect(g).connect(p) | |
| p.connect(G.music) | |
| const s = new GainNode(ctx, { gain: verb }) | |
| p.connect(s).connect(G.verb) | |
| src.start(at) | |
| src.stop(at + 4) | |
| lfo.start(at + 0.4) | |
| lfo.stop(at + 4) | |
| } | |
| function pad(G: G, at: number, notes: number[], dur: number, level: number, bright: number) { | |
| const { ctx } = G | |
| const out = new GainNode(ctx, { gain: 0 }) | |
| env(out, at, Math.min(3, dur * 0.35), Math.max(0, dur - 6), 3, level) | |
| const lp = new BiquadFilterNode(ctx, { type: 'lowpass', frequency: 500 + bright * 1600, Q: 0.4 }) | |
| lp.frequency.setValueAtTime(400 + bright * 1000, at) | |
| lp.frequency.linearRampToValueAtTime(500 + bright * 1800, at + dur * 0.5) | |
| lp.frequency.linearRampToValueAtTime(400 + bright * 1000, at + dur + 3) | |
| lp.connect(out) | |
| out.connect(G.music) | |
| const s = new GainNode(ctx, { gain: 0.5 }) | |
| out.connect(s).connect(G.verb) | |
| notes.forEach((m, i) => { | |
| for (const [type, det, amp] of [['triangle', -5, 0.5], ['triangle', 6, 0.5], ['sine', 0, 0.6]] as const) { | |
| const o = new OscillatorNode(ctx, { type, frequency: midi(m), detune: det + (i % 2 ? 3 : -3) }) | |
| const og = new GainNode(ctx, { gain: (amp / notes.length) * (m < 50 ? 1.2 : 1) }) | |
| const p = new StereoPannerNode(ctx, { pan: ((i / Math.max(1, notes.length - 1)) * 2 - 1) * 0.5 }) | |
| o.connect(og).connect(p).connect(lp) | |
| o.start(at) | |
| o.stop(at + dur + 3.2) | |
| } | |
| }) | |
| } | |
| function bell(G: G, at: number, m: number, vel: number, bus: 'music' | 'sfx' = 'sfx') { | |
| const { ctx } = G | |
| const f0 = midi(m) | |
| const parts: [number, number, number][] = [[1, 1, 4.5], [2.0, 0.35, 3], [2.76, 0.45, 2.4], [5.4, 0.22, 1.2], [8.93, 0.1, 0.6]] | |
| const out = new GainNode(ctx, { gain: vel }) | |
| out.connect(G[bus]) | |
| const s = new GainNode(ctx, { gain: 0.6 }) | |
| out.connect(s).connect(G.verb) | |
| for (const [r, a, d] of parts) { | |
| const o = new OscillatorNode(ctx, { type: 'sine', frequency: f0 * r }) | |
| const g = new GainNode(ctx, { gain: 0 }) | |
| g.gain.setValueAtTime(0, at) | |
| g.gain.linearRampToValueAtTime(a, at + 0.004) | |
| g.gain.exponentialRampToValueAtTime(0.0001, at + d) | |
| o.connect(g).connect(out) | |
| o.start(at) | |
| o.stop(at + d + 0.05) | |
| } | |
| } | |
| function drum(G: G, at: number, vel: number, f0 = 130) { | |
| const { ctx } = G | |
| const o = new OscillatorNode(ctx, { type: 'sine', frequency: f0 }) | |
| o.frequency.setValueAtTime(f0, at) | |
| o.frequency.exponentialRampToValueAtTime(f0 * 0.42, at + 0.18) | |
| const g = new GainNode(ctx, { gain: 0 }) | |
| g.gain.setValueAtTime(0, at) | |
| g.gain.linearRampToValueAtTime(vel, at + 0.005) | |
| g.gain.exponentialRampToValueAtTime(0.0001, at + 1.1) | |
| o.connect(g).connect(G.sfx) | |
| const s = new GainNode(ctx, { gain: 0.3 }) | |
| g.connect(s).connect(G.verb) | |
| o.start(at) | |
| o.stop(at + 1.2) | |
| const n = new AudioBufferSourceNode(ctx, { buffer: noise() }) | |
| const lp = new BiquadFilterNode(ctx, { type: 'lowpass', frequency: 900 }) | |
| const ng = new GainNode(ctx, { gain: 0 }) | |
| ng.gain.setValueAtTime(vel * 0.5, at) | |
| ng.gain.exponentialRampToValueAtTime(0.0001, at + 0.12) | |
| n.connect(lp).connect(ng).connect(G.sfx) | |
| n.start(at, G.rnd() * 3) | |
| n.stop(at + 0.15) | |
| } | |
| function boom(G: G, at: number, vel: number) { | |
| const { ctx } = G | |
| const o = new OscillatorNode(ctx, { type: 'sine', frequency: 58 }) | |
| o.frequency.setValueAtTime(70, at) | |
| o.frequency.exponentialRampToValueAtTime(38, at + 1.8) | |
| const g = new GainNode(ctx, { gain: 0 }) | |
| g.gain.setValueAtTime(0, at) | |
| g.gain.linearRampToValueAtTime(vel, at + 0.02) | |
| g.gain.exponentialRampToValueAtTime(0.0001, at + 2.8) | |
| o.connect(g).connect(G.sfx) | |
| o.start(at) | |
| o.stop(at + 3) | |
| const n = new AudioBufferSourceNode(ctx, { buffer: noise() }) | |
| const lp = new BiquadFilterNode(ctx, { type: 'lowpass', frequency: 220 }) | |
| const ng = new GainNode(ctx, { gain: 0 }) | |
| ng.gain.setValueAtTime(0, at) | |
| ng.gain.linearRampToValueAtTime(vel * 0.8, at + 0.03) | |
| ng.gain.exponentialRampToValueAtTime(0.0001, at + 1.6) | |
| n.connect(lp).connect(ng).connect(G.sfx) | |
| const s = new GainNode(ctx, { gain: 0.4 }) | |
| ng.connect(s).connect(G.verb) | |
| n.start(at, G.rnd() * 3) | |
| n.stop(at + 1.7) | |
| } | |
| function whoosh(G: G, at: number, dur: number, vel: number, f0 = 300, f1 = 2600) { | |
| const { ctx } = G | |
| const n = new AudioBufferSourceNode(ctx, { buffer: noise() }) | |
| const bp = new BiquadFilterNode(ctx, { type: 'bandpass', frequency: f0, Q: 1.1 }) | |
| bp.frequency.setValueAtTime(f0, at) | |
| bp.frequency.exponentialRampToValueAtTime(f1, at + dur * 0.6) | |
| bp.frequency.exponentialRampToValueAtTime(f0 * 1.5, at + dur) | |
| const g = new GainNode(ctx, { gain: 0 }) | |
| g.gain.setValueAtTime(0, at) | |
| g.gain.linearRampToValueAtTime(vel, at + dur * 0.55) | |
| g.gain.linearRampToValueAtTime(0, at + dur) | |
| n.connect(bp).connect(g).connect(G.sfx) | |
| const s = new GainNode(ctx, { gain: 0.35 }) | |
| g.connect(s).connect(G.verb) | |
| n.start(at, G.rnd() * 3) | |
| n.stop(at + dur + 0.05) | |
| } | |
| function tick(G: G, at: number, vel: number, f = 1100) { | |
| const { ctx } = G | |
| for (const [r, a] of [[1, 1], [1.58, 0.5]] as const) { | |
| const o = new OscillatorNode(ctx, { type: 'sine', frequency: f * r }) | |
| const g = new GainNode(ctx, { gain: 0 }) | |
| g.gain.setValueAtTime(0, at) | |
| g.gain.linearRampToValueAtTime(vel * a, at + 0.002) | |
| g.gain.exponentialRampToValueAtTime(0.0001, at + 0.12) | |
| o.connect(g).connect(G.sfx) | |
| const s = new GainNode(ctx, { gain: 0.25 }) | |
| g.connect(s).connect(G.verb) | |
| o.start(at) | |
| o.stop(at + 0.15) | |
| } | |
| } | |
| function stamp(G: G, at: number, vel: number) { | |
| const { ctx } = G | |
| const o = new OscillatorNode(ctx, { type: 'sine', frequency: 150 }) | |
| o.frequency.setValueAtTime(150, at) | |
| o.frequency.exponentialRampToValueAtTime(55, at + 0.12) | |
| const g = new GainNode(ctx, { gain: 0 }) | |
| g.gain.setValueAtTime(0, at) | |
| g.gain.linearRampToValueAtTime(vel, at + 0.004) | |
| g.gain.exponentialRampToValueAtTime(0.0001, at + 0.35) | |
| o.connect(g).connect(G.sfx) | |
| o.start(at) | |
| o.stop(at + 0.4) | |
| const n = new AudioBufferSourceNode(ctx, { buffer: noise() }) | |
| const bp = new BiquadFilterNode(ctx, { type: 'bandpass', frequency: 1800, Q: 0.7 }) | |
| const ng = new GainNode(ctx, { gain: 0 }) | |
| ng.gain.setValueAtTime(vel * 0.6, at) | |
| ng.gain.exponentialRampToValueAtTime(0.0001, at + 0.09) | |
| n.connect(bp).connect(ng).connect(G.sfx) | |
| n.start(at, G.rnd() * 3) | |
| n.stop(at + 0.1) | |
| } | |
| function shimmer(G: G, at: number, vel: number, up = true) { | |
| const degs = [10, 11, 12, 13, 14, 15, 16] | |
| degs.forEach((d, i) => { | |
| const m = pentaNote(up ? d : degs[degs.length - 1 - i] ) | |
| const { ctx } = G | |
| const o = new OscillatorNode(ctx, { type: 'sine', frequency: midi(m) }) | |
| const g = new GainNode(ctx, { gain: 0 }) | |
| const t = at + i * 0.09 | |
| g.gain.setValueAtTime(0, t) | |
| g.gain.linearRampToValueAtTime(vel, t + 0.01) | |
| g.gain.exponentialRampToValueAtTime(0.0001, t + 1.8) | |
| const p = new StereoPannerNode(ctx, { pan: (i / 6) * 1.2 - 0.6 }) | |
| o.connect(g).connect(p).connect(G.sfx) | |
| const s = new GainNode(ctx, { gain: 0.8 }) | |
| p.connect(s).connect(G.verb) | |
| o.start(t) | |
| o.stop(t + 1.9) | |
| }) | |
| } | |
| function wind(G: G, at: number, dur: number, level: number, bright = 0.5) { | |
| if (level <= 0.001) return | |
| const { ctx } = G | |
| const n = new AudioBufferSourceNode(ctx, { buffer: noise(), loop: true }) | |
| const bp = new BiquadFilterNode(ctx, { type: 'bandpass', frequency: 500, Q: 0.9 }) | |
| const r = G.rnd | |
| bp.frequency.setValueAtTime(350 + bright * 500, at) | |
| for (let k = 1; k <= 4; k++) bp.frequency.linearRampToValueAtTime(300 + bright * 900 * (0.5 + r()), at + (dur * k) / 4) | |
| const g = new GainNode(ctx, { gain: 0 }) | |
| env(g, at, Math.min(2.5, dur * 0.3), Math.max(0, dur - 5), 2.5, level) | |
| n.connect(bp).connect(g).connect(G.sfx) | |
| n.start(at, r() * 5) | |
| n.stop(at + dur + 0.1) | |
| } | |
| // ---------------- 乐谱(确定性) ---------------- | |
| interface Ev { | |
| t: number | |
| d: number // 事件总长(含尾音),必须 < PRE | |
| bus: 'music' | 'sfx' | 'voice' | |
| play: (G: G, at: number) => void | |
| } | |
| interface Mood { | |
| level: number | |
| chords: number[][] | |
| every: number | |
| bright: number | |
| pluck: number // 每秒拨弦数 | |
| reg: number // 拨弦音区(五声音阶序号) | |
| drone: number | |
| } | |
| const CH = { | |
| D: [50, 57, 64, 66], | |
| Bm: [47, 54, 62, 69], | |
| G: [43, 55, 62, 69], | |
| Em: [52, 55, 62, 71], | |
| A: [45, 57, 64, 71], | |
| Dhi: [50, 62, 66, 69, 76], | |
| } | |
| const MOOD: Record<string, Mood> = { | |
| prologue: { level: 0.85, chords: [CH.Bm, CH.G, CH.D, CH.A], every: 10, bright: 0.25, pluck: 0.32, reg: 5, drone: 0.6 }, | |
| shot1: { level: 0.8, chords: [CH.D, CH.Bm, CH.G, CH.D], every: 8, bright: 0.45, pluck: 0.55, reg: 7, drone: 0.3 }, | |
| shot2: { level: 0.85, chords: [CH.Em, CH.G, CH.D, CH.A, CH.Bm, CH.G], every: 8, bright: 0.4, pluck: 0.5, reg: 6, drone: 0.4 }, | |
| shot3: { level: 0.95, chords: [CH.Bm, CH.Em, CH.G, CH.A, CH.Dhi, CH.Dhi, CH.G, CH.D], every: 9, bright: 0.35, pluck: 0.35, reg: 8, drone: 0.5 }, | |
| shot4: { level: 0.7, chords: [CH.Em, CH.Bm, CH.Em, CH.A], every: 10, bright: 0.25, pluck: 0.28, reg: 4, drone: 0.6 }, | |
| shot5: { level: 0.6, chords: [CH.G, CH.D, CH.Em], every: 12, bright: 0.2, pluck: 0.22, reg: 6, drone: 0.2 }, | |
| depth: { level: 0.8, chords: [CH.Bm, CH.G, CH.D, CH.A], every: 8, bright: 0.4, pluck: 0.35, reg: 6, drone: 0.4 }, | |
| recite: { level: 0.7, chords: [CH.D, CH.G, CH.Bm, CH.A], every: 6, bright: 0.5, pluck: 0.9, reg: 8, drone: 0.2 }, | |
| epilogue: { level: 1, chords: [CH.G, CH.A, CH.Dhi, CH.Dhi], every: 8, bright: 0.6, pluck: 0.5, reg: 9, drone: 0.4 }, | |
| } | |
| /** 配乐总电平(含旁白闪避、段落静默) */ | |
| function musicLevel(t: number) { | |
| // 场景电平平滑过渡 | |
| let lv = 0 | |
| for (const s of TL.scenes) { | |
| const w = Math.min(smooth(inv(s.start - 1.5, s.start + 1.5, t)), 1 - smooth(inv(s.end - 1.5, s.end + 1.5, t))) | |
| if (w > 0) lv += w * MOOD[s.id].level | |
| } | |
| // 镜头五:无鸟兽音迹 → 音乐渐隐至静默 | |
| const e5 = LINE.e5 | |
| const sil = smooth(inv(e5.start - 0.5, e5.start + 2, t)) * (1 - smooth(inv(LINE.e8.end + 0.5, TL.scenes.find((s) => s.id === 'depth')!.start + 1, t))) | |
| lv *= 1 - sil * 0.97 | |
| // 日出高潮推升 | |
| lv *= 1 + 0.35 * Math.min(smooth(inv(LINE.c12.start, LINE.c13.start + 1, t)), 1 - smooth(inv(LINE.c16.end, LINE.c19.start, t))) | |
| // 旁白闪避 | |
| let duck = 1 | |
| for (const l of ALL_LINES) { | |
| if (t < l.start - 0.6 || t > l.end + 0.8) continue | |
| const k = Math.min(smooth(inv(l.start - 0.35, l.start, t)), 1 - smooth(inv(l.end + 0.1, l.end + 0.6, t))) | |
| duck = Math.min(duck, 1 - 0.55 * k) | |
| } | |
| return lv * duck | |
| } | |
| function windLevel(t: number) { | |
| const sc = TL.scenes | |
| const pro = sc[0] | |
| let w = 0 | |
| w = Math.max(w, 0.35 * (1 - smooth(inv(pro.end - 2, pro.end + 1, t))) * (t < pro.end + 1 ? 1 : 0)) | |
| w = Math.max(w, 0.55 * Math.min(smooth(inv(LINE.p3.start - 0.5, LINE.p3.start + 1, t)), 1 - smooth(inv(LINE.p4.start, LINE.p4.start + 2, t)))) | |
| w = Math.max(w, 0.45 * Math.min(smooth(inv(LINE.b1.start - 1, LINE.b1.start, t)), 1 - smooth(inv(LINE.b5.start - 1, LINE.b5.start + 1, t)))) | |
| w = Math.max(w, 0.35 * Math.min(smooth(inv(LINE.b10.start - 1, LINE.b10.start, t)), 1 - smooth(inv(LINE.b11.start, LINE.b12.start, t)))) | |
| w = Math.max(w, 0.4 * Math.min(smooth(inv(LINE.c1.start - 2, LINE.c2.start, t)), 1 - smooth(inv(LINE.c8.start, LINE.c11.start, t)))) | |
| w = Math.max(w, 0.3 * Math.min(smooth(inv(LINE.e5.start, LINE.e6.start, t)), 1 - smooth(inv(LINE.e8.end, LINE.e8.end + 3, t)))) | |
| w = Math.max(w, 0.18 * Math.min(smooth(inv(LINE.h1.start - 2, LINE.h1.start, t)), 1 - smooth(inv(LINE.h2.end, LINE.h3.end, t)))) | |
| return w | |
| } | |
| let EVENTS: Ev[] | null = null | |
| function buildEvents(): Ev[] { | |
| const ev: Ev[] = [] | |
| const rnd = mulberry32(1774) | |
| const add = (t: number, d: number, bus: Ev['bus'], play: Ev['play']) => ev.push({ t, d, bus, play }) | |
| // 旁白 | |
| for (const l of ALL_LINES) add(l.start, (voices[l.id]?.duration ?? l.dur) + 0.3, 'voice', (G, at) => { | |
| const b = voices[l.id] | |
| if (!b) return | |
| const s = new AudioBufferSourceNode(G.ctx, { buffer: b }) | |
| s.connect(G.voice) | |
| s.start(at) | |
| }) | |
| // 配乐:笙垫 + 低音持续 + 古琴拨弦 | |
| for (const s of TL.scenes) { | |
| const m = MOOD[s.id] | |
| let ci = 0 | |
| for (let t = s.start - 1; t < s.end; t += m.every - 2) { | |
| const chord = m.chords[ci++ % m.chords.length] | |
| const dur = Math.min(m.every + 1, 11) | |
| add(t, dur + 3.3, 'music', (G, at) => pad(G, at, chord, dur, 0.22, m.bright)) | |
| if (m.drone > 0) add(t, dur + 3.3, 'music', (G, at) => pad(G, at, [chord[0] - 12], dur, 0.14 * m.drone, 0.1)) | |
| } | |
| // 拨弦:随机游走旋律,句读处更密 | |
| let deg = m.reg | |
| let t = s.start + 1.5 + rnd() * 2 | |
| while (t < s.end - 1) { | |
| const step = [-2, -1, -1, 0, 1, 1, 2][Math.floor(rnd() * 7)] | |
| deg = Math.max(m.reg - 3, Math.min(m.reg + 4, deg + step)) | |
| const note = pentaNote(deg) | |
| const slide = rnd() < 0.25 ? (rnd() < 0.5 ? -200 : -100) : 0 | |
| const vel = 0.16 + rnd() * 0.12 | |
| const pan = (rnd() - 0.5) * 0.8 | |
| const tt = t | |
| add(tt, 4.2, 'music', (G, at) => pluck(G, at, note, vel, slide, pan)) | |
| if (rnd() < 0.2) { | |
| const n2 = pentaNote(deg - 5) | |
| add(tt + 0.02, 4.2, 'music', (G, at) => pluck(G, at, n2, vel * 0.6, 0, -pan)) | |
| } | |
| t += (0.6 + rnd() * 1.6) / Math.max(0.15, m.pluck) | |
| } | |
| } | |
| // 风雪 | |
| for (let t = -2; t < DURATION; t += 6) { | |
| const lv = windLevel(t + 4) | |
| if (lv > 0.01) add(t, 10.2, 'sfx', (G, at) => wind(G, at, 10, lv * 0.5, 0.4 + 0.3 * Math.sin(t))) | |
| } | |
| // ---- 音效 ---- | |
| TL.scenes.forEach((s, i) => { | |
| if (i > 0) add(s.start - 0.5, 1.8, 'sfx', (G, at) => whoosh(G, at, 1.6, 0.22)) | |
| if (s.intro > 2) { | |
| add(s.start + 0.5, 3, 'sfx', (G, at) => boom(G, at, 0.28)) | |
| add(s.start + 1.0, 0.6, 'sfx', (G, at) => stamp(G, at, 0.5)) | |
| add(s.start + 0.6, 5, 'sfx', (G, at) => bell(G, at, 69, 0.12)) | |
| } | |
| }) | |
| add(0.4, 3, 'sfx', (G, at) => boom(G, at, 0.3)) | |
| add(LINE.p1.start + 0.6, 5, 'sfx', (G, at) => bell(G, at, 62, 0.12)) | |
| add(LINE.p2.start + 1.0, 0.6, 'sfx', (G, at) => stamp(G, at, 0.4)) | |
| add(LINE.p5.start + 0.6, 5, 'sfx', (G, at) => bell(G, at, 57, 0.18)) | |
| add(LINE.p5.start + 1.6, 0.6, 'sfx', (G, at) => stamp(G, at, 0.5)) | |
| add(LINE.p8.start, 3, 'sfx', (G, at) => boom(G, at, 0.35)) | |
| add(LINE.p8.start, 2.2, 'sfx', (G, at) => { | |
| for (let k = 0; k < 18; k++) tick(G, at + k * 0.11, 0.05, 2400) | |
| }) | |
| for (let i = 0; i < 5; i++) add(LINE.p9.start + i * 0.42, 0.3, 'sfx', (G, at) => tick(G, at, 0.16, 900)) | |
| // 镜头一 | |
| add(wordTime('a3', '汶水'), 2, 'sfx', (G, at) => whoosh(G, at, 1.6, 0.06, 600, 1800)) | |
| add(wordTime('a3', '济水'), 2, 'sfx', (G, at) => whoosh(G, at, 1.6, 0.06, 600, 1800)) | |
| add(wordTime('a5', '古长城'), 1, 'sfx', (G, at) => drum(G, at, 0.18, 90)) | |
| add(wordTime('a7', '日观峰'), 5, 'sfx', (G, at) => bell(G, at, 81, 0.12)) | |
| for (let i = 0; i < 4; i++) add(LINE.a9.start + LINE.a9.dur * (0.35 + i * 0.16) - 0.3, 0.6, 'sfx', (G, at) => stamp(G, at, 0.22)) | |
| // 镜头二 | |
| for (const w of ['京师', '齐河', '长清', '穿', '越', '至于']) add(wordTime('b2', w), 0.3, 'sfx', (G, at) => tick(G, at, 0.12, 1000)) | |
| add(LINE.b3.start, 4, 'sfx', (G, at) => wind(G, at, 3.5, 0.5, 0.8)) | |
| for (let i = 0; i < 4; i++) add(LINE.b4.start + i * 0.55, 1.2, 'sfx', (G, at) => drum(G, at, 0.16, 110)) | |
| for (const w of ['循以入', '越中岭', '复循西谷', '遂至其巅']) add(wordTime('b7', w), 0.3, 'sfx', (G, at) => tick(G, at, 0.12, 1200)) | |
| add(LINE.b11.start, 2.2, 'sfx', (G, at) => whoosh(G, at, 2, 0.16, 250, 1600)) | |
| add(wordTime('b12', '明烛'), 3, 'sfx', (G, at) => shimmer(G, at, 0.05)) | |
| add(wordTime('b12', '苍山'), 3, 'sfx', (G, at) => boom(G, at, 0.2)) | |
| // 镜头三 | |
| add(wordTime('c3', '正是除夕') + 0.15, 0.6, 'sfx', (G, at) => stamp(G, at, 0.55)) | |
| for (const b of DRUM_BEATS()) add(b, 1.3, 'sfx', (G, at) => drum(G, at, 0.38, 120)) | |
| add(LINE.c5.start - 0.2, 6, 'sfx', (G, at) => wind(G, at, 5.5, 0.75, 0.9)) | |
| add(LINE.c9.start, 3, 'sfx', (G, at) => shimmer(G, at, 0.04)) | |
| add(LINE.c11.start, 3, 'sfx', (G, at) => shimmer(G, at, 0.06)) | |
| add(LINE.c13.start + 0.1, 3, 'sfx', (G, at) => boom(G, at, 0.4)) | |
| add(LINE.c13.start + 0.15, 5, 'sfx', (G, at) => bell(G, at, 50, 0.25)) | |
| add(LINE.c13.start + 0.15, 5, 'sfx', (G, at) => bell(G, at, 62, 0.14)) | |
| add(LINE.c15.start, 3, 'sfx', (G, at) => shimmer(G, at, 0.05, false)) | |
| add(LINE.c20.start - 0.7, 2, 'sfx', (G, at) => whoosh(G, at, 1.8, 0.18)) | |
| add(LINE.c22.start, 5, 'sfx', (G, at) => bell(G, at, 69, 0.1)) | |
| // 镜头四 | |
| for (const w of ['亭西', '岱祠', '碧霞元君祠', '皇帝行宫']) add(wordTime('d2', w), 0.3, 'sfx', (G, at) => tick(G, at, 0.1, 800)) | |
| add(LINE.d4.start - 0.4, 1.2, 'sfx', (G, at) => drum(G, at, 0.12, 200)) | |
| add(LINE.d7.start, 5, 'sfx', (G, at) => bell(G, at, 74, 0.08)) | |
| // 镜头五 | |
| add(LINE.e8.end + 0.15, 0.6, 'sfx', (G, at) => stamp(G, at, 0.6)) | |
| add(LINE.e8.end + 0.2, 5, 'sfx', (G, at) => bell(G, at, 57, 0.08)) | |
| // 深读 | |
| add(LINE.f2.start, 5, 'sfx', (G, at) => bell(G, at, 62, 0.08)) | |
| for (const id of ['f3', 'f4', 'f5']) add(LINE[id].start, 1.2, 'sfx', (G, at) => drum(G, at, 0.12, 150)) | |
| add(wordTime('f6', '雅洁'), 5, 'sfx', (G, at) => bell(G, at, 69, 0.12)) | |
| add(wordTime('f7', '日上'), 3, 'sfx', (G, at) => boom(G, at, 0.25)) | |
| add(wordTime('f7', '日上'), 5, 'sfx', (G, at) => bell(G, at, 62, 0.14)) | |
| // 背诵 | |
| for (let i = 0; i < 5; i++) add(LINE.g1.start + i * 0.25, 0.3, 'sfx', (G, at) => tick(G, at, 0.1, 1000 + i * 120)) | |
| for (const id of ['g8', 'g9', 'g10']) add(LINE[id].start, 0.6, 'sfx', (G, at) => stamp(G, at, 0.3)) | |
| for (const w of ['偻', '罅', '圜']) add(wordTime('g11', w) - 0.1, 0.6, 'sfx', (G, at) => stamp(G, at, 0.3)) | |
| add(LINE.g11.end + 0.2, 0.6, 'sfx', (G, at) => stamp(G, at, 0.5)) | |
| // 尾声 | |
| add(LINE.h3.start, 5, 'sfx', (G, at) => bell(G, at, 69, 0.1)) | |
| add(LINE.h4.end + 0.4, 3, 'sfx', (G, at) => boom(G, at, 0.35)) | |
| add(LINE.h4.end + 0.45, 5, 'sfx', (G, at) => bell(G, at, 50, 0.22)) | |
| add(LINE.h4.end + 0.5, 5, 'sfx', (G, at) => bell(G, at, 62, 0.14)) | |
| add(LINE.h4.end + 2.0, 0.6, 'sfx', (G, at) => stamp(G, at, 0.5)) | |
| for (const e of ev) e.d = Math.min(e.d, PRE - 0.5) | |
| ev.sort((a, b) => a.t - b.t) | |
| return ev | |
| } | |
| const segCache = new Map<string, Promise<AudioBuffer>>() | |
| /** 渲染第 k 段(30 秒)。music=false 时不含背景音乐(保留旁白与音效) */ | |
| export function renderSegment(k: number, music: boolean): Promise<AudioBuffer> { | |
| const key = k + ':' + music | |
| let p = segCache.get(key) | |
| if (p) return p | |
| p = doRender(k, music) | |
| segCache.set(key, p) | |
| // 只保留有限缓存 | |
| if (segCache.size > 8) { | |
| const first = segCache.keys().next().value as string | |
| if (first !== key) segCache.delete(first) | |
| } | |
| return p | |
| } | |
| export function clearSegCache() { | |
| segCache.clear() | |
| } | |
| async function doRender(k: number, music: boolean): Promise<AudioBuffer> { | |
| if (!EVENTS) EVENTS = buildEvents() | |
| const s0 = k * SEG * SR | |
| const s1 = Math.min(TOTAL_SAMPLES, (k + 1) * SEG * SR) | |
| const n = s1 - s0 | |
| const pre = k === 0 ? 0 : PRE | |
| const t0 = s0 / SR | |
| const base = t0 - pre | |
| const len = n + pre * SR | |
| const ctx = new OfflineAudioContext(2, len, SR) | |
| const master = new GainNode(ctx, { gain: 1.25 }) | |
| const comp = new DynamicsCompressorNode(ctx, { threshold: -18, knee: 8, ratio: 3, attack: 0.005, release: 0.25 }) | |
| const makeup = new GainNode(ctx, { gain: 1.25 }) | |
| const limiter = new DynamicsCompressorNode(ctx, { threshold: -4, knee: 0, ratio: 20, attack: 0.001, release: 0.08 }) | |
| master.connect(comp).connect(makeup).connect(limiter).connect(ctx.destination) | |
| const verb = new ConvolverNode(ctx, { buffer: ir() }) | |
| const verbIn = new GainNode(ctx, { gain: 0.5 }) | |
| verbIn.connect(verb).connect(master) | |
| const musicBus = new GainNode(ctx, { gain: 0 }) | |
| const musicPost = new GainNode(ctx, { gain: music ? 0.5 : 0 }) | |
| musicBus.connect(musicPost).connect(master) | |
| // 音乐的混响也需要随开关静音 | |
| const musicVerb = new GainNode(ctx, { gain: music ? 1 : 0 }) | |
| musicVerb.connect(verbIn) | |
| const sfx = new GainNode(ctx, { gain: 0.55 }) | |
| sfx.connect(master) | |
| const voice = new GainNode(ctx, { gain: 1.05 }) | |
| const vHp = new BiquadFilterNode(ctx, { type: 'highpass', frequency: 70 }) | |
| const vEq = new BiquadFilterNode(ctx, { type: 'peaking', frequency: 3200, gain: 2, Q: 0.8 }) | |
| voice.connect(vHp).connect(vEq).connect(master) | |
| const vs = new GainNode(ctx, { gain: 0.06 }) | |
| vEq.connect(vs).connect(verbIn) | |
| // 配乐电平自动化(10Hz 采样) | |
| const steps = Math.ceil((len / SR) * 10) + 1 | |
| const curve = new Float32Array(steps) | |
| for (let i = 0; i < steps; i++) curve[i] = musicLevel(base + i / 10) | |
| musicBus.gain.setValueCurveAtTime(curve, 0, (steps - 1) / 10) | |
| // 混响发送:音乐走 musicVerb,其余直连 | |
| const G: G = { ctx, music: musicBus, sfx, voice, verb: verbIn, rnd: mulberry32(k * 7 + 1) } | |
| const Gm: G = { ...G, verb: musicVerb } | |
| const tEnd = base + len / SR | |
| for (const e of EVENTS) { | |
| if (e.t + e.d < base || e.t >= tEnd) continue | |
| if (e.t < base) continue // 预卷足够长,不会截断 | |
| if (e.bus === 'music' && !music) continue | |
| const at = e.t - base | |
| e.play(e.bus === 'music' ? Gm : G, at) | |
| } | |
| const out = await ctx.startRendering() | |
| if (!pre) return out | |
| const res = new AudioBuffer({ length: n, sampleRate: SR, numberOfChannels: 2 }) | |
| for (let c = 0; c < 2; c++) res.copyToChannel(out.getChannelData(c).subarray(pre * SR, pre * SR + n), c) | |
| return res | |
| } | |
| export { clamp } | |