Wit.ai Microphone Demo

In this example below you will see how to do a Wit.ai Microphone Demo with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by ianjennings, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright ianjennings ©
  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Wit.ai Microphone Demo</title>
  
  
  
  
  
</head>

<body>

  <link rel="stylesheet" href="https://s3-us-west-2.amazonaws.com/s.cdpn.io/100379/microphone_2.css">  <script src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/100379/microphone.js"></script>

<div id="microphone"></div>
<pre id="result"></pre>
<div id="info"></div>
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/ianjennings/witai-microphone-demo-aAFxn */
 var mic = new Wit.Microphone(document.getElementById("microphone"));
    var info = function (msg) {
      document.getElementById("info").innerHTML = msg;
    };
    mic.onready = function () {
      info("Microphone is ready to record");
    };
    mic.onaudiostart = function () {
      info("Recording started");
    };
    mic.onaudioend = function () {
      info("Recording stopped, processing started");
    };
    mic.onerror = function (err) {
      info("Error: " + err);
    };
    mic.onresult = function (intent, entities) {
      var r = kv("intent", intent);

      for (var k in entities) {
        var e = entities[k];

        if (!(e instanceof Array)) {
          r += kv(k, e.value);
        } else {
          for (var i = 0; i < e.length; i++) {
            r += kv(k, e[i].value);
          }
        }
      }

      document.getElementById("result").innerHTML = r;
    };
    mic.connect("PYKE2SEOG5SO46P2XMUXQOJTA77MT6FH");
    // mic.start();
    // mic.stop();

    function kv (k, v) {
      if (toString.call(v) !== "[object String]") {
        v = JSON.stringify(v);
      }
      return k + "=" + v + "\n";
    }

Comments