-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathGoogleCloudTextToSpeech.java
30 lines (24 loc) · 1.04 KB
/
GoogleCloudTextToSpeech.java
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
// "ssml" is the input provided in the Speech Synthesis Markup Language (SSML).
// Alternatively, you can use the "setText" method to provide raw text.
// To use the WaveNet architecture, specificy a "voice" between "en-US-Wavenet-A" and "en-US-Wavenet-F".
// For US English, set "languageCode" to "en-US".
try (TextToSpeechClient client = TextToSpeechClient.create()) {
SynthesisInput input = SynthesisInput
.newBuilder()
.setSsml(ssml)
.build();
VoiceSelectionParams voice = VoiceSelectionParams
.newBuilder()
.setLanguageCode(languageCode)
.setName(voice)
.build();
AudioConfig audioConfig = AudioConfig
.newBuilder()
.setAudioEncoding(AudioEncoding.MP3)
.build();
SynthesizeSpeechResponse response = client.synthesizeSpeech(input, voice, audioConfig);
byte[] audioContent = response.getAudioContent().toByteArray();
try (FileOutputStream fileOutputStream = new FileOutputStream(outputFile)) {
fileOutputStream.write(audioContent);
}
}