2023-09-21 21:48:00 +08:00
|
|
|
import { unique } from 'radash';
|
|
|
|
import { setAudioCaptionEnable } from '/imports/ui/core/local-states/useAudioCaptionEnable';
|
2024-06-14 21:30:48 +08:00
|
|
|
import { useIsLiveTranscriptionEnabled } from '/imports/ui/services/features';
|
2024-06-13 00:06:07 +08:00
|
|
|
import getFromUserSettings from '/imports/ui/services/users-settings';
|
|
|
|
import { Caption } from './live/queries';
|
2024-06-06 21:50:03 +08:00
|
|
|
import Session from '/imports/ui/services/storage/in-memory';
|
2023-09-21 21:48:00 +08:00
|
|
|
|
2024-06-13 00:06:07 +08:00
|
|
|
export const splitTranscript = (obj: Caption) => {
|
|
|
|
const CAPTIONS_CONFIG = window.meetingClientSettings.public.captions;
|
|
|
|
const CHARACTERS_PER_LINE = CAPTIONS_CONFIG.lineLimit;
|
|
|
|
const LINES_PER_MESSAGE = CAPTIONS_CONFIG.lines;
|
|
|
|
const transcripts = [];
|
|
|
|
const words = obj.captionText.split(' ');
|
|
|
|
|
|
|
|
let currentLine = '';
|
|
|
|
let result = '';
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
|
|
for (const word of words) {
|
|
|
|
if ((currentLine + word).length <= CHARACTERS_PER_LINE) {
|
|
|
|
currentLine += `${word} `;
|
|
|
|
} else {
|
|
|
|
result += `${currentLine.trim()}\n`;
|
|
|
|
currentLine = `${word} `;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.split('\n').length > LINES_PER_MESSAGE) {
|
|
|
|
transcripts.push(result);
|
|
|
|
result = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.length) {
|
|
|
|
transcripts.push(result);
|
|
|
|
}
|
|
|
|
transcripts.push(currentLine.trim());
|
|
|
|
|
|
|
|
return transcripts.map((t) => { return { ...obj, captionText: t }; });
|
|
|
|
};
|
|
|
|
|
2024-06-14 21:30:48 +08:00
|
|
|
export const useIsAudioTranscriptionEnabled = () => useIsLiveTranscriptionEnabled();
|
2023-09-21 21:48:00 +08:00
|
|
|
|
2024-06-13 00:06:07 +08:00
|
|
|
const getSpeechProvider = () => {
|
2024-05-29 21:26:11 +08:00
|
|
|
const PROVIDER = window.meetingClientSettings.public.app.audioCaptions.provider;
|
2024-06-13 00:06:07 +08:00
|
|
|
return getFromUserSettings('bbb_transcription_provider', PROVIDER);
|
2024-05-29 21:26:11 +08:00
|
|
|
};
|
2023-09-21 21:48:00 +08:00
|
|
|
|
2024-06-13 00:06:07 +08:00
|
|
|
export const isWebSpeechApi = () => getSpeechProvider() === 'webspeech';
|
|
|
|
export const isGladia = () => getSpeechProvider() === 'gladia';
|
|
|
|
|
2023-09-21 21:48:00 +08:00
|
|
|
export const getSpeechVoices = () => {
|
2024-05-29 21:26:11 +08:00
|
|
|
const LANGUAGES = window.meetingClientSettings.public.app.audioCaptions.language.available;
|
2023-09-21 21:48:00 +08:00
|
|
|
if (!isWebSpeechApi()) return LANGUAGES;
|
|
|
|
|
2024-07-24 22:39:11 +08:00
|
|
|
if (!window.speechSynthesis) return null;
|
|
|
|
|
2023-09-21 21:48:00 +08:00
|
|
|
return unique(
|
|
|
|
window
|
|
|
|
.speechSynthesis
|
|
|
|
.getVoices()
|
|
|
|
.map((v) => v.lang)
|
|
|
|
.filter((v) => LANGUAGES.includes(v)),
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export const setAudioCaptions = (value: boolean) => {
|
|
|
|
setAudioCaptionEnable(value);
|
|
|
|
// @ts-ignore - Exist while we have meteor in the project
|
2024-06-06 21:50:03 +08:00
|
|
|
Session.setItem('audioCaptions', value);
|
2023-09-21 21:48:00 +08:00
|
|
|
};
|
|
|
|
|
2024-06-13 00:06:07 +08:00
|
|
|
export const setSpeechLocale = (value: string, setUserSpeechLocale: (a: string, b: string) => void) => {
|
|
|
|
setUserSpeechLocale(value, getSpeechProvider());
|
|
|
|
};
|
|
|
|
|
2024-06-04 04:14:32 +08:00
|
|
|
// SpeechLocale or CaptionLocale
|
|
|
|
export const setUserLocaleProperty = (value: string, setUserLocaleCallback: (a: string, b: string) => void) => {
|
2024-05-29 21:26:11 +08:00
|
|
|
const PROVIDER = window.meetingClientSettings.public.app.audioCaptions.provider;
|
2024-06-04 04:14:32 +08:00
|
|
|
setUserLocaleCallback(value, PROVIDER);
|
2023-09-21 21:48:00 +08:00
|
|
|
};
|
|
|
|
|
2024-05-29 21:26:11 +08:00
|
|
|
export const useFixedLocale = () => {
|
|
|
|
const FORCE_LOCALE = window.meetingClientSettings.public.app.audioCaptions.language.forceLocale;
|
2024-06-14 21:30:48 +08:00
|
|
|
return useIsAudioTranscriptionEnabled() && FORCE_LOCALE;
|
2024-05-29 21:26:11 +08:00
|
|
|
};
|
2023-09-21 21:48:00 +08:00
|
|
|
|
|
|
|
export default {
|
|
|
|
getSpeechVoices,
|
2024-06-14 21:30:48 +08:00
|
|
|
useIsAudioTranscriptionEnabled,
|
2024-06-04 04:14:32 +08:00
|
|
|
setUserLocaleProperty,
|
2024-06-13 00:06:07 +08:00
|
|
|
setSpeechLocale,
|
2023-09-21 21:48:00 +08:00
|
|
|
setAudioCaptions,
|
|
|
|
isWebSpeechApi,
|
|
|
|
useFixedLocale,
|
2024-06-13 00:06:07 +08:00
|
|
|
isGladia,
|
|
|
|
splitTranscript,
|
2023-09-21 21:48:00 +08:00
|
|
|
};
|