speak-ez
v1.0.3
Published
Browser Text-to-Speech (TTS using WebSpeech API) made easy
Downloads
54
Maintainers
Readme
SpeakEZ
SpeakEZ is a lightweight TypeScript library that simplifies the use of the Web Speech API for text-to-speech functionality in web applications. It provides an easy-to-use interface for configuring speech parameters, selecting voices, and handling speech synthesis with improved sentence splitting for better results.
Features
- Simple and intuitive API for text-to-speech synthesis.
- Configurable speech parameters: volume, rate, pitch, and voice selection.
- Automatic sentence splitting for improved speech quality.
- Promise-based interface for easy integration with modern JavaScript/TypeScript applications.
Installation
You can install SpeakEZ via npm:
npm install speak-ezUsage
Here's a basic example of how to use SpeakEZ in your project:
import SpeakEZ from 'speak-ez';
const speakWords = async (text: string) => {
// Create an instance of SpeakEZ
const ez = new SpeakEZ();
// Check for browser support
if (!ez.isSupportedBrowser) {
console.error('Speech Synthesis is not supported in this browser.');
return;
}
// Speak the provided text
ez.speak(text);
};
speakWords('Hello world!');API
new SpeakEZ()
Creates a new instance of SpeakEZ with optional configuration.
init(config?)
Initializes the SpeakEZ instance with the provided configuration and fetches available voices. This is not strictly necessary unless you want to set a custom voice. Individual properties can be set directly on the instance.
config Object Properties
lang: Language code for selecting the default voice (e.g., 'en-US').volume: Volume level (0.0 to 1.0).rate: Speech rate (0.1 to 10).pitch: Speech pitch (0.0 to 2.0).voice: Selected voice for speech synthesis (string or SpeechSynthesisVoice).shouldSplitSentences: Boolean indicating whether to split text into sentences for better speech synthesisvoiceLoadTimeoutInMilliseconds: Timeout in milliseconds to wait for voices to load (default: 1000).
speak(text, options?)
Speaks the provided text with optional event listeners.
text
The text to be spoken.
options Object Properties
listeners: An object containing event listeners for speech synthesis events (start,end,error,pause,resume,mark,boundary).interrupt: Boolean indicating whether to interrupt ongoing speech (default:false).
pause()
Pauses the current speech synthesis.
resume()
Resumes the paused speech synthesis.
cancel()
Cancels the current speech synthesis.
Properties
isSupportedBrowser: Boolean indicating if the browser supports Speech Synthesis.voices: Array of available speech synthesis voices.volume: Volume level (0.0 to 1.0).rate: Speech rate (0.1 to 10).pitch: Speech pitch (0.0 to 2.0).shouldSplitSentences: Boolean indicating whether to split text into sentences for better speech synthesisvoice: Selected voice for speech synthesis.speaking: Boolean indicating if speech synthesis is currently in progress.pending: Boolean indicating if speech synthesis is pending.paused: Boolean indicating if speech synthesis is paused.
License
SpeakEZ is licensed under the MIT License. See the LICENSE file for more information.
