<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Fer4.9.js</title>
    <description>The latest articles on DEV Community by Fer4.9.js (@mobilepadawan).</description>
    <link>https://dev.to/mobilepadawan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F869957%2F992bc7d8-fe15-4f41-b964-db9653597d8a.jpeg</url>
      <title>DEV Community: Fer4.9.js</title>
      <link>https://dev.to/mobilepadawan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mobilepadawan"/>
    <language>en</language>
    <item>
      <title>One JS Class to speak them all</title>
      <dc:creator>Fer4.9.js</dc:creator>
      <pubDate>Sat, 01 Jun 2024 02:26:35 +0000</pubDate>
      <link>https://dev.to/mobilepadawan/one-js-class-to-speak-them-all-dl1</link>
      <guid>https://dev.to/mobilepadawan/one-js-class-to-speak-them-all-dl1</guid>
      <description>&lt;p&gt;&lt;strong&gt;Hi to everyone! 👋🏼&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First of all: &lt;em&gt;sorry for my English&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;I'm a guy from Argentina coding in different programming languages since December 1994.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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).&lt;/p&gt;

&lt;p&gt;I never found an opportunity, until now, to implement most of these modern features of this so-versatile programming language.&lt;/p&gt;

&lt;h2&gt;
  
  
  What was the result
&lt;/h2&gt;

&lt;p&gt;A JS library with just &lt;code&gt;3.17 KB&lt;/code&gt; of weight (unminified) or &lt;code&gt;1.84 KB&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3jo8a24ejbqjfpfz6kot.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3jo8a24ejbqjfpfz6kot.png" alt="Speakit JS in one line of code" width="800" height="267"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This library just &lt;code&gt;extends&lt;/code&gt; the features of the Speech Synthesis Object and simplify in some properties and methods the Speech Synthesis and SpeechSynthesisUtterance objects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test if your web browser supports Speech Synthesis&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Speakit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TTStest&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Get the available voices list in a web browser&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Speakit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getVoices&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;voices&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;voices&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This method is based in JS Promises and returning an object array with all the voices available.&lt;/p&gt;

&lt;p&gt;Some web browsers like &lt;code&gt;Chrome&lt;/code&gt; and &lt;code&gt;Microsoft Edge&lt;/code&gt; has a combination of synthesized voices and Natural voices available. The other ones like &lt;code&gt;Safari&lt;/code&gt; or &lt;code&gt;Firefox&lt;/code&gt; just have synthesized voices.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.utteranceRate&lt;/code&gt; and &lt;code&gt;.utterancePitch&lt;/code&gt; properties let you configure the tone and pitch of the voice. They drives values very sensitive and need several tests in different web browsers (&lt;em&gt;mobile and desktop&lt;/em&gt;) to found the best tuning.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Speakit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;utteranceRate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1.02&lt;/span&gt;
&lt;span class="nx"&gt;Speakit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;utterancePitch&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1.00&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to reproduce a text
&lt;/h2&gt;

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

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;Speakit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello world!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                 &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;en-NZ&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                 &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Microsoft Sam Online (Natural) - English (Hongkong))&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Text successfully readed.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error reading the text:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;.readText()&lt;/code&gt; 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 &lt;code&gt;.then()&lt;/code&gt; control method to do something after the sentence finish played.&lt;/p&gt;

&lt;h2&gt;
  
  
  Controlling the .readText() action
&lt;/h2&gt;

&lt;p&gt;Of course Speakit JS let you controlling the audible reproducing action by using &lt;code&gt;.pauseSpeaking()&lt;/code&gt;, &lt;code&gt;.resumeSpeaking()&lt;/code&gt; or &lt;code&gt;.stopSpeaking()&lt;/code&gt; methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  Speech Synthesis availability
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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. &lt;/p&gt;

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

&lt;p&gt;If you wanna try this JS Library, go for it to my official repo at: &lt;a href="https://github.com/mobilepadawan/Speakit-JS"&gt;https://github.com/mobilepadawan/Speakit-JS&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can also find it in NPMJS web by using &lt;code&gt;npn install speakit-js&lt;/code&gt; command. This JS library just works in frontend webapps and PWAs.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>speech</category>
      <category>synthesis</category>
      <category>class</category>
    </item>
  </channel>
</rss>
