-
Notifications
You must be signed in to change notification settings - Fork 0
/
speech.js
84 lines (64 loc) · 1.98 KB
/
speech.js
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
var workaround=false;
if (typeof SpeechRecognition === 'undefined')
{
var SpeechRecognition;
var SpeechGrammarList;
var SpeechRecognitionEvent;
if (!(typeof webkitSpeechRecognition === 'undefined'))
{
SpeechRecognition = webkitSpeechRecognition;
SpeechGrammarList = webkitSpeechGrammarList;
SpeechRecognitionEvent = webkitSpeechRecognitionEvent;
workaround = true;
}
}
var recognition;
var speechRecognitionList;
$(document).ready(function(){
if (typeof SpeechRecognition === 'undefined')
{
return
}
$.ajax({ url: "grammar.jsgf", success: loadGrammar, dataType: "text", beforeSend: function ( xhr ) { xhr.overrideMimeType("text/plain; charset=x-user-defined"); } } );
//loadGrammar("grammar.jsgf");
})
function loadGrammar(grammar)
{
recognition = new SpeechRecognition();
speechRecognitionList = new SpeechGrammarList();
//webkit workaround
if (workaround)
{
speechRecognitionList.addFromUri("grammar.jsgf", 1);
}
else
{
speechRecognitionList.addFromString(grammar, 1);
}
recognition.grammars = speechRecognitionList;
recognition.continuous = false;
recognition.lang = 'en-US';
recognition.interimResults = true;
recognition.maxAlternatives = 1;
//console.log(grammar);
recognition.onnomatch = function(event) {
console.log("No match: ", event.results);
}
recognition.onresult = function(event) {
var text = event.results[0][0].transcript;
$("#query").val(text);
console.log('Confidence: ' + event.results[0][0].confidence);
if (event.results[0].isFinal)
{
send();
}
}
recognition.onerror = function(event) {
console.log('Error: ', event.error);
console.log('Error info: ', event.message);
}
}
function speechButton(event)
{
recognition.start();
}