Skip to content

Instantly share code, notes, and snippets.

@jerkovicl
Created February 24, 2023 15:06
Show Gist options
  • Save jerkovicl/59e4919ddbd634274631bdf5336dbe9a to your computer and use it in GitHub Desktop.
Save jerkovicl/59e4919ddbd634274631bdf5336dbe9a to your computer and use it in GitHub Desktop.
// Imports the Google Cloud client library
import { TextToSpeechClient } from '@google-cloud/text-to-speech';
import fs from 'fs-extra';
import { promisify } from 'util';
// Creates a client
const client = new TextToSpeechClient();
const convertSSMLtoAudio = async (ssmlText: any, langCode: string, gender: string) =>
// eslint-disable-next-line no-async-promise-executor
new Promise(async (resolve, reject) => {
try {
const audioFileName = `${Date.now().toString()}.mp3`;
// Constructs the request
const request: any = {
// Select the text to synthesize
input: { ssml: `<speak>${ssmlText}</speak>` },
// Select the language and SSML Voice Gender (optional)
voice: { languageCode: langCode ?? 'en-US', name: 'en-US-Wavenet-C', ssmlGender: gender ?? 'NEUTRAL' },
// Select the type of audio encoding
audioConfig: { audioEncoding: 'MP3', speakingRate: 1.1 },
};
// Performs the text-to-speech request
const [response] = await client.synthesizeSpeech(request);
// Write the binary audio content to a local file
const writeFile = promisify(fs.writeFile);
await writeFile(`assets/audio/${audioFileName}`, response.audioContent, 'binary');
// await writeFile(path.join(__dirname, '../../../', 'assets/audio', audioFileName), response.audioContent, 'binary');
// console.log('Audio content written to file: output.mp3');
resolve(audioFileName);
} catch (err) {
reject(err);
}
});
const convertTextToAudio = async (text: string, langCode: string, gender: string) =>
// eslint-disable-next-line no-async-promise-executor
new Promise(async (resolve, reject) => {
try {
const audioFileName = `${Date.now().toString()}.mp3`;
// Constructs the request
const request: any = {
// Select the text to synthesize
input: { text: text },
// Select the language and SSML Voice Gender (optional)
voice: { languageCode: langCode ?? 'en-US', ssmlGender: gender ?? 'NEUTRAL' },
// Select the type of audio encoding
audioConfig: { audioEncoding: 'MP3', speakingRate: 1.1 },
};
// Performs the text-to-speech request
const [response] = await client.synthesizeSpeech(request);
// Write the binary audio content to a local file
const writeFile = promisify(fs.writeFile);
await writeFile(`assets/audio/${audioFileName}`, response.audioContent, 'binary');
// await writeFile(path.join(__dirname, '../../../', 'assets/audio', audioFileName), response.audioContent, 'binary');
// console.log('Audio content written to file: output.mp3');
resolve(audioFileName);
} catch (err) {
reject(err);
}
});
/**
* @description Function to delete file from public directory.
* @param fileName fileName to be deleted from server to remove from conversation cache
*/
const removeAudioFile = async (fileName: string) => {
fs.unlink(`public/${fileName}`, (err) => {
if (err) {
console.log('DELETING FILE ERROR : ', err);
}
});
};
const TextToSpeech = { convertSSMLtoAudio, convertTextToAudio, removeAudioFile };
export { TextToSpeech };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment