2022-03-25 03:05:20 +08:00
|
|
|
import _ from 'lodash';
|
2022-04-28 03:42:07 +08:00
|
|
|
import { diff } from '@mconf/bbb-diff';
|
2022-03-25 03:05:20 +08:00
|
|
|
import { Session } from 'meteor/session';
|
2022-04-01 03:40:07 +08:00
|
|
|
import Auth from '/imports/ui/services/auth';
|
2022-03-25 03:05:20 +08:00
|
|
|
import { makeCall } from '/imports/ui/services/api';
|
2022-04-14 21:59:54 +08:00
|
|
|
import logger from '/imports/startup/client/logger';
|
|
|
|
import Users from '/imports/api/users';
|
2022-03-25 03:05:20 +08:00
|
|
|
import AudioService from '/imports/ui/components/audio/service';
|
|
|
|
|
2022-04-12 21:53:53 +08:00
|
|
|
const THROTTLE_TIMEOUT = 1000;
|
|
|
|
|
2022-04-12 04:48:01 +08:00
|
|
|
const CONFIG = Meteor.settings.public.app.audioCaptions;
|
|
|
|
const ENABLED = CONFIG.enabled;
|
2022-04-28 22:06:19 +08:00
|
|
|
const LANGUAGES = CONFIG.language.available;
|
2022-04-11 21:45:09 +08:00
|
|
|
|
2022-03-25 03:05:20 +08:00
|
|
|
const SpeechRecognitionAPI = window.SpeechRecognition || window.webkitSpeechRecognition;
|
|
|
|
|
2022-04-12 04:48:01 +08:00
|
|
|
const hasSpeechRecognitionSupport = () => typeof SpeechRecognitionAPI !== 'undefined';
|
2022-03-25 03:05:20 +08:00
|
|
|
|
2022-04-12 21:53:53 +08:00
|
|
|
const setSpeechVoices = () => {
|
|
|
|
if (typeof window.speechSynthesis === 'undefined') return;
|
|
|
|
|
2022-04-29 01:24:50 +08:00
|
|
|
Session.set('speechVoices', _.uniq(window.speechSynthesis.getVoices().map((v) => v.lang)));
|
2022-04-12 21:53:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Trigger getVoices
|
|
|
|
setSpeechVoices();
|
|
|
|
|
|
|
|
const getSpeechVoices = () => {
|
|
|
|
const voices = Session.get('speechVoices') || [];
|
|
|
|
|
|
|
|
return voices.filter((v) => LANGUAGES.includes(v));
|
|
|
|
};
|
|
|
|
|
2022-04-29 03:13:21 +08:00
|
|
|
const setSpeechLocale = (value) => {
|
2022-04-12 21:53:53 +08:00
|
|
|
const voices = getSpeechVoices();
|
2022-04-29 03:13:21 +08:00
|
|
|
if (voices.includes(value) || value === '') {
|
|
|
|
makeCall('setSpeechLocale', value);
|
2022-04-12 21:53:53 +08:00
|
|
|
} else {
|
2022-04-14 21:59:54 +08:00
|
|
|
logger.error({
|
2022-04-29 03:13:21 +08:00
|
|
|
logCode: 'captions_speech_locale',
|
|
|
|
}, 'Captions speech set locale error');
|
2022-04-12 21:53:53 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-06-20 23:15:03 +08:00
|
|
|
const useFixedLocale = () => isEnabled() && CONFIG.language.forceLocale;
|
2022-04-12 21:53:53 +08:00
|
|
|
|
2022-04-12 04:48:01 +08:00
|
|
|
const initSpeechRecognition = () => {
|
2022-06-20 23:11:49 +08:00
|
|
|
if (!isEnabled()) return null;
|
2022-03-25 03:05:20 +08:00
|
|
|
if (hasSpeechRecognitionSupport()) {
|
2022-04-12 21:53:53 +08:00
|
|
|
// Effectivate getVoices
|
|
|
|
setSpeechVoices();
|
2022-03-25 03:05:20 +08:00
|
|
|
const speechRecognition = new SpeechRecognitionAPI();
|
|
|
|
speechRecognition.continuous = true;
|
|
|
|
speechRecognition.interimResults = true;
|
|
|
|
|
2022-06-20 23:15:03 +08:00
|
|
|
if (useFixedLocale() || localeAsDefaultSelected()) {
|
|
|
|
setSpeechLocale(getLocale());
|
2022-04-29 03:13:21 +08:00
|
|
|
} else {
|
|
|
|
setSpeechLocale(navigator.language);
|
|
|
|
}
|
2022-04-12 21:53:53 +08:00
|
|
|
|
2022-03-25 03:05:20 +08:00
|
|
|
return speechRecognition;
|
|
|
|
}
|
|
|
|
|
2022-04-14 21:59:54 +08:00
|
|
|
logger.warn({
|
|
|
|
logCode: 'captions_speech_unsupported',
|
|
|
|
}, 'Captions speech unsupported');
|
2022-04-12 21:53:53 +08:00
|
|
|
|
2022-03-25 03:05:20 +08:00
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2022-04-28 03:42:07 +08:00
|
|
|
let prevId = '';
|
|
|
|
let prevTranscript = '';
|
|
|
|
const updateTranscript = (id, transcript, locale) => {
|
|
|
|
// If it's a new sentence
|
|
|
|
if (id !== prevId) {
|
|
|
|
prevId = id;
|
|
|
|
prevTranscript = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
const transcriptDiff = diff(prevTranscript, transcript);
|
|
|
|
|
|
|
|
let start = 0;
|
|
|
|
let end = 0;
|
|
|
|
let text = '';
|
|
|
|
if (transcriptDiff) {
|
|
|
|
start = transcriptDiff.start;
|
|
|
|
end = transcriptDiff.end;
|
|
|
|
text = transcriptDiff.text;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stores current transcript as previous
|
|
|
|
prevTranscript = transcript;
|
|
|
|
|
|
|
|
makeCall('updateTranscript', id, start, end, text, transcript, locale);
|
|
|
|
};
|
2022-03-25 03:05:20 +08:00
|
|
|
|
2022-04-01 03:40:07 +08:00
|
|
|
const throttledTranscriptUpdate = _.throttle(updateTranscript, THROTTLE_TIMEOUT, {
|
2022-03-25 03:05:20 +08:00
|
|
|
leading: false,
|
|
|
|
trailing: true,
|
|
|
|
});
|
|
|
|
|
2022-04-01 03:40:07 +08:00
|
|
|
const updateInterimTranscript = (id, transcript, locale) => {
|
|
|
|
throttledTranscriptUpdate(id, transcript, locale);
|
|
|
|
};
|
2022-03-25 03:05:20 +08:00
|
|
|
|
2022-04-01 03:40:07 +08:00
|
|
|
const updateFinalTranscript = (id, transcript, locale) => {
|
|
|
|
throttledTranscriptUpdate.cancel();
|
|
|
|
updateTranscript(id, transcript, locale);
|
2022-03-25 03:05:20 +08:00
|
|
|
};
|
|
|
|
|
2022-04-14 21:59:54 +08:00
|
|
|
const getSpeechLocale = (userId = Auth.userID) => {
|
|
|
|
const user = Users.findOne({ userId }, { fields: { speechLocale: 1 } });
|
2022-03-25 03:05:20 +08:00
|
|
|
|
2022-04-14 21:59:54 +08:00
|
|
|
if (user) return user.speechLocale;
|
|
|
|
|
|
|
|
return '';
|
|
|
|
};
|
|
|
|
|
|
|
|
const hasSpeechLocale = (userId = Auth.userID) => getSpeechLocale(userId) !== '';
|
2022-03-25 03:05:20 +08:00
|
|
|
|
2022-04-12 21:53:53 +08:00
|
|
|
const isLocaleValid = (locale) => LANGUAGES.includes(locale);
|
2022-03-25 03:05:20 +08:00
|
|
|
|
2022-04-14 01:23:18 +08:00
|
|
|
const isEnabled = () => ENABLED;
|
2022-04-12 04:48:01 +08:00
|
|
|
|
2022-04-14 01:23:18 +08:00
|
|
|
const isActive = () => isEnabled() && hasSpeechRecognitionSupport() && hasSpeechLocale();
|
2022-03-25 03:05:20 +08:00
|
|
|
|
|
|
|
const getStatus = () => {
|
|
|
|
const active = isActive();
|
2022-04-12 04:48:01 +08:00
|
|
|
const locale = getSpeechLocale();
|
2022-03-25 03:05:20 +08:00
|
|
|
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 {
|
2022-04-12 04:48:01 +08:00
|
|
|
locale,
|
2022-03-29 03:23:46 +08:00
|
|
|
connected,
|
|
|
|
talking,
|
2022-03-25 03:05:20 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-04-01 03:40:07 +08:00
|
|
|
const generateId = () => `${Auth.userID}-${Date.now()}`;
|
|
|
|
|
2022-06-20 23:15:03 +08:00
|
|
|
const localeAsDefaultSelected = () => CONFIG.language.defaultSelectLocale;
|
|
|
|
|
|
|
|
const getLocale = () => {
|
|
|
|
const { locale } = CONFIG.language;
|
|
|
|
if (locale === 'browserLanguage') return navigator.language;
|
|
|
|
if (locale === 'disabled') return '';
|
|
|
|
return locale;
|
|
|
|
};
|
|
|
|
|
2022-03-25 03:05:20 +08:00
|
|
|
export default {
|
2022-04-12 04:48:01 +08:00
|
|
|
LANGUAGES,
|
2022-03-25 03:05:20 +08:00
|
|
|
hasSpeechRecognitionSupport,
|
|
|
|
initSpeechRecognition,
|
2022-04-01 03:40:07 +08:00
|
|
|
updateInterimTranscript,
|
|
|
|
updateFinalTranscript,
|
2022-04-12 21:53:53 +08:00
|
|
|
getSpeechVoices,
|
2022-04-12 04:48:01 +08:00
|
|
|
getSpeechLocale,
|
|
|
|
setSpeechLocale,
|
2022-04-14 21:59:54 +08:00
|
|
|
hasSpeechLocale,
|
2022-04-12 21:53:53 +08:00
|
|
|
isLocaleValid,
|
2022-03-25 03:05:20 +08:00
|
|
|
isEnabled,
|
|
|
|
isActive,
|
|
|
|
getStatus,
|
2022-04-01 03:40:07 +08:00
|
|
|
generateId,
|
2022-06-20 23:15:03 +08:00
|
|
|
useFixedLocale,
|
2022-03-25 03:05:20 +08:00
|
|
|
};
|