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

Commit

Permalink
Samples: Use async I/O when writing MP3 files
Browse files Browse the repository at this point in the history
  • Loading branch information
Rebecca Taylor committed Mar 24, 2018
1 parent ed07e6b commit fced808
Showing 1 changed file with 52 additions and 36 deletions.
88 changes: 52 additions & 36 deletions samples/synthesize.js
Expand Up @@ -35,15 +35,19 @@ function synthesizeText(text, outputFile) {
};

client
.synthesizeSpeech(request)
.then(async results => {
const audioContent = results[0].audioContent;

await fs.writeFile(outputFile, audioContent, 'binary');
console.log(`Audio content written to file: ${outputFile}`);
})
.catch(err => {
console.error('ERROR:', err);
.synthesizeSpeech(request, (err, response) => {
if (err) {
console.error('ERROR:', err);
return;
}

fs.writeFile(outputFile, response.audioContent, 'binary', err => {
if (err) {
console.error('ERROR:', err);
return;
}
console.log(`Audio content written to file: ${outputFile}`);
});
});
// [END tts_synthesize_text]
}
Expand All @@ -68,15 +72,19 @@ function synthesizeSsml(ssml, outputFile) {
};

client
.synthesizeSpeech(request)
.then(async results => {
const audioContent = results[0].audioContent;

await fs.writeFile(outputFile, audioContent, 'binary');
console.log(`Audio content written to file: ${outputFile}`);
})
.catch(err => {
console.error('ERROR:', err);
.synthesizeSpeech(request, (err, response) => {
if (err) {
console.error('ERROR:', err);
return;
}

fs.writeFile(outputFile, response.audioContent, 'binary', err => {
if (err) {
console.error('ERROR:', err);
return;
}
console.log(`Audio content written to file: ${outputFile}`);
});
});
// [END tts_synthesize_ssml]
}
Expand All @@ -101,15 +109,19 @@ function synthesizeTextFile(textFile, outputFile) {
};

client
.synthesizeSpeech(request)
.then(async results => {
const audioContent = results[0].audioContent;

await fs.writeFile(outputFile, audioContent, 'binary');
console.log(`Audio content written to file: ${outputFile}`);
})
.catch(err => {
console.error('ERROR:', err);
.synthesizeSpeech(request, (err, response) => {
if (err) {
console.error('ERROR:', err);
return;
}

fs.writeFile(outputFile, response.audioContent, 'binary', err => {
if (err) {
console.error('ERROR:', err);
return;
}
console.log(`Audio content written to file: ${outputFile}`);
});
});
// [END tts_synthesize_text_file]
}
Expand All @@ -134,15 +146,19 @@ function synthesizeSsmlFile(ssmlFile, outputFile) {
};

client
.synthesizeSpeech(request)
.then(async results => {
const audioContent = results[0].audioContent;

await fs.writeFile(outputFile, audioContent, 'binary');
console.log(`Audio content written to file: ${outputFile}`);
})
.catch(err => {
console.error('ERROR:', err);
.synthesizeSpeech(request, (err, response) => {
if (err) {
console.error('ERROR:', err);
return;
}

fs.writeFile(outputFile, response.audioContent, 'binary', err => {
if (err) {
console.error('ERROR:', err);
return;
}
console.log(`Audio content written to file: ${outputFile}`);
});
});
// [END tts_synthesize_ssml_file]
}
Expand Down

0 comments on commit fced808

Please sign in to comment.