This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Rebecca Taylor
committed
Mar 23, 2018
1 parent
5131b3a
commit 4b01e93
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
}); |