this is tsx for astro shadcnui: // src/components/VoiceAssistant.tsx import React, {…
- User
this is tsx for astro shadcnui: // src/components/VoiceAssistant.tsx import React, { useState, useRef, useEffect } from "react"; import { CardFooter, CardDescription, CardContent, CardHeader, Card, } from "./ui/card"; import { Button } from "./ui/button"; import { Input } from "./ui/input"; import { PlayIcon, CircleStop, MessageCircle, XIcon } from "lucide-react"; import * as THREE from "three";
const api = "http://localhost:5000"; // Update this if deploying const bgcolor = new THREE.Color().setHex(0x11161f); const visualcolor = new THREE.Color().setHex(0x1d92c4);
const VoiceAssistant: React.FC = () => { const [chatVisible, setChatVisible] = useState(false); const [isListening, setIsListening] = useState(false); const [isThinking, setIsThinking] = useState(false); const [isTalking, setIsTalking] = useState(false); const [messages, setMessages] = useState<{ user: string; bot: string }[]>([]); const [input, setInput] = useState(""); const audioRef = useRef(null); const [analyser, setAnalyser] = useState<THREE.AudioAnalyser | null>(null); const visualizerRef = useRef(null);
const sceneRef = useRef<THREE.Scene | null>(null); const cameraRef = useRef<THREE.PerspectiveCamera | null>(null); const rendererRef = useRef<THREE.WebGLRenderer | null>(null); const sphereRef = useRef<THREE.Mesh | null>(null); const particlesRef = useRef<THREE.Points | null>(null);
const isThinkingRef = useRef(isThinking); const isTalkingRef = useRef(isTalking);
const recognitionRef = useRef<SpeechRecognition | null>(null);
const toggleChat = () => setChatVisible(!chatVisible);
// Handle sending a message to the chat API const handleSendMessage = async () => { if (!input.trim()) return;
setIsThinking(true); // Start thinking animation try { const response = await fetch(`${api}/chat`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ message: input }), }); const data = await response.json(); const botResponse = data.response; setMessages((prev) => [...prev, { user: input, bot: botResponse }]); setInput(""); await playBotResponse(botResponse); } catch (error) { console.error("Error fetching chat response:", error); } finally { setIsThinking(false); // Stop thinking animation }};
// Play bot response using text-to-speech API const playBotResponse = async (text: string) => { try { const response = await fetch(
${api}/text-to-speech, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ text }), });const audioBlob = await response.blob(); const audioUrl = URL.createObjectURL(audioBlob); if (audioRef.current) { audioRef.current.src = audioUrl; audioRef.current.onplay = () => { setIsTalking(true); }; audioRef.current.onended = () => { setIsTalking(false); }; audioRef.current.play(); } } catch (error) { console.error("Error fetching TTS response:", error); }};
// Setup Three.js Visualizer const initVisualizer = () => { if (!visualizerRef.current) return;
// Clear any existing content while (visualizerRef.current.firstChild) { visualizerRef.current.removeChild(visualizerRef.current.firstChild); } const scene = new THREE.Scene(); scene.background = bgcolor; const width = visualizerRef.current.clientWidth; const height = visualizerRef.current.clientHeight; const camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000); camera.position.z = 5; const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(width, height); visualizerRef.current.appendChild(renderer.domElement); // Add dynamic lighting const ambientLight = new THREE.AmbientLight(0xffffff, 0.5); scene.add(ambientLight); const pointLight = new THREE.PointLight(0x1d92c4, 1); pointLight.position.set(5, 5, 5); scene.add(pointLight); // Sphere Geometry const geometry = new THREE.SphereGeometry(1.5, 32, 32); const material = new THREE.MeshStandardMaterial({ color: visualcolor }); const sphere = new THREE.Mesh(geometry, material); scene.add(sphere); sphereRef.current = sphere; // Particle System for "Thinking" state const particlesGeometry = new THREE.BufferGeometry(); const particlesCount = 500; const positions = new Float32Array(particlesCount * 3); for (let i = 0; i < particlesCount; i++) { positions[i * 3] = (Math.random() - 0.5) * 10; positions[i * 3 + 1] = (Math.random() - 0.5) * 10; positions[i * 3 + 2] = (Math.random() - 0.5) * 10; } particlesGeometry.setAttribute( "position", new THREE.BufferAttribute(positions, 3) ); const particlesMaterial = new THREE.PointsMaterial({ color: 0x1d92c4, size: 0.05, }); const particles = new THREE.Points(particlesGeometry, particlesMaterial); scene.add(particles); particlesRef.current = particles; // Store references sceneRef.current = scene; cameraRef.current = camera; rendererRef.current = renderer; const animate = () => { requestAnimationFrame(animate); // Rotate the sphere if (sphereRef.current) { sphereRef.current.rotation.y += 0.003; sphereRef.current.rotation.x += 0.005; } // Update particles if thinking if (particlesRef.current) { if (isThinkingRef.current) { particlesRef.current.rotation.y += 0.01; (particlesRef.current.material as THREE.PointsMaterial).color.set( 0xff0000 ); // Change color when thinking } else { particlesRef.current.rotation.y += 0.002; (particlesRef.current.material as THREE.PointsMaterial).color.set( 0x1d92c4 ); // Default color } } // Update sphere scale based on audio data when talking if (analyser && isTalkingRef.current && sphereRef.current) { const data = analyser.getFrequencyData(); const scale = 1 + data[0] / 256; sphereRef.current.scale.set(scale, scale, scale); } else if (sphereRef.current) { // Reset scale when not talking sphereRef.current.scale.set(1, 1, 1); } renderer.render(scene, camera); }; animate(); // Handle window resize const handleResize = () => { if (!cameraRef.current || !rendererRef.current || !visualizerRef.current) return; const width = visualizerRef.current.clientWidth; const height = visualizerRef.current.clientHeight; cameraRef.current.aspect = width / height; cameraRef.current.updateProjectionMatrix(); rendererRef.current.setSize(width, height); }; window.addEventListener("resize", handleResize); // Clean up on unmount return () => { window.removeEventListener("resize", handleResize); renderer.dispose(); };};
useEffect(() => { initVisualizer(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []);
// Update refs when isThinking or isTalking changes useEffect(() => { isThinkingRef.current = isThinking; }, [isThinking]);
useEffect(() => { isTalkingRef.current = isTalking; }, [isTalking]);
// Set up the audio analyser useEffect(() => { if (audioRef.current && !analyser) { const listener = new THREE.AudioListener(); const audio = new THREE.Audio(listener); audio.setMediaElementSource(audioRef.current); const newAnalyser = new THREE.AudioAnalyser(audio, 32); setAnalyser(newAnalyser); } }, [analyser]);
// Voice Recognition useEffect(() => { if ( !("webkitSpeechRecognition" in window) && !("SpeechRecognition" in window) ) { console.error("Speech recognition not supported in this browser."); return; }
if (!recognitionRef.current) { const SpeechRecognitionClass = (window as any).SpeechRecognition || (window as any).webkitSpeechRecognition; recognitionRef.current = new SpeechRecognitionClass(); recognitionRef.current.continuous = true; recognitionRef.current.interimResults = false; recognitionRef.current.lang = "ar-SA"; recognitionRef.current.onresult = (event: SpeechRecognitionEvent) => { const transcript = Array.from(event.results) .map((result) => result[0].transcript) .join(""); console.log("Recognized speech:", transcript); setInput(transcript); handleSendMessage(); }; recognitionRef.current.onerror = (event: SpeechRecognitionErrorEvent) => { console.error("Speech recognition error:", event.error); }; recognitionRef.current.onend = () => { if (isListening) { recognitionRef.current?.start(); } }; } if (isListening) { recognitionRef.current.start(); } else { recognitionRef.current.stop(); } // Clean up on unmount return () => { recognitionRef.current?.stop(); }; // eslint-disable-next-line react-hooks/exhaustive-deps}, [isListening]);
return ( {/* Audio Visualizer */}
{/* Chat Interface Fixed at the Bottom */} {chatVisible && ( <Card className="fixed bottom-0 w-full max-w-lg mx-auto bg-background shadow-lg border-t border-t-border"> <CardHeader className="text-xl font-bold p-4"> AI Chat Assistant </CardHeader> <CardContent className="p-4"> <div className="h-40 overflow-y-auto p-2 bg-background rounded-md"> {messages.map((msg, index) => ( <div key={index} className="mb-4"> <p className="font-semibold text-blue-500">User:</p> <p>{msg.user}</p> <p className="font-semibold text-green-500 mt-2">ملهم:</p> <p>{msg.bot}</p> </div> ))} </div> <div className="flex mt-4"> <Input placeholder="Type a message..." value={input} onChange={(e) => setInput(e.target.value)} onKeyDown={(e) => e.key === "Enter" && handleSendMessage() } /> <Button onClick={handleSendMessage} className="ml-2 text-foreground" > Send </Button> </div> </CardContent> </Card> )} {/* Control Buttons at the Bottom */} {!chatVisible && ( <div className="fixed bottom-4 w-full flex justify-center space-x-4"> <Button onClick={() => setIsListening(!isListening)} variant={isListening ? "destructive" : "default"} className={isListening ? undefined : "text-foreground"} > {isListening ? ( <> <CircleStop className="mr-2" /> Stop </> ) : ( <> <PlayIcon className="mr-2" /> Start </> )} </Button> </div> )} {/* Chat Toggle Button */} <div className="fixed bottom-16 w-full flex justify-center"> <Button onClick={toggleChat} variant="secondary"> {chatVisible ? ( <> <XIcon className="mr-2" /> Hide Chat </> ) : ( <> <MessageCircle className="mr-2" /> Show Chat </> )} </Button> </div> {/* Audio Element for TTS */} <audio ref={audioRef} /> </div>); };
export default VoiceAssistant;
add animations and make it send automatcliy what user said
- Assistant
To enhance your
VoiceAssistantcomponent by adding animations and ensuring that user speech is sent automatically, we'll make the following updates:-
Add UI Animations: We'll use Framer Motion for smooth and declarative animations. This will enhance the user experience by providing smooth transitions when showing/hiding the chat and animating buttons.
-
Automatic Message Sending: Ensure that once the user finishes speaking, the message is automatically sent without requiring an additional button click.
-
Enhance Three.js Visualizer Animations: Further improve the existing animations in your Three.js visualizer for a more dynamic and engaging experience.
Step 1: Install Framer Motion
First, install Framer Motion to handle UI animations:
npm install framer-motionStep 2: Update the
VoiceAssistantComponentHere's the updated
VoiceAssistant.tsxwith the requested enhancements:// src/components/VoiceAssistant.tsx import React, { useState, useRef, useEffect } from "react"; import { CardFooter, CardDescription, CardContent, CardHeader, Card, } from "./ui/card"; import { Button } from "./ui/button"; import { Input } from "./ui/input"; import { PlayIcon, CircleStop, MessageCircle, XIcon } from "lucide-react"; import * as THREE from "three"; import { motion, AnimatePresence } from "framer-motion"; const api = "http://localhost:5000"; // Update this if deploying const bgcolor = new THREE.Color().setHex(0x11161f); const visualcolor = new THREE.Color().setHex(0x1d92c4); const VoiceAssistant: React.FC = () => { const [chatVisible, setChatVisible] = useState(false); const [isListening, setIsListening] = useState(false); const [isThinking, setIsThinking] = useState(false); const [isTalking, setIsTalking] = useState(false); const [messages, setMessages] = useState<{ user: string; bot: string }[]>([]); const [input, setInput] = useState(""); const audioRef = useRef<HTMLAudioElement>(null); const [analyser, setAnalyser] = useState<THREE.AudioAnalyser | null>(null); const visualizerRef = useRef<HTMLDivElement>(null); const sceneRef = useRef<THREE.Scene | null>(null); const cameraRef = useRef<THREE.PerspectiveCamera | null>(null); const rendererRef = useRef<THREE.WebGLRenderer | null>(null); const sphereRef = useRef<THREE.Mesh | null>(null); const particlesRef = useRef<THREE.Points | null>(null); const isThinkingRef = useRef(isThinking); const isTalkingRef = useRef(isTalking); const recognitionRef = useRef<SpeechRecognition | null>(null); const toggleChat = () => setChatVisible(!chatVisible); // Handle sending a message to the chat API const handleSendMessage = async (message: string) => { if (!message.trim()) return; setIsThinking(true); // Start thinking animation try { const response = await fetch(`${api}/chat`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ message }), }); const data = await response.json(); const botResponse = data.response; setMessages((prev) => [...prev, { user: message, bot: botResponse }]); setInput(""); await playBotResponse(botResponse); } catch (error) { console.error("Error fetching chat response:", error); } finally { setIsThinking(false); // Stop thinking animation } }; // Play bot response using text-to-speech API const playBotResponse = async (text: string) => { try { const response = await fetch(`${api}/text-to-speech`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ text }), }); const audioBlob = await response.blob(); const audioUrl = URL.createObjectURL(audioBlob); if (audioRef.current) { audioRef.current.src = audioUrl; audioRef.current.onplay = () => { setIsTalking(true); }; audioRef.current.onended = () => { setIsTalking(false); }; audioRef.current.play(); } } catch (error) { console.error("Error fetching TTS response:", error); } }; // Setup Three.js Visualizer const initVisualizer = () => { if (!visualizerRef.current) return; // Clear any existing content while (visualizerRef.current.firstChild) { visualizerRef.current.removeChild(visualizerRef.current.firstChild); } const scene = new THREE.Scene(); scene.background = bgcolor; const width = visualizerRef.current.clientWidth; const height = visualizerRef.current.clientHeight; const camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 1000); camera.position.z = 5; const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true }); renderer.setSize(width, height); visualizerRef.current.appendChild(renderer.domElement); // Add dynamic lighting const ambientLight = new THREE.AmbientLight(0xffffff, 0.5); scene.add(ambientLight); const pointLight = new THREE.PointLight(0x1d92c4, 1); pointLight.position.set(5, 5, 5); scene.add(pointLight); // Sphere Geometry const geometry = new THREE.SphereGeometry(1.5, 32, 32); const material = new THREE.MeshStandardMaterial({ color: visualcolor }); const sphere = new THREE.Mesh(geometry, material); scene.add(sphere); sphereRef.current = sphere; // Particle System for "Thinking" state const particlesGeometry = new THREE.BufferGeometry(); const particlesCount = 500; const positions = new Float32Array(particlesCount * 3); for (let i = 0; i < particlesCount; i++) { positions[i * 3] = (Math.random() - 0.5) * 10; positions[i * 3 + 1] = (Math.random() - 0.5) * 10; positions[i * 3 + 2] = (Math.random() - 0.5) * 10; } particlesGeometry.setAttribute( "position", new THREE.BufferAttribute(positions, 3) ); const particlesMaterial = new THREE.PointsMaterial({ color: 0x1d92c4, size: 0.05, }); const particles = new THREE.Points(particlesGeometry, particlesMaterial); scene.add(particles); particlesRef.current = particles; // Store references sceneRef.current = scene; cameraRef.current = camera; rendererRef.current = renderer; const animate = () => { requestAnimationFrame(animate); // Rotate the sphere if (sphereRef.current) { sphereRef.current.rotation.y += 0.003; sphereRef.current.rotation.x += 0.005; } // Update particles if thinking if (particlesRef.current) { if (isThinkingRef.current) { particlesRef.current.rotation.y += 0.01; (particlesRef.current.material as THREE.PointsMaterial).color.set( 0xff0000 ); // Change color when thinking // Add pulsating effect particlesRef.current.scale.setScalar( 1 + Math.sin(Date.now() * 0.005) * 0.1 ); } else { particlesRef.current.rotation.y += 0.002; (particlesRef.current.material as THREE.PointsMaterial).color.set( 0x1d92c4 ); // Default color particlesRef.current.scale.setScalar(1); } } // Update sphere scale based on audio data when talking if (analyser && isTalkingRef.current && sphereRef.current) { const data = analyser.getFrequencyData(); const scale = 1 + data[0] / 256; sphereRef.current.scale.set(scale, scale, scale); } else if (sphereRef.current) { // Reset scale when not talking sphereRef.current.scale.set(1, 1, 1); } renderer.render(scene, camera); }; animate(); // Handle window resize const handleResize = () => { if (!cameraRef.current || !rendererRef.current || !visualizerRef.current) return; const width = visualizerRef.current.clientWidth; const height = visualizerRef.current.clientHeight; cameraRef.current.aspect = width / height; cameraRef.current.updateProjectionMatrix(); rendererRef.current.setSize(width, height); }; window.addEventListener("resize", handleResize); // Clean up on unmount return () => { window.removeEventListener("resize", handleResize); renderer.dispose(); }; }; useEffect(() => { initVisualizer(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); // Update refs when isThinking or isTalking changes useEffect(() => { isThinkingRef.current = isThinking; }, [isThinking]); useEffect(() => { isTalkingRef.current = isTalking; }, [isTalking]); // Set up the audio analyser useEffect(() => { if (audioRef.current && !analyser) { const listener = new THREE.AudioListener(); const audio = new THREE.Audio(listener); audio.setMediaElementSource(audioRef.current); const newAnalyser = new THREE.AudioAnalyser(audio, 32); setAnalyser(newAnalyser); } }, [analyser]); // Voice Recognition useEffect(() => { if ( !("webkitSpeechRecognition" in window) && !("SpeechRecognition" in window) ) { console.error("Speech recognition not supported in this browser."); return; } if (!recognitionRef.current) { const SpeechRecognitionClass = (window as any).SpeechRecognition || (window as any).webkitSpeechRecognition; recognitionRef.current = new SpeechRecognitionClass(); recognitionRef.current.continuous = false; // Set to false to stop automatically recognitionRef.current.interimResults = false; recognitionRef.current.lang = "ar-SA"; // Change to your desired language recognitionRef.current.onresult = (event: SpeechRecognitionEvent) => { const transcript = Array.from(event.results) .map((result) => result[0].transcript) .join(""); console.log("Recognized speech:", transcript); // Automatically send the recognized speech handleSendMessage(transcript); }; recognitionRef.current.onerror = (event: SpeechRecognitionErrorEvent) => { console.error("Speech recognition error:", event.error); }; recognitionRef.current.onend = () => { if (isListening) { recognitionRef.current?.start(); } }; } if (isListening) { recognitionRef.current.start(); } else { recognitionRef.current.stop(); } // Clean up on unmount return () => { recognitionRef.current?.stop(); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [isListening]); return ( <div className="flex flex-col items-center h-screen overflow-hidden"> {/* Audio Visualizer */} <div ref={visualizerRef} className="w-full flex-grow bg-background overflow-hidden" ></div> {/* Chat Interface with Animations */} <AnimatePresence> {chatVisible && ( <motion.div initial={{ y: "100%", opacity: 0 }} animate={{ y: 0, opacity: 1 }} exit={{ y: "100%", opacity: 0 }} transition={{ duration: 0.5 }} className="fixed bottom-0 w-full max-w-lg mx-auto" > <Card className="bg-background shadow-lg border-t border-t-border"> <CardHeader className="text-xl font-bold p-4"> AI Chat Assistant </CardHeader> <CardContent className="p-4"> <div className="h-40 overflow-y-auto p-2 bg-background rounded-md"> {messages.map((msg, index) => ( <div key={index} className="mb-4 animate-fadeIn" // Using Tailwind CSS for a simple fade-in animation > <p className="font-semibold text-blue-500">User:</p> <p>{msg.user}</p> <p className="font-semibold text-green-500 mt-2">ملهم:</p> <p>{msg.bot}</p> </div> ))} </div> <div className="flex mt-4"> <Input placeholder="Type a message..." value={input} onChange={(e) => setInput(e.target.value)} onKeyDown={(e) => e.key === "Enter" && handleSendMessage(input) } disabled={isListening} // Disable manual input while listening /> <Button onClick={() => handleSendMessage(input)} className="ml-2 text-foreground" disabled={isListening} // Disable manual send while listening > Send </Button> </div> </CardContent> </Card> </motion.div> )} </AnimatePresence> {/* Control Buttons with Animations */} <div className="fixed bottom-4 w-full flex justify-center space-x-4"> <motion.div whileHover={{ scale: 1.1 }} whileTap={{ scale: 0.95 }} transition={{ type: "spring", stiffness: 300 }} > <Button onClick={() => setIsListening(!isListening)} variant={isListening ? "destructive" : "default"} className={isListening ? undefined : "text-foreground"} > {isListening ? ( <> <CircleStop className="mr-2" /> Stop </> ) : ( <> <PlayIcon className="mr-2" /> Start </> )} </Button> </motion.div> <motion.div whileHover={{ scale: 1.1 }} whileTap={{ scale: 0.95 }} transition={{ type: "spring", stiffness: 300 }} > <Button onClick={toggleChat} variant="secondary" className="flex items-center" > {chatVisible ? ( <> <XIcon className="mr-2" /> Hide Chat </> ) : ( <> <MessageCircle className="mr-2" /> Show Chat </> )} </Button> </motion.div> </div> {/* Audio Element for TTS */} <audio ref={audioRef} /> </div> ); }; export default VoiceAssistant;Step 3: Add Tailwind CSS Animations (Optional)
For additional animations like fading in messages, you can extend your Tailwind CSS configuration. If you haven't already set up custom animations, you can add a simple fade-in animation.
-
Update
tailwind.config.js:// tailwind.config.js module.exports = { // ... theme: { extend: { keyframes: { fadeIn: { '0%': { opacity: 0 }, '100%': { opacity: 1 }, }, }, animation: { fadeIn: 'fadeIn 0.5s ease-in-out', }, }, }, plugins: [], }; -
Apply the Animation to Message Components:
In the updated code above, notice the
animate-fadeInclass applied to each message. This utilizes the customfadeInanimation defined in Tailwind CSS.<div key={index} className="mb-4 animate-fadeIn" // Using Tailwind CSS for a simple fade-in animation > {/* Message Content */} </div>
Step 4: Explanation of Changes
-
Framer Motion Integration:
- Import Statements: Imported
motionandAnimatePresencefromframer-motion. - Chat Interface Animation: Wrapped the chat interface with
AnimatePresenceandmotion.divto animate its entrance and exit.<AnimatePresence> {chatVisible && ( <motion.div initial={{ y: "100%", opacity: 0 }} animate={{ y: 0, opacity: 1 }} exit={{ y: "100%", opacity: 0 }} transition={{ duration: 0.5 }} className="fixed bottom-0 w-full max-w-lg mx-auto" > {/* Chat Content */} </motion.div> )} </AnimatePresence> - Control Buttons Animation: Added hover and tap animations to the buttons for a more interactive feel.
<motion.div whileHover={{ scale: 1.1 }} whileTap={{ scale: 0.95 }} transition={{ type: "spring", stiffness: 300 }} > <Button>/* Button Content */</Button> </motion.div>
- Import Statements: Imported
-
Automatic Message Sending:
- Speech Recognition Configuration: Changed
recognitionRef.current.continuoustofalseto stop automatically after capturing speech. This ensures that theonresultevent fires once the user finishes speaking. - Handle Send in
onresult: Directly callshandleSendMessagewith the recognized transcript.recognitionRef.current.onresult = (event: SpeechRecognitionEvent) => { const transcript = Array.from(event.results) .map((result) => result[0].transcript) .join(""); console.log("Recognized speech:", transcript); // Automatically send the recognized speech handleSendMessage(transcript); };
- Speech Recognition Configuration: Changed
-
UI Enhancements:
- Disable Input While Listening: When the assistant is listening, disable manual input and the send button to prevent conflicting inputs.
<Input placeholder="Type a message..." value={input} onChange={(e) => setInput(e.target.value)} onKeyDown={(e) => e.key === "Enter" && handleSendMessage(input) } disabled={isListening} // Disable manual input while listening /> <Button onClick={() => handleSendMessage(input)} className="ml-2 text-foreground" disabled={isListening} // Disable manual send while listening > Send </Button>
- Disable Input While Listening: When the assistant is listening, disable manual input and the send button to prevent conflicting inputs.
-
Enhanced Three.js Animations:
- Pulsating Particles: Added a pulsating effect to the particles when in the "thinking" state.
particlesRef.current.scale.setScalar( 1 + Math.sin(Date.now() * 0.005) * 0.1 );
- Pulsating Particles: Added a pulsating effect to the particles when in the "thinking" state.
-
Responsive Design and Cleanup:
- Ensured that the Three.js renderer resizes appropriately with the window and cleans up resources on component unmount.
Step 5: Testing the Component
After implementing the above changes, ensure that you test the component thoroughly:
-
Start Listening: Click the "Start" button to begin speech recognition. Speak clearly, and once you stop, the message should automatically send and appear in the chat interface.
-
Animations: Observe the smooth transitions when showing/hiding the chat and the interactive button animations.
-
Three.js Visualizer: Ensure that the visualizer responds correctly when listening, thinking, and talking states are toggled.
-
Error Handling: Test the application in different browsers to ensure that the Speech Recognition API is supported. Handle scenarios where speech recognition might fail gracefully.
Additional Recommendations
-
Language Support: Ensure that the
langattribute in the speech recognition is set to the desired language. You can make this dynamic based on user preferences. -
Accessibility: Enhance accessibility by adding appropriate ARIA labels and ensuring that animations are not hindered by reduced motion settings.
-
Performance Optimization: Monitor the performance of the Three.js visualizer, especially on low-end devices. Optimize geometry and materials as needed.
-
Security Considerations: If deploying the application, ensure that API endpoints are secured, and consider using HTTPS instead of HTTP.
Conclusion
By integrating Framer Motion for UI animations and ensuring that speech inputs are sent automatically, your
VoiceAssistantcomponent will offer a more engaging and seamless user experience. The enhanced Three.js visualizer further adds to the interactive feel of the application. Make sure to test across different devices and browsers to ensure compatibility and performance. -