From 302e93de9dba5f4f47b87c5d2c7fb9638bd6b021 Mon Sep 17 00:00:00 2001 From: Tyler Marques Date: Thu, 15 May 2025 11:34:02 -0400 Subject: [PATCH] Updating Elevenlabs to speak fully when not in debug --- ai_animation/src/main.ts | 2 +- ai_animation/src/phase.ts | 30 +++++++++++++++++------------- ai_animation/src/speech.ts | 13 ++++++++++--- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/ai_animation/src/main.ts b/ai_animation/src/main.ts index 67b2767..1473002 100644 --- a/ai_animation/src/main.ts +++ b/ai_animation/src/main.ts @@ -152,7 +152,7 @@ function animate() { gameState.unitAnimations.forEach((anim) => anim.update()) // If all animations are complete and we're in playback mode - if (gameState.unitAnimations.length === 0 && gameState.isPlaying && !gameState.messagesPlaying) { + if (gameState.unitAnimations.length === 0 && gameState.isPlaying && !gameState.messagesPlaying && !gameState.isSpeaking) { // Schedule next phase after a pause delay console.log(`Scheduling next phase in ${config.playbackSpeed}ms`); gameState.playbackTimer = setTimeout(() => advanceToNextPhase(), config.playbackSpeed); diff --git a/ai_animation/src/phase.ts b/ai_animation/src/phase.ts index 3cba8ce..91e46a3 100644 --- a/ai_animation/src/phase.ts +++ b/ai_animation/src/phase.ts @@ -138,19 +138,23 @@ export function advanceToNextPhase() { console.log("Added summary to news banner, preparing to call speakSummary"); // Speak the summary and advance after - speakSummary(currentPhase.summary) - .then(() => { - console.log("Speech completed successfully"); - if (gameState.isPlaying) { - moveToNextPhase(); - } - }) - .catch((error) => { - console.error("Speech failed with error:", error); - if (gameState.isPlaying) { - moveToNextPhase(); - } - }); + if (!gameState.isSpeaking) { + speakSummary(currentPhase.summary) + .then(() => { + console.log("Speech completed successfully"); + if (gameState.isPlaying) { + moveToNextPhase(); + } + }) + .catch((error) => { + console.error("Speech failed with error:", error); + if (gameState.isPlaying) { + moveToNextPhase(); + } + }); + } else { + console.error("Attempted to start speaking when already speaking...") + } } else { console.log("No summary available, skipping speech"); // No summary to speak, advance immediately diff --git a/ai_animation/src/speech.ts b/ai_animation/src/speech.ts index 9d67f18..a6abcc5 100644 --- a/ai_animation/src/speech.ts +++ b/ai_animation/src/speech.ts @@ -1,4 +1,5 @@ import { gameState } from "./gameState"; +import { config } from "./config"; // --- ElevenLabs Text-to-Speech configuration --- let ELEVENLABS_API_KEY = ''; @@ -95,8 +96,13 @@ export async function speakSummary(summaryText: string): Promise { try { // Truncate text to first 100 characters for ElevenLabs - const truncatedText = textToSpeak.substring(0, 100); - + // FIXME: Is this meant to be truncated? Presumably not + //const textForSpeaking = textToSpeak.substring(0, 100); + if (config.isDebugMode) { + const textForSpeaking = textToSpeak.substring(0, 100); + } else { + const textForSpeaking = textToSpeak + } // Hit ElevenLabs TTS endpoint with the truncated text const headers = { "xi-api-key": ELEVENLABS_API_KEY, @@ -108,7 +114,7 @@ export async function speakSummary(summaryText: string): Promise { method: "POST", headers: headers, body: JSON.stringify({ - text: truncatedText, + text: textForSpeaking, model_id: MODEL_ID, }) }); @@ -142,5 +148,6 @@ export async function speakSummary(summaryText: string): Promise { console.error("Failed to generate TTS from ElevenLabs"); // Make sure to clear the flag if there's any exception gameState.isSpeaking = false; + throw err; } }