Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

90
LINES

< > BotCompany Repo | #1030591 // Test Google TTS [needs API key]

JavaX source code (Dynamic Module) [tags: use-pretranspiled] - run with: Stefan's OS

Uses 30908K of libraries. Click here for Pure Java version (8530L/44K).

1  
!7
2  
3  
set flag NoLib1400401.
4  
lib 1400452 // google tts
5  
lib 1400453 // google api java client
6  
7  
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
8  
import com.google.api.client.json.jackson2.JacksonFactory;
9  
import com.google.cloud.texttospeech.v1.AudioConfig;
10  
import com.google.cloud.texttospeech.v1.AudioEncoding;
11  
import com.google.cloud.texttospeech.v1.SsmlVoiceGender;
12  
import com.google.cloud.texttospeech.v1.SynthesisInput;
13  
import com.google.cloud.texttospeech.v1.SynthesizeSpeechResponse;
14  
import com.google.cloud.texttospeech.v1.TextToSpeechClient;
15  
import com.google.cloud.texttospeech.v1.VoiceSelectionParams;
16  
import com.google.protobuf.ByteString;
17  
import com.google.cloud.texttospeech.v1.TextToSpeechSettings;
18  
19  
import com.google.auth.Credentials;
20  
import com.google.api.gax.core.FixedCredentialsProvider;
21  
import com.google.auth.oauth2.GoogleCredentials;
22  
23  
cmodule2 TestGoogleTTS > DynPrintLog {
24  
  /* female voices:
25  
    en-US-Wavenet-C
26  
    en-US-Wavenet-E
27  
    en-US-Wavenet-F
28  
    en-US-Wavenet-G
29  
    en-US-Wavenet-H
30  
    F and H sound nice
31  
  */
32  
    
33  
  switchable S voiceName = "en-US-Wavenet-C";
34  
  switchable S text = "Hello darling";
35  
  
36  
  start-thread {
37  
    //dm_reloadOnFieldChange('voiceName, 'text);
38  
  }
39  
  
40  
  void render ctex {
41  
    temp InputStream in = newFileInputStream(javaxSecretDir("google-botcompany-credentials.json"));
42  
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JacksonFactory.getDefaultInstance(), new InputStreamReader(in));
43  
44  
    Credentials credentials = GoogleCredentials.fromStream(
45  
      fileInputStream(assertFileExists(javaxSecretDir("google-tts-key.json")));
46  
    TextToSpeechSettings textToSpeechSettings =
47  
      TextToSpeechSettings.newBuilder()
48  
        .setCredentialsProvider(FixedCredentialsProvider.create(credentials))
49  
        .build();
50  
    temp TextToSpeechClient textToSpeechClient = TextToSpeechClient.create(textToSpeechSettings);
51  
52  
    // Set the text input to be synthesized
53  
    SynthesisInput input = SynthesisInput.newBuilder().setText(text).build();
54  
55  
    // Build the voice request, select the language code ("en-US") and the ssml voice gender
56  
    // ("neutral")
57  
    VoiceSelectionParams voice =
58  
        VoiceSelectionParams.newBuilder()
59  
            .setLanguageCode("en-US")
60  
            .setName(voiceName)
61  
            .build();
62  
63  
    // Select the type of audio file you want returned
64  
    AudioConfig audioConfig =
65  
        AudioConfig.newBuilder().setAudioEncoding(AudioEncoding.MP3).build();
66  
67  
    // Perform the text-to-speech request on the text input with the selected voice parameters and
68  
    // audio file type
69  
    SynthesizeSpeechResponse response =
70  
        textToSpeechClient.synthesizeSpeech(input, voice, audioConfig);
71  
72  
    // Get the audio contents from the response
73  
    ByteString audioContents = response.getAudioContent();
74  
75  
    // Write the response to the output file.
76  
    File audioFile = javaxCachesDir("Speech Output/Google TTS/" + sanitizeFileName(voiceName) + "/" + uniqueFileNameUsingMD5_80_v2(text) + ".mp3");
77  
    {
78  
      temp OutputStream out = fileOutputStream(audioFile);
79  
      out.write(audioContents.toByteArray());
80  
    }
81  
    printFileInfo(audioFile);
82  
    playMp3(audioFile);
83  
  }
84  
  
85  
  visual
86  
    jhsplit(super, withCenteredButtons(northAndCenter(
87  
      dm_inputFieldWithLabel voiceName(),
88  
      dm_wordWrapTextAreaWithTitle text()),
89  
      "Render", rThreadEnter render));
90  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030591
Snippet name: Test Google TTS [needs API key]
Eternal ID of this version: #1030591/29
Text MD5: c269a7eb419a8811dbef63ff14e0f984
Transpilation MD5: 8fda67d2bc407eaa0592881d2aacd52f
Author: stefan
Category: javax / networking
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-01-14 17:22:40
Source code size: 3467 bytes / 90 lines
Pitched / IR pitched: No / No
Views / Downloads: 256 / 1019
Version history: 28 change(s)
Referenced in: [show references]