forked from microsoft/azurechat
-
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.
Merge pull request microsoft#199 from microsoft/speech
add Speech and code refactor
- Loading branch information
Showing
29 changed files
with
867 additions
and
302 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
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
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,21 @@ | ||
import { FC } from "react"; | ||
import { RecordSpeech } from "./record-speech"; | ||
import { useSpeechContext } from "./speech-context"; | ||
import { StopSpeech } from "./stop-speech"; | ||
|
||
interface MicrophoneProps { | ||
disabled: boolean; | ||
} | ||
|
||
export const Microphone: FC<MicrophoneProps> = (props) => { | ||
const { isPlaying } = useSpeechContext(); | ||
return ( | ||
<> | ||
{isPlaying ? ( | ||
<StopSpeech disabled={props.disabled} /> | ||
) : ( | ||
<RecordSpeech disabled={props.disabled} /> | ||
)} | ||
</> | ||
); | ||
}; |
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,39 @@ | ||
import { Button } from "@/components/ui/button"; | ||
import { Mic } from "lucide-react"; | ||
import { FC, useState } from "react"; | ||
import { useSpeechContext } from "./speech-context"; | ||
|
||
interface Prop { | ||
disabled: boolean; | ||
} | ||
|
||
export const RecordSpeech: FC<Prop> = (props) => { | ||
const [isPressed, setIsPressed] = useState(false); | ||
|
||
const { startRecognition, stopRecognition } = useSpeechContext(); | ||
|
||
const handleMouseDown = async () => { | ||
await startRecognition(); | ||
setIsPressed(true); | ||
}; | ||
|
||
const handleMouseUp = () => { | ||
stopRecognition(); | ||
setIsPressed(false); | ||
}; | ||
|
||
return ( | ||
<Button | ||
type="button" | ||
size="icon" | ||
variant={"ghost"} | ||
disabled={props.disabled} | ||
onMouseDown={handleMouseDown} | ||
onMouseUp={handleMouseUp} | ||
onMouseLeave={handleMouseUp} | ||
className={isPressed ? "bg-red-400 hover:bg-red-400" : ""} | ||
> | ||
<Mic size={18} /> | ||
</Button> | ||
); | ||
}; |
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,42 @@ | ||
import React, { createContext } from "react"; | ||
import { useSpeechRecognizer } from "./use-speech-recognizer"; | ||
import { useSpeechSynthesizer } from "./use-speech-synthesizer"; | ||
|
||
interface SpeechContextProps { | ||
textToSpeech: (textToSpeak: string) => Promise<void>; | ||
stopPlaying: () => void; | ||
isPlaying: boolean; | ||
startRecognition: () => void; | ||
stopRecognition: () => void; | ||
speech: string; | ||
setSpeechText: (text: string) => void; | ||
resetMicrophoneUsed: () => void; | ||
isMicrophoneUsed: boolean; | ||
} | ||
|
||
const SpeechContext = createContext<SpeechContextProps | null>(null); | ||
|
||
export const SpeechProvider = ({ children }: { children: React.ReactNode }) => { | ||
const speechSynthesizer = useSpeechSynthesizer(); | ||
const speechRecognizer = useSpeechRecognizer(); | ||
|
||
return ( | ||
<SpeechContext.Provider | ||
value={{ | ||
...speechSynthesizer, | ||
...speechRecognizer, | ||
}} | ||
> | ||
{children} | ||
</SpeechContext.Provider> | ||
); | ||
}; | ||
|
||
export const useSpeechContext = () => { | ||
const context = React.useContext(SpeechContext); | ||
if (!context) { | ||
throw new Error("SpeechContext is null"); | ||
} | ||
|
||
return context; | ||
}; |
Oops, something went wrong.