npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

text-to-speech-api

v1.0.1

Published

A simple NPM package to convert text to voice using native JavaScript API.

Downloads

146

Readme

TextToSpeech

TextToSpeech API is a lightweight JavaScript class that provides an easy way to convert text to speech using the native Web Speech API. It allows customization of voice selection, speech rate, and pitch, making it a versatile tool for web applications that require voice synthesis capabilities.

Features

  • Convert any text to speech.
  • Select specific voice by name (optional).
  • Adjust speech rate and pitch (optional).
  • Simple and easy-to-use interface.

Installation

Using Node.js

You can install the package via npm if you plan to use it in a Node.js environment:

npm install text-to-speech-api

Including in HTML

For web applications, you can directly include the script in your HTML file:

<script src="./path/to/index.js"></script>

Usage

1. Importing the Package

If using Node.js:

const TextToSpeech = require('text-to-speech-api');

If using directly in the browser, ensure the script is included in your HTML file.

2. Basic Conversion

To convert text to speech, simply call the convert method of the TextToSpeech class:

const text = "Hello, this is a text to voice conversion!";
TextToSpeech.convert(text);

3. Selecting a Voice

You can specify a voice by name, adjusting the rate and pitch as needed:

const text = "Hello, this is a text to voice conversion!";
const voiceName = "Google US English"; // Adjust to the desired voice name

TextToSpeech.convert(text, voiceName, 1.2, 1.0);

4. Adjusting Speech Rate and Pitch

The convert method accepts optional parameters for rate and pitch:

  • Rate: A number that determines how fast the speech is spoken (default is 1).
  • Pitch: A number that affects the highness or lowness of the voice (default is 1).

Example:

TextToSpeech.convert("This is a faster voice!", null, 1.5, 1);

5. Error Handling

The convert method throws an error if the SpeechSynthesis API is not supported:

try {
    TextToSpeech.convert("Hello, world!");
} catch (error) {
    console.error(error.message);
}

Example HTML Implementation

Here is a simple example of how to use TextToSpeech in an HTML file:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Text to Voice Example</title>
    <script src="./path/to/index.js"></script>
</head>
<body>
    <h1>Text to Voice Example</h1>
    <button id="speakButton">Speak Text</button>

    <script>
        document.getElementById('speakButton').addEventListener('click', () => {
            const text = "Hello, this is a text to voice conversion!";
            TextToSpeech.convert(text);
        });
    </script>
</body>
</html>

Changelog

[1.0.1]

Updated

  • Enhanced user experience.

[1.0.0]

Initial Release

  • Released the TextToSpeech class with basic functionality to convert text to speech using the native Web Speech API.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Author

Developed by Masum Billah.
For any issues, suggestions, or contributions, feel free to reach out via email.