DEV Community

Cover image for Generating Text-to-Speech Audio Files Using Google Text-to-Speech API in Node.js
FredAbod
FredAbod

Posted on

Generating Text-to-Speech Audio Files Using Google Text-to-Speech API in Node.js

In this article, we'll explore how to convert text into speech and save it as an audio file using the Google Text-to-Speech (TTS) API in a Node.js environment. We'll leverage the google-tts-api package along with Axios for making HTTP requests and fs for file system operations.

Before we begin, make sure you have Node.js installed on your machine. You'll also need to set up a new Node.js project and install the required packages:



npm init -y
npm install google-tts-api axios


Enter fullscreen mode Exit fullscreen mode

Let's dive right in

Diving

Now let's break down the code you'll add to your file



const fs = require('fs');
const tts = require('google-tts-api');
const axios = require('axios');

// Function to convert text to speech and save as an audio file
async function textToSpeech(text, language, outputFile) {
  try {
    const url = await tts.getAudioUrl(text, {
      lang: language || 'en',
      slow: false,
      host: 'https://translate.google.com',
    });

    const response = await axios.get(url, { responseType: 'arraybuffer' });

    fs.writeFileSync(outputFile, Buffer.from(response.data));
    console.log(`Audio file saved to: ${outputFile}`);
  } catch (error) {
    console.error('Error converting text to speech:', error);
  }
}

// Example usage
const textToConvert = 'Hello, this is a test.'

const outputFileName = 'output.mp3';
const languageCode = 'en'; // Change this to the desired language code

textToSpeech(textToConvert, languageCode, outputFileName);


Enter fullscreen mode Exit fullscreen mode
  • We import the necessary modules: fs for file system operations, google-tts-api for accessing the Google TTS API, and axios for making HTTP requests.
  • The textToSpeech function takes three parameters: the text to convert, the language code (defaulting to English if not provided), and the output file name.
  • Inside the function, we generate the audio URL using tts.getAudioUrl, specifying language and speed parameters.
  • We then use axios to fetch the audio data from the generated URL and write it to the output file using fs.writeFileSync.
  • Any errors that occur during the process are caught and logged.
  • This code snippet converts the text "Hello, this is a test." into speech and saves it as an MP3 file named "output.mp3" in the specified language.

Now run your code and your output.mp3 file should appear
Do not forget to like and follow

Bow

Top comments (0)