Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: temp voice recognition fix #17

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading