|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | + |
| 3 | +import { float32ToLinear16 } from "../../src/audio/conversions"; |
| 4 | +import { createAgentHarness } from "../helpers/harness"; |
| 5 | + |
| 6 | +const audioConfig = { |
| 7 | + input: { encoding: "linear16", sampleRate: 16000, channels: 1 }, |
| 8 | + output: { encoding: "linear16", sampleRate: 24000, channels: 1 }, |
| 9 | +} as const; |
| 10 | + |
| 11 | +describe("audio input normalization", () => { |
| 12 | + it("normalizes Float32Array input to linear16", async () => { |
| 13 | + const harness = createAgentHarness({ audio: audioConfig }); |
| 14 | + const { agent, events, stt } = harness; |
| 15 | + |
| 16 | + agent.start(); |
| 17 | + await events.waitForEvent("agent:started"); |
| 18 | + |
| 19 | + const float32 = new Float32Array([0, 0.5, -0.5, 1, -1]); |
| 20 | + const expected = new Int16Array(float32ToLinear16(float32)); |
| 21 | + |
| 22 | + agent.sendAudio(float32); |
| 23 | + |
| 24 | + expect(stt.receivedAudio.length).toBe(1); |
| 25 | + const received = new Int16Array(stt.receivedAudio[0]); |
| 26 | + expect(received).toEqual(expected); |
| 27 | + |
| 28 | + agent.stop(); |
| 29 | + await events.waitForEvent("agent:stopped"); |
| 30 | + }); |
| 31 | + |
| 32 | + it("preserves Int16Array input for linear16", async () => { |
| 33 | + const harness = createAgentHarness({ audio: audioConfig }); |
| 34 | + const { agent, events, stt } = harness; |
| 35 | + |
| 36 | + agent.start(); |
| 37 | + await events.waitForEvent("agent:started"); |
| 38 | + |
| 39 | + const int16 = new Int16Array([1, -2, 3, -4]); |
| 40 | + agent.sendAudio(int16); |
| 41 | + |
| 42 | + expect(stt.receivedAudio.length).toBe(1); |
| 43 | + expect(new Int16Array(stt.receivedAudio[0])).toEqual(int16); |
| 44 | + |
| 45 | + agent.stop(); |
| 46 | + await events.waitForEvent("agent:stopped"); |
| 47 | + }); |
| 48 | + |
| 49 | + it("normalizes Uint8Array input to configured encoding", async () => { |
| 50 | + const harness = createAgentHarness({ audio: audioConfig }); |
| 51 | + const { agent, events, stt } = harness; |
| 52 | + |
| 53 | + agent.start(); |
| 54 | + await events.waitForEvent("agent:started"); |
| 55 | + |
| 56 | + const bytes = new Uint8Array([1, 2, 3, 4]); |
| 57 | + agent.sendAudio(bytes); |
| 58 | + |
| 59 | + expect(stt.receivedAudio.length).toBe(1); |
| 60 | + expect(new Uint8Array(stt.receivedAudio[0])).toEqual(bytes); |
| 61 | + |
| 62 | + agent.stop(); |
| 63 | + await events.waitForEvent("agent:stopped"); |
| 64 | + }); |
| 65 | + |
| 66 | + it("normalizes Buffer input to configured encoding", async () => { |
| 67 | + const harness = createAgentHarness({ audio: audioConfig }); |
| 68 | + const { agent, events, stt } = harness; |
| 69 | + |
| 70 | + agent.start(); |
| 71 | + await events.waitForEvent("agent:started"); |
| 72 | + |
| 73 | + const buffer = Buffer.from([5, 6, 7]); |
| 74 | + agent.sendAudio(buffer); |
| 75 | + |
| 76 | + expect(stt.receivedAudio.length).toBe(1); |
| 77 | + expect(new Uint8Array(stt.receivedAudio[0])).toEqual(new Uint8Array(buffer)); |
| 78 | + |
| 79 | + agent.stop(); |
| 80 | + await events.waitForEvent("agent:stopped"); |
| 81 | + }); |
| 82 | + |
| 83 | + it("rejects Blob input with a guidance error", async () => { |
| 84 | + const harness = createAgentHarness({ audio: audioConfig }); |
| 85 | + const { agent, events } = harness; |
| 86 | + |
| 87 | + agent.start(); |
| 88 | + await events.waitForEvent("agent:started"); |
| 89 | + |
| 90 | + const blob = new Blob([new Uint8Array([1, 2, 3])]); |
| 91 | + expect(() => agent.sendAudio(blob)).toThrow( |
| 92 | + "Blob input requires async conversion. Use `await blob.arrayBuffer()` before calling sendAudio()", |
| 93 | + ); |
| 94 | + |
| 95 | + agent.stop(); |
| 96 | + await events.waitForEvent("agent:stopped"); |
| 97 | + }); |
| 98 | +}); |
0 commit comments