Install Google Cloud Client Library
- Make sure you've installed the Node.js environment in your project since we will be using JavaScript along with Node.js modules.
- Install the essential Node.js client library for accessing Google Cloud Text-to-Speech API. Execute the following command to install the necessary Google Cloud library:
npm install @google-cloud/text-to-speech
Import and Authenticate the Client Library
- Import the `@google-cloud/text-to-speech` library in your JavaScript file to access the Text-to-Speech functionalities.
- Authenticate the library using service account credentials. These credentials are usually stored in a JSON file.
const textToSpeech = require('@google-cloud/text-to-speech');
const fs = require('fs');
const util = require('util');
// Creates a client
const client = new textToSpeech.TextToSpeechClient();
Create Synthesis Request
- Prepare a request object that specifies the text you want to convert to speech as well as the voice and audio format configurations.
- Set up the input text and the associated voice parameters like the language code and gender of the voice.
const request = {
input: { text: 'Hello, world!' },
// Select the language and SSML Voice Gender (optional)
voice: { languageCode: 'en-US', ssmlGender: 'NEUTRAL' },
// Select the type of audio encoding
audioConfig: { audioEncoding: 'MP3' },
};
Synthesize Speech
- Invoke the synthesize speech method on the Text-to-Speech client, providing the request object. This method converts the provided text into synthesized audio content.
- Handle the promise returned by the synthesis function, which contains the audio data.
async function synthesizeText() {
// Performs the Text-to-Speech request
const [response] = await client.synthesizeSpeech(request);
// Write the binary audio content to a local file
const writeFile = util.promisify(fs.writeFile);
await writeFile('output.mp3', response.audioContent, 'binary');
console.log('Audio content written to file: output.mp3');
}
synthesizeText();
Run Your Script and Handle Errors
- Execute your Node.js script to convert the specified text to speech and save the resulting audio to a file.
- Implement error handling mechanisms to capture and report any issues encountered during the synthesis process.
node your_script.js
- Check the console for messages indicating the progress and outcome of the request, and verify the generated audio file for correctness.