Skip to content

Commit

Permalink
docs(samples): updates effects profile sample (#442)
Browse files Browse the repository at this point in the history
* feat: updates effects profile sample with more guidance
* feat: update test

Co-authored-by: Justin Beckwith <justin.beckwith@gmail.com>
  • Loading branch information
telpirion and JustinBeckwith authored Sep 22, 2020
1 parent ac33ef4 commit b5d0352
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 143 deletions.
127 changes: 36 additions & 91 deletions texttospeech/audioProfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@
// limitations under the License.

'use strict';
async function synthesizeText(
text,
outputFile,
effectsProfileId,
languageCode,
ssmlGender
async function main(
text = 'Hello Everybody! This is an audio profile optimized sound byte.',
outputFile = './resources/phone.mp3',
languageCode = 'en-US',
ssmlGender = 'FEMALE'
) {
//[START tts_synthesize_text_audio_profile_beta]
//[START tts_synthesize_text_audio_profile]

/**
* TODO(developer): Uncomment these variables before running the sample.
*/
// const text = 'Text you want to vocalize';
// const outputFile = 'YOUR_OUTPUT_FILE_LOCAtION;
// const languageCode = 'LANGUAGE_CODE_FOR_OUTPUT';
// const ssmlGender = 'SSML_GENDER_OF_SPEAKER';

// Imports the Google Cloud client library
const speech = require('@google-cloud/text-to-speech');
Expand All @@ -30,91 +37,29 @@ async function synthesizeText(
// Creates a client
const client = new speech.TextToSpeechClient();

const request = {
input: {text: text},
voice: {languageCode: languageCode, ssmlGender: ssmlGender},
audioConfig: {audioEncoding: 'MP3', effectsProfileId: effectsProfileId},
};
async function synthesizeWithEffectsProfile() {
// Add one or more effects profiles to array.
// Refer to documentation for more details:
// https://cloud.google.com/text-to-speech/docs/audio-profiles
const effectsProfileId = ['telephony-class-application'];

const [response] = await client.synthesizeSpeech(request);
const writeFile = util.promisify(fs.writeFile);
await writeFile(outputFile, response.audioContent, 'binary');
console.log(`Audio content written to file: ${outputFile}`);
// [END tts_synthesize_text_audio_profile_beta]
}
const request = {
input: {text: text},
voice: {languageCode: languageCode, ssmlGender: ssmlGender},
audioConfig: {audioEncoding: 'MP3', effectsProfileId: effectsProfileId},
};

const [response] = await client.synthesizeSpeech(request);
const writeFile = util.promisify(fs.writeFile);
await writeFile(outputFile, response.audioContent, 'binary');
console.log(`Audio content written to file: ${outputFile}`);
}
// [END tts_synthesize_text_audio_profile]

async function main() {
require('yargs')
.demand(1)
.command(
'synthesize <text>',
'Detects speech in a local audio file.',
{},
opts =>
synthesizeText(
opts.text,
opts.outputFile,
opts.effectsProfileId,
opts.languageCode,
opts.ssmlGender
)
)
.options({
text: {
alias: 't',
default: 'Hey Everybody! This is a test!',
global: true,
requiresArg: true,
type: 'string',
},
outputFile: {
alias: 'f',
default: './resources/test.mp3',
global: true,
requiresArg: false,
type: 'string',
},
effectsProfileId: {
alias: 'e',
default: 'telephony-class-application',
global: true,
requiresArg: true,
type: 'string',
},
languageCode: {
alias: 'l',
default: 'en-US',
global: true,
requiresArg: true,
tnodeype: 'string',
},
ssmlGender: {
alias: 'g',
default: 'FEMALE',
global: true,
requiresArg: true,
type: 'string',
},
})
.array('effectsProfileId')
.example('node $0 synthesize "Enter Phrase to Test Here"')
.example(
'node $0 synthesize "This is optimized for Phone" -f ./resources/phone.mp3 -e telephony-class-application -l en-US'
)
.example(
'node $0 synthesize "This is optimized for a Wearable, like a watch" -f ./resources/watch.mp3 -e wearable-class-device -l en-US'
)
.example(
'node $0 synthesize "This is optimized for Home Entertainment System" -f ./resources/homestereo.mp3 -e large-home-entertainment-class-device'
)
.example(
'node $0 synthesize "This is optimized for the Car" -f ./resources/car.mp3 -e large-automotive-class-device'
)
.wrap(120)
.recommendCommands()
.epilogue('For more information, see https://cloud.google.com/speech/docs')
.help()
.strict().argv;
synthesizeWithEffectsProfile();
}

main().catch(console.error);
main(...process.argv.slice(2)).catch(err => {
console.error(err);
process.exitCode = 1;
});
61 changes: 9 additions & 52 deletions texttospeech/test/audioProfile.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@ const cp = require('child_process');
const execSync = cmd => cp.execSync(cmd, {encoding: 'utf-8'});

const cmd = 'node audioProfile.js';
const text = 'Hello Everybody! This is an Audio Profile Optimized Sound Byte.';
const outputFile1 = 'phonetest.mp3';
const outputFile2 = 'homeTheatreTest.mp3';
const outputFile3 = 'carAudioTest.mp3';
const outputFile4 = 'watchAudioTest.mp3';
const text =
'"Hello Everybody! This is an Audio Profile Optimized Sound Byte."';
const outputFile = 'phonetest.mp3';

describe('audio profile', () => {
after(() => {
Expand All @@ -37,54 +35,13 @@ describe('audio profile', () => {
// Ignore error
}
}
[outputFile1, outputFile2, outputFile3, outputFile4].map(unlink);
[outputFile].map(unlink);
});

it('should synthesize Speech for Telephone Audio Profile', async () => {
assert.strictEqual(fs.existsSync(outputFile1), false);
const output = execSync(
`${cmd} synthesize '${text}' -f ${outputFile1} -e telephony-class-application`
);
assert.match(
output,
new RegExp(`Audio content written to file: ${outputFile1}`)
);
assert.ok(fs.existsSync(outputFile1));
});

it('should synthesize Speech for Home Theatre Audio Profile', async () => {
assert.strictEqual(fs.existsSync(outputFile2), false);
const output = execSync(
`${cmd} synthesize '${text}' -f ${outputFile2} -e large-home-entertainment-class-device`
);
assert.match(
output,
new RegExp(`Audio content written to file: ${outputFile2}`)
);
assert.ok(fs.existsSync(outputFile2));
});

it('should synthesize Speech for Car Audio Audio Profile', async () => {
assert.strictEqual(fs.existsSync(outputFile3), false);
const output = execSync(
`${cmd} synthesize '${text}' -f ${outputFile3} -e large-automotive-class-device`
);
assert.match(
output,
new RegExp(`Audio content written to file: ${outputFile3}`)
);
assert.ok(fs.existsSync(outputFile3));
});

it('should synthesize Speech for Watch Audio Profile', async () => {
assert.strictEqual(fs.existsSync(outputFile4), false);
const output = execSync(
`${cmd} synthesize '${text}' -f ${outputFile4} -e wearable-class-device`
);
assert.match(
output,
new RegExp(`Audio content written to file: ${outputFile4}`)
);
assert.ok(fs.existsSync(outputFile4));
it('should synthesize human audio using hardware profile', async () => {
assert.strictEqual(fs.existsSync(outputFile), false);
const output = execSync(`${cmd} ${text} ${outputFile}`);
assert.match(output, new RegExp('Audio content written to file:'));
assert.ok(fs.existsSync(outputFile));
});
});

0 comments on commit b5d0352

Please sign in to comment.