feat: ElevenLabs TTS George voice + fix HA toggle optimistic update

This commit is contained in:
2026-05-31 05:58:22 +00:00
parent b080ecb4bd
commit a96f8a3f85
4 changed files with 103 additions and 14 deletions
+32 -12
View File
@@ -1781,22 +1781,42 @@ function loadVoices() {
synth.onvoiceschanged = set;
}
function speak(text) {
let _ttsAudio = null;
async function speak(text) {
if (!text) return;
if (_ttsAudio) { _ttsAudio.pause(); _ttsAudio = null; }
synth?.cancel();
const reactor = document.getElementById('arcReactor');
reactor?.classList.add('speaking');
try {
const res = await fetch('/api/tts', {
method: 'POST',
headers: {'Content-Type':'application/json','X-Session-Token': sessionToken},
body: JSON.stringify({text: text.substring(0, 400)}),
});
if (!res.ok) throw new Error('tts');
const blob = await res.blob();
const url = URL.createObjectURL(blob);
_ttsAudio = new Audio(url);
_ttsAudio.onended = () => { URL.revokeObjectURL(url); _ttsAudio = null; reactor?.classList.remove('speaking'); };
_ttsAudio.onerror = () => { reactor?.classList.remove('speaking'); _ttsAudio = null; };
await _ttsAudio.play();
} catch(e) {
reactor?.classList.remove('speaking');
_speakFallback(text);
}
}
function _speakFallback(text) {
if (!synth || !text) return;
synth.cancel();
const utter = new SpeechSynthesisUtterance(text);
if (selectedVoice) utter.voice = selectedVoice;
utter.rate = 0.92;
utter.pitch = 0.85;
utter.volume = 1;
utter.onstart = () => {
document.getElementById('arcReactor').classList.add('speaking');
};
utter.onend = () => {
document.getElementById('arcReactor').classList.remove('speaking');
};
utter.rate = 0.92; utter.pitch = 0.85; utter.volume = 1;
const reactor = document.getElementById('arcReactor');
utter.onstart = () => reactor?.classList.add('speaking');
utter.onend = () => reactor?.classList.remove('speaking');
synth.speak(utter);
}