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

transpeech

v1.1.0

Published

TranSpeech is a small voice and text library. It allows you to recognize and synthesize speech using a browser, and translate text.

Downloads

3

Readme

TranSpeech

TranSpeech is a small voice and text library. It allows you to recognize and synthesize speech using a browser, and translate text.

Limitations

TranSpeech uses modern browser api in its work and may have compatibility problems. You can check the compatibility with the browsers at the following links:

Speech Recognition API

Speech Synthesis API

Fetch API

The library also uses the free version of Google Translate API, which allows you to translate only small passages of text, such as sentences. If you need more functionality, in accordance with the MIT license, you can change the library code to fit your needs.

Starting from version 1.0.0, you can get a list of supported features before creating an instance using the static property availableFeatures. If the field of the received object is not negative, the browser supports this functionality.

Install

npm install --save transpeech

Usage

First you need to import the library into your script.

import TranSpeech from 'transpeech';

Then you need to create an instance of the class. You can pass an object with the necessary functionality to the constructor. The object is similar to the object returned by the availableFeatures method.

For example, if the browser does not support speech recognition, you can use the following code:

const ts = new TranSpeech({ recognition: false });

If the settings object is not passed, all features will be requested.

You can also pass the parameter silent. If it is positive, all messages to the browser console will be disabled.

API

Properties

ready

It is true if the class constructor completed successfully, and an instance is ready for use.

if (ts.ready) {
  // your code here
}

permissionStatus

Stores the permission status of access to the media device (e.g. microphone). Possible values: 'granted', 'denied', 'prompt'.

console.log(ts.permissionStatus); // 'prompt'

voices

Contains an array of available voices for the Speech Synthesis API. Each voice object contains the following fields:

Number id // Voice ID
String name // Voice name
String lang // Languade code
Boolean offline // True if this voice available offline

Language codes

console.log(ts.voices[0]);

// {
//   id: 0
//   name: "Microsoft David Desktop - English (United States)"
//   lang: "en-US"
//   offline: true
// }

activeVoice

Returns a voice object that is selected as ative.

console.log(ts.activeVoice); 

// {
//   id: 5
//   name: "Google UK English Female"
//   lang: "en-GB"
//   offline: false
// }

isRecognitionActive

true if recognition is active right now and false otherwise.

if (ts.isRecognitionActive) {
  // your code here
}

recognitionLang

Used to get and set the language of recognized speech.

ts.recognitionLang = 'en';

Methods

requestPermission()

Parameters
MediaStreamConstraints request

Asks the user for permission to access the microphone (by default) or other device. Can be used without params. Returns the promise that is resolved to the MediaStream object, if the user gives permission.

ts.requestPermission().then((mediaStream) => {
  console.log(ts.permissionStatus); // 'granted'
  // Do something with mediaStream
});

setActiveVoice(voiceId)

Parameters
Number | String voiceId // Voice ID, Voice lang code or part of a Voice Name

Sets the voice object as active. It can accept an ID (Number), a language code or its part (String), or part of a voice name (String). Returns false if a voice with such parameters was not found.

ts.setActiveVoice('gb');
console.log(ts.activeVoice);

// {
//   id: 5
//   name: "Google UK English Female"
//   lang: "en-GB"
//   offline: false
// }

speak(text)

Parameters
String text // The text to be synthesized

Synthesizes text with an active voice.

ts.speak('Hello world');

translate(text, lang)

Parameters
String text // The text to be translated
String lang // Lang code

Returns a promise that resolves to a string containing the translated text.

ts.translate('Hello', 'es').then(result => {
  console.log(result);
});

// Hola

startRecognition()

Parameters
none

Starts voice recognition.

ts.startRecognition();
console.log(isRecognitionActive); // true

stopRecognition()

Parameters
none

Stops voice recognition.

ts.stopRecognition();
console.log(isRecognitionActive); // false

Events

Import the event list as follows.

import { Events } from 'transpeech';

VoicesReady

The constructor receives available voices asynchronously. The event fires when voices are received.

ts.on(Events.VoicesReady, () => {
  console.log(ts.voices[0]);
});

PermissionStatusReady

Event fires when browser permissions are received.

ts.on(Events.PermissionStatusReady, () => {
  console.log(ts.permissionStatus);
});

Ready

Event fires when everything is prepared and the instance is fully ready for use.

const ts = new TranSpeech();

ts.on(Events.Ready, () => {
  // Any code here
});

PartlyRecognized

Fires when a sentence is partially recognized.

ts.on(Events.PartlyRecognized, (result) => {
  console.log(result);
});

FullyRecognized

Fires when a sentence is fully recognized.

ts.on(Events.FullyRecognized, (result) => {
  console.log(result);
});

Author

Andrey Bokhan - GitHub

License

This project is licensed under the MIT License.