Speech Synthesis in JavaScript
Nikita Dmitriev
Posted on July 14, 2023
Backstory
Hi. I want to develop my personal assistant. I needed to implement speech synthesis. I had two ways:
- Use online speech synthesis like googleTTS
- Use offline npm modules and my system
So, I chose the second one.
And, here I’ll tell you everything about my adventure and how to use speech synthesis on your device using node.js
New era
I decided to use Say.js but the repository is abandoned by the author, and I needed to set “UTF-8” encoding for my voice. Originally I wanted to create a fork for Say.js module, but more convenient was to create my own module (I renamed variables, added encodings and created convenient documentation README). So, I developed my module based on Say.js named “speakertts”. If you want to contribute follow github
How it works
This module uses node.js module “child-process” to call powershell and execute built-in SpeechSynthesizer object (I talk about windows). You can use your powershell and paste this code:
Add-Type -AssemblyName System.speech
$speak = New-Object System.Speech.Synthesis.SpeechSynthesizer
$speak.Speak('Hello...')
You can read more here
How to use
-
Install speakertts module
npm i speakertts
-
Get your voices using
getInstalledVoices()
function
const speaker = require('speakertts') speaker.getInstalledVoices((error, voices) => { if (error) { console.error('Error:', error); } else { console.log('Installed voices:', voices); } });
-
Call SpeechSynthesizer using
speak
function
speaker.speak('Hello, world', 'Your voice', 1, 'ASCII', (error) => { if (error) { return console.error('Error speaking!', error) } console.log('text to speech complete') })
It was basics, other functions you can look up on github or npm
How to set your not system voice (for windows)
You just need to install SAPI format voice, and repeat previous chapter
Posted on July 14, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.