@micdrop/gladia
Version:
Gladia implementation for @micdrop/server
142 lines • 4.74 kB
JavaScript
// src/GladiaSTT.ts
import { convertToPCM, STT } from "@micdrop/server";
import WebSocket from "ws";
var SAMPLE_RATE = 16e3;
var BIT_DEPTH = 16;
var GladiaSTT = class extends STT {
// Store audio chunks to send them again if reconnecting
constructor(options) {
super();
this.options = options;
this.audioChunksPending = [];
// Register real-time transcription to get a WebSocket URL
this.getURL = async () => {
const response = await fetch("https://api.gladia.io/v2/live", {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Gladia-Key": this.options.apiKey
},
body: JSON.stringify({
encoding: "wav/pcm",
sample_rate: SAMPLE_RATE,
bit_depth: BIT_DEPTH,
channels: 1,
messages_config: {
receive_final_transcripts: true,
receive_speech_events: false,
receive_pre_processing_events: false,
receive_realtime_processing_events: false,
receive_post_processing_events: false,
receive_acknowledgments: false,
receive_errors: true,
receive_lifecycle_events: false
},
...this.options.settings
})
});
if (!response.ok) {
throw new Error(
`${response.status}: ${await response.text() || response.statusText}`
);
}
const { url } = await response.json();
return url;
};
// Connect to Gladia
this.initWS = async (url) => {
return new Promise((resolve, reject) => {
const socket = new WebSocket(url);
this.socket = socket;
socket.addEventListener("open", () => {
this.log("Connection opened");
resolve();
});
socket.addEventListener("error", (error) => {
reject(error);
});
socket.addEventListener("close", ({ code, reason }) => {
this.socket?.removeAllListeners();
this.socket = void 0;
if (code !== 1e3) {
this.reconnect();
} else {
this.log("Connection closed", { code, reason });
}
});
socket.addEventListener("message", (event) => {
const message = JSON.parse(event.data.toString());
if (message.type === "transcript" && message.data.is_final) {
const transcript = message.data.utterance.text;
this.log(`Received transcript: "${transcript}"`);
this.emit("Transcript", transcript);
this.audioChunksPending.length = 0;
}
});
});
};
this.initPromise = this.getURL().then(this.initWS).catch((error) => {
console.error("[GladiaSTT] Connection error:", error);
});
}
transcribe(audioStream) {
const pcmStream = convertToPCM(audioStream, SAMPLE_RATE, BIT_DEPTH);
pcmStream.on("data", async (chunk) => {
await this.initPromise;
this.audioChunksPending.push(chunk);
this.socket?.send(chunk);
this.log(`Sent audio chunk (${chunk.byteLength} bytes)`);
});
pcmStream.on("end", async () => {
if (this.audioChunksPending.length === 0) return;
await this.initPromise;
this.sendSilence(1);
});
}
destroy() {
super.destroy();
if (this.reconnectTimeout) {
clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = void 0;
}
this.socket?.removeAllListeners();
if (this.socket?.readyState === WebSocket.OPEN) {
this.socket?.close(1e3);
}
this.socket = void 0;
}
reconnect() {
this.initPromise = new Promise((resolve, reject) => {
this.log("Reconnecting...");
this.reconnectTimeout = setTimeout(() => {
this.reconnectTimeout = void 0;
this.getURL().then(this.initWS).then(() => {
if (this.audioChunksPending.length > 0) {
this.log("Sending audio chunks again");
this.audioChunksPending.forEach(
(chunk) => this.socket?.send(chunk)
);
}
}).then(resolve).catch((error) => {
this.log("Reconnection error:", error);
reject(error);
});
}, 1e3);
});
}
sendSilence(durationSeconds) {
if (!this.socket) return;
const numSamples = Math.round(SAMPLE_RATE * durationSeconds);
const bytesPerSample = BIT_DEPTH / 8;
const silenceBuffer = Buffer.alloc(numSamples * bytesPerSample, 0);
this.socket.send(silenceBuffer);
this.audioChunksPending.push(silenceBuffer);
this.log(
`Sent ${durationSeconds * 1e3}ms of silence (${silenceBuffer.byteLength} bytes) after stream end`
);
}
};
export {
GladiaSTT
};
//# sourceMappingURL=index.mjs.map