Skip to content

Text to vertical video in 100+ styles: AI codes each scene (HTML/SVG/WebGL), renders a 9:16 MP4 with music and English/Vietnamese voice. MCP, CLI and REST for AI agents.

10.9 KB
Raw Blame
// Tiếng động tự tổng hợp (DSP thuần JS, không file tải về) → WAV 48 kHz stereo, đỉnh −1 dBFS (tiếng quá chói hạ thêm theo lvl).
//   node engine/audio/sfx.mjs <tên> [-o file.wav]     node engine/audio/sfx.mjs --list
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import {
  SR, osc, fm, noise, filt, sum, mul, softclip, pan, reverb, normalizePeak, fade, writeWav,
  envAD, envBell, envADSR, rng, hashStr, midiHz, ns, mixInto,
} from './dsp.mjs';

const exp = (tau) => (t) => Math.exp(-t / tau);
const sweep = (f0, f1, dur, curve = 1) => (t) => f0 * Math.pow(f1 / f0, Math.pow(Math.min(1, t / dur), curve)); // quét mũ
const withVerb = (dry, opt, wet = 0.25) => {
  const [rl, rr] = reverb(dry, opt);
  const L = new Float32Array(rl.length), R = new Float32Array(rr.length);
  for (let i = 0; i < L.length; i++) { const d = i < dry.length ? dry[i] : 0; L[i] = d + rl[i] * wet; R[i] = d + rr[i] * wet; }
  return [L, R];
};
/** Tiếng gõ 1 phím: nhiễu dải 2–5 kHz rất ngắn + thân trầm. */
const keyClick = (r, bright = 3500) => sum(0.06, [
  [mul(filt(noise(0.03, r), 'bp', bright, 1.2), exp(0.004))],
  [osc(0.05, (t) => 220 - 900 * t, { amp: exp(0.008) }), 0, 0.5],
]);
const bell = (f, dur, { ratio = 1.4, index = 3, tau = 0.5 } = {}) =>
  sum(dur, [
    [fm(dur, f, ratio, (t) => index * Math.exp(-t / (tau * 0.3)), envAD(0.002, tau))],
    [osc(dur, f * 2.76, { amp: envAD(0.001, tau * 0.35) }), 0, 0.25],
    [osc(dur, f * 5.4, { amp: envAD(0.001, tau * 0.15) }), 0, 0.12],
  ]);

const DEFS = {
  whoosh: { desc: 'gió lướt qua, có quét trái → phải (0,55s)', f: (r) => {
    const d = 0.55, n = filt(noise(d, r), 'bp', (t) => 350 * Math.pow(9, Math.sin(Math.PI * Math.min(1, t / d)) ** 1.5), 1.1);
    return pan(mul(n, envBell(d, 0.55, 1.6)), (t) => -0.7 + 1.4 * (t / d));
  } },
  swoosh: { desc: 'lướt nhanh, sáng (0,3s)', f: (r) => {
    const d = 0.3, n = filt(noise(d, r), 'bp', sweep(1200, 6000, d, 0.7), 1.4);
    return pan(mul(n, envBell(d, 0.4, 1.5)), (t) => 0.6 - 1.2 * (t / d));
  } },
  swipe: { desc: 'quẹt màn hình (0,18s)', f: (r) => mul(filt(noise(0.18, r), 'hp', sweep(800, 5000, 0.18)), envBell(0.18, 0.3, 1)) },
  pop: { desc: 'pốp bong bóng chat (0,12s)', f: (r) => sum(0.14, [
    [osc(0.14, sweep(1100, 320, 0.05), { amp: envAD(0.001, 0.035) })],
    [mul(filt(noise(0.01, r), 'hp', 3000), exp(0.002)), 0, 0.3],
  ]) },
  bubble: { desc: 'bloop đi lên (0,16s)', f: () => osc(0.16, sweep(380, 1300, 0.09, 0.6), { amp: (t) => Math.min(1, t / 0.004) * Math.exp(-t / 0.05) }) },
  'water-drop': { desc: 'giọt nước "tách" (0,25s)', f: () => sum(0.25, [
    [osc(0.25, sweep(600, 2000, 0.04, 0.5), { amp: (t) => Math.min(1, t / 0.002) * Math.exp(-t / 0.03) })],
    [osc(0.25, sweep(1500, 1700, 0.2), { amp: (t) => (t < 0.04 ? 0 : 0.35 * Math.exp(-(t - 0.04) / 0.05)) })],
  ]) },
  click: { desc: 'nút bấm UI (0,05s)', f: (r) => sum(0.05, [
    [mul(filt(noise(0.006, r), 'hp', 2500), exp(0.0015))],
    [osc(0.03, 1800, { amp: exp(0.004) }), 0, 0.6],
  ]) },
  tick: { desc: 'tích đồng hồ / đếm số (0,05s)', f: (r) => sum(0.05, [
    [osc(0.04, 3200, { amp: exp(0.006) })],
    [mul(filt(noise(0.01, r), 'bp', 6000, 2), exp(0.002)), 0, 0.5],
  ]) },
  typing: { desc: 'gõ phím 7 nhịp (0,75s)', f: (r) => {
    const parts = []; let t = 0;
    for (let k = 0; k < 7; k++) { parts.push([keyClick(r, 2500 + r() * 2500), t, 0.7 + r() * 0.3]); t += 0.07 + r() * 0.06; }
    return sum(0.8, parts);
  } },
  ding: { desc: 'chuông "ding" trong (1,2s)', f: () => withVerb(bell(1318.5, 1.2, { tau: 0.45 }), { room: 0.78, tail: 0.8 }, 0.18) },
  chime: { desc: 'chuông gió 3 nốt đi lên (1,3s)', f: () => {
    const notes = [84, 88, 91].map((m, i) => [bell(midiHz(m), 1.0, { tau: 0.35, index: 2 }), i * 0.08, 0.7]);
    return withVerb(sum(1.3, notes), { room: 0.8, tail: 0.9 }, 0.3);
  } },
  coin: { desc: 'nhặt xu 2 nốt (0,5s)', f: () => sum(0.5, [
    [osc(0.08, midiHz(83), { wave: 'soft', amp: () => 0.8 })],
    [osc(0.42, midiHz(88), { wave: 'soft', amp: (t) => 0.8 * Math.exp(-t / 0.12) }), 0.075],
  ]) },
  notification: { desc: 'thông báo 2 nốt êm (0,5s)', f: () => sum(0.55, [
    [osc(0.3, midiHz(81), { amp: envAD(0.006, 0.08) })], [osc(0.3, midiHz(93), { amp: envAD(0.006, 0.05) }), 0, 0.15],
    [osc(0.4, midiHz(88), { amp: envAD(0.006, 0.12) }), 0.13], [osc(0.4, midiHz(100), { amp: envAD(0.006, 0.06) }), 0.13, 0.15],
  ]) },
  success: { desc: 'hợp âm trưởng đi lên — xong việc (0,9s)', f: () => {
    const notes = [72, 76, 79, 84].map((m, i) => [osc(0.6, midiHz(m), { wave: 'tri', amp: envAD(0.004, i === 3 ? 0.25 : 0.1) }), i * 0.07, 0.6]);
    return withVerb(sum(0.9, notes), { room: 0.75, tail: 0.5 }, 0.2);
  } },
  error: { desc: 'báo lỗi 2 nốt trầm rè (0,4s)', lvl: -6, f: () => {
    const tone = (m) => filt(osc(0.16, midiHz(m), { wave: 'square', amp: envADSR(0.005, 0.03, 0.7, 0.03, 0.12) }), 'lp', 1600);
    return sum(0.4, [[tone(57)], [tone(54), 0.18]]);
  } },
  riser: { desc: 'dâng căng 1,5s (đặt trước mốc chốt 1,5s)', f: (r) => {
    const d = 1.5, up = (t) => Math.pow(t / d, 2.2);
    return sum(d, [
      [filt(noise(d, r, up), 'bp', sweep(400, 7000, d, 1.3), 1.5)],
      [osc(d, sweep(150, 1400, d, 1.6), { wave: 'saw', amp: (t) => 0.25 * up(t) }), 0, 1],
      [osc(d, sweep(152, 1420, d, 1.6), { wave: 'saw', amp: (t) => 0.25 * up(t) }), 0, 1],
    ]);
  } },
  impact: { desc: 'nổ trầm điện ảnh (1,6s)', f: (r) => {
    const body = sum(1.6, [
      [osc(1.6, sweep(90, 32, 0.6), { amp: envAD(0.003, 0.45) })],
      [filt(noise(0.4, r, exp(0.06)), 'lp', 900), 0, 0.8],
      [filt(noise(0.03, r, exp(0.006)), 'hp', 1500), 0, 0.4],
    ]);
    return withVerb(softclip(body, 1.8), { room: 0.86, damp: 0.6, tail: 1.2 }, 0.25);
  } },
  boom: { desc: '= impact, dài và trầm hơn (2,2s)', f: (r) => {
    const body = sum(2.2, [[osc(2.2, sweep(70, 28, 0.9), { amp: envAD(0.005, 0.7) })], [filt(noise(0.8, r, exp(0.15)), 'lp', 500), 0, 0.9]]);
    return withVerb(softclip(body, 2.2), { room: 0.88, damp: 0.7, tail: 1.4 }, 0.3);
  } },
  thud: { desc: 'rơi đặt vật nặng (0,3s)', f: (r) => sum(0.3, [
    [osc(0.3, sweep(110, 50, 0.12), { amp: envAD(0.002, 0.07) })],
    [filt(noise(0.04, r, exp(0.008)), 'lp', 700), 0, 0.6],
  ]) },
  punch: { desc: 'đấm/đập mạnh (0,3s)', lvl: -3, f: (r) => softclip(sum(0.3, [
    [osc(0.3, sweep(160, 55, 0.1), { amp: envAD(0.001, 0.06) })],
    [filt(noise(0.06, r, exp(0.012)), 'bp', 1100, 0.8), 0, 1.2],
  ]), 2.5) },
  stamp: { desc: 'đóng dấu "cộp" (0,6s)', f: (r) => withVerb(softclip(sum(0.25, [
    [osc(0.25, sweep(140, 70, 0.05), { amp: envAD(0.001, 0.04) })],
    [filt(noise(0.05, r, exp(0.01)), 'bp', 1800, 1.3), 0, 1.1],
  ]), 1.5), { room: 0.7, damp: 0.5, tail: 0.35 }, 0.25) },
  glitch: { desc: 'nhiễu số rè giật (0,4s)', lvl: -7, f: (r) => {
    const d = 0.4, out = new Float32Array(ns(d)); let i = 0;
    while (i < out.length) {
      const len = ns(0.012 + r() * 0.04), f = 80 + r() * 1800, kind = r(), on = r() > 0.2;
      for (let k = 0; k < len && i < out.length; k++, i++) {
        const t = k / SR, sq = Math.sin(6.2832 * f * t) > 0 ? 1 : -1;
        out[i] = on ? (kind < 0.5 ? sq : (k % 6 === 0 ? r() * 2 - 1 : out[i - 1] || 0)) * 0.7 : 0;
      }
    }
    return filt(out, 'lp', 6000);
  } },
  sparkle: { desc: 'lấp lánh nhiều hạt (0,9s)', f: (r) => {
    const L = new Float32Array(ns(1.1)), R = new Float32Array(L.length);
    for (let k = 0; k < 16; k++) {
      const at = r() * 0.6, f = 2500 + r() * 5500, p = r() * 2 - 1;
      const [l, rr] = pan(osc(0.25, f, { amp: envAD(0.002, 0.05 + r() * 0.06) }), p);
      mixInto(L, l, at, 0.45 * (1 - at)); mixInto(R, rr, at, 0.45 * (1 - at));
    }
    return [L, R];
  } },
  paper: { desc: 'giấy sột soạt (0,5s)', f: (r) => {
    const d = 0.5, n = filt(noise(d, r), 'bp', 3200, 0.8);
    let g = 0; const grain = new Float32Array(n.length);
    for (let i = 0; i < n.length; i++) { if (i % 240 === 0) g = r() < 0.45 ? r() : g * 0.4; grain[i] = g; }
    return mul(n.map((x, i) => x * grain[i]), envBell(d, 0.4, 1));
  } },
  'page-flip': { desc: 'lật trang (0,4s)', f: (r) => sum(0.4, [
    [mul(filt(noise(0.32, r), 'bp', sweep(5000, 1200, 0.32), 0.9), (t) => envBell(0.32, 0.6, 1)(t) * (0.7 + 0.3 * Math.sin(t * 160)))],
    [filt(noise(0.05, r, exp(0.012)), 'lp', 1500), 0.3, 0.8],
  ]) },
  heartbeat: { desc: 'tim đập lub-dub (0,9s)', f: () => {
    const beat = (g) => osc(0.3, sweep(70, 40, 0.1), { amp: (t) => g * Math.min(1, t / 0.01) * Math.exp(-t / 0.07) });
    return filt(softclip(sum(0.9, [[beat(1)], [beat(0.75), 0.24]]), 1.5), 'lp', 300);
  } },
  'camera-shutter': { desc: 'chụp ảnh tách-tách (0,25s)', f: (r) => {
    const snap = () => sum(0.06, [[mul(filt(noise(0.04, r), 'bp', 3000, 1.5), exp(0.006))], [osc(0.03, 600, { amp: exp(0.005) }), 0, 0.4]]);
    return sum(0.25, [[snap()], [mul(filt(noise(0.08, r), 'bp', 5000, 1), exp(0.02)), 0.02, 0.25], [snap(), 0.09, 0.8]]);
  } },
  zap: { desc: 'tia điện "dzzt" (0,3s)', f: () => filt(sum(0.3, [
    [fm(0.3, sweep(2400, 90, 0.25, 0.6), 0.5, (t) => 6 * Math.exp(-t / 0.1), envAD(0.002, 0.08))],
    [osc(0.3, sweep(1800, 70, 0.25), { wave: 'saw', amp: envAD(0.001, 0.06) }), 0, 0.4],
  ]), 'lp', 7000) },
  braam: { desc: 'BRAAAM kèn đồng trailer (2,4s)', f: (r) => {
    const d = 2.4, out = new Float32Array(ns(d));
    for (const m of [26, 33, 38, 45]) for (const det of [-0.12, 0, 0.11]) mixInto(out, osc(d, midiHz(m + det), { wave: 'saw', amp: envADSR(0.04, 0.4, 0.7, 1.2, 1.2), phase: r() }), 0, 0.18);
    const opened = filt(out, 'lp', (t) => 200 + 1800 * Math.exp(-t / 0.5), 1.2);
    return withVerb(softclip(opened, 1.6), { room: 0.88, damp: 0.6, tail: 1.2 }, 0.3);
  } },
  hiss: { desc: 'xì hơi (0,7s)', f: (r) => mul(filt(noise(0.7, r), 'hp', 3500), (t) => Math.min(1, t / 0.02) * Math.exp(-t / 0.25)) },
  beep: { desc: 'bíp điện tử ngắn (0,15s)', lvl: -7, f: () => osc(0.15, 1000, { wave: 'soft', amp: envADSR(0.003, 0.01, 0.8, 0.02, 0.11) }) },
};

export const SFX_NAMES = Object.keys(DEFS);
export const SFX_LIST = SFX_NAMES.map((n) => ({ name: n, desc: DEFS[n].desc }));

/** Tổng hợp 1 tiếng → [L, R] Float32Array 48 kHz, đỉnh −1 dBFS. */
export function synthSfx(name, seed = 7) {
  const d = DEFS[name];
  if (!d) throw new Error(`sfx "${name}" không có. Có: ${SFX_NAMES.join(', ')}`);
  let out = d.f(rng(seed + hashStr(name)));
  if (!Array.isArray(out)) out = [out, Float32Array.from(out)];
  return normalizePeak(fade(out, 0.0005, 0.01), -1 + (d.lvl || 0));
}
export function writeSfx(name, file, seed) { writeWav(file, synthSfx(name, seed)); return file; }

if (path.resolve(process.argv[1] || '') === fileURLToPath(import.meta.url)) {
  const a = process.argv.slice(2);
  if (!a[0] || a[0] === '--list') { for (const s of SFX_LIST) console.log(`${s.name.padEnd(15)} ${s.desc}`); process.exit(0); }
  const o = a.indexOf('-o') >= 0 ? a[a.indexOf('-o') + 1] : `${a[0]}.wav`;
  console.log(writeSfx(a[0], o));
}