Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Chrome on Android #130

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 39 additions & 16 deletions annyang.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
var debugState = false;
var debugStyle = 'font-weight: bold; color: #00f;';
var pauseListening = false;
var combinedTranscript = '';
var combinedTranscriptTimeoutId;

// The command matching code is a modified version of Backbone.Router by Jeremy Ashkenas, under the MIT license.
var optionalParam = /\s*\((.*?)\)\s*/g;
Expand Down Expand Up @@ -158,6 +160,14 @@
};

recognition.onend = function() {
if (combinedTranscriptTimeoutId) {
if (debugState) {
root.console.log('Ignoring combined transcript: %c', combinedTranscript, debugStyle);
}
clearTimeout(combinedTranscriptTimeoutId);
combinedTranscriptTimeoutId = null;
combinedTranscript = '';
}
invokeCallbacks(callbacks.end);
// annyang will auto restart if it is closed automatically and not by user action.
if (autoRestart) {
Expand All @@ -182,7 +192,9 @@
// Map the results to an array
var SpeechRecognitionResult = event.results[event.resultIndex];
var results = [];
for (var k = 0; k<SpeechRecognitionResult.length; k++) {
// Enforce maxAlternatives (not respected by Chrome on Android)
var K = Math.min(SpeechRecognitionResult.length, recognition.maxAlternatives);
for (var k = 0; k < K; k++) {
results[k] = SpeechRecognitionResult[k].transcript;
}

Expand All @@ -192,25 +204,36 @@
for (var i = 0; i<results.length; i++) {
// the text recognized
commandText = results[i].trim();
if (debugState) {
root.console.log('Speech recognized: %c'+commandText, debugStyle);
if (commandText == combinedTranscript) {
// If commandText is the concatenation of all commandTexts seen since the last time
// start() was called, and onend() gets called within the next 200ms, this is an
// unexpected extra result returned by Chrome on Android, so ignore it.
combinedTranscriptTimeoutId = setTimeout(onValidResult, 200);
return false;
}
return onValidResult();
function onValidResult() {
if (debugState) {
root.console.log('Speech recognized: %c'+commandText, debugStyle);
}

// try and match recognized text to one of the commands on the list
for (var j = 0, l = commandsList.length; j < l; j++) {
var result = commandsList[j].command.exec(commandText);
if (result) {
var parameters = result.slice(1);
if (debugState) {
root.console.log('command matched: %c'+commandsList[j].originalPhrase, debugStyle);
if (parameters.length) {
root.console.log('with parameters', parameters);
// try and match recognized text to one of the commands on the list
for (var j = 0, l = commandsList.length; j < l; j++) {
var result = commandsList[j].command.exec(commandText);
if (result) {
combinedTranscript += combinedTranscript ? " " + commandText : commandText;
var parameters = result.slice(1);
if (debugState) {
root.console.log('command matched: %c'+commandsList[j].originalPhrase, debugStyle);
if (parameters.length) {
root.console.log('with parameters', parameters);
}
}
// execute the matched command
commandsList[j].callback.apply(this, parameters);
invokeCallbacks(callbacks.resultMatch, commandText, commandsList[j].originalPhrase, results);
return true;
}
// execute the matched command
commandsList[j].callback.apply(this, parameters);
invokeCallbacks(callbacks.resultMatch, commandText, commandsList[j].originalPhrase, results);
return true;
}
}
}
Expand Down