@broadreachalliance/q-assistant
Version:
A React-based chat window supporting both voice and text communication modes.
44 lines (43 loc) • 1.36 kB
JavaScript
import React, { createContext, useContext, useEffect, useState } from "react";
const SpeechContext = /*#__PURE__*/createContext();
export const SpeechProvider = ({
children
}) => {
const [speechStatus, setSpeechStatus] = useState({
sttSupported: false,
ttsSupported: false,
voices: []
});
const [speechRecognition, setSpeechRecognition] = useState();
const [utterence, setUtterence] = useState();
const initSpeechCapabilities = () => {
const sttSupported = "SpeechRecognition" in window || "webkitSpeechRecognition" in window;
const ttsSupported = "speechSynthesis" in window;
let availableVoices = [];
if (ttsSupported) {
availableVoices = window.speechSynthesis.getVoices();
}
setSpeechStatus({
sttSupported,
ttsSupported,
voices: availableVoices
});
};
useEffect(() => {
initSpeechCapabilities();
// Listen for voice changes (browser might load voices asynchronously)
if ("speechSynthesis" in window) {
window.speechSynthesis.onvoiceschanged = initSpeechCapabilities;
}
}, []);
return /*#__PURE__*/React.createElement(SpeechContext.Provider, {
value: {
speechStatus,
speechRecognition,
setSpeechRecognition,
utterence,
setUtterence
}
}, children);
};
export const useSpeech = () => useContext(SpeechContext);