mbz-voice-sdk
Version:
🎙️ MBZ Voice SDK: Easily add voice recognition, Gemini-based AI replies, and TTS to any web app.
78 lines (60 loc) • 2.23 kB
JavaScript
export class MBZVoiceAgent {
constructor({ apiUrl = "/ask", lang = "en-US", speak = true } = {}) {
this.apiUrl = apiUrl;
this.lang = lang;
this.speak = speak;
// Setup recognition
const Speech = window.SpeechRecognition || window.webkitSpeechRecognition;
if (!Speech) throw new Error("SpeechRecognition not supported in this browser.");
this.recognition = new Speech();
this.recognition.lang = this.lang;
this.recognition.interimResults = false;
// Bind recognition result handler
this.recognition.onresult = this._handleResult.bind(this);
// Internal callbacks
this._onTranscript = null;
this._onResponse = null;
}
listen() {
this.recognition.start();
}
mute() {
this.speak = false;
}
unmute() {
this.speak = true;
}
onTranscript(callback) {
this._onTranscript = callback;
}
onResponse(callback) {
this._onResponse = callback;
}
async _handleResult(event) {
const text = event.results[0][0].transcript;
if (this._onTranscript) this._onTranscript(text);
try {
const res = await fetch(this.apiUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ query: text }),
});
const data = await res.json();
const reply = data.answer;
if (this._onResponse) this._onResponse(reply);
if (this.speak) {
const utterance = new SpeechSynthesisUtterance(reply);
utterance.lang = this.lang;
utterance.pitch = 1;
utterance.rate = 1;
utterance.volume = 1;
const voices = window.speechSynthesis.getVoices();
const preferred = voices.find(v => v.name.includes("Google") || v.name.includes("Female"));
if (preferred) utterance.voice = preferred;
window.speechSynthesis.speak(utterance);
}
} catch (err) {
if (this._onResponse) this._onResponse("⚠️ Failed to connect to backend.");
}
}
}