Skip to content

Commit

Permalink
fix: temp voice recognition fix
Browse files Browse the repository at this point in the history
  • Loading branch information
adetyaz committed Nov 19, 2024
1 parent 1ac947b commit d1f20d0
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions src/components/voice-assistant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const VoiceAssistant = ({
userType,
}: any) => {
const [isListening, setIsListening] = useState(false)
const [transcript, setTranscript] = useState('')
// const [transcript, setTranscript] = useState('')
// const [response, setResponse] = useState('')

// console.log(userType)
Expand Down Expand Up @@ -95,30 +95,30 @@ export const VoiceAssistant = ({
},
])

useEffect(() => {
const synth = window.speechSynthesis

// Create a speech synthesis utterance
const utterance = new SpeechSynthesisUtterance(
`Welcome, Feel free to explore ${brandName} and discover hidden treasures together with your AI companion.`
)

// Speak the message after a delay of 5 seconds
const timeoutId = setTimeout(() => {
if (!synth.speaking) {
synth.speak(utterance)
}
}, 5000)

// Cleanup function to cancel speech synthesis and timeout if necessary
return () => {
clearTimeout(timeoutId)
if (synth.speaking) {
synth.cancel()
console.log('Speech synthesis canceled')
}
}
}, [])
// useEffect(() => {
// const synth = window.speechSynthesis

// // Create a speech synthesis utterance
// const utterance = new SpeechSynthesisUtterance(
// `Welcome, Feel free to explore ${brandName} and discover hidden treasures together with your AI companion.`
// )

// // Speak the message after a delay of 5 seconds
// const timeoutId = setTimeout(() => {
// if (!synth.speaking) {
// synth.speak(utterance)
// }
// }, 5000)

// // Cleanup function to cancel speech synthesis and timeout if necessary
// return () => {
// clearTimeout(timeoutId)
// if (synth.speaking) {
// synth.cancel()
// console.log('Speech synthesis canceled')
// }
// }
// }, [])

useEffect(() => {
// Feature detection for webkitSpeechRecognition
Expand All @@ -129,13 +129,14 @@ export const VoiceAssistant = ({

if (SpeechRecognition) {
recognition = new SpeechRecognition()

recognition.continuous = false
recognition.interimResults = false
recognition.lang = 'en-US'

recognition.onresult = (event: any) => {
const speechToText = event.results[0][0].transcript
setTranscript(speechToText)
// setTranscript(speechToText)
addMessage({ role: 'user', content: speechToText })
getOpenAIResponse(speechToText)
}
Expand Down Expand Up @@ -236,11 +237,11 @@ export const VoiceAssistant = ({

return (
<div className='flex flex-col justify-center items-center text-center'>
{transcript && (
{/* {transcript && (
<div className='mb-4 bg-black text-white p-4 rounded-md w-3/4'>
<p>User: &nbsp;{transcript}</p>
</div>
)}
)} */}
{/* {response && (
<div className='mb-4 bg-black text-white p-4 rounded-md w-3/4'>
<p>Assistant: &nbsp;{response}</p>
Expand Down

0 comments on commit d1f20d0

Please sign in to comment.