Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
easy-speech
Advanced tools
This project was created, because it's always a struggle to get the synthesis
part of Web Speech API
running on most major browsers.
speechSynthesis
across multiple browsersEasySpeech.debug
hookNote: 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.
The live demo is available at https://leaonline.github.io/easy-speech/
You can use it to test your browser for speechSynthesis
support and functionality.
Table of Contents generated with DocToc
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 definitionsYou 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>
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.
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))
The init-routine will go through several stages to setup the environment:
onvoiceschanged
is available: use onvoiceschanged
onvoiceschanged
is not available: fallback to timeoutonvoiceschanged
is fired but no voices available: fallback to timeoutinterval
until a maxTimeout
is reachedIf your init routing has still not detected / loaded any voices, allthough speechSynth is supported please leave an issue!
If voices are found it will place a fallback voice by the following rules:
default
property set to true
use this as fallback voicenavigator.language
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()
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
.
There is an own FAQ section available that aims to help with common issues.
There is a full API documentation available: api docs
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.
This project used several resources to gain insights about how to get the best cross-browser SpeechSynthesis running:
MIT, see license file
FAQs
Cross browser Speech Synthesis
The npm package easy-speech receives a total of 1,337 weekly downloads. As such, easy-speech popularity was classified as popular.
We found that easy-speech demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.