const MAX_FRAME_SAMPLES = 3_200;
export function serializeAudioFrame(tabId, pcm16, createdAt, durationMs) {
    return { type: "m1.audio.frame", tabId, samples: Array.from(pcm16), createdAt, durationMs };
}
export function parseAudioFrame(value) {
    if (!record(value) || value.type !== "m1.audio.frame" || typeof value.tabId !== "number" || typeof value.createdAt !== "number" || typeof value.durationMs !== "number" || !Array.isArray(value.samples) || value.samples.length < 1 || value.samples.length > MAX_FRAME_SAMPLES)
        return undefined;
    const pcm16 = new Int16Array(value.samples.length);
    for (let index = 0; index < value.samples.length; index++) {
        const sample = value.samples[index];
        if (typeof sample !== "number" || !Number.isInteger(sample) || sample < -32768 || sample > 32767)
            return undefined;
        pcm16[index] = sample;
    }
    return { tabId: value.tabId, frame: { pcm16: pcm16.buffer, createdAt: value.createdAt, durationMs: value.durationMs } };
}
function record(value) { return typeof value === "object" && value !== null; }
