import { InvalidInputError, VoxShotError } from "../errors.js"; import type { AudioPlayer } from "../platform.js"; import { encodeWav } from "./wav.js"; /** * The result of a synthesis call: raw samples plus the conveniences most * applications need (playback, WAV export, blob creation). */ export class SynthesizedAudio { readonly samples: Float32Array; readonly sampleRate: number; readonly #player: AudioPlayer | undefined; constructor(samples: Float32Array, sampleRate: number, player?: AudioPlayer) { if (!Number.isFinite(sampleRate) || sampleRate <= 0) { throw new InvalidInputError("sampleRate must be a positive finite number."); } this.samples = samples; this.sampleRate = sampleRate; this.#player = player; } /** Length of the signal in seconds. */ get duration(): number { return this.samples.length / this.sampleRate; } /** Encode the signal as a 16 bit PCM WAV file. */ toWav(): ArrayBuffer { return encodeWav(this.samples, this.sampleRate); } /** Encode the signal as an `audio/wav` blob, ready for an `