Speech synthesis made easy - Browser based text to speech (TTS)
Installation
npm install speak-tts
Description
Speech synthesis (tts) for the browser. Wrapping the browser Speech Synthesis API (https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesis) and providing a similar interface, it improves it by :
- giving a Promise-base API (the init() and speak() methods respectively returns a Promise) -> init() get resolved once voices are loaded -> speak() get resolved once the full text has been spoken
- handling the fact that Chrome load voices in an asynchronous manner when others browsers don't -> onvoiceschanged listener gets triggered in all browsers
- handling some quirks and bugs of IOS/android devices and some chrome/firefox versions
- splitting sentences into several speeches to make it sound more natural, especially for older versions of Chrome (can be disabled)
- throwing specific exceptions: explicit exceptions will be thrown if you pass parameters with incompatible values to any of the methods
Work in Chrome, opera and Safari (including ios8 and ios9 devices). Tested successfully on Ipad and Android. See browser support here : http://caniuse.com/#feat=speech-synthesis
Demo
Here is a demo: Here
Usage
Import the library :
// var Speech = require('speak-tts') //if you use es5
Check for browser support :
const speech = // will throw an exception if not browser supportedifspeech // returns a boolean console
Init the speech component :
const speech = speech
You can pass the following properties to the init function:
- volume //default 1
- lang // default is determined by your browser if not provided
- voice : the voice to use // default is chosen by your browser if not provided
- rate // default 1
- pitch // default 1
- splitSentences // default true
- listeners // object of listeners to attach to the SpeechSynthesis object
// Example with full conf Speech
Read a text :
speech
You can pass the following properties to the speak function:
- text: text to be spoken
- queue // default true: if set to false, the current speech utterance will be interrupted
- listeners // object of listeners to attach to the SpeechSynthesisUtterance (see list on https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance)
Read a text (example with all params):
speech
Set language (note that the language must be supported by the client browser) :
Speech
Set the voice (note that the voice must be supported by the client browser) :
Speech // you can pass a SpeechSynthesisVoice as returned by the init() function or just its name
Set the rate :
Speech
Set the volume :
Speech
Set the pitch :
Speech
Pause talking in progress:
Speech
Resume talking in progress:
Speech
Cancel talking in progress:
Speech
Get boolean indicating if the utterance queue contains as-yet-unspoken utterances:
Speech
Get boolean indicating if talking is paused:
Speech
Get boolean indicating if talking is in progress:
Speech
Supported languages (list may be incomplete and depends on your browser)
ar-SA
cs-CZ
da-DK
de-DE
el-GR
en
en-AU
en-GB
en-IE
en-IN
en-US
en-ZA
es-AR
es-ES
es-MX
es-US
fi-FI
fr-CA
fr-FR
he-IL
hi-IN
hu-HU
id-ID
it-IT
ja-JP
ko-KR
nb-NO
nl-BE
nl-NL
pl-PL
pt-BR
pt-PT
ro-RO
ru-RU
sk-SK
sv-SE
th-TH
tr-TR
zh-CN
zh-HK
zh-TW
Tests
These will be added soon. Please do not hesitate to add some !
About the Author
I am a full-stack Javascript developer based in Lyon, France.
License
speak-tts is dual licensed under the MIT license and GPL. For more information click here.