-
Notifications
You must be signed in to change notification settings - Fork 2k
/
synthesize.js
166 lines (145 loc) · 5.53 KB
/
synthesize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright 2018 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.
'use strict';
async function synthesizeText(text, outputFile) {
// [START tts_synthesize_text]
const textToSpeech = require('@google-cloud/text-to-speech');
const fs = require('fs');
const util = require('util');
const client = new textToSpeech.TextToSpeechClient();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const text = 'Text to synthesize, eg. hello';
// const outputFile = 'Local path to save audio file to, e.g. output.mp3';
const request = {
input: {text: text},
voice: {languageCode: 'en-US', ssmlGender: 'FEMALE'},
audioConfig: {audioEncoding: 'MP3'},
};
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]
}
async function synthesizeSsml(ssml, outputFile) {
// [START tts_synthesize_ssml]
const textToSpeech = require('@google-cloud/text-to-speech');
const fs = require('fs');
const util = require('util');
const client = new textToSpeech.TextToSpeechClient();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const ssml = '<speak>Hello there.</speak>';
// const outputFile = 'Local path to save audio file to, e.g. output.mp3';
const request = {
input: {ssml: ssml},
voice: {languageCode: 'en-US', ssmlGender: 'FEMALE'},
audioConfig: {audioEncoding: 'MP3'},
};
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_ssml]
}
async function synthesizeTextFile(textFile, outputFile) {
// [START tts_synthesize_text_file]
const textToSpeech = require('@google-cloud/text-to-speech');
const fs = require('fs');
const util = require('util');
const client = new textToSpeech.TextToSpeechClient();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const textFile = 'Local path to text file, eg. input.txt';
// const outputFile = 'Local path to save audio file to, e.g. output.mp3';
const request = {
input: {text: fs.readFileSync(textFile)},
voice: {languageCode: 'en-US', ssmlGender: 'FEMALE'},
audioConfig: {audioEncoding: 'MP3'},
};
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_file]
}
async function synthesizeSsmlFile(ssmlFile, outputFile) {
// [START tts_synthesize_ssml_file]
const textToSpeech = require('@google-cloud/text-to-speech');
const fs = require('fs');
const util = require('util');
const client = new textToSpeech.TextToSpeechClient();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
// const ssmlFile = 'Local path to SSML file, eg. input.ssml';
// const outputFile = 'Local path to save audio file to, e.g. output.mp3';
const request = {
input: {ssml: fs.readFileSync(ssmlFile)},
voice: {languageCode: 'en-US', ssmlGender: 'FEMALE'},
audioConfig: {audioEncoding: 'MP3'},
};
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_ssml_file]
}
async function main() {
require(`yargs`) // eslint-disable-line
.demand(1)
.command('text <text>', 'Synthesizes audio file from text', {}, opts =>
synthesizeText(opts.text, opts.outputFile)
)
.command('ssml <ssml>', 'Synthesizes audio file from SSML', {}, opts =>
synthesizeSsml(opts.ssml, opts.outputFile)
)
.command(
'text-file <textFile>',
'Synthesizes audio file from text in a file',
{},
opts => synthesizeTextFile(opts.textFile, opts.outputFile)
)
.command(
'ssml-file <ssmlFile>',
'Synthesizes audio file from SSML in a file',
{},
opts => synthesizeSsmlFile(opts.ssmlFile, opts.outputFile)
)
.options({
outputFile: {
alias: 'o',
default: 'output.mp3',
global: true,
requiresArg: true,
type: 'string',
},
})
.example('node $0 text "hello" -o hello.mp3')
.example('node $0 ssml "<speak>Hello there.</speak>" -o hello.mp3')
.example('node $0 text-file resources/hello.txt -o output.mp3')
.example('node $0 ssml-file resources/hello.ssml -o output.mp3')
.wrap(120)
.recommendCommands()
.epilogue(
'For more information, see https://cloud.google.com/text-to-speech/docs'
)
.help()
.strict().argv;
}
main().catch(console.error);