Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

Commit

Permalink
Samples: add quickstart
Browse files Browse the repository at this point in the history
  • Loading branch information
Rebecca Taylor committed Mar 23, 2018
1 parent 5131b3a commit 4b01e93
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
52 changes: 52 additions & 0 deletions samples/quickstart.js
@@ -0,0 +1,52 @@
/**
* Copyright 2018, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// [START vision_quickstart]
const fs = require('fs');

// Imports the Google Cloud client library
const textToSpeech = require('@google-cloud/text-to-speech');

// Creates a client
const client = new textToSpeech.TextToSpeechClient();

// 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
client.synthesizeSpeech(request)
.then(results => {
// The binary audio content returned from the API
const audioContent = results[0].audioContent;

// Write the audio content to a local file
fs.writeFileSync('output.mp3', audioContent, 'binary');
console.log('Audio content written to file: output.mp3');
})
.catch(err => {
console.error('ERROR:', err);
});
// [END vision_quickstart]
38 changes: 38 additions & 0 deletions samples/system-test/quickstart.test.js
@@ -0,0 +1,38 @@
/**
* Copyright 2017, Google, Inc.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const fs = require(`fs`);
const path = require(`path`);
const test = require(`ava`);
const tools = require(`@google-cloud/nodejs-repo-tools`);

const outputFile = `output.mp3`;
const cmd = `node quickstart.js`;
const cwd = path.join(__dirname, `..`);

test.before(tools.stubConsole);
test.after.always(tools.restoreConsole);
test.after.always(async () => {
await fs.unlink(outputFile);
});

test(`should synthesize speech to local mp3 file`, async t => {
t.false(fs.existsSync(outputFile));
const output = await tools.runAsync(`${cmd}`, cwd);
t.true(output.includes(`Audio content written to file: output.mp3`));
t.true(fs.existsSync(outputFile));
});

0 comments on commit 4b01e93

Please sign in to comment.