refactor: formaliza formatos de áudio e alinhamento PCM

This commit is contained in:
alex alves
2026-11-03 16:43:22 -03:00
parent 4d7ff46a36
commit 76d15b8e52
23 changed files with 349 additions and 82 deletions

View File

@@ -223,3 +223,35 @@ test("late PCM after stop is ignored", async () => {
assert.equal(player.playing, false);
assert.equal(player.sources.size, 0);
});
test("uses the output sample rate declared by the server", async () => {
const context = new FakeAudioContext();
const player = new PCMPlayer({ audioContext: context });
player.configure({ encoding: "pcm_s16le", sample_rate: 24000, channels: 1 });
await player.enqueue(pcm([100, 200, 300, 400]));
assert.equal(player.sampleRate, 24000);
assert.equal(context.sources[0].buffer.duration, 4 / 24000);
});
for (const format of [
undefined,
{ encoding: "wav", sample_rate: 48000, channels: 1 },
{ encoding: "pcm_s16le", sample_rate: 48000, channels: 2 },
{ encoding: "pcm_s16le", sample_rate: "48000", channels: 1 },
{ encoding: "pcm_s16le", sample_rate: 0, channels: 1 },
{ encoding: "pcm_s16le", sample_rate: 192000, channels: 1 },
]) {
test(`rejects unsupported playback metadata: ${JSON.stringify(format)}`, () => {
const player = new PCMPlayer({ audioContext: new FakeAudioContext() });
assert.throws(() => player.configure(format), /Formato de áudio/);
});
}
test("rejects format changes while audio is scheduled", async () => {
const player = new PCMPlayer({ audioContext: new FakeAudioContext() });
await player.enqueue(pcm([100, 200]));
assert.throws(() => player.configure({
encoding: "pcm_s16le", sample_rate: 24000, channels: 1,
}), /durante a reprodução/);
});

View File

@@ -733,3 +733,14 @@ test("barge-in resets player before new TTS response", () => {
assert.equal(finished, false);
assert.equal(resetCount, 1);
});
test("session.ready configures output before starting microphone capture", () => {
const { controller } = makeController();
const operations = [];
const format = { encoding: "pcm_s16le", sample_rate: 24000, channels: 1 };
controller.player = { configure: (received) => operations.push(received) };
controller.startListening = () => operations.push("listen");
controller.handleGatewayEvent({ type: "session.ready", output_audio: format });
assert.deepEqual(operations, [format, "listen"]);
});