forked from GoogleCloudPlatform/nodejs-docs-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(texttospeech): modernize quickstart for texttospeech (GoogleClo…
…udPlatform#3726) * chore(texttospeech): modernize quickstart for texttospeech * chore(texttospeech): add testing * chore: update filename * chore: match filename across files and don't conflict with original * chore: updating package manifest * chore(texttospeech): rev versions of node * fix(texttospeech): cleaning up comments * refactor: remove chai, enhance consistency, idiomatic usage --------- Co-authored-by: Adam Ross <adamross@google.com> Co-authored-by: Tony Pujals <tonypujals@google.com>
- Loading branch information
1 parent
c56d4ce
commit 620fb04
Showing
10 changed files
with
248 additions
and
105 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
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
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,47 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// 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. | ||
|
||
import {writeFile} from 'node:fs/promises'; | ||
import {TextToSpeechClient} from '@google-cloud/text-to-speech'; | ||
|
||
async function main() { | ||
// Create a Text-to-Speech client instance | ||
const client = new TextToSpeechClient(); | ||
|
||
// The text that should be converted to speech | ||
const text = 'hello, world!'; | ||
|
||
const outputFile = 'quickstart_output.mp3'; | ||
|
||
// Configure the text-to-speech request | ||
const request = { | ||
input: {text}, | ||
// Select the language and SSML voice gender (optional) | ||
voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'}, | ||
// Configure the audio format of the output | ||
audioConfig: {audioEncoding: 'MP3'}, | ||
}; | ||
|
||
// Performs the text-to-speech request | ||
const [response] = await client.synthesizeSpeech(request); | ||
|
||
// Save the generated binary audio content to a local file | ||
await writeFile(outputFile, response.audioContent, 'binary'); | ||
console.log(`Audio content written to file: ${outputFile}`); | ||
} | ||
|
||
main().catch(err => { | ||
console.error(err); | ||
process.exitCode = 1; | ||
}); |
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
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
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
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
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,57 @@ | ||
// Copyright 2024 Google LLC | ||
// | ||
// 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. | ||
|
||
import assert from 'node:assert/strict'; | ||
import {existsSync, unlinkSync} from 'node:fs'; | ||
import * as cp from 'node:child_process'; | ||
|
||
import {after, beforeEach, describe, it} from 'mocha'; | ||
|
||
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'}); | ||
const outputFile = 'quickstart_output.mp3'; | ||
|
||
function removeOutput() { | ||
try { | ||
// Remove if outputFile exists | ||
unlinkSync(outputFile); | ||
} catch { | ||
// OK to ignore error if outputFile doesn't exist already | ||
} | ||
} | ||
|
||
describe('quickstart', () => { | ||
// Remove file if it exists | ||
beforeEach(() => { | ||
removeOutput(); | ||
}); | ||
|
||
// Remove file after testing | ||
after(() => { | ||
removeOutput(); | ||
}); | ||
|
||
it('should synthesize speech to local mp3 file', () => { | ||
// Verifies that outputFile doesn't exist | ||
assert.equal( | ||
existsSync(outputFile), | ||
false, | ||
`found pre-existing ${outputFile}, please rename or remove and retry the test` | ||
); | ||
const output = execSync('node quickstart.mjs'); | ||
assert.ok( | ||
new RegExp(`Audio content written to file: ${outputFile}`).test(output) | ||
); | ||
assert.ok(existsSync(outputFile)); | ||
}); | ||
}); |
Oops, something went wrong.