2022-03-25 03:05:20 +08:00
|
|
|
import _ from 'lodash';
|
|
|
|
import { Session } from 'meteor/session';
|
|
|
|
import { makeCall } from '/imports/ui/services/api';
|
|
|
|
import AudioService from '/imports/ui/components/audio/service';
|
|
|
|
|
|
|
|
const DEFAULT_LANGUAGE = 'pt-BR';
|
|
|
|
const ENABLED = Meteor.settings.public.app.enableAudioCaptions;
|
|
|
|
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 pushAudioTranscript = (transcript, type) => makeCall('pushAudioTranscript', transcript, type);
|
|
|
|
|
|
|
|
const throttledTranscriptPush = _.throttle(pushAudioTranscript, THROTTLE_TIMEOUT, {
|
|
|
|
leading: false,
|
|
|
|
trailing: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
const pushInterimTranscript = (transcript) => throttledTranscriptPush(transcript, 'interim');
|
|
|
|
|
|
|
|
const pushFinalTranscript = (transcript) => {
|
|
|
|
throttledTranscriptPush.cancel();
|
|
|
|
pushAudioTranscript(transcript, 'final');
|
|
|
|
};
|
|
|
|
|
|
|
|
const getSpeech = () => Session.get('speech') || false;
|
|
|
|
|
|
|
|
const setSpeech = (value) => Session.set('speech', value);
|
|
|
|
|
|
|
|
const isEnabled = () => ENABLED && hasSpeechRecognitionSupport();
|
|
|
|
|
|
|
|
const isActive = () => isEnabled() && getSpeech();
|
|
|
|
|
|
|
|
const getStatus = () => {
|
|
|
|
const active = isActive();
|
|
|
|
const audio = AudioService.isConnected() && !AudioService.isEchoTest() && !AudioService.isMuted();
|
2022-03-29 03:23:46 +08:00
|
|
|
const connected = Meteor.status().connected && active && audio;
|
|
|
|
const talking = AudioService.isTalking();
|
2022-03-25 03:05:20 +08:00
|
|
|
|
|
|
|
return {
|
|
|
|
locale: DEFAULT_LANGUAGE,
|
2022-03-29 03:23:46 +08:00
|
|
|
connected,
|
|
|
|
talking,
|
2022-03-25 03:05:20 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export default {
|
|
|
|
hasSpeechRecognitionSupport,
|
|
|
|
initSpeechRecognition,
|
|
|
|
pushInterimTranscript,
|
|
|
|
pushFinalTranscript,
|
|
|
|
getSpeech,
|
|
|
|
setSpeech,
|
|
|
|
isEnabled,
|
|
|
|
isActive,
|
|
|
|
getStatus,
|
|
|
|
};
|