import { AudioDocument } from "@suaveplan/audio-core/document/document"; const PAGE_SIZE = 1024 * 1024; /** Build an `AudioDocument` from a flat mono `Float32Array`. */ export function makeDoc( samples: Float32Array, sampleRate = 48_000 ): AudioDocument { const pages: { channelData: Float32Array[]; startSample: number }[] = []; let offset = 0; while (offset < samples.length) { const end = Math.min(offset + PAGE_SIZE, samples.length); pages.push({ channelData: [samples.slice(offset, end)], startSample: offset, }); offset = end; } if (pages.length === 0) { pages.push({ channelData: [new Float32Array(0)], startSample: 0 }); } return new AudioDocument({ sampleRate, channels: 1, sourceLength: samples.length, pages, }); } export function sineWave( durationSec: number, freqHz: number, sampleRate = 48_000 ): Float32Array { const n = Math.floor(durationSec * sampleRate); const out = new Float32Array(n); for (let i = 0; i < n; i++) out[i] = Math.sin((2 * Math.PI * freqHz * i) / sampleRate); return out; } export function whiteNoise(length: number, seed = 12345): Float32Array { let s = seed >>> 0; const out = new Float32Array(length); for (let i = 0; i < length; i++) { s = (1_664_525 * s + 1_013_904_223) >>> 0; out[i] = (s / 0xffff_ffff) * 2 - 1; } return out; }