node gTTS how to increase the speed of generated output

104 Views Asked by At

I am using npm package https://www.npmjs.com/package/gtts to generate audio file based on input text provided. I wanted to increase the speed of generated output to 1.25 which I am unable to do. Required some assistance.

Below is the code for reference:

const gTTS = require('gtts');
     
let speech = 'Welcome to GeeksforGeeks';
const  gtts = new gTTS(speech, 'en');
 
gtts.save('Voice.mp3', function (err, result){
    if(err) { throw new Error(err); }
    console.log("Text to speech converted!");
});

I tried using say as well but I am unable to change the voice to a female voice. Here is the code for that as well.

const say = require('say');
const { exec } = require('child_process');

const text = 'Welcome to GeeksforGeeks';
const speed = 2.0; // Adjust the speed (2.0 for double speed, 0.5 for half speed)

// Function to convert text to speech and save as WAV file
function textToSpeech(text, callback) {
  say.export(text, null, speed, 'output.wav', callback);
}

// Function to adjust audio speed using soundstretch
function adjustAudioSpeed(inputFile, outputFile, speed, callback) {
  const command = `soundstretch "${inputFile}" "${outputFile}" -tempo=${speed}`;
  exec(command, callback);
}

// Usage
textToSpeech(text, function () {
  console.log('Text to speech converted!');
  
  const inputFileName = 'output.wav';
  const outputFileName = 'output_adjusted.wav';

  adjustAudioSpeed(inputFileName, outputFileName, speed, function (error, stdout, stderr) {
    if (error) {
      console.error('Error:', error);
    } else {
      console.log('Audio speed adjusted!');
    }
  });
});
0

There are 0 best solutions below