npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

hip-web-speech-cognitive-services

v8.1.1-hip.5

Published

Web Speech API adapter to use Cognitive Services Speech Services for both speech-to-text and text-to-speech service.

Readme

web-speech-cognitive-services

Web Speech API adapter to use Cognitive Services Speech Services for both speech-to-text and text-to-speech service.

npm version Build Status

Description

Speech technologies enables a lot of interesting scenarios, including Intelligent Personal Assistant and provide alternative inputs for assistive technologies.

Although W3C standardized speech technologies in browser, speech-to-text and text-to-speech support are still scarce. However, cloud-based speech technologies are very mature.

This polyfill provides W3C Speech Recognition and Speech Synthesis API in browser by using Azure Cognitive Services Speech Services. This will bring speech technologies to all modern first-party browsers available on both PC and mobile platforms.

Demo

Before getting started, please obtain a Cognitive Services subscription key from your Azure subscription.

Try out our demo at https://compulim.github.io/web-speech-cognitive-services. If you don't have a subscription key, you can still try out our demo in a speech-supported browser.

We use react-dictate-button and react-say to quickly setup the playground.

Browser requirements

Speech recognition requires WebRTC API and the page must hosted thru HTTPS or localhost. Although iOS 12 support WebRTC, native apps using WKWebView do not support WebRTC.

Special requirement for Safari

Speech synthesis requires Web Audio API. For Safari, user gesture (click or tap) is required to play audio clips using Web Audio API. To ready the Web Audio API to use without user gesture, you can synthesize an empty string, which will not trigger any network calls but playing an empty hardcoded short audio clip. If you already have a "primed" AudioContext object, you can also pass it as an option.

How to use

There are two ways to use this package:

  1. Using <script> to load the bundle
  2. Install from NPM

Using <script> to load the bundle

To use the ponyfill directly in HTML, you can use our published bundle from unpkg.

In the sample below, we use the bundle to perform text-to-speech with a voice named "Aria24kRUS".

<!DOCTYPE html>
<html lang="en-US">
  <head>
    <script src="https://unpkg.com/web-speech-cognitive-services/umd/web-speech-cognitive-services.production.min.js"></script>
  </head>
  <body>
    <script>
      const { speechSynthesis, SpeechSynthesisUtterance } = window.WebSpeechCognitiveServices.create({
        credentials: {
          region: 'westus',
          subscriptionKey: 'YOUR_SUBSCRIPTION_KEY'
        }
      });

      speechSynthesis.addEventListener('voiceschanged', () => {
        const voices = speechSynthesis.getVoices();
        const utterance = new SpeechSynthesisUtterance('Hello, World!');

        utterance.voice = voices.find(voice => /Aria24kRUS/u.test(voice.name));

        speechSynthesis.speak(utterance);
      });
    </script>
  </body>
</html>

We do not host the bundle. You should always use Subresource Integrity to protect bundle integrity when loading from a third-party CDN.

The voiceschanged event come shortly after you created the ponyfill. You will need to wait until the event arrived before able to choose a voice for your utterance.

Install from NPM

For production build, run npm install web-speech-cognitive-services.

For development build, run npm install web-speech-cognitive-services@master.

Since Speech Services SDK is not on NPM yet, we will bundle the SDK inside this package for now. When Speech Services SDK release on NPM, we will define it as a peer dependency.

Polyfilling vs. ponyfilling

In JavaScript, polyfill is a technique to bring newer features to older environment. Ponyfill is very similar, but instead polluting the environment by default, we prefer to let the developer to choose what they want. This article talks about polyfill vs. ponyfill.

In this package, we prefer ponyfill because it do not pollute the hosting environment. You are also free to mix-and-match multiple speech recognition engines under a single environment.

Options

The following list all options supported by the adapter.

Setting up for sovereign clouds

You can use the adapter to connect to sovereign clouds, including Azure Government (United States) and Microsoft Azure China.

Please refer to this article on limitations when using Cognitive Services Speech Services on sovereign clouds.

Azure Government (United States)

createPonyfill({
  credentials: {
    authorizationToken: 'YOUR_AUTHORIZATION_TOKEN',
    speechRecognitionHostname: 'virginia.stt.speech.azure.us',
    speechSynthesisHostname: 'virginia.tts.speech.azure.us'
  }
});

Microsoft Azure China

createPonyfill({
  credentials: {
    authorizationToken: 'YOUR_AUTHORIZATION_TOKEN',
    speechRecognitionHostname: 'chinaeast2.stt.speech.azure.cn',
    speechSynthesisHostname: 'chinaeast2.tts.speech.azure.cn'
  }
});

Code snippets

For readability, we omitted the async function in all code snippets. To run the code, you will need to wrap the code using an async function.

Speech recognition (speech-to-text)

import { createSpeechRecognitionPonyfill } from 'web-speech-cognitive-services/lib/SpeechServices/SpeechToText';

const {
  SpeechRecognition
} = await createSpeechRecognitionPonyfill({
  credentials: {
    region: 'westus',
    subscriptionKey: 'YOUR_SUBSCRIPTION_KEY'
  }
});

const recognition = new SpeechRecognition();

recognition.interimResults = true;
recognition.lang = 'en-US';

recognition.onresult = ({ results }) => {
  console.log(results);
};

recognition.start();

Note: most browsers requires HTTPS or localhost for WebRTC.

Integrating with React

You can use react-dictate-button to integrate speech recognition functionality to your React app.

import createPonyfill from 'web-speech-cognitive-services/lib/SpeechServices';
import DictateButton from 'react-dictate-button';

const {
  SpeechGrammarList,
  SpeechRecognition
} = await createPonyfill({
  credentials: {
    region: 'westus',
    subscriptionKey: 'YOUR_SUBSCRIPTION_KEY'
  }
});

export default props =>
  <DictateButton
    onDictate={ ({ result }) => alert(result.transcript) }
    speechGrammarList={ SpeechGrammarList }
    speechRecognition={ SpeechRecognition }
  >
    Start dictation
  </DictateButton>

Speech synthesis (text-to-speech)

import { createSpeechSynthesisPonyfill } from 'web-speech-cognitive-services/lib/SpeechServices/TextToSpeech';

const {
  speechSynthesis,
  SpeechSynthesisUtterance
} = await createSpeechSynthesisPonyfill({
  credentials: {
    region: 'westus',
    subscriptionKey: 'YOUR_SUBSCRIPTION_KEY'
  }
});

speechSynthesis.addEventListener('voiceschanged', () => {
  const voices = speechSynthesis.getVoices();
  const utterance = new SpeechSynthesisUtterance('Hello, World!');

  utterance.voice = voices.find(voice => /Aria24kRUS/u.test(voice.name));

  speechSynthesis.speak(utterance);
});

Note: speechSynthesis is camel-casing because it is an instance.

List of supported regions can be found in this article.

pitch, rate, voice, and volume are supported. Only onstart, onerror, and onend events are supported.

Integrating with React

You can use react-say to integrate speech synthesis functionality to your React app.

import createPonyfill from 'web-speech-cognitive-services/lib/SpeechServices';
import React, { useEffect, useState } from 'react';
import Say from 'react-say';

export default () => {
  const [ponyfill, setPonyfill] = useState();

  useEffect(async () => {
    setPonyfill(await createPonyfill({
      credentials: {
        region: 'westus',
        subscriptionKey: 'YOUR_SUBSCRIPTION_KEY'
      }
    }));
  }, [setPonyfill]);

  return (
    ponyfill &&
      <Say
        speechSynthesis={ ponyfill.speechSynthesis }
        speechSynthesisUtterance={ ponyfill.SpeechSynthesisUtterance }
        text="Hello, World!"
      />
  );
};

Using authorization token

Instead of exposing subscription key on the browser, we strongly recommend using authorization token.

import createPonyfill from 'web-speech-cognitive-services/lib/SpeechServices';

const ponyfill = await createPonyfill({
  credentials: {
    authorizationToken: 'YOUR_AUTHORIZATION_TOKEN',
    region: 'westus'
  }
});

You can also provide an async function that will fetch the authorization token and Azure region on-demand. You should cache the authorization token for subsequent request. For simplicity of this code snippets, we are not caching the result.

import createPonyfill from 'web-speech-cognitive-services/lib/SpeechServices';

const ponyfill = await createPonyfill({
  credentials: () => fetch('https://example.com/your-token').then(res => ({
    authorizationToken: res.text(),
    region: 'westus'
  }))
});

List of supported regions can be found in this article.

Lexical and ITN support

Lexical and ITN support is unique in Cognitive Services Speech Services. Our adapter added additional properties transcriptITN, transcriptLexical, and transcriptMaskedITN to surface the result, in addition to transcript and confidence.

Biasing towards some words for recognition

In some cases, you may want the speech recognition engine to be biased towards "Bellevue" because it is not trivial for the engine to recognize between "Bellevue", "Bellview" and "Bellvue" (without "e"). By giving a list of words, teh speech recognition engine will be more biased to your choice of words.

Since Cognitive Services does not works with weighted grammars, we built another SpeechGrammarList to better fit the scenario.

import createPonyfill from 'web-speech-cognitive-services/lib/SpeechServices';

const {
  SpeechGrammarList,
  SpeechRecognition
} = await createPonyfill({
  credentials: {
    region: 'westus',
    subscriptionKey: 'YOUR_SUBSCRIPTION_KEY'
  }
});

const recognition = new SpeechRecognition();

recognition.grammars = new SpeechGrammarList();
recognition.grammars.phrases = ['Tuen Mun', 'Yuen Long'];

recognition.onresult = ({ results }) => {
  console.log(results);
};

recognition.start();

Custom Speech support

Please refer to "What is Custom Speech?" for tutorial on creating your first Custom Speech model.

To use custom speech for speech recognition, you need to pass the endpoint ID while creating the ponyfill.

import createPonyfill from 'web-speech-cognitive-services/lib/SpeechServices';

const ponyfill = await createPonyfill({
  credentials: {
    region: 'westus',
    subscriptionKey: 'YOUR_SUBSCRIPTION_KEY'
  },
  speechRecognitionEndpointId: '12345678-1234-5678-abcd-12345678abcd',
});

Custom Voice support

Please refer to "Get started with Custom Voice" for tutorial on creating your first Custom Voice model.

To use Custom Voice for speech synthesis, you need to pass the deployment ID while creating the ponyfill, and pass the voice model name as voice URI.

import createPonyfill from 'web-speech-cognitive-services/lib/SpeechServices';

const ponyfill = await createPonyfill({
  credentials: {
    region: 'westus',
    subscriptionKey: 'YOUR_SUBSCRIPTION_KEY'
  },
  speechSynthesisDeploymentId: '12345678-1234-5678-abcd-12345678abcd',
});

const { speechSynthesis, SpeechSynthesisUtterance } = ponyfill;

const utterance = new SpeechSynthesisUtterance('Hello, World!');

utterance.voice = { voiceURI: 'your-model-name' };

await speechSynthesis.speak(utterance);

Event order

According to W3C specifications, the result event can be fire at any time after audiostart event.

In continuous mode, finalized result event will be sent as early as possible. But in non-continuous mode, we observed browsers send finalized result event just before audioend, instead of as early as possible.

By default, we follow event order observed from browsers (a.k.a. strict event order). For a speech recognition in non-continuous mode and with interims, the observed event order will be:

  1. start
  2. audiostart
  3. soundstart
  4. speechstart
  5. result (these are interim results, with isFinal property set to false)
  6. speechend
  7. soundend
  8. audioend
  9. result (with isFinal property set to true)
  10. end

You can loosen event order by setting looseEvents to true. For the same scenario, the event order will become:

  1. start
  2. audiostart
  3. soundstart
  4. speechstart
  5. result (these are interim results, with isFinal property set to false)
  6. result (with isFinal property set to true)
  7. speechend
  8. soundend
  9. audioend
  10. end

For error events (abort, "no-speech" or other errors), we always sent it just before the last end event.

In some cases, loosening event order may improve recognition performance. This will not break conformance to W3C standard.

Test matrix

For detailed test matrix, please refer to SPEC-RECOGNITION.md or SPEC-SYNTHESIS.md.

Known issues

  • Speech recognition
    • Interim results do not return confidence, final result do have confidence
      • We always return 0.5 for interim results
    • Cognitive Services support grammar list but not in JSGF format, more work to be done in this area
      • Although Google Chrome support grammar list, it seems the grammar list is not used at all
  • Speech synthesis
    • onboundary, onmark, onpause, and onresume are not supported/fired
    • pause will pause immediately and do not pause on word breaks due to lack of boundary

Roadmap

  • Speech recognition
    • [x] Add tests for lifecycle events
    • [x] Support stop() and abort() function
    • [x] Add dynamic phrases
    • [x] Add reference grammars
    • [x] Add continuous mode
    • [ ] ~Investigate support of Opus (OGG) encoding~
    • [x] Support custom speech
    • [x] Support ITN, masked ITN, and lexical output
  • Speech synthesis
    • [x] Event: add pause/resume support
    • [x] Properties: add paused/pending/speaking support
    • [x] Support custom voice fonts

Contributions

Like us? Star us.

Want to make it better? File us an issue.

Don't like something you see? Submit a pull request.