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 🙏

© 2024 – Pkg Stats / Ryan Hefner

cloud-text-to-speech

v1.0.3

Published

Single interface to Google, Microsoft and Amazon Text-To-Speech.

Downloads

16

Readme

Cloud Text-To-Speech

Npm Version Npm Downloads Total Npm Downloads Week GitHub License GitHub Sponsor Buy Me a Coffee

Single interface to Google, Microsoft, and Amazon Text-To-Speech. NodeJS implementation of:

Features

  • Universal implementation for accessing all providers with one interface.
  • Separate implementation for every provider so we could access every functionality.
  • Sanitize SSML input per provider so we send only supported SSML elements.
  • Locale names in English and native language so we could display language selector.
  • Fake name generation for Google voices that are generated randomly based on voice locale.
  • Accessible configurable output format (per provider), rate, and pitch.

Feature Requests

We welcome and value your ideas and suggestions to improve this project! To submit and vote for feature requests, please visit our Feature Requests Board.

On the board, you can:

  • Submit new feature requests: Share your ideas on how we can enhance the project.
  • Vote on existing requests: Help prioritize the most popular features by voting for the ones you find most valuable.

Thank you for contributing to the development and improvement of Cloud Text-To-Speech!

Getting Started

There are essentially two ways to use Cloud Text-To-Speech:

  • Universal: Using TtsUniversal to be able to configure the TTS provider dynamically and us it.
    • Single: Using TtsProviders.google, TtsProviders.microsoft, TtsProviders.amazon to use the single provider at a time.
    • Combine: Using TtsProviders.combine to combine all providers and get all voices at once.
  • Provider: Using TtsGoogle, TtsMicrosoft, TtsAmazon to get the most from provider's API.

Universal(Single)

To init configuration use:

//Do init once and run it before any other method
TtsUniversal.init({
  provider: TtsProviders.amazon,
  googleParams: { apiKey: "API-KEY" },
  microsoftParams: { subscriptionKey: "SUBSCRIPTION-KEY", region: "eastus" },
  amazonParams: { keyId: "KEY-ID", accessKey: "ACCESS-KEY", region: "us-east-1" },
  withLogs: true
});

To change provider use:

TtsUniversal.setProvider(TtsProviders.microsoft);

To get the list of all voices use:

//Get voices
const voicesResponse = await TtsUniversal.getVoices();
const voices = voicesResponse.voices;

//Print all available voices
console.log(voices);

//Pick an English Voice
const voice = voices.find((voice) => voice.locale.code.startsWith('en-'));

To convert TTS and get audio use:

//Generate Audio for a text
const text = "Amazon, Microsoft and Google Text-to-Speech API are awesome";

const ttsParams = new TtsParamsUniversal({
  voice: voice,
  audioFormat: AudioOutputFormatUniversal.mp3_64k,
  text: text,
  rate: 'slow', //optional
  pitch: 'default', //optional
});

const ttsResponse = await TtsUniversal.convertTts(ttsParams);

//Get the audio bytes.
const audioBytes = ttsResponse.audio;

Universal(Combine)

To init configuration use:

//Do init once and run it before any other method
TtsUniversal.init({
  provider: TtsProviders.combine,
  googleParams: { apiKey: "API-KEY" },
  microsoftParams: { subscriptionKey: "SUBSCRIPTION-KEY", region: "eastus" },
  amazonParams: { keyId: "KEY-ID", accessKey: "ACCESS-KEY", region: "us-east-1" },
  withLogs: true
});

To change provider use:

TtsUniversal.setProvider(TtsProviders.combine);

To get the list of all voices use:

//Get voices
const voicesResponse = await TtsUniversal.getVoices();
const voices = voicesResponse.voices;

//Print all available voices
console.log(voices);

//Pick an English Voice
const voice = voices.find((voice) => voice.locale.code.startsWith('en-'));

To convert TTS and get audio use:

//Generate Audio for a text
const text = "Amazon, Microsoft and Google Text-to-Speech API are awesome";

const ttsParams = new TtsParamsUniversal({
  voice: voice,
  audioFormat: AudioOutputFormatUniversal.mp3_64k,
  text: text,
  rate: 'slow', //optional
  pitch: 'default', //optional
});

const ttsResponse = await TtsUniversal.convertTts(ttsParams);

//Get the audio bytes.
const audioBytes = ttsResponse.audio;

Google

To init configuration use:

//Do init once and run it before any other method
TtsGoogle.init({
  params: { apiKey: 'API-KEY' },
  withLogs: true,
});

To get the list of all voices use:

//Get voices
const voicesResponse = await TtsGoogle.getVoices();
const voices = voicesResponse.voices;

//Print all voices
console.log(voices);

//Pick an English Voice
const voice = voices.find((voice) => voice.locale.code.startsWith('en-'));

To convert TTS and get audio use:

//Generate Audio for a text
const text = '<speak>Google<break time="2s"> Speech Service Text-to-Speech API is awesome!</speak>';

const ttsParams = new TtsParamsGoogle({
  voice: voice,
  audioFormat: AudioOutputFormatGoogle.mp3,
  text: text,
  rate: 'slow', //optional
  pitch: 'default', //optional
});

const ttsResponse = await TtsGoogle.convertTts(ttsParams);

//Get the audio bytes.
const audioBytes = ttsResponse.audio;

Microsoft

To init configuration use:

//Do init once and run it before any other method
TtsMicrosoft.init({
  params: { subscriptionKey: 'SUBSCRIPTION-KEY', region: 'eastus' },
  withLogs: true,
});

To get the list of all voices use:

//Get voices
const voicesResponse = await TtsMicrosoft.getVoices();
const voices = voicesResponse.voices;

//Print all voices
console.log(voices);

//Pick an English Voice
const voice = voices.find((voice) => voice.locale.code.startsWith('en-'));

To convert TTS and get audio use:

//Generate Audio for a text
const text = '<speak>Microsoft<break time="2s"> Speech Service Text-to-Speech API is awesome!</speak>';

const ttsParams = new TtsParamsMicrosoft({
  voice: voice,
  audioFormat: AudioOutputFormatMicrosoft.audio48Khz192kBitrateMonoMp3,
  text: text,
  rate: 'slow', //optional
  pitch: 'default', //optional
});

const ttsResponse = await TtsMicrosoft.convertTts(ttsParams);

//Get the audio bytes.
const audioBytes = ttsResponse.audio;

Amazon

To init configuration use:

//Do init once and run it before any other method
TtsAmazon.init({
  params: { keyId: 'KEY-ID', accessKey: 'ACCESS-KEY', region: 'us-east-1' },
  withLogs: true,
});

To get the list of all voices use:

//Get voices
const voicesResponse = await TtsAmazon.getVoices();
const voices = voicesResponse.voices;

//Print all voices
console.log(voices);

//Pick an English Voice
const voice = voices.find((voice) => voice.locale.code.startsWith('en-'));

To convert TTS and get audio use:

//Generate Audio for a text
const text = '<speak>Amazon<break time="2s"> Speech Service Text-to-Speech API is awesome!</speak>';

const ttsParams = new TtsParamsAmazon({
  voice: voice,
  audioFormat: AudioOutputFormatAmazon.mp3,
  text: text,
  rate: 'slow', // optional
  pitch: 'default', // optional
});

const ttsResponse = await TtsAmazon.convertTts(ttsParams);

//Get the audio bytes.
const audioBytes = ttsResponse.audio;

Notes

There are things you should take care of:

  • Securing of your API keys and credentials, they could be extracted from your web or mobile app.
  • For fixing SSML/XML before passing it to TTS Params, you could use the xmldom package's, methods (new XMLSerializer()).serializeToString(new DOMParser().parseFromString(ssml, 'text/xml')).
  • Audio has uniform format for all providers, it is Uint8Array that you could use to play it or save it to file.