Web Speech API example

Web Speech API example. SpeechRecognition in, speechSynthesis out. A transcript, then a spoken string. No assistant object.

The problem

A page cannot hear you, and it cannot speak, unless you ask two separate engines. There is no assistant object. There is a transcript, then whatever you do with it.

The method

SpeechRecognition turns the mic into a string. Interim results flicker. isFinal is the one you keep. speechSynthesis turns a string into audio. Matching dark mode is your code, not the API.

What to do

  1. Run SpeechRecognition, then Start. Speak. Watch interim become final.
  2. Run getVoices(). Pick a voice.
  3. Speak the last transcript, or a sample if you have not said anything yet.

SpeechRecognition

Prefixed as webkitSpeechRecognition in Chromium. continuous keeps the session open. interimResults streams guesses before isFinal.

const SpeechRecognition = window.SpeechRecognition || window.webkitSpeechRecognition
const rec = new SpeechRecognition()
rec.lang = "en-US"
rec.continuous = true
rec.interimResults = true

start()

start() begins capture. stop() ends it and may still emit a result. abort() ends it and drops the result. Chrome may fire end even when continuous is true. If you still want to listen, this lab calls start() again.

rec.onresult = (event) => {
  const piece = event.results[event.resultIndex]
  if (piece.isFinal) final = piece[0].transcript
  else interim = piece[0].transcript
}
rec.start()
rec.stop()
rec.abort()

After the string

The engine stopped at a transcript. This page looks for these phrases in the final string and then runs ordinary DOM code. Theme stays on this page. Nothing navigates away.

if (final.includes("dark mode")) document.documentElement.dataset.theme = "dark"
if (final.includes("light mode")) document.documentElement.dataset.theme = "light"

    speechSynthesis.getVoices()

    Voices often arrive late. Listen for voiceschanged if the first call is empty. The selected voice is what speak() uses.

    const voices = speechSynthesis.getVoices()
    speechSynthesis.addEventListener("voiceschanged", () => {
      speechSynthesis.getVoices()
    })

    Pick a voice.

    speechSynthesis.speak()

    A SpeechSynthesisUtterance is the string, the lang, and the voice. If there is a final transcript, that is the text. If not, a sample sentence.

    const utter = new SpeechSynthesisUtterance(text)
    utter.lang = "en-US"
    utter.voice = voice
    speechSynthesis.speak(utter)
    speechSynthesis.cancel()

    All Web API labs

    • HTML usermedia element example
    • MediaRecorder API example
    • Web Audio API example