Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async function listVoices() {
// [START tts_list_voices]
const textToSpeech = require('@google-cloud/text-to-speech');
const client = new textToSpeech.TextToSpeechClient();
const [result] = await client.listVoices({});
const voices = result.voices;
console.log('Voices:');
voices.forEach(voice => {
console.log(`Name: ${voice.name}`);
console.log(` SSML Voice Gender: ${voice.ssmlGender}`);
console.log(` Natural Sample Rate Hertz: ${voice.naturalSampleRateHertz}`);
console.log(` Supported languages:`);
voice.languageCodes.forEach(languageCode => {
console.log(` ${languageCode}`);
});
});
// [END tts_list_voices]
}
async function ssmlToAudio(ssmlText, outFile) {
// Creates a client
const client = new textToSpeech.TextToSpeechClient();
// Constructs the request
const request = {
// Select the text to synthesize
input: {ssml: ssmlText},
// Select the language and SSML Voice Gender (optional)
voice: {languageCode: 'en-US', ssmlGender: 'MALE'},
// Select the type of audio encoding
audioConfig: {audioEncoding: 'MP3'},
};
// 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(outFile, response.audioContent, 'binary');
function main(
){
// [START tts_quickstart]
// Imports the Google Cloud client library
const textToSpeech = require('@google-cloud/text-to-speech');
// Import other required libraries
const fs = require('fs');
const util = require('util');
// Creates a client
const client = new textToSpeech.TextToSpeechClient();
async function quickStart() {
// The text to synthesize
const text = 'hello, world!';
// Construct the request
const request = {
input: {text: text},
// Select the language and SSML voice gender (optional)
voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'},
// select the type of audio encoding
audioConfig: {audioEncoding: 'MP3'},
};
// Performs the text-to-speech request
const [response] = await client.synthesizeSpeech(request);
async function synthesizeTextFile(textFile, outputFile) {
// [START tts_synthesize_text_file]
const textToSpeech = require('@google-cloud/text-to-speech');
const fs = require('fs');
const util = require('util');
const client = new textToSpeech.TextToSpeechClient();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const textFile = 'Local path to text file, eg. input.txt';
// const outputFile = 'Local path to save audio file to, e.g. output.mp3';
const request = {
input: {text: fs.readFileSync(textFile)},
voice: {languageCode: 'en-US', ssmlGender: 'FEMALE'},
audioConfig: {audioEncoding: 'MP3'},
};
const [response] = await client.synthesizeSpeech(request);
const writeFile = util.promisify(fs.writeFile);
await writeFile(outputFile, response.audioContent, 'binary');
async function syntheticAudio(text, outFile) {
// Replace special characters with HTML Ampersand Character Codes
// These codes prevent the API from confusing text with SSML tags
// For example, '<' --> '<' and '&' --> '&'
let escapedLines = text.replace(/&/g, '&');
escapedLines = escapedLines.replace(/"/g, '"');
escapedLines = escapedLines.replace(//g, '>');
// Convert plaintext to SSML
// Tag SSML so that there is a 2 second pause between each address
const expandedNewline = escapedLines.replace(/\n/g, '\n');
const ssmlText = '' + expandedNewline + '';
// Creates a client
const client = new textToSpeech.TextToSpeechClient();
// Constructs the request
const request = {
// Select the text to synthesize
input: {ssml: ssmlText},
// Select the language and SSML Voice Gender (optional)
voice: {languageCode: 'en-US', ssmlGender: 'MALE'},
// Select the type of audio encoding
audioConfig: {audioEncoding: 'MP3'},
};
// 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(outFile, response.audioContent, 'binary');
it('should have an underlying Google Cloud object', () => {
expect(provider.instance).toEqual(jasmine.any(TextToSpeechClient))
})
function main() {
const textToSpeechClient = new TextToSpeechClient();
}
const textToSpeech = require('@google-cloud/text-to-speech');
const fs = require('fs');
const crypto = require('crypto');
const config = {
keyFilename: './keyfile.json'
};
const client = new textToSpeech.TextToSpeechClient(config);
const text = 'this is a test message!';
const hash = crypto.createHash('md5').update(text).digest('hex');
const xml =
`
/xml/${hash}.xml
`
const request = {
input: {
text: text
},
voice: {
languageCode: 'en-US',
name: 'en-US-Wavenet-F',
ssmlGender: 'FEMALE'
function getTtsAudio(str, cb) {
const ttsClient = new TextToSpeech.TextToSpeechClient();
const ttsRequest = {
input: { text: str },
voice: { languageCode: 'en-US', name: 'en-US-Wavenet-F', ssmlGender: 'FEMALE' },
audioConfig: { audioEncoding: 'MP3' },
};
ttsClient.synthesizeSpeech(ttsRequest, (err, res) => {
if (err) { cb(err, null); }
else { cb(null, res.audioContent); }
});
}
}
if (config.voiceName) {
let voiceName = (config.voiceNameType === 'msg') ? helper.getByString(data, config.voiceName) : config.voiceName;
parameters.voice.voice = voiceName;
}
if (config.ssmlGender) {
let ssmlGender = (config.ssmlGenderType === 'msg') ? helper.getByString(data, config.ssmlGender) : config.ssmlGender;
parameters.voice.ssmlGender = ssmlGender;
}
if (input.match(//ig)) parameters.input.ssml = input;
else parameters.input.text = input;
const texttospeech = require('@google-cloud/text-to-speech');
let client = new texttospeech.v1beta1.TextToSpeechClient({credentials: node.auth.cred});
let res = { audioContent: [] }
client.synthesizeSpeech(parameters)
.then((results) => {
res.audioContent = (results[0] && results[0].audioContent) ? results[0].audioContent : [];
if (!config.getVoices) return 1;
else return client.listVoices({languageCode: parameters.voice.languageCode = languageCode})
})
.then((voices) => {
if (voices !== 1 && voices[0] && voices[0].voices) {
res.voices = (voices[0] && voices[0].voices) ? voices[0].voices : [];
}
helper.setByString(data, config.output || 'payload', res);
return node.send(data);
}).catch((err) => {