summaryrefslogtreecommitdiffstats
path: root/chrome/test/data/speech/web_speech_api_test.html
blob: b6dcd091cdac15c56caeeea927a5956f709cb633 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<html>
  <head>
    <title>speechreco test</title>
    <script>
function cls() {
  document.getElementById('log').innerHTML = '';
}

function log(msg) {
  var d = new Date();
  var t = d.getHours() + ':' +
          d.getMinutes() + ':' +
          d.getSeconds() + '.' +
          d.getMilliseconds();
  var table = document.getElementById('log');
  var row = table.insertRow(-1);
  var cell1 = row.insertCell(-1);
  var time = document.createElement("pre");
  time.innerHTML = t;
  cell1.appendChild(time);
  var cell2 = row.insertCell(-1);
  var message = document.createElement("pre");
  message.innerHTML = msg;
  cell2.appendChild(message);
}

function res2str(results) {
  if (!results || !results.length)
    return "<no items>";
  var s = '';
  for (var i = 0; i < results.length; ++i) {
    s += "&nbsp;" + i + ":{";
    var r = results[i];
    s += typeof(r) + ",";
    if (r.final)
      s += '(final) ';
    s += '[';
    for (var i = 0; i < r.length; ++i) {
      s += r.item(i).transcript + ' (' + r.item(i).confidence + ')';
      if (i != r.length - 1)
        s += ',<br>&nbsp;&nbsp;&nbsp;';
    }
    s += ']';
    s += "}<br>";
  }
  return s;
}

function setup() {
  try {
    window.reco = new webkitSpeechRecognition();
  } catch(e) {
    document.write("The Web Speech API is not supported by this browser.");
    return;
  }

  reco.maxAlternatives = 25;

  // Set default handlers.
  for (var prop in reco) {
    if (!prop.match('^on'))
      continue;
    reco[prop] = function() {
      log('on' + event.type);
    }
  }

  // Set specific handlers.
  reco.onerror = function(e) {
    log('onerror ' + e.error);
  }
  reco.onresult = function(e) {
    try {
      log('onresult ' + res2str(e.results));
    } catch(ex) {
      log('onresult - exception');
    }
  }

  if (reco.onresults != undefined) {
    log('onresults exists');
  }

  log('created reco object');
}

function start() {
  log('start()');
  reco.continuous = document.getElementById('continuous').checked;
  reco.interimResults = document.getElementById('interim').checked;
  log('reco.continuous = ' + reco.continuous);
  log('reco.interimResults = ' + reco.interimResults);
  try {
    reco.start();
  } catch(e) {
    log('exception: ' + e);
  }
}

function stop() {
  log('stop()');
  reco.stop();
}

function abort() {
  log('abort()');
  reco.abort();
}

function startOther() {
  log('startOther()');
  var other = new webkitSpeechRecognition();
  other.continuous = true;

  // Set default handlers.
  for (var prop in other) {
    if (!prop.match('^on'))
      continue;
    other[prop] = function() {
      log('other on' + event.type);
    }
  }

  other.start();
}
    </script>
  </head>
  <body onload="setup()">
    <input type="button" onclick="cls()" value="cls">
    <input type="button" onclick="start()" value="start()">
    <input type="button" onclick="stop()" value="stop()">
    <input type="button" onclick="abort()" value="abort()">
    <input type="button" onclick="startOther()" value="startOther()">
    <input type="checkbox" checked id="continuous">continuous
    <input type="checkbox" checked id="interim">interimResults
    <!-- <pre id="log"></pre> -->
    <table id="log" width="350px" border="0"></table>
  </body>
</html>