1.0.0 • Published 6 months ago

tts-react-web-synthesis v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

��# tts-react-web-synthesis

CI codecov NPM version

tts-react provides a hook (useTts) and component (TextToSpeech) to convert text to speech. In most cases you want the hook so you can use custom styling on the audio controls.

By default tts-react uses the SpeechSynthesis and SpeechSynthesisUtterance API's. You can fallback to the HTMLAudioElement API by providing a fetchAudioData prop to the hook or component.

Install

npm i react react-dom tts-react-web-synthesis

Demo (Storybook)

morganney.github.io/tts-react

Example

Hook

You can use the hook to create a Speak component that converts the text to speech on render:

import { useTts } from 'tts-react-web-synthesis'
import type { TTSHookProps } from 'tts-react-web-synthesis'

type SpeakProps = Pick<TTSHookProps, 'children'>

const Speak = ({ children }: SpeakProps) => (
  <>{useTts({ children, autoPlay: true }).ttsChildren}</>
)

const App = () => {
  return (
    <Speak>
      <p>This text will be spoken on render.</p>
    </Speak>
  )
}

Or create a more advanced component with controls for adjusting the speaking:

import { useTts } from 'tts-react-web-synthesis'
import type { TTSHookProps } from 'tts-react-web-synthesis'

interface CustomProps extends TTSHookProps {
  highlight?: boolean
}

const CustomTTSComponent = ({ children, highlight = false }: CustomProps) => {
  const { ttsChildren, state, play, stop, pause } = useTts({
    children,
    markTextAsSpoken: highlight
  })

  return (
    <div>
      <>
        <button disabled={state.isPlaying} onClick={play}>Play</button>
        <button disabled={!state.isPlaying} onClick={pause}>Pause</button>
        <button onClick={stop}>Stop</button>
      </>
      {ttsChildren}
    </div>
  )
}

const App = () => {
  return (
    <CustomTTSComponent highlight>
      <p>Some text to be spoken and highlighted.</p>
    </CustomTTSComponent>
  )
}

Component

Use the TextToSpeech component to get up and running quickly:

import { TextToSpeech, Positions, Sizes } from 'tts-react-web-synthesis'

const App = () => {
  return (
    <TextToSpeech
      markTextAsSpoken
      align="vertical"
      size={Sizes.SMALL}
      position={Positions.TL}>
      <p>Some text to be spoken.</p>
    </TextToSpeech>
  )
}

useTts

The hook returns the internal state of the audio being spoken, getters/setters of audio attributes, callbacks that can be used to control playing/stopping/pausing/etc. of the audio, and modified children if using markTextAsSpoken. The parameters accepted are described in the Props section. The response object is described by the TTSHookResponse type.

const {
  get,
  set,
  state,
  spokenText,
  ttsChildren,
  play,
  stop,
  pause,
  replay,
  playOrPause,
  playOrStop,
  toggleMute
} = useTts({
  lang,
  voice,
  children,
  autoPlay,
  markTextAsSpoken,
  markColor,
  markBackgroundColor,
  onStart,
  onBoundary,
  onPause,
  onEnd,
  onError,
  onVolumeChange,
  onPitchChange,
  onRateChange,
  fetchAudioData
})

interface TTSHookProps extends MarkStyles {
  /** The spoken text is extracted from here. */
  children: ReactNode
  /** The `SpeechSynthesisUtterance.lang` to use. */
  lang?: string
  /** The `SpeechSynthesisUtterance.voice` to use. */
  voice?: SpeechSynthesisVoice
  /** The initial rate of the speaking audio. */
  rate?: number
  /** The initial volume of the speaking audio. */
  volume?: number
  /** Whether the text should be spoken automatically, i.e. on render. */
  autoPlay?: boolean
  /** Whether the spoken word should be wrapped in a `<mark>` element. */
  markTextAsSpoken?: boolean
  /** Callback when the volume is changed.  */
  onVolumeChange?: (newVolume: number) => void
  /** Callback when the rate is changed.  */
  onRateChange?: (newRate: number) => void
  /** Callback when the pitch is changed.  */
  onPitchChange?: (newPitch: number) => void
  /** Callback when there is an error of any kind. */
  onError?: (msg: string) => void
  /** Callback when speaking/audio starts playing. */
  onStart?: (evt: SpeechSynthesisEvent | Event) => void
  /** Callback when the speaking/audio is paused. */
  onPause?: (evt: SpeechSynthesisEvent | Event) => void
  /** Calback when the current utterance/audio has ended. */
  onEnd?: (evt: SpeechSynthesisEvent | Event) => void
  /** Callback when a word boundary/mark has been reached. */
  onBoundary?: (evt: SpeechSynthesisEvent | Event) => void
  /** Function to fetch audio and speech marks for the spoken text. */
  fetchAudioData?: (spokenText: string) => Promise<TTSAudioData>
}
interface TTSHookResponse {
  set: {
    lang: (value: string) => void
    rate: (value: number) => void
    pitch: (value: number) => void
    volume: (value: number) => void
    preservesPitch: (value: boolean) => void
  }
  get: {
    lang: () => string
    rate: () => number
    pitch: () => number
    volume: () => number
    preservesPitch: () => boolean
  }
  /** State of the current speaking/audio. */
  state: TTSHookState
  /** The text extracted from the children elements and used to synthesize speech. */
  spokenText: string
  play: () => void
  stop: () => void
  pause: () => void
  replay: () => void
  /** Toggles between muted/unmuted, i.e. volume is zero or non-zero. */
  toggleMute: (callback?: (wasMuted: boolean) => void) => void
  /** Toggles between play/stop. */
  playOrStop: () => void
  /** Toggles between play/pause. */
  playOrPause: () => void
  /** The original children with a possible <mark> included if using `markTextAsSpoken`. */
  ttsChildren: ReactNode
}
interface TTSHookState {
  voices: SpeechSynthesisVoice[]
  boundary: BoundaryUpdate
  isPlaying: boolean
  isPaused: boolean
  isMuted: boolean
  isError: boolean
  isReady: boolean
}
interface TTSBoundaryUpdate {
  word: string
  startChar: number
  endChar: number
}

fetchAudioData

Using fetchAudioData will bypass SpeechSynthesis and use the HTMLAudioElement.

(spokenText: string) => Promise<TTSAudioData>

When using fetchAudioData it must return TTSAudioData which has the following shape:

interface PollySpeechMark {
  end: number
  start: number
  time: number
  type: 'word'
  value: string
}
interface TTSAudioData {
  audio: string
  marks?: PollySpeechMark[]
}

The audio property must be a URL that can be applied to HTMLAudioElement.src, including a data URL. If using markTextAsSpoken then you must also return the marks that describe the word boundaries. PollySpeechMarks have the same shape as the Speech Marks used by Amazon Polly, with the restriction that they must be of type: 'word'.

Props

Most of these are supported by the useTts hook, but those marked with an asterisk are exclusive to the TextToSpeech component.

* Only applies to TextToSpeech component.

NameRequiredTypeDefaultDescription
childrenyesReactNodenoneProvides the text that will be spoken.
langnostringThe one used by SpeechSynthesisUtterance.lang.Sets the SpeechSynthesisUtterance.lang. Overrides voice when set and voice.lang does not match lang.
voicenoSpeechSynthesisVoiceNone or the voice provided by audio from TTSAudioData.The voice heard when the text is spoken. Calling set.lang may override this value.
autoPlaynobooleanfalseWhether the audio of the text should automatically be spoken when ready.
markTextAsSpokennobooleanfalseWhether the word being spoken should be highlighted.
markColornostringnoneColor of the text that is currently being spoken. Only applies with markTextAsSpoken.
markBackgroundColornostringnoneBackground color of the text that is currently being spoken. Only applies with markTextAsSpoken.
fetchAudioDatano(text: string) => Promise<TTSAudioData>noneFunction to return the optional SpeechMarks[] and audio URL for the text to be spoken. See fetchAudioData for more details.
*allowMutingnobooleantrueWhether an additional button will be shown on the component that allows muting the audio.
*onMuteToggledno(wasMuted: boolean) => voidnoneCallback when the user clicks the mute button shown from allowMuting being enabled. Can be used to toggle global or local state like whether autoPlay should be enabled.
onStartno(evt: SpeechSynthesisEvent \| Event) => voidnoneCallback when the speaking/audio has started (or resumed) playing.
onPauseno(evt: SpeechSynthesisEvent \| Event) => voidnoneCallback when the speaking/audio has been paused.
onEndno(evt: SpeechSynthesisEvent \| Event) => voidnoneCallback when the speaking/audio has stopped.
onBoundaryno(boundary: TTSBoundaryUpdate, evt: SpeechSynthesisEvent \| Event) => voidnoneCallback when a word boundary/mark has been reached.
onErrorno(msg: string) => voidnoneCallback when there is an error of any kind playing the spoken text. The error message (if any) will be provided.
onVolumeChangeno(newVolume: number) => voidnoneCallback when the volume has changed.
onRateChangeno(newRate: number) => voidnoneCallback when the rate has changed.
onPitchChangeno(newPitch: number) => voidnoneCallback when the pitch has changed.
*alignno'horizontal' \| 'vertical''horizontal'How to align the controls within the TextToSpeech component.
*sizeno'small' \| 'medium' \| 'large''medium'The relative size of the controls within the TextToSpeech component.
*positionno'topRight' \| 'topLeft' \| 'bottomRight' \| 'bottomLeft''topRight'The relative positioning of the controls within the TextToSpeech component.
*useStopOverPausenobooleanfalseWhether the controls should display a stop button instead of a pause button. On Android devices, SpeechSynthesis.pause() behaves like cancel(), so you can use this prop in that context.

FAQ

1.0.0

6 months ago