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

116
LINES

< > BotCompany Repo | #1014011 // hSpeechRecognition - HTML continues speech recognition using Google

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (3297L/19K).

sS hSpeechRecognition(S jsOnSpeech, bool autoStart) {
  ret hSpeechRecognition(jsOnSpeech, autoStart, "de-DE", true);
}

// requires JQuery
sS hSpeechRecognition(S jsOnSpeech, bool autoStart, S language, bool autoTurnOn, O... _) {
  optPar S noWebKit = "No webkit";
  optPar S speechUI = "";
  ret hjavascript(([[
    var bigOn = #BIGON#, srPause = false, listening = false;
    var recognition;
    var firstStart = true;
    var srUpdatesBlocked = false;

    function resultHTML(html) {
      if ($("#speechResults")) $("#speechResults").html(html);
    }

    function srUpdate() {
      if (srUpdatesBlocked) return;
      var btn = $("#speechOnBtn, .speechOnBtn");
      //console.log("srUpdate: btn=" + btn.length);
      btn.prop('disabled', false);
      btn.html(bigOn ? "Stop listening" : "Listen");
      //console.log("srUpdate: bigOn=" + bigOn + ", srPause=" + srPause);

      var should = bigOn && !srPause;
      $(".listenStatus").css("visibility", should ? "visible" : "hidden");
      if (should && !listening) startRecognition();
      else if (!should && listening) stopRecognition();
    }
    
    function stopRecognition() {
      listening = false;
      recognition.stop();
      srUpdate();
    }
    
    function isMobile() {
      return /mobile/i.test(navigator.userAgent);
    }
    
    function startRecognition() {
      listening = true;
      recognition.start();
      if (firstStart) {
        firstStart = false;
        resultHTML(isMobile() ? "Please talk after the blip" : "You can talk now");
      }
      srUpdate();
    }
    
    function startSpeechRecog() {
      resultHTML("Starting");
      console.log("startSpeechRecog");
  
      if (!('webkitSpeechRecognition' in window)) {
        console.log("No webkit");
        document.writeln(]] + jsQuote(noWebKit) + [[);
        return;
      }
      
      console.log("Have webkit");
      document.writeln(]] + jsQuote(speechUI) + [[);
      
      recognition = new webkitSpeechRecognition();
      resultHTML("Speech recognition available.");
      recognition.lang = "#LANGUAGE#";
      
      recognition.onerror = function(event) { 
        var s = "&nbsp;";
        if (event.error != "no-speech") s = "Error: " + event.error;
        resultHTML(s);
      }
      
      recognition.onresult = function(event) { 
        var result = event.results[0];
        var transcript = result[0].transcript;
        resultHTML("You said: \"" + transcript + "\"");
        #ONSPEECH#
        if (transcript == "stop listening") {
          bigOn = false;
          srUpdate();
        }
      }
      
      recognition.onnomatch = function(event) { 
        resultHTML("-");
      }
      
      recognition.onend = function(event) { 
        listening = false;
        srUpdatesBlocked = true; // block for a short time
        setTimeout(function() {
          srUpdatesBlocked = false;
          srUpdate();
        }, 100);
      }
      
      srUpdate();
    }
    
    // legacy
    function startOrStop() { startOrStop(); }
    
    function startOrStopSpeechRecog() {
      bigOn = !bigOn;
      srUpdate();
    }
    
    window.srUpdate = srUpdate;
  ]] + (autoStart ? "startSpeechRecog();" : ""))
    .replace("#BIGON#", str(autoTurnOn))
    .replace("#LANGUAGE#", language)
    .replace("#ONSPEECH#", jsOnSpeech));
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1014011
Snippet name: hSpeechRecognition - HTML continues speech recognition using Google
Eternal ID of this version: #1014011/43
Text MD5: 6aad98163b18457985bb409ea9610d21
Transpilation MD5: 91799d1a347a9d6502e81580b776e2ef
Author: stefan
Category: javax / html
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-07-24 00:53:18
Source code size: 3446 bytes / 116 lines
Pitched / IR pitched: No / No
Views / Downloads: 563 / 778
Version history: 42 change(s)
Referenced in: [show references]