bigbluebutton-Github/bigbluebutton-html5/imports/ui/components/captions/speech/service.js
2023-03-02 10:25:08 -03:00

44 lines
1.3 KiB
JavaScript

import { throttle } from '/imports/utils/throttle';
import { makeCall } from '/imports/ui/services/api';
const DEFAULT_LANGUAGE = 'en-US';
const THROTTLE_TIMEOUT = 2000;
const SpeechRecognitionAPI = window.SpeechRecognition || window.webkitSpeechRecognition;
const hasSpeechRecognitionSupport = () => typeof SpeechRecognitionAPI !== 'undefined';
const initSpeechRecognition = (locale = DEFAULT_LANGUAGE) => {
if (hasSpeechRecognitionSupport()) {
const speechRecognition = new SpeechRecognitionAPI();
speechRecognition.continuous = true;
speechRecognition.interimResults = true;
speechRecognition.lang = locale;
return speechRecognition;
}
return null;
};
const pushSpeechTranscript = (locale, transcript, type) => makeCall('pushSpeechTranscript', locale, transcript, type);
const throttledTranscriptPush = throttle(pushSpeechTranscript, THROTTLE_TIMEOUT, {
leading: false,
trailing: true,
});
const pushInterimTranscript = (locale, transcript) => throttledTranscriptPush(locale, transcript, 'interim');
const pushFinalTranscript = (locale, transcript) => {
throttledTranscriptPush.cancel();
pushSpeechTranscript(locale, transcript, 'final');
};
export default {
hasSpeechRecognitionSupport,
initSpeechRecognition,
pushInterimTranscript,
pushFinalTranscript,
};