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

104
LINES

< > BotCompany Repo | #1018551 // Speech Recognizer Using Chrome [Dyn Module]

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

Uses 911K of libraries. Click here for Pure Java version (11740L/62K).

!7

cmodule SpeechRecognizer {
  switchable bool autoStart;
  //bool hideChromeWindow; // doesn't work
  transient new Flag flag;
  switchable bool showHostTrayIcon = true;
  switchable bool passiveMode; // use "passive log" to prevent normal programs from reacting to speech
  switchable bool flatInfoBox = true;
  
  transient S botName = "Chrome Speech.";
  transient O recognizer;
  
  start {
    O[] menuItems = litobjectarray(
      "Start Listening", rThread startListening,
      "Stop Listening", rThread stopListening,
      "Switch to German", rThread switchToGerman,
      "Switch to English", rThread switchToEnglish);
      
    dm_addTrayIcon_left(componentPopupMenuItems(onLeftClick(
      toolTip(jimage_16(#1101635), "Continuous Speech Recognizer"),
      rThread startListening),
      menuItems));
    dm_reloadOnFieldChange showHostTrayIcon();
    if (showHostTrayIcon)
      dm_systemTrayIcon(#1101635, "Continuous Speech Recognizer", itemPlusArray(rThread startListening, menuItems));
    dm_watchField passiveMode(r {
      //sendOpt(botName, passiveMode ? "use passive log" : "use active log");
      setAll(recognizer, doLog := passiveMode, usePassiveLog := passiveMode);
      updateTitle();
    });
    updateTitle();
    if (autoStart) startListening();
  }
  
  void updateTitle {
    setModuleName((passiveMode ? "[PASSIVE]" : "") + " Speech Recognizer Using Chrome");
  }
  
  visual
    jfullcenter(
      northAndCenterWithMargin(dm_checkBox passiveMode(),
      jButtonWithInset("Start listening", rThread doIt)));

  void setLanguage(S language) { if (sendOpt("botName", "language *", language) == null) startRecognizer(true); }
  void switchToEnglish { setLanguage("en-US"); }
  void switchToGerman { setLanguage("de-DE"); }

  void startListening { doIt(false); }
  void justOnce { doIt(true); }
  
  void doIt enter { doIt(false); }
  
  void doIt(bool justOnce) enter {
    if (sendOptQuietly(botName, justOnce ? "just once" : "start recognition") == null)
      startRecognizer(justOnce);
  }
  
  // only call if it's not running already
  void startRecognizer(bool justOnce) {
    print("Starting recognizer");
    recognizer = hotwireDependent(#1009816);
    setAll(recognizer, doLog := passiveMode/*, +hideChromeWindow*/, usePassiveLog := passiveMode);
    set(recognizer, onUtterance := voidfunc(S text) {
      if (passiveMode) ret;
      
      S corrected = dm_correctRecognizedSpeech(text);
      if (neq(corrected, text)) {
        print("Correcting " + quote(text) + " to " + quote(corrected));
        vmBus_send('correctedRecognizedSpeech, text, corrected);
        text = corrected;
      } else
        print("Recognized: " + text);
        
      if (flatInfoBox && !dm_osInForeground())
        flatInfoBox(text);
        
      S info = "Chrome Speech";
      /*if (!vmBus_anyTrue dontApplyRecognizedSpeech())
        dm_setAIBarTextAndFireWithInfo(text, info);*/
      dm_rawSpeechRecognized(module(), text, info, null);
      /*if (ai_isStopListeningCommand(text)) {
        call(recognizer, 'stopRecognition);
        playBlip();
      }*/
    });
    set(recognizer, +justOnce);
    /*if (flag.raise())
      dm_putRecognizedSpeechInAIBar();*/
    runMain(recognizer);
  }
  
  void stopListening enter {
    sendOpt(botName, "stop recognition");
  }
  
  void enhanceFrame(JInternalFrame f) {
    super.enhanceFrame(f);
    internalFrameTitleMenuItems(f,
      "Switch to German", rThread switchToGerman,
      "Switch to English", rThread switchToEnglish);
  }
} 

download  show line numbers  debug dex  old transpilations   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, unoaxrwscvea, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1018551
Snippet name: Speech Recognizer Using Chrome [Dyn Module]
Eternal ID of this version: #1018551/59
Text MD5: cd6bb3ceb71099a443fb84525f6e8908
Transpilation MD5: ef2a547300713f694d72a6208af8ebc1
Author: stefan
Category: javax / stefan's os
Type: JavaX source code (Dynamic Module)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-07-21 21:45:25
Source code size: 3632 bytes / 104 lines
Pitched / IR pitched: No / No
Views / Downloads: 555 / 33868
Version history: 58 change(s)
Referenced in: [show references]