-
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 #33 from chimobi-justice/ft/text-to-speech
Text to speech to articles
- Loading branch information
Showing
42 changed files
with
523 additions
and
530 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,80 @@ | ||
import { Flex, Text, Tooltip } from '@chakra-ui/react' | ||
import { FunctionComponent, useEffect, useState } from 'react' | ||
import { IoMicCircleSharp, IoPlayCircle, IoPlayCircleOutline } from 'react-icons/io5' | ||
|
||
interface TextSpeechProps { | ||
title?: string; | ||
content?: string; | ||
} | ||
|
||
const TextSpeech: FunctionComponent<TextSpeechProps> = ({ title, content }) => { | ||
const [isSpeaking, setIsSpeaking] = useState(false); | ||
const [isPaused, setIsPaused] = useState(false); | ||
const [utterance, setUtterance] = useState<SpeechSynthesisUtterance | null>(null); | ||
|
||
const synth = window.speechSynthesis; | ||
|
||
useEffect(() => { | ||
const newUtterance = new SpeechSynthesisUtterance(`${title} ${content}`); | ||
newUtterance.lang = "en-US"; | ||
setUtterance(newUtterance); | ||
|
||
return () => { | ||
synth.cancel(); | ||
} | ||
}, [title, content]); | ||
|
||
|
||
const handlePlay = () => { | ||
if (utterance && !isSpeaking) { | ||
synth.speak(utterance); | ||
setIsSpeaking(true); | ||
setIsPaused(false); | ||
} else if (isPaused) { | ||
synth.resume(); | ||
setIsPaused(false); | ||
} | ||
} | ||
|
||
const handlePause = () => { | ||
if (synth.speaking && !isPaused) { | ||
synth.pause(); | ||
setIsPaused(true); | ||
} | ||
} | ||
|
||
const handleStop = () => { | ||
synth.cancel(); | ||
setIsSpeaking(false); | ||
setIsPaused(false); | ||
} | ||
|
||
return ( | ||
<Flex gap={3} alignItems={"center"}> | ||
{!isSpeaking || isPaused ? ( | ||
<Tooltip label={isPaused ? 'Resume' : 'Play'} placement="top"> | ||
<Text as="span"> | ||
<IoPlayCircleOutline size="21px" onClick={handlePlay} cursor="pointer" /> | ||
</Text> | ||
</Tooltip> | ||
): ( | ||
<Tooltip label={"Stop Listening"} placement="top"> | ||
<Text as="span"> | ||
<IoPlayCircle size="25px" onClick={handleStop} cursor="pointer" /> | ||
</Text> | ||
</Tooltip> | ||
)} | ||
|
||
{isSpeaking && !isPaused && ( | ||
<Tooltip label={"Pause Listening"} placement="top"> | ||
<Text as="span"> | ||
|
||
<IoMicCircleSharp size={"25px"} onClick={handlePause} cursor="pointer" /> | ||
</Text> | ||
</Tooltip> | ||
)} | ||
</Flex> | ||
) | ||
} | ||
|
||
export default TextSpeech; |
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
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
Oops, something went wrong.