• Stars
    star
    138
  • Rank 264,508 (Top 6 %)
  • Language
    JavaScript
  • Created almost 3 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

๐Ÿ”Š Cross browser Speech Synthesis also known as Text to speech or TTS; no dependencies; uses Web Speech API

Easy Speech

Cross browser Speech Synthesis; no dependencies.


API docs ยป

JavaScript Style Guide Project Status: Active โ€“ The project has reached a stable, usable state and is being actively developed. Test suite CodeQL Semantic Analysis npm npm bundle size npm bundle size

โญ๏ธ Why EasySpeech?

This project was created, because it's always a struggle to get the synthesis part of Web Speech API running on most major browsers.

โœจ Features

  • ๐Ÿช„ Single API for using speechSynthesis across multiple browsers
  • ๐ŸŒˆ Async API (Promises, async/await)
  • ๐Ÿš€ Hooks for all events; global and/or voice-instance-specific
  • ๐ŸŒฑ Easy to set up and integrate: auto-detects and loads available voices
  • ๐Ÿ”ง Includes fixes or workarounds for many browser-specific quirks
  • ๐Ÿ“ Internal logging via EasySpeech.debug hook
  • ๐Ÿ“ฆ Multiple build targets
  • ๐ŸŽฎ Live demo to test your browser

Note: this is not a polyfill package, if your target browser does not support speech synthesis or the Web Speech API, this package is not usable.

๐Ÿš€ Live Demo

The live demo is available at https://jankapunkt.github.io/easy-speech/ You can use it to test your browser for speechSynthesis support and functionality.

live demo screenshot

Table of Contents

Table of Contents generated with DocToc

๐Ÿ“ฆ Installation

Install from npm via

$ npm install easy-speech

You can also use the various builds for different targets, see the dist folder:

  • /dist/EasySpeech.js - ESM
  • /dist/EasySpeech.cjs.js - CommonJs
  • /dist/EasySpeech.es5.js - Legacy node compatible
  • /dist/EasySpeech.iife.js - Legacy compatible build, works even with older or exotic browsers, as long as they support Promises (PRs welcome to transform to callbacks!)
  • /dist/index.d.ts - TypeScript type definitions

You can use them via CDN:

<!-- esm -->
<script type="module">
  import easySpeech from 'https://cdn.jsdelivr.net/npm/easy-speech/+esm'
</script>
<!-- classic -->
<script src="https://cdn.jsdelivr.net/npm/easy-speech/dist/EasySpeech.iife.js"></script>

๐Ÿ‘จโ€๐Ÿ’ป Usage

Import EasySpeech and first, detect, if your browser is capable of tts (text to speech):

import EasySpeech from 'easy-speech'

EasySpeech.detect()

it returns an Object with the following information:

{
  speechSynthesis: SpeechSynthesis|undefined,
  speechSynthesisUtterance: SpeechSynthesisUtterance|undefined,
  speechSynthesisVoice: SpeechSynthesisVoice|undefined,
  speechSynthesisEvent: SpeechSynthesisEvent|undefined,
  speechSynthesisErrorEvent: SpeechSynthesisErrorEvent|undefined,
  onvoiceschanged: Boolean,
  onboundary: Boolean,
  onend: Boolean,
  onerror: Boolean,
  onmark: Boolean,
  onpause: Boolean,
  onresume: Boolean,
  onstart: Boolean
}

If at least SpeechSynthesis and SpeechSynthesisUtterance are defined you are good to go.

๐Ÿš€ Initialize

Preparing everything to work is not as clear as it should, especially when targeting cross-browser functionality. The asynchronous init function will help you with this situation:

EasySpeech.init({ maxTimeout: 5000, interval: 250 })
    .then(() => console.debug('load complete'))
    .catch(e => console.error(e))

๐Ÿ’ฝ Loading voices

The init-routine will go through several stages to setup the environment:

  • detect and that SpeechSynthesis is basically supported, if not -> fail
  • load voices directly
  • if not loaded but onvoiceschanged is available: use onvoiceschanged
  • if onvoiceschanged is not available: fallback to timeout
  • if onvoiceschanged is fired but no voices available: fallback to timeout
  • timeout reloads voices in a given interval until a maxTimeout is reached
  • if voices are loaded until then -> complete
  • if no voices found -> fail

If your init routing has still not detected / loaded any voices, allthough speechSynth is supported please leave an issue!

Placing a fallback voice

If voices are found it will place a fallback voice by the following rules:

  • If there is a voice among all voices with the default property set to true use this as fallback voice
  • Otherwise find the first matching voice by current navigator.language
  • Otherwise use the first voice in the Array

Note: This fallback voice is not overridden by EasySpeech.defaults(), your default voice will be used in favor but the fallback voice will always be there in case no voice is found when calling EasySpeech.speak()

๐Ÿ“ข Speak a voice

This is as easy as it gets:

await EasySpeech.speak({
  text: 'Hello, world!',
  voice: myLangVoice, // optional, will use a default or fallback
  pitch: 1,
  rate: 1,
  volume: 1,
  // there are more events, see the API for supported events
  boundary: e => console.debug('boundary reached')
})

The Promise will automatically resolve when the speaking ends or rejects when an error occurred. You can additionally attach these event listeners if you like or use EasySpeech.on to attach default listeners to every time you call EasySpeech.speak.

๐Ÿ˜ตโ€๐Ÿ’ซ Troubleshooting / FAQ

There is an own FAQ section available that aims to help with common issues.

๐Ÿ”ฌ API

There is a full API documentation available: api docs

โŒจ๏ธ Contribution and development

Every contribution is welcomed, please open issues if anything is not working as expected.

If you intend to contribute code, please read the guidelines on contributing.

๐Ÿ“– Resources

This project used several resources to gain insights about how to get the best cross-browser SpeechSynthesis running:

โš–๏ธ License

MIT, see license file

More Repositories

1

latexcv

๐Ÿ‘” A collection of cv and resume templates written in LaTeX. Leave an issue if your language is not supported!
TeX
2,366
star
2

meteor-blaze-bs4

Generic Bootstrap 4 components library for Meteor Blaze.
JavaScript
20
star
3

meteor-react-native-starter

Boilerplate with auth, backend, db and many more! Clone, install, start coding!
JavaScript
18
star
4

npm-package-template

๐Ÿ“ฆ minimal NPM package template using Babel, Mocha, Chai, C8, Standardjs, JSDoc and Rollup.
JavaScript
14
star
5

js-set-extension

Extending the Set class in order to support mathematical set properties and operations.
JavaScript
12
star
6

master-thesis

LaTex master thesis templates that allow quick publishing as well as custom design.
TeX
10
star
7

meteor-http

Make HTTP calls to remote servers using fetch API. Fork of the core http package.
JavaScript
9
star
8

real-iq

The most practical IQ test in the world. Written in vanilla js/html/css.
HTML
9
star
9

PDFView

An Actionscript 3 based PDF viewer build from scratch
ActionScript
8
star
10

meteor-migration-helper

Detect which parts of your Meteor.js server environment need to be migrated in your current 2.x code.
JavaScript
5
star
11

js-detect-os

Detect current running OS including version and codename.
JavaScript
5
star
12

meteor-autoform-tags

Provides a form field to enter tags.
JavaScript
5
star
13

meteor-autoform-bpmn

Include bpmn-js modeler as autoform template.
JavaScript
4
star
14

meteor-template-monitor

Insights to your template usages
JavaScript
4
star
15

meteor-auth-server-example

This is a very simplified proof-of-concept project, aiming to show how to use a Meteor app as OAuth2 authorization (and thus authentication) server.
JavaScript
4
star
16

bpmntools

Primitive approach of integrating the bpmn-js modeler (from bpmn.io) into a meteor project.
JavaScript
4
star
17

latex-ci

Simple proof of concept project on how to setup ci services for your latex project.
Shell
3
star
18

js-class-privacy

Create a given ES6 class with private members using Proxy and closures. Keeps class code clean, encourages SRP and DRY.
JavaScript
3
star
19

microservices-with-meteor

An example setup to show how to use Microservices with Meteor
JavaScript
3
star
20

meteor-autoform-password2

Autoform password input using Bootstrap 4 and with extra functionality.
JavaScript
2
star
21

jankapunkt

My personal repo
2
star
22

meteor-remote-collections

Meteor package. Loads remote collections and subscribes via ddp. Allows multiple connections.
JavaScript
2
star
23

meteor-crudable

Configure and create CRUD methods and publictions for your collections.
JavaScript
2
star
24

meteor-autoform-faicon

AutoForm extension to select a font awesome icon from a list of icons. Will only save the icon classname without the `fa-` prefix.
JavaScript
2
star
25

meteor-microservices-examples

A collection of different examples of utilizing microservices in Meteor.
JavaScript
2
star
26

meteor-bootstrapping-admin-example

A simple example how to bootstrap an Admin account safely.
JavaScript
2
star
27

ggj2018

My game for the ggj 2018
CSS
1
star
28

meteor-template-loader

The one and only template loader you need! Allows to import Templates dynamically at lookup-time. All code < 200 lines.
JavaScript
1
star
29

meteor-collection-factory

A common factory to create standardized collection instances.
JavaScript
1
star
30

thin-storage

Thin storage layer for small projects, (hopefully) works with any backend
HTML
1
star
31

meteor-autoform-trix

Get the Trix wysiwyg-editor as configurable AutoForm extension.
JavaScript
1
star
32

bpmn-validation

A node package to validate bpmn models, based on the BPMN 2.0 standard
JavaScript
1
star
33

ggj23

all our code for global game jam 2023
Java
1
star
34

badge-collection

A collection of badges I often use
1
star
35

simpl-schema-factory

Common factory for SimpleSchema instances.
JavaScript
1
star
36

meteor-autoform-bpmn-example

Demo for the AutoForm extension jkuester:autoform-bpmn
JavaScript
1
star
37

CreepCaster

A mobile ritual game for the GlobalGameJam 2016.
Processing
1
star
38

js-design-pattern

Examples of software design pattern in ES 6 JavaScript. For learning purposes.
JavaScript
1
star
39

cmmntools

Meteor integration of cmmn modeler from bpmn.io
JavaScript
1
star
40

latex-presentation-with-handouts

Creating a beamer presentation and handouts by using the same source files. Write once, compile to both.
TeX
1
star
41

meteor-reactive-queue

Simple and lightweight reactive queue implementation
JavaScript
1
star
42

meteor-public-remote-api

A handy little class, which helps to expose public collections, methods and subscriptions into an api JSON file.
JavaScript
1
star
43

js-raycaster-engine

A Javascript simple 2.5d raycaster engine.
JavaScript
1
star
44

moodle-js-webservices-lib

Javascript library of moodle web services names and their required parameters to create valid ws calls to moodle.
JavaScript
1
star