← GPT-AI-VIDEO
CODE · 1.8 KB

tools/generate-editorial-audio.mjs

Workspace snapshot · 09/04 14:52

import fs from "node:fs";
import path from "node:path";

const rate = 48000;
const seconds = 30;
const count = rate * seconds;
const data = Buffer.alloc(count * 2);
let seed = 90210;
const random = () => ((seed = (seed * 1664525 + 1013904223) >>> 0) / 4294967296) * 2 - 1;

for (let i = 0; i < count; i += 1) {
  const t = i / rate;
  const beat = t * 2;
  const phase = beat - Math.floor(beat);
  const kick = Math.sin(2 * Math.PI * (52 + 55 * Math.exp(-phase * 22)) * t) * Math.exp(-phase * 18);
  const tickPhase = (beat * 2) - Math.floor(beat * 2);
  const tick = random() * Math.exp(-tickPhase * 70) * (Math.floor(beat * 2) % 2 ? 0.11 : 0.055);
  const pulse = Math.sin(2 * Math.PI * 82.41 * t) * (0.032 + 0.018 * Math.sin(2 * Math.PI * 0.1 * t));
  const air = random() * 0.012 * (0.5 + 0.5 * Math.sin(2 * Math.PI * 0.05 * t));
  const accentPhase = t - Math.floor(t);
  const accent = Math.sin(2 * Math.PI * 660 * t) * Math.exp(-accentPhase * 35) * (Math.floor(t) % 4 === 3 ? 0.075 : 0);
  const fadeIn = Math.min(1, t / 0.6);
  const fadeOut = Math.min(1, (seconds - t) / 1.4);
  const value = Math.max(-1, Math.min(1, (kick * 0.22 + tick + pulse + air + accent) * fadeIn * fadeOut));
  data.writeInt16LE(Math.round(value * 32767), i * 2);
}

const header = Buffer.alloc(44);
header.write("RIFF", 0);
header.writeUInt32LE(36 + data.length, 4);
header.write("WAVEfmt ", 8);
header.writeUInt32LE(16, 16);
header.writeUInt16LE(1, 20);
header.writeUInt16LE(1, 22);
header.writeUInt32LE(rate, 24);
header.writeUInt32LE(rate * 2, 28);
header.writeUInt16LE(2, 32);
header.writeUInt16LE(16, 34);
header.write("data", 36);
header.writeUInt32LE(data.length, 40);

const output = path.resolve("public/pengin-editorial-beat.wav");
fs.mkdirSync(path.dirname(output), { recursive: true });
fs.writeFileSync(output, Buffer.concat([header, data]));
console.log(output);