New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

tts-google

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tts-google

Google tts for Node.js

latest
Source
npmnpm
Version
1.0.3
Version published
Weekly downloads
3
200%
Maintainers
1
Weekly downloads
 
Created
Source

tts-google

A simple and efficient Google Text-to-Speech (TTS) library for Node.js that converts text to speech using Google's TTS service.

Features

  • 🎵 Convert text to speech using Google's TTS API
  • 🌍 Support for 50+ languages and language variants
  • 💾 Save audio to file (MP3 format)
  • 🔊 Stream audio data
  • 📦 Get audio as buffer for further processing
  • 🖥️ Built-in HTTP server for TTS service
  • 🚀 Promise-based API with async/await support

Installation

npm install tts-google

Dependencies

This package requires the following dependencies:

npm install multistream async fake-useragent request

Quick Start

const TTS = require('tts-google');

// Create TTS instance
const tts = new TTS('en'); // English

// Convert text to speech and save as file
async function example() {
    try {
        const filename = await tts.saveToFile('Hello World!', 'output.mp3');
        console.log(`Audio saved as: ${filename}`);
    } catch (error) {
        console.error('Error:', error);
    }
}

example();

API Reference

Constructor

const tts = new TTS(language)
  • language (string, optional): Language code (default: 'en')

Methods

saveToFile(text, filename)

Converts text to speech and saves as an audio file.

const filename = await tts.saveToFile('Hello World!', 'hello.mp3');

Parameters:

  • text (string): Text to convert to speech
  • filename (string): Output filename (MP3 format)

Returns: Promise that resolves to the filename

getAudioBuffer(text)

Converts text to speech and returns audio data as a Buffer.

const audioBuffer = await tts.getAudioBuffer('Hello World!');
console.log(`Buffer size: ${audioBuffer.length} bytes`);

Parameters:

  • text (string): Text to convert to speech

Returns: Promise that resolves to a Buffer containing audio data

playAudio(text)

Streams audio data (requires additional audio player implementation).

await tts.playAudio('Hello World!');

Note: This method provides a stream but requires an audio player like node-speaker to actually play the audio.

Static Methods

TTS.getAvailableLanguages()

Returns an object containing all supported languages.

const languages = TTS.getAvailableLanguages();
console.log(languages);
// Output: { 'en': 'English', 'es': 'Spanish', ... }

Supported Languages

The library supports 50+ languages including:

Click to expand full language list
{
  'af': 'Afrikaans',
  'sq': 'Albanian', 
  'ar': 'Arabic',
  'hy': 'Armenian',
  'ca': 'Catalan',
  'zh': 'Chinese',
  'zh-cn': 'Chinese (Mandarin/China)',
  'zh-tw': 'Chinese (Mandarin/Taiwan)', 
  'zh-yue': 'Chinese (Cantonese)',
  'hr': 'Croatian',
  'cs': 'Czech',
  'da': 'Danish',
  'nl': 'Dutch',
  'en': 'English',
  'en-au': 'English (Australia)',
  'en-uk': 'English (United Kingdom)',
  'en-us': 'English (United States)',
  'eo': 'Esperanto',
  'fi': 'Finnish',
  'fr': 'French',
  'de': 'German',
  'el': 'Greek',
  'ht': 'Haitian Creole',
  'hi': 'Hindi',
  'hu': 'Hungarian',
  'is': 'Icelandic',
  'id': 'Indonesian',
  'it': 'Italian',
  'ja': 'Japanese',
  'ko': 'Korean',
  'la': 'Latin',
  'lv': 'Latvian',
  'mk': 'Macedonian',
  'no': 'Norwegian',
  'pl': 'Polish',
  'pt': 'Portuguese',
  'pt-br': 'Portuguese (Brazil)',
  'ro': 'Romanian',
  'ru': 'Russian',
  'sr': 'Serbian',
  'si': 'Sinhala (Sinhalese)',
  'sk': 'Slovak',
  'es': 'Spanish',
  'es-es': 'Spanish (Spain)',
  'es-us': 'Spanish (United States)',
  'sw': 'Swahili',
  'sv': 'Swedish',
  'ta': 'Tamil',
  'th': 'Thai',
  'tr': 'Turkish',
  'vi': 'Vietnamese',
  'cy': 'Welsh'
}

Advanced Usage

Multiple Languages

// English TTS
const ttsEn = new TTS('en');
const englishAudio = await ttsEn.getAudioBuffer('Hello World!');

// Spanish TTS
const ttsEs = new TTS('es');
const spanishAudio = await ttsEs.getAudioBuffer('¡Hola Mundo!');

// French TTS
const ttsFr = new TTS('fr');
const frenchAudio = await ttsFr.getAudioBuffer('Bonjour le monde!');

Error Handling

const tts = new TTS('en');

try {
    await tts.saveToFile('Your text here', 'output.mp3');
    console.log('Success!');
} catch (error) {
    if (error.message.includes('Language not supported')) {
        console.error('Invalid language code');
    } else {
        console.error('TTS Error:', error.message);
    }
}

Using with Express.js

const express = require('express');
const TTS = require('tts-google');

const app = express();
const tts = new TTS('en');

app.get('/speak', async (req, res) => {
    try {
        const text = req.query.text || 'Hello World!';
        const audioBuffer = await tts.getAudioBuffer(text);
        
        res.set({
            'Content-Type': 'audio/mpeg',
            'Content-Length': audioBuffer.length
        });
        res.send(audioBuffer);
    } catch (error) {
        res.status(500).json({ error: error.message });
    }
});

app.listen(3000, () => {
    console.log('TTS Server running on port 3000');
});

Testing

Run the included tests:

npm test

The test suite includes:

  • Language availability check
  • TTS instance creation
  • Audio buffer generation
  • File saving functionality
  • Multi-language support

Technical Details

  • Text Limit: Each TTS request is limited to 100 characters. Longer text is automatically chunked.
  • Output Format: MP3 audio format
  • Rate Limiting: The library uses fake user agents to avoid rate limiting
  • Encoding: Supports UTF-8 text encoding

Troubleshooting

Common Issues

  • "Language not supported" error

    • Check if your language code is in the supported languages list
    • Use lowercase language codes (e.g., 'en' not 'EN')
  • Network errors

    • Ensure you have internet connectivity
    • Google's TTS service may occasionally be unavailable
  • File permission errors

    • Make sure the output directory is writable
    • Check file path permissions

Getting Help

If you encounter issues:

  • Check the GitHub Issues
  • Run the test suite to verify functionality
  • Ensure all dependencies are properly installed

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

ISC License

Author

K.Prabhasha - Evelocore

Keywords: tts, google, gtts, node, speech, voice, text-to-speech, audio, mp3

Keywords

tts

FAQs

Package last updated on 14 Oct 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts