forked from weaigc/bingo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support continous voice chatting
- Loading branch information
Showing
12 changed files
with
138 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
$duration-list: 474 433 407 458 400 427; | ||
|
||
.voice-button { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
cursor: pointer; | ||
> div { | ||
background: #52467b; | ||
bottom: 1px; | ||
height: 3px; | ||
width: 1px; | ||
margin: 0px 1px; | ||
border-radius: 5px; | ||
animation: sound 0ms -600ms linear infinite alternate; | ||
@for $i from 1 through length($duration-list) { | ||
$duration: nth($duration-list, $i); | ||
&:nth-child(n + #{$i}) { | ||
left: $i * 10px; | ||
animation-duration: #{$duration}ms; | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
@keyframes sound { | ||
0% { | ||
opacity: .35; | ||
height: 3px; | ||
} | ||
100% { | ||
opacity: 1; | ||
height: 20px; | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import './index.scss' | ||
|
||
export default function Voice(props: React.ComponentProps<'div'>) { | ||
return ( | ||
<div className="voice-button" {...props}> | ||
{Array.from({ length: 6 }).map(() => <div />)} | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import React, { useEffect } from 'react' | ||
import 'regenerator-runtime/runtime' | ||
import { useSetAtom } from 'jotai' | ||
import SpeechRecognition, { useSpeechRecognition } from 'react-speech-recognition' | ||
import { useBing } from '@/lib/hooks/use-bing' | ||
import Image from 'next/image' | ||
import VoiceIcon from '@/assets/images/voice.svg' | ||
import VoiceButton from './ui/voice' | ||
import { voiceListenAtom } from '@/state' | ||
|
||
const Voice = ({ setInput, sendMessage }: Pick<ReturnType<typeof useBing>, 'setInput' | 'sendMessage'>) => { | ||
const { | ||
transcript, | ||
listening, | ||
resetTranscript, | ||
browserSupportsSpeechRecognition | ||
} = useSpeechRecognition() | ||
|
||
if (!browserSupportsSpeechRecognition) { | ||
return null | ||
} | ||
|
||
const setListen = useSetAtom(voiceListenAtom) | ||
|
||
useEffect(() => { | ||
if (/[ 。](发送|撤回|退出)。?$/.test(transcript)) { | ||
const command = RegExp.$1 | ||
if (command === '退出') { | ||
SpeechRecognition.stopListening() | ||
} else if (command === '发送') { | ||
sendMessage(transcript.slice(0, -3)) | ||
} | ||
|
||
resetTranscript() | ||
return () => { | ||
SpeechRecognition.stopListening() | ||
} | ||
} | ||
setInput(transcript) | ||
}, [transcript]) | ||
|
||
useEffect(() => { | ||
setListen(listening) | ||
}, [listening]) | ||
|
||
return listening ? ( | ||
<VoiceButton onClick={SpeechRecognition.stopListening} /> | ||
) : ( | ||
<Image alt="start voice" src={VoiceIcon} width={24} className="-mt-0.5" onClick={() => SpeechRecognition.startListening({ continuous: true, language: 'zh-CN' })} /> | ||
) | ||
}; | ||
export default Voice; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters