Speech synthesis is an application programming interface (API) in web development that allows web browsers to generate synthesized speech, commonly known as text-to-speech (TTS). It enables developers to incorporate speech synthesis functionality into web pages, applications, and other online experiences.
With the speech synthesis API, you can convert text into speech, and the synthesized speech can then be played through the user’s speakers or headphones. Spoken content is especially useful for making web content more accessible to individuals with visual impairments and for adding interactive voice elements to web applications.
J1 Template Integration
Speak2Me, a speech synthesise module for J1 Template, enables a so-called Text-To-Speech converter. The converter functionality allows users to sit back and listen to the browser read aloud the important contents on a Web page.
Longer texts on Chromium-based browsers like Chrome, Edge, or Opera using the standard Speech API will be canceled automatically after 15 seconds. This is fixed by the module Speak2Me by sending pause-resume signals every 10 seconds.
Additionally, the module is chunking the text to be spoken. Speak2Me implements a virtual pause-resume strategy to reconstruct the API functions for pause and resume. On a pause, technically, a stop is sent to the Speech-API. If the speech output is resumed, the speech out continues on the last spoken chunk by stating a new session with the Speech-API.
API Reference
Leverage the powerful selector options of jQuery to specify which parts of the Web page that you want spoken. For example, depending on how you have the Web page organized, a single line of code, like the following, can direct Speak2Me to speak the entire contents of an article or a blog post.
$('article').speak2me('speak')
Or how about only the primary headers and paragraphs
$('h1,h2,p').speak2me('speak')
Internally, Speak2Me clones the matched set of elements and all their descendant elements and text nodes. It then parses this clone using a default set of rules, deciding what should be spoken and ignored, then adding the appropriate pauses to make everything sound more like a narrative. |
Examples
Do not speak the content of <h5>
, <h6>
, and <em>
tags.
$().speak2me('ignore','h5','h6','em')
Speak the content of <button>
and <code>
tags.
$().speak2me('recognize','button','code')
When i.e.
is encountered, say That is
.
$().speak2me('replace','i.e.','That is')
Change default intro text spoken when an HTML tga <img>
is encountered.
$().speak2me('customize','img','Embedded')
Change default intro and outro text spoken when an HTML tag <ol>
is encountered.
$().speak2me('customize','ol','Start of numbered List.','End of Numbered List')
Base Functions
Method | Description |
---|---|
| Speaks aloud the specified DOM element(s) and their descendants. |
| Pauses the speaking. |
| Resumes the speaking after it has been paused. |
| Stops the speaking permanently. |
Voice Parameters
Method | Description | Default|Range |
---|---|---|
| Sets the rate of the speaking voice. | Default: 1.1 |
| Sets the pitch of the speaking voice. | Default: 1.0 |
| Sets the volume of the speaking voice. | Default: 1.0 |
Omitting number resets the parameter to its default value; changes take effect only when next speak call is made |
Read-Only Attributes
Method | Description |
---|---|
| Returns |
| Returns |
| Returns |
$().speak2me('isSpeaking') returns the value of true even when paused |
Get and Set Voices
Language codes consist of two-characters that specify the language, followed by a hyphen, followed by additional characters that specify the particular country or regional dialect of that language. For example, the codes en-US'' and en-GB'' are both English language, but each represent a different country. |
Method | Description |
---|---|
| Returns an array of voice objects; each object has two properties: |
| Populates the DOM element(s) selector with a dropdown menu for voice selection; optional text overwrites default dropdown menu instruction |
| Sets the voice. Must exactly match one of the names returned when using |
| Sets the voice by finding the first voice that matches the two digit language code (case-insensitive). |
| Sets the voice by finding the first voice that exactly matches the complete language code |
Setting a Voice Setting a voice by specifying only a two-digit language code is useful for when you have text on the page in another language, but don’t want to bother checking to see if that language is available. For example, a page otherwise in English may have a paragraph in German that you want spoken. That paragraph can have a link like this:
|
If the German language is available, it will be appropriately spoken. If not, the current voice will remain.
Text Manipulation
Method | Description |
---|---|
| Adds one or more HTML tags to the default array of ignored HTML tags. Omitting tagName clears the array of user-specified ignored HTML tags. |
| Removes one or more HTML tags from the default array of ignored HTML tags. Omitting tagName clears the array of user-specified recognized HTML tags. |
| Replaces oldText with newText when speaking. This is case-insensitive Multiple pairs of text can be specified. Omitting parameters deletes previous replace commands |
| Replaces default text spoken prior to the description of the HTML tags |
| Replaces default text spoken prior to and after the content of the HTML tags |
HTML Data Attributes
Data Attribute | Description |
---|---|
| Content from that DOM element and its descendents are ignored. |
| Content from that DOM element is spoken, overriding the default. |
| Content from that DOM element is spelled out. |
| Specified text is spoken prior to the content of its DOM element. |
| Specified text is spoken after to the content of its DOM element. |
| Specified text is spoken in place of the content of its DOM element. |
Reference Information
Chaining calls is acceptable. For example, the following works just fine.
$('article').speak2me('rate',1.3).speak2me('speak');
Ignored Tags: audio, button, canvas, code, del, dialog, dl, embed, form, head, iframe, meter, nav, noscript, object, s, script, select, style, textarea, video |
HTML Tag | Default Prepend Text | Default Append Text |
---|---|---|
| There’s an embedded image with the description, | n/a |
| There’s an embedded table with the caption, | n/a |
| There’s an embedded figure with the caption, | n/a |
| Quote, | , Unquote, |
| Start of list. | End of list. |
| Start of list. | End of list. |
| Blockquote start. | Blockquote end. |
A comma , followed by a space results in a pause when spoken. A period . results in a slightly longer pause. |