2025-01-29 19:46:58 -05:00
|
|
|
/* eslint-disable jsx-a11y/media-has-caption */
|
2024-08-15 11:34:25 -04:00
|
|
|
import { useEffect, useMemo } from 'react';
|
|
|
|
|
import { useRecoilValue } from 'recoil';
|
|
|
|
|
import type { TMessageAudio } from '~/common';
|
2025-04-15 04:39:01 +02:00
|
|
|
import { useLocalize, useTTSBrowser, useTTSExternal } from '~/hooks';
|
|
|
|
|
import { VolumeIcon, VolumeMuteIcon, Spinner } from '~/components';
|
2024-08-15 11:34:25 -04:00
|
|
|
import { logger } from '~/utils';
|
|
|
|
|
import store from '~/store';
|
|
|
|
|
|
|
|
|
|
export function BrowserTTS({ isLast, index, messageId, content, className }: TMessageAudio) {
|
|
|
|
|
const localize = useLocalize();
|
|
|
|
|
const playbackRate = useRecoilValue(store.playbackRate);
|
|
|
|
|
|
|
|
|
|
const { toggleSpeech, isSpeaking, isLoading, audioRef } = useTTSBrowser({
|
|
|
|
|
isLast,
|
|
|
|
|
index,
|
|
|
|
|
messageId,
|
|
|
|
|
content,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const renderIcon = (size: string) => {
|
|
|
|
|
if (isLoading === true) {
|
|
|
|
|
return <Spinner size={size} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isSpeaking === true) {
|
|
|
|
|
return <VolumeMuteIcon size={size} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <VolumeIcon size={size} />;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const messageAudio = document.getElementById(`audio-${messageId}`) as HTMLAudioElement | null;
|
|
|
|
|
if (!messageAudio) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (playbackRate != null && playbackRate > 0 && messageAudio.playbackRate !== playbackRate) {
|
|
|
|
|
messageAudio.playbackRate = playbackRate;
|
|
|
|
|
}
|
|
|
|
|
}, [audioRef, isSpeaking, playbackRate, messageId]);
|
|
|
|
|
|
|
|
|
|
logger.log(
|
|
|
|
|
'MessageAudio: audioRef.current?.src, audioRef.current',
|
|
|
|
|
audioRef.current?.src,
|
|
|
|
|
audioRef.current,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<button
|
|
|
|
|
className={className}
|
|
|
|
|
onClickCapture={() => {
|
|
|
|
|
if (audioRef.current) {
|
|
|
|
|
audioRef.current.muted = false;
|
|
|
|
|
}
|
|
|
|
|
toggleSpeech();
|
|
|
|
|
}}
|
|
|
|
|
type="button"
|
|
|
|
|
title={isSpeaking === true ? localize('com_ui_stop') : localize('com_ui_read_aloud')}
|
|
|
|
|
>
|
|
|
|
|
{renderIcon('19')}
|
|
|
|
|
</button>
|
|
|
|
|
<audio
|
|
|
|
|
ref={audioRef}
|
|
|
|
|
controls
|
|
|
|
|
preload="none"
|
|
|
|
|
controlsList="nodownload nofullscreen noremoteplayback"
|
|
|
|
|
style={{
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
overflow: 'hidden',
|
|
|
|
|
display: 'none',
|
|
|
|
|
height: '0px',
|
|
|
|
|
width: '0px',
|
|
|
|
|
}}
|
|
|
|
|
src={audioRef.current?.src}
|
|
|
|
|
onError={(error) => {
|
2024-08-17 00:03:27 +02:00
|
|
|
logger.error('Error fetching audio:', error);
|
2024-08-15 11:34:25 -04:00
|
|
|
}}
|
|
|
|
|
id={`audio-${messageId}`}
|
|
|
|
|
autoPlay
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function ExternalTTS({ isLast, index, messageId, content, className }: TMessageAudio) {
|
|
|
|
|
const localize = useLocalize();
|
|
|
|
|
const playbackRate = useRecoilValue(store.playbackRate);
|
|
|
|
|
|
|
|
|
|
const { toggleSpeech, isSpeaking, isLoading, audioRef } = useTTSExternal({
|
|
|
|
|
isLast,
|
|
|
|
|
index,
|
|
|
|
|
messageId,
|
|
|
|
|
content,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const renderIcon = (size: string) => {
|
|
|
|
|
if (isLoading === true) {
|
|
|
|
|
return <Spinner size={size} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isSpeaking === true) {
|
|
|
|
|
return <VolumeMuteIcon size={size} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return <VolumeIcon size={size} />;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const messageAudio = document.getElementById(`audio-${messageId}`) as HTMLAudioElement | null;
|
|
|
|
|
if (!messageAudio) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (playbackRate != null && playbackRate > 0 && messageAudio.playbackRate !== playbackRate) {
|
|
|
|
|
messageAudio.playbackRate = playbackRate;
|
|
|
|
|
}
|
|
|
|
|
}, [audioRef, isSpeaking, playbackRate, messageId]);
|
|
|
|
|
|
|
|
|
|
logger.log(
|
|
|
|
|
'MessageAudio: audioRef.current?.src, audioRef.current',
|
|
|
|
|
audioRef.current?.src,
|
|
|
|
|
audioRef.current,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
<button
|
|
|
|
|
className={className}
|
|
|
|
|
onClickCapture={() => {
|
|
|
|
|
if (audioRef.current) {
|
|
|
|
|
audioRef.current.muted = false;
|
|
|
|
|
}
|
|
|
|
|
toggleSpeech();
|
|
|
|
|
}}
|
|
|
|
|
type="button"
|
|
|
|
|
title={isSpeaking === true ? localize('com_ui_stop') : localize('com_ui_read_aloud')}
|
|
|
|
|
>
|
|
|
|
|
{renderIcon('19')}
|
|
|
|
|
</button>
|
|
|
|
|
<audio
|
|
|
|
|
ref={audioRef}
|
|
|
|
|
controls
|
|
|
|
|
preload="none"
|
|
|
|
|
controlsList="nodownload nofullscreen noremoteplayback"
|
|
|
|
|
style={{
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
overflow: 'hidden',
|
|
|
|
|
display: 'none',
|
|
|
|
|
height: '0px',
|
|
|
|
|
width: '0px',
|
|
|
|
|
}}
|
|
|
|
|
src={audioRef.current?.src}
|
|
|
|
|
onError={(error) => {
|
2024-08-17 00:03:27 +02:00
|
|
|
logger.error('Error fetching audio:', error);
|
2024-08-15 11:34:25 -04:00
|
|
|
}}
|
|
|
|
|
id={`audio-${messageId}`}
|
|
|
|
|
autoPlay
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|