openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
93 lines (92 loc) • 3.31 kB
JavaScript
import { createStreamingPcmResampler, mulawToPcm, pcmToMulaw } from "openclaw/plugin-sdk/realtime-voice-provider";
//#region extensions/openai/realtime-quicksilver-audio-buffer.ts
const RELAY_FRAME_SAMPLES = 480;
const MAX_PENDING_RELAY_FRAMES = 250;
const OPENAI_QUICKSILVER_RELAY_FRAME_BYTES = RELAY_FRAME_SAMPLES * 2;
const OPENAI_QUICKSILVER_MAX_PENDING_AUDIO_BYTES = 960 * MAX_PENDING_RELAY_FRAMES;
function assertOpenAIQuicksilverPcmOutput(e) {
if (e && (e.encoding !== "pcm16" || e.sampleRateHz !== 24e3 || e.channels !== 1)) throw new Error("GPT-Live direct audio output requires mono PCM16 at 24 kHz");
}
function closeOpenAIQuicksilverAudioOutput(e) {
if (e) {
Atomics.store(new Int32Array(e.state), 0, 1);
e.port.close();
}
}
/** Keeps telephony resampling state and its delayed output tail with the audio adapter. */
var OpenAIQuicksilverAudioAdapter = class {
constructor(e) {
this.config = e;
this.inbound = createStreamingPcmResampler(8e3, 24e3);
this.outbound = createStreamingPcmResampler(24e3, 8e3);
this.telephony = e.audioFormat?.encoding === "g711_ulaw";
}
decodeInput(e) {
return this.telephony ? this.inbound.process(mulawToPcm(e)) : e;
}
sendOutput(e) {
const t = this.telephony ? pcmToMulaw(this.outbound.process(e)) : e;
if (t.length > 0) this.config.onAudio(t);
}
finishOutput() {
if (!this.telephony) return;
const e = pcmToMulaw(this.outbound.flush());
this.outbound = createStreamingPcmResampler(24e3, 8e3);
if (e.length > 0) this.config.onAudio(e);
}
reset() {
this.inbound = createStreamingPcmResampler(8e3, 24e3);
this.outbound = createStreamingPcmResampler(24e3, 8e3);
}
};
var OpenAIQuicksilverPendingAudio = class {
constructor(e = OPENAI_QUICKSILVER_MAX_PENDING_AUDIO_BYTES, t = 2) {
this.maxBytes = e;
this.sampleBytes = t;
this.readOffset = 0;
this.pendingBytes = 0;
}
get length() {
return this.pendingBytes;
}
append(e) {
const t = e.length - e.length % this.sampleBytes;
if (t === 0) return;
const n = Math.min(t, this.maxBytes);
const r = t - n;
const i = this.storage ??= Buffer.alloc(this.maxBytes);
const a = Math.max(0, this.pendingBytes + n - this.maxBytes);
this.readOffset = (this.readOffset + a) % this.maxBytes;
this.pendingBytes -= a;
const o = (this.readOffset + this.pendingBytes) % this.maxBytes;
const s = Math.min(n, this.maxBytes - o);
e.copy(i, o, r, r + s);
if (s < n) e.copy(i, 0, r + s, r + n);
this.pendingBytes += n;
}
readInto(e) {
const t = e.length - e.length % this.sampleBytes;
const n = Math.min(t, this.pendingBytes);
const r = this.storage;
if (n === 0 || !r) return 0;
const i = Math.min(n, this.maxBytes - this.readOffset);
r.copy(e, 0, this.readOffset, this.readOffset + i);
if (i < n) r.copy(e, i, 0, n - i);
this.readOffset = (this.readOffset + n) % this.maxBytes;
this.pendingBytes -= n;
if (this.pendingBytes === 0) this.readOffset = 0;
return n;
}
take(e = this.length) {
const t = Buffer.alloc(Math.min(e, this.length));
this.readInto(t);
return t;
}
clear() {
this.storage = void 0;
this.readOffset = 0;
this.pendingBytes = 0;
}
};
//#endregion
export { closeOpenAIQuicksilverAudioOutput as i, OpenAIQuicksilverPendingAudio as n, assertOpenAIQuicksilverPcmOutput as r, OpenAIQuicksilverAudioAdapter as t };