🌩️ feat: cloud-based browser voices (#3297)

* initial voice support

* feat: local voices; feat: switch cloud-based voices

* feat: apply voice to hook
This commit is contained in:
Marco Beretta 2024-07-10 22:44:12 +02:00 committed by GitHub
parent 7d5b03dd98
commit b34a4ddac1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 177 additions and 37 deletions

View file

@ -1,12 +1,24 @@
import { useRecoilState } from 'recoil';
import { useState } from 'react';
import store from '~/store';
function useTextToSpeechBrowser() {
const [cloudBrowserVoices] = useRecoilState(store.cloudBrowserVoices);
const [isSpeaking, setIsSpeaking] = useState(false);
const [voiceName] = useRecoilState(store.voice);
const generateSpeechLocal = (text: string) => {
const synth = window.speechSynthesis;
const voices = synth.getVoices().filter((v) => cloudBrowserVoices || v.localService === true);
const voice = voices.find((v) => v.name === voiceName);
if (!voice) {
return;
}
synth.cancel();
const utterance = new SpeechSynthesisUtterance(text);
utterance.voice = voice;
utterance.onend = () => {
setIsSpeaking(false);
};