From 86ec6b9e7c6550ba50a3a68376ac1529f3725958 Mon Sep 17 00:00:00 2001 From: albin-karlsson <55614148+albin-karlsson@users.noreply.github.com> Date: Sat, 20 Apr 2024 11:12:42 +0200 Subject: [PATCH 1/3] Add skip, pause and resume functionality --- client/src/components/Output.jsx | 69 ++++++++++++++------------------ 1 file changed, 30 insertions(+), 39 deletions(-) diff --git a/client/src/components/Output.jsx b/client/src/components/Output.jsx index bc6e6ef..9ea2801 100644 --- a/client/src/components/Output.jsx +++ b/client/src/components/Output.jsx @@ -1,5 +1,6 @@ import React, { useState, useEffect } from "react"; import TextOutput from "./TextOutput"; +import AudioOutput from "./AudioOutput"; import ConversationControls from "./ConversationControls"; function Output({ conversation }) { @@ -7,58 +8,48 @@ function Output({ conversation }) { const [currentSnippetIndex, setCurrentSnippetIndex] = useState(0); const [currentMessageTextSnippet, setCurrentMessageTextSnippet] = useState(""); - const [initialize, setInitialize] = useState(true); // Flag to control initialization const [isPaused, setIsPaused] = useState(false); - // Function to calculate the display time based on text length const calculateDisplayTime = (text) => Math.max(3, text.length * 0.05); function handlePauseResume() { - if (!isPaused) { - console.log("Pausing"); - } else { - console.log("Resuming"); - } - - setIsPaused(!isPaused); + setIsPaused(!isPaused); // Toggle pause state } function handleSkipForward() { - // TODO: See if last message before skipping forward - - console.log("Skipping forward"); + if (conversation.length > currentMessageIndex) { + const snippets = + conversation[currentMessageIndex].text.split(/(?<=\.\s)/); + if (currentSnippetIndex < snippets.length - 1) { + // Move to the next snippet within the same message + setCurrentSnippetIndex(currentSnippetIndex + 1); + } else if (currentMessageIndex < conversation.length - 1) { + // Move to the first snippet of the next message + setCurrentMessageIndex(currentMessageIndex + 1); + setCurrentSnippetIndex(0); + } + } } useEffect(() => { - if (initialize && conversation.length > 0) { - setCurrentMessageIndex(0); - setCurrentSnippetIndex(0); - setInitialize(false); // Reset initialization flag after first setup - } - }, [conversation, initialize]); + if (conversation.length > 0 && !isPaused) { + const snippets = + conversation[currentMessageIndex].text.split(/(?<=\.\s)/); + if (snippets.length > currentSnippetIndex) { + setCurrentMessageTextSnippet(snippets[currentSnippetIndex]); + const timeout = setTimeout(() => { + if (currentSnippetIndex < snippets.length - 1) { + setCurrentSnippetIndex(currentSnippetIndex + 1); + } else if (currentMessageIndex < conversation.length - 1) { + setCurrentMessageIndex(currentMessageIndex + 1); + setCurrentSnippetIndex(0); + } + }, calculateDisplayTime(snippets[currentSnippetIndex]) * 1000); - useEffect(() => { - const processSnippets = () => { - if (conversation.length > currentMessageIndex) { - const snippets = - conversation[currentMessageIndex].text.split(/(?<=\.\s)/); - if (snippets.length > currentSnippetIndex) { - setCurrentMessageTextSnippet(snippets[currentSnippetIndex]); - const timeout = setTimeout(() => { - if (currentSnippetIndex < snippets.length - 1) { - setCurrentSnippetIndex(currentSnippetIndex + 1); - } else if (currentMessageIndex < conversation.length - 1) { - setCurrentMessageIndex(currentMessageIndex + 1); - setCurrentSnippetIndex(0); - } - }, calculateDisplayTime(snippets[currentSnippetIndex]) * 1000); - return () => clearTimeout(timeout); - } + return () => clearTimeout(timeout); // Cleanup timeout on unmount or dependency change } - }; - - processSnippets(); - }, [currentMessageIndex, currentSnippetIndex, conversation]); + } + }, [currentMessageIndex, currentSnippetIndex, conversation, isPaused]); // Include isPaused in dependencies return (
From 63ef0d7e0ccefe26b2c2e085a35ae5fb4d2e5062 Mon Sep 17 00:00:00 2001 From: albin-karlsson <55614148+albin-karlsson@users.noreply.github.com> Date: Sat, 20 Apr 2024 12:07:38 +0200 Subject: [PATCH 2/3] Add audio --- client/src/components/AudioOutput.jsx | 37 ++++++++++++++++++-- client/src/components/Contact.jsx | 15 ++++++-- client/src/components/Council.jsx | 22 ++++++++---- client/src/components/Output.jsx | 49 +++++++++++++++------------ 4 files changed, 88 insertions(+), 35 deletions(-) diff --git a/client/src/components/AudioOutput.jsx b/client/src/components/AudioOutput.jsx index e32964f..50141a9 100644 --- a/client/src/components/AudioOutput.jsx +++ b/client/src/components/AudioOutput.jsx @@ -1,7 +1,38 @@ -import React from "react"; +// In the AudioOutput component +import React, { useEffect, useRef } from "react"; -function AudioOutput() { - return <>; +function AudioOutput({ currentAudioBuffer }) { + const audioRef = useRef(null); + + useEffect(() => { + if (currentAudioBuffer) { + console.log( + "Creating blob with ArrayBuffer of size:", + currentAudioBuffer.byteLength + ); + const blob = new Blob([currentAudioBuffer], { type: "audio/mp3" }); + const url = URL.createObjectURL(blob); + console.log("Blob URL:", url); // Check the generated URL + audioRef.current.src = url; + audioRef.current + .play() + .catch((err) => console.error("Error playing audio:", err)); + + return () => { + console.log("Revoking URL:", url); + URL.revokeObjectURL(url); + }; + } + }, [currentAudioBuffer]); + + return ( +