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

NameTypeDescriptionDefault ValueOptional
textstringThe text to be spoken.""No
pitchnumberThe pitch of the voice (0 to 2).1Yes
ratenumberThe speed of the speech (0.1 to 10).1Yes
volumenumberThe volume of the speech (0 to 1).1Yes
chunkSizenumberThe maximum number of characters per chunk for long texts.200Yes
onError(error: string) => voidCallback function to handle errors during speech synthesis.undefinedYes

Return Values

PropertyTypeDescription
speak() => voidStarts speaking the text.
pause() => voidPauses the speech.
resume() => voidResumes paused speech.
cancel() => voidStops and cancels the speech.
setVoice(name: string) => voidSets the voice by name (use voices array for available options).
isSpeakingbooleanIndicates whether speech is currently active.
isPausedbooleanIndicates whether speech is paused.
voicesstring[]Array of available voice names.
isSupportedbooleanIndicates 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

NameTypeDescriptionDefault ValueOptional
langstringThe language for speech recognition."en-US"Yes
continuousbooleanWhether to continuously listen for speech or stop after the first result.trueYes

Return Values

PropertyTypeDescription
startListening() => voidStarts listening for speech input.
stopListening() => voidStops listening for speech input.
isListeningbooleanIndicates whether speech recognition is active.
isStoppedbooleanIndicates whether speech recognition was explicitly stopped.
transcriptstringThe recognized text from speech input.
reset() => voidResets the transcript and stops listening.
isSupportedbooleanIndicates whether the browser supports the Web Speech API.
errorpermission | device | apiThe type of error that occurred.
retry() => voidRetries speech recognition after an error.