Logo

dev-resources.site

for different kinds of informations.

One JS Class to speak them all

Published at
6/1/2024
Categories
javascript
speech
synthesis
class
Author
mobilepadawan
Categories
4 categories in total
javascript
open
speech
open
synthesis
open
class
open
Author
13 person written this
mobilepadawan
open
One JS Class to speak them all

Hi to everyone! πŸ‘‹πŸΌ

First of all: sorry for my English.

I'm a guy from Argentina coding in different programming languages since December 1994.

In my last holiday time I coded a JS Library improving and grouping in a simple JS Class the Speech Synthesis capacities included in one place the main features of this amazing way to transform a sentence in an audible thing.

I did feel myself as a fortunate coder who widely profited some of the modern capabilities included on #JavaScript to create JS Classes with private and static members (properties and methods).

I never found an opportunity, until now, to implement most of these modern features of this so-versatile programming language.

What was the result

A JS library with just 3.17 KB of weight (unminified) or 1.84 KB of weight (minified) with the power of transforming a text or sentence in an audible thing. The best of all this thing is to resume in just one line of code a native feature of JS that needs between 5 to 10 lines of code for implementing using full Vanilla JS.

Speakit JS in one line of code

This library just extends the features of the Speech Synthesis Object and simplify in some properties and methods the Speech Synthesis and SpeechSynthesisUtterance objects.

Test if your web browser supports Speech Synthesis

Speakit.TTStest();
Enter fullscreen mode Exit fullscreen mode

Get the available voices list in a web browser

Speakit.getVoices().then(voices => console.table(voices))
Enter fullscreen mode Exit fullscreen mode

This method is based in JS Promises and returning an object array with all the voices available.

Some web browsers like Chrome and Microsoft Edge has a combination of synthesized voices and Natural voices available. The other ones like Safari or Firefox just have synthesized voices.

The .utteranceRate and .utterancePitch properties let you configure the tone and pitch of the voice. They drives values very sensitive and need several tests in different web browsers (mobile and desktop) to found the best tuning.

Speakit.utteranceRate = 1.02
Speakit.utterancePitch = 1.00
Enter fullscreen mode Exit fullscreen mode

How to reproduce a text

You need to call the .readText() method and send two arguments:
1) the string of the text or sentence to reproduce
2) the ISO language code selected (en-GB, es-AR, pt-BR)

This method has a third argument (optional) and represents the voice object corresponding to a tone or accent available in the voice list.
For example, if you are using Microsoft Edge to test your webapp and select en-HK as the language to reproduce audible text, you may choose Microsoft Sam Online (Natural) - English (Hongkong) by sending it as the third parameter.

Speakit.readText('Hello world!', 
                 'en-NZ', 
                 'Microsoft Sam Online (Natural) - English (Hongkong))'
.then(()=> console.log('Text successfully readed.') )
.catch((error)=> console.error('Error reading the text:', error) )
Enter fullscreen mode Exit fullscreen mode

The .readText() method works as a Promise. While it is reproducing the sentence, will wait to the end of reproduction to resolve the JS Promise. That let you establish a .then() control method to do something after the sentence finish played.

Controlling the .readText() action

Of course Speakit JS let you controlling the audible reproducing action by using .pauseSpeaking(), .resumeSpeaking() or .stopSpeaking() methods.

Speech Synthesis availability

This amazing JavaScript feature is available in most of the modern browsers since many years ago. In some web browsers you will find the Natural voices options, giving you a Headline in the Speech Synthesis experience of your webapps.

I invite you all to test my JS library and send me comments or suggestions to bring it to the next level. I am working already in some improvements of it.

I didn't mention all the properties and capabilities of Speakit-JS in this article. Go for it and dig all the available features.

If you wanna try this JS Library, go for it to my official repo at: https://github.com/mobilepadawan/Speakit-JS

You can also find it in NPMJS web by using npn install speakit-js command. This JS library just works in frontend webapps and PWAs.

class Article's
30 articles in total
Favicon
Day3 Class and object in java:
Favicon
Day2 java program
Favicon
Day 6 - Object & Class
Favicon
Unflatten in PyTorch
Favicon
Day 4 java class
Favicon
Get 10th Class Math Notes (Matric Part 2) – Download Now for Free
Favicon
Day 18 - Object Oriented Programming
Favicon
Flatten in PyTorch
Favicon
Elevate Learning with Our E-Class: The Ultimate Student Performance Tracking Software
Favicon
Elevate Learning with Class Connect Pro: The Ultimate Student Performance Tracking Software
Favicon
ExtendableError usage in changesets errors package
Favicon
WordPress Training In Hyderabad
Favicon
Understanding the Distinction Between Class and Object in Object-Oriented Programming
Favicon
OOP concepts are importants
Favicon
Class : Inheritanceβœ… | Struct : Inheritance ❌
Favicon
One JS Class to speak them all
Favicon
Classes in C# | Uzbek
Favicon
Javascript classes explanation in a simple way
Favicon
Unveiling the Power of the :empty() CSS Pseudo-Class
Favicon
Mastering the :not() CSS Pseudo-Class
Favicon
Exploring the :has() CSS Pseudo-Class
Favicon
Unveiling the Java Superhero: The Mysterious Object Class
Favicon
How to use functions in react class components?
Favicon
Safeguarding the Seas: The Strategic Importance of Virginia-Class Submarines
Favicon
Mastering Metaclasses in Python using real-life scenarios
Favicon
TypeScript: Namespace VS Class
Favicon
Understanding Object-Oriented Programming: Unveiling the Power of Classes
Favicon
Learning Python classes
Favicon
Java Polymorphism Best Practices: Writing Clean and Extensible Code
Favicon
This Is How I Prepare The Very Best Regularization / Classification Images Dataset For Stable Diffusion

Featured ones: