SHARE
TWEET

Untitled

a guest Oct 14th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // export default {
  2. var CommandBot = function() {
  3.   var keyword = "robot";
  4.   var commands = [];
  5.   var noCommandRecognisedCallback = null;
  6.   var languageRecognition = "en-US";
  7.  
  8.   //Keyword handling
  9.   var rKeyword = new webkitSpeechRecognition();
  10.   rKeyword.continuous = true;
  11.   rKeyword.interimResults = true;
  12.  
  13.   var rCommand = new webkitSpeechRecognition();
  14.   rCommand.continuous = false;
  15.   rCommand.interimResults = true;
  16.  
  17.   //Speech synthesis
  18.   var synth = window.speechSynthesis;
  19.   var languageSynthesisDefault = "en-US";
  20.   var languageSynthesis = "en-US";
  21.   var voices = [];
  22.  
  23.   window.speechSynthesis.onvoiceschanged = function() {
  24.     voices = synth.getVoices();
  25.   };
  26.  
  27.   //Callback setup
  28.  
  29.   rKeyword.onresult = function(event) {
  30.     for (var i = event.resultIndex; i < event.results.length; ++i) {
  31.       var array = transcriptToArray(event.results[i][0].transcript);
  32.  
  33.       if (array.indexOf(keyword.toLowerCase()) != -1) {
  34.         stopListeningKeyword();
  35.         startListeningCommand();
  36.       }
  37.  
  38.       document.getElementById("speech-result").innerHTML =
  39.         "I heard: " + event.results[i][0].transcript;
  40.     }
  41.   };
  42.  
  43.   rCommand.onresult = function(event) {
  44.     for (var i = event.resultIndex; i < event.results.length; ++i) {
  45.       if (event.results[i].isFinal) {
  46.         //Final results
  47.  
  48.         var commandFound = false;
  49.         for (var j = 0; j < commands.length; j++) {
  50.           commandFound =
  51.             commandFound ||
  52.             commands[j].tryCommand(event.results[i][0].transcript);
  53.         }
  54.  
  55.         if (!commandFound && noCommandRecognisedCallback != null) {
  56.           noCommandRecognisedCallback();
  57.         }
  58.  
  59.         stopListeningCommand();
  60.         startListeningKeyword();
  61.       }
  62.       document.getElementById("speech-result").textContent =
  63.         "I heard: " + event.results[i][0].transcript;
  64.     }
  65.   };
  66.  
  67.   //Private functions
  68.  
  69.   this.startListeningKeyword = function() {
  70.     document.getElementById("instructions").textContent =
  71.       "Awaiting the keyword: " + keyword;
  72.     rKeyword.start();
  73.   };
  74.  
  75.   this.stopListeningKeyword = function() {
  76.     rKeyword.abort();
  77.   };
  78.  
  79.   this.startListeningCommand = function() {
  80.     document.getElementById("instructions").textContent = "Listening...";
  81.     document.getElementById("speech-result").classList.add("listening");
  82.     rCommand.start();
  83.   };
  84.  
  85.   this.stopListeningCommand = function() {
  86.     document.getElementById("speech-result").classList.remove("listening");
  87.     rCommand.abort();
  88.   };
  89.  
  90.   var transcriptToArray = function(transcript) {
  91.     return transcript.toLowerCase().split(" ");
  92.   };
  93.  
  94.   //Public functions
  95.   this.getLanguageRecognition = function() {
  96.     return languageRecognition;
  97.   };
  98.  
  99.   this.getLanguageSynthesis = function() {
  100.     for (var i = 0; i < voices.length; i++) {
  101.       if (voices[i].lang == languageSynthesis) {
  102.         return languageSynthesis;
  103.       }
  104.     }
  105.     return languageSynthesisDefault;
  106.   };
  107.  
  108.   this.setLanguageRecognition = function(lang) {
  109.     languageRecognition = lang;
  110.     rKeyword.lang = languageRecognition;
  111.     rCommand.lang = languageRecognition;
  112.   };
  113.  
  114.   this.setLanguageSynthesis = function(lang) {
  115.     languageSynthesis = lang;
  116.   };
  117.  
  118.   this.setKeyword = function(kw) {
  119.     keyword = kw;
  120.   };
  121.  
  122.   this.getKeyword = function() {
  123.     return keyword;
  124.   };
  125.  
  126.   this.addCommand = function(command, callback) {
  127.     commands.push(new Command(command, callback));
  128.   };
  129.  
  130.   this.setNoCommandRecognised = function(callback) {
  131.     noCommandRecognisedCallback = callback;
  132.   };
  133.  
  134.   this.speak = function(sentence) {
  135.     var utterance = new SpeechSynthesisUtterance(sentence);
  136.     utterance.rate = 0.85;
  137.     utterance.lang = this.getLanguageSynthesis();
  138.     synth.speak(utterance);
  139.   };
  140.  
  141.   this.run = function() {
  142.     startListeningKeyword();
  143.   };
  144. };
  145.  
  146. var Command = function(t, cb) {
  147.   var keys = t.toLowerCase().split(",");
  148.  
  149.   var callback = cb;
  150.  
  151.   this.tryCommand = function(transcript) {
  152.     transcript = transcript.toLowerCase();
  153.     if (this.testCommand(transcript)) {
  154.       this.invoke(transcript, this.getParameter(transcript));
  155.       return true;
  156.     }
  157.     return false;
  158.   };
  159.  
  160.   this.testCommand = function(transcript) {
  161.     //return speechArray.indexOf(command) != -1
  162.     for (var i = 0; i < keys.length; i++) {
  163.       if (transcript.indexOf(keys[i]) != -1) {
  164.         return true;
  165.       }
  166.     }
  167.     return false;
  168.   };
  169.  
  170.   this.getParameter = function(transcript) {
  171.     for (var i = 0; i < keys.length; i++) {
  172.       var index = transcript.indexOf(keys[i]);
  173.       if (index != -1) {
  174.         return transcript.substr(index + keys[i].length + 1);
  175.       }
  176.     }
  177.     return null;
  178.   };
  179.  
  180.   this.invoke = function(transcript, parameter) {
  181.     callback(transcript, parameter);
  182.   };
  183. };
  184. // }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top