How to use @google-cloud/text-to-speech - 10 common examples

To help you get started, we’ve selected a few @google-cloud/text-to-speech examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github googleapis / nodejs-text-to-speech / samples / listVoices.js View on Github external
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]
}
github googleapis / nodejs-text-to-speech / samples / ssmlAddresses.js View on Github external
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');
github googleapis / nodejs-text-to-speech / samples / quickstart.js View on Github external
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);
github googleapis / nodejs-text-to-speech / samples / synthesize.js View on Github external
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');
github googleapis / nodejs-translate / samples / hybridGlossaries.js View on Github external
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');
github eheikes / tts / test / providers / gcp.spec.js View on Github external
it('should have an underlying Google Cloud object', () => {
      expect(provider.instance).toEqual(jasmine.any(TextToSpeechClient))
    })
github googleapis / nodejs-text-to-speech / system-test / fixtures / sample / src / index.ts View on Github external
function main() {
  const textToSpeechClient = new TextToSpeechClient();
}
github krestaino / prankstr / routes / google.js View on Github external
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'
github malob / article-to-audio-cloud-function / index.js View on Github external
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); }
  });
}
github NGRP / node-red-contrib-viseo / node-red-contrib-google-speech / google-speech-text.js View on Github external
}
        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) => {

@google-cloud/text-to-speech

Cloud Text-to-Speech API client for Node.js

Apache-2.0
Latest version published 3 days ago

Package Health Score

92 / 100
Full package analysis