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

90
LINES

< > BotCompany Repo | #1000737 // "kevin" function (talk as Kevin with FreeTTS)

JavaX fragment (include)

1  
// speech libraries!
2  
lib 1000722
3  
lib 1000723
4  
lib 1000724
5  
lib 1000730
6  
lib 1000731
7  
lib 1000732
8  
lib 1000726
9  
lib 1000727
10  
11  
import javax.speech.*;
12  
import javax.speech.synthesis.*;
13  
14  
static Lock kevin_lock = fairLock();
15  
16  
sbool kevin_debug, kevin_disable, kevin_verbose;
17  
static Synthesizer kevin_synth;
18  
19  
static float kevin_volume = 1f;
20  
static Float kevin_pitch, kevin_pitchRange, kevin_speakingRate;
21  
22  
public static S kevin(String text) ctex {
23  
  kevin_lock.lock();
24  
  if (kevin_verbose) print("Kevin: " + text);
25  
  try {
26  
    if (kevin_disable || kevin_synth != null && empty(trim(text))) return text;
27  
    
28  
    if (kevin_synth == null) {
29  
      String voiceName = "kevin16";
30  
    
31  
      System.setProperty("FreeTTSSynthEngineCentral", "com.sun.speech.freetts.jsapi.FreeTTSEngineCentral");
32  
      System.setProperty("freetts.voices", "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");
33  
      Central.registerEngineCentral("com.sun.speech.freetts.jsapi.FreeTTSEngineCentral");
34  
  
35  
      SynthesizerModeDesc desc = new SynthesizerModeDesc(null, "general", Locale.US, null, null);
36  
  
37  
      if (kevin_debug) print("Creating synth");
38  
      Synthesizer synth = kevin_synth = Central.createSynthesizer(desc);
39  
      synth.allocate();
40  
      desc = (SynthesizerModeDesc) synth.getEngineModeDesc();
41  
      Voice[] voices = desc.getVoices();
42  
      Voice voice = null;
43  
      for (Voice entry : voices) {
44  
          if(entry.getName().equals(voiceName)) {
45  
              voice = entry;
46  
              break;
47  
          }
48  
      }
49  
      if (kevin_debug) print("Voice: " + voice);
50  
      synth.getSynthesizerProperties().setVoice(voice);
51  
      synth.resume();
52  
    }
53  
    
54  
    SynthesizerProperties props = kevin_synth.getSynthesizerProperties();
55  
    props.setVolume(kevin_volume);
56  
    if (kevin_pitch != null) props.setPitch(kevin_pitch);
57  
    if (kevin_pitchRange != null) props.setPitchRange(kevin_pitchRange);
58  
    if (kevin_speakingRate != null) props.setSpeakingRate(kevin_speakingRate);
59  
    
60  
    if (kevin_debug) print("Speaking...");
61  
    logStructureWithDate(cereprocLog(), litorderedmap(action := "Begin talking", voice := "Kevin", +text));
62  
    kevin_synth.speakPlainText(text, null);
63  
    if (kevin_debug) print("Waiting");
64  
    // TODO: We wait later anyway
65  
    long state;
66  
    int n = 0;
67  
    while (((state = kevin_synth.getEngineState()) & Synthesizer.QUEUE_EMPTY) != 0) {
68  
      sleep(50);
69  
      if (++n >= 10000/50) {
70  
        print("Engine state: " + state + ", aborting");
71  
        break;
72  
      }
73  
    }
74  
    
75  
    //kevin_synth.waitEngineState(Synthesizer.QUEUE_EMPTY);
76  
    if (kevin_debug) print("Done");
77  
    logStructureWithDate(cereprocLog(), litorderedmap(action := "End talking", voice := "Kevin", +text));
78  
    
79  
    // keep synth for next time
80  
    //kevin_synth.deallocate();
81  
    
82  
    ret text;
83  
  } finally {
84  
    kevin_lock.unlock();
85  
  }
86  
}
87  
88  
static void kevin_init() {
89  
  kevin("");
90  
}

Author comment

Began life as a copy of #1000721

download  show line numbers  debug dex  old transpilations   

Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1000737
Snippet name: "kevin" function (talk as Kevin with FreeTTS)
Eternal ID of this version: #1000737/10
Text MD5: eabe18dee4a8caf723f8f30d422111da
Author: stefan
Category: javax / speech
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-11-19 17:12:21
Source code size: 2953 bytes / 90 lines
Pitched / IR pitched: No / No
Views / Downloads: 869 / 1035
Version history: 9 change(s)
Referenced in: [show references]