Docs
useSpeakup
Use the useSpeakup hook to convert text to speech, with options to control pitch, rate, volume, and voice selection.
Loading...
Installation
pnpm dlx shadcn@latest add https://usekit.kiron.dev/k/use-speakup
Text To Voice
The useTextToVoice
hook provides text-to-speech functionality using the Web Speech API. It supports chunking long texts into smaller parts for better performance.
Example
"use client"
import { useTextToVoice } from "@/hooks/use-speakup"
export function Component() {
const {
speak,
pause,
resume,
cancel,
isSpeaking,
isPaused,
voices,
setVoice,
} = useTextToVoice({
text: "Hello, this is a long text that will be chunked into smaller parts.",
pitch: 1.2,
rate: 1.1,
volume: 0.8,
chunkSize: 100,
})
return (
<div>
<button onClick={speak}>Speak</button>
<button onClick={pause} disabled={!isSpeaking}>
Pause
</button>
<button onClick={resume} disabled={!isPaused}>
Resume
</button>
<button onClick={cancel}>Stop</button>
<select onChange={(e) => setVoice(e.target.value)}>
{voices.map((voice) => (
<option key={voice} value={voice}>
{voice}
</option>
))}
</select>
</div>
)
}
Parameters
Name | Type | Description | Default Value | Optional |
---|---|---|---|---|
text | string | The text to be spoken. | "" | No |
pitch | number | The pitch of the voice (0 to 2). | 1 | Yes |
rate | number | The speed of the speech (0.1 to 10). | 1 | Yes |
volume | number | The volume of the speech (0 to 1). | 1 | Yes |
chunkSize | number | The maximum number of characters per chunk for long texts. | 200 | Yes |
onError | (error: string) => void | Callback function to handle errors during speech synthesis. | undefined | Yes |
Return Values
Property | Type | Description |
---|---|---|
speak | () => void | Starts speaking the text. |
pause | () => void | Pauses the speech. |
resume | () => void | Resumes paused speech. |
cancel | () => void | Stops and cancels the speech. |
setVoice | (name: string) => void | Sets the voice by name (use voices array for available options). |
isSpeaking | boolean | Indicates whether speech is currently active. |
isPaused | boolean | Indicates whether speech is paused. |
voices | string[] | Array of available voice names. |
isSupported | boolean | Indicates whether the browser supports the Web Speech API. |
Voice To Text
The useVoiceToText
hook provides speech-to-text functionality using the Web Speech API. It supports continuous listening and error handling.
Example
"use client"
import { useVoiceToText } from "@/hooks/use-speakup"
export function Component() {
const {
startListening,
stopListening,
isListening,
isStopped,
transcript,
reset,
isSupported,
} = useVoiceToText({ lang: "en-US", continuous: true })
return (
<div>
<button onClick={startListening} disabled={isListening}>
Start Listening
</button>
<button onClick={stopListening} disabled={!isListening}>
Stop Listening
</button>
<button onClick={reset}>Reset</button>
<p>{transcript}</p>
{!isSupported && <p>Your browser does not support speech recognition.</p>}
{isStopped && <p>Speech recognition stopped.</p>}
</div>
)
}
Parameters
Name | Type | Description | Default Value | Optional |
---|---|---|---|---|
lang | string | The language for speech recognition. | "en-US" | Yes |
continuous | boolean | Whether to continuously listen for speech or stop after the first result. | true | Yes |
Return Values
Property | Type | Description |
---|---|---|
startListening | () => void | Starts listening for speech input. |
stopListening | () => void | Stops listening for speech input. |
isListening | boolean | Indicates whether speech recognition is active. |
isStopped | boolean | Indicates whether speech recognition was explicitly stopped. |
transcript | string | The recognized text from speech input. |
reset | () => void | Resets the transcript and stops listening. |
isSupported | boolean | Indicates whether the browser supports the Web Speech API. |
error | permission | device | api | The type of error that occurred. |
retry | () => void | Retries speech recognition after an error. |