function Grammar(grammar_name, path, obj_path, min_score, confirm_score, halt) {
this.grammar_name = grammar_name;
this.path = path;
this.min_score = min_score;
this.confirm_score = confirm_score;
this.halt = halt;
this.obj_path = obj_path;
if (!this.min_score) {
this.min_score = 1;
}
if (!this.confirm_score) {
this.confirm_score = 400;
}
}
function SpeechDetect(session, mod, ip) {
this.ip = ip;
this.session = session;
this.mod = mod;
this.grammar_name = undefined;
this.grammar_hash = new Array();
this.grammar_name = false;
this.audio_base = "";
this.audio_ext = ".wav";
this.tts_eng = false;
this.tts_voice = false;
this.AutoUnload = false;
this.debug = false;
this.setTTS = function (tts_eng, tts_voice) {
this.tts_eng = tts_eng;
this.tts_voice = tts_voice;
}
this.setAudioBase = function (audio_base) {
this.audio_base = audio_base;
}
this.setAudioExt= function (audio_ext) {
this.audio_ext = audio_ext;
}
this.addGrammar = function(grammar_object) {
this.grammar_hash[grammar_object.grammar_name] = grammar_object;
}
this.streamFile = function(str) {
var rv;
if (!str) {
console_log("error", "No file specified!\n");
return;
}
files = str.split(",");
for( x = 0; x < files.length; x++) {
if (!files[x] || files[x] == "noop") {
continue;
}
this.session.streamFile(this.audio_base + files[x] + this.audio_ext);
}
}
this.speak = function(str) {
return this.session.speak(this.tts_eng, this.tts_voice, str);
}
this.setGrammar = function (grammar_name) {
var grammar_object = this.grammar_hash[grammar_name];
if (!grammar_object) {
console_log("error", "Missing Grammar!\n");
return false;
}
if (this.grammar_name) {
if (this.AutoUnload) {
console_log("debug", "Unloading grammar " + this.grammar_name + "\n");
this.session.execute("detect_speech", "nogrammar " + this.grammar_name);
}
if (grammar_object.path) {
this.session.execute("detect_speech", "grammar " + grammar_name + " " + grammar_object.path);
} else {
this.session.execute("detect_speech", "grammar " + grammar_name);
}
} else {
this.session.execute("detect_speech", this.mod + " " + grammar_name + " " + grammar_object.path + " " + this.ip);
}
this.grammar_name = grammar_name;
}
this.pause = function() {
this.session.execute("detect_speech", "pause");
}
this.resume = function() {
this.session.execute("detect_speech", "resume");
}
this.stop = function() {
this.session.execute("detect_speech", "stop");
}
this.onInput = function(type, inputEvent, _this) {
if (type == "event") {
var speech_type = inputEvent.getHeader("Speech-Type");
var rv = new Array();
if (!_this.grammar_name) {
console_log("error", "No Grammar name!\n");
_this.session.hangup();
return;
}
var grammar_object = _this.grammar_hash[_this.grammar_name];
if (!grammar_object) {
console_log("error", "Can't find grammar for " + _this.grammar_name + "\n");
_this.session.hangup();
return;
}
if (speech_type == "begin-speaking") {
if (grammar_object.halt) {
return;
}
} else {
var body = inputEvent.getBody();
var interp = new XML(body);
_this.lastDetect = body;
if (_this.debug) {
console_log("debug", "----XML:\n" + body);
console_log("debug", "----Heard [" + interp.input + "]\n");
console_log("debug", "----Hit score " + interp.@score + "\n");
}
if (interp.@score < grammar_object.min_score) {
delete interp;
rv.push("_no_idea_");
return rv;
} else {
if (interp.@score < grammar_object.confirm_score) {
rv.push("_confirm_");
}
eval("xo = interp." + grammar_object.obj_path + ";");
for (x = 0; x < xo.length(); x++) {
rv.push(xo[x]);
}
delete interp;
return rv;
}
}
}
}
}
function SpeechObtainer(asr, req, wait_time) {
this.items = new Array();
this.collected_items = new Array();
this.index = 0;
this.collected_index = 0;
this.req = req;
this.tts_eng = undefined;
this.tts_voice = false;
this.asr = asr;
this.top_sound = false;
this.add_sound = false;
this.dup_sound = false;
this.bad_sound = false;
this.needConfirm = false;
this.grammar_name = false;
this.audio_base = asr.audio_base;
this.audio_ext = asr.audio_ext;
this.tts_eng = asr.tts_eng;
this.tts_voice = asr.tts_voice;
this.debug = asr.debug;
if (!req) {
req = 1;
}
if (!wait_time) {
wait_time = 5000;
}
this.waitTime = wait_time + 0;
this.setTTS = function (tts_eng, tts_voice) {
this.tts_eng = tts_eng;
this.tts_voice = tts_voice;
}
this.setAudioBase = function (audio_base) {
this.audio_base = audio_base;
}
this.setAudioExt= function (audio_ext) {
this.audio_ext = audio_ext;
}
this.setGrammar = function (grammar_name, path, obj_path, min_score, confirm_score, halt) {
var grammar_object = new Grammar(grammar_name, path, obj_path, min_score, confirm_score, halt);
this.asr.addGrammar(grammar_object);
this.grammar_name = grammar_name;
}
this.setTopSound = function (top_sound) {
this.top_sound = top_sound;
}
this.setBadSound = function (bad_sound) {
this.bad_sound = bad_sound;
}
this.setDupSound = function (dup_sound) {
this.dup_sound = dup_sound;
}
this.setAddSound = function (add_sound) {
this.add_sound = add_sound;
}
this.addItem = function(item) {
ia = item.split(",");
var x;
for (x = 0; x < ia.length; x++) {
this.items[this.index++] = ia[x];
}
}
this.addRegEx = function(item) {
this.items[this.index++] = item;
}
this.reset = function() {
this.collected_index = 0;
delete this.collected_items;
this.collected_items = new Array();
}
this.streamFile = function(str) {
var rv;
if (!str) {
console_log("error", "No file specified!\n");
return;
}
files = str.split(",");
for( x = 0; x < files.length; x++) {
if (!files[x] || files[x] == "noop") {
continue;
}
rv = this.asr.session.streamFile(this.audio_base + files[x] + this.audio_ext , "", this.asr.onInput, this.asr);
if (rv) {
break;
}
}
return rv;
}
this.speak = function(str) {
return this.asr.session.speak(this.tts_eng, this.tts_voice, str, this.asr.onInput, this.asr);
}
this.react = function(say_str, play_str) {
var rv;
this.asr.resume();
if (this.tts_eng && this.tts_voice) {
rv = this.speak(say_str);
} else {
rv = this.streamFile(play_str);
}
if (!rv) {
rv = this.asr.session.collectInput(this.asr.onInput, this.asr, 500);
}
return rv;
}
this.run = function() {
var rv;
var hit;
var dup;
if (this.collected_index) {
this.reset();
}
if (!this.grammar_name) {
console_log("error", "No Grammar name!\n");
this.session.hangup();
return false;
}
this.asr.setGrammar(this.grammar_name);
while(this.asr.session.ready() && this.collected_index < this.req) {
var x;
this.needConfirm = false;
if (!rv) {
rv = this.react(this.top_sound, this.top_sound);
}
if (!rv) {
this.asr.resume();
rv = this.asr.session.collectInput(this.asr.onInput, this.asr, this.waitTime);
}
hit = false;
if (rv) {
for (y = 0; y < rv.length; y++) {
if (rv[y] == "_confirm_") {
this.needConfirm = true;
if (this.debug) {
console_log("debug", "----We need to confirm this one\n");
}
continue;
}
for(x = 0 ; x < this.index; x++) {
if (this.debug) {
console_log("debug", "----Testing " + rv[y] + " =~ [" + this.items[x] + "]\n");
}
var re = new RegExp(this.items[x]);
match = re.exec(rv[y]);
if (match) {
for (i = 0; i < match.length; i++) {
dup = false;
for(z = 0; z < this.collected_items.length; z++) {
if (this.collected_items[z] == match[i]) {
dup = true;
break;
}
}
if (dup) {
if (this.dup_sound) {
rv = this.react(this.dup_sound + " " + match[i], this.dup_sound + "," + match[i]);
}
} else {
if (this.debug) {
console_log("debug", "----Adding " + match[i] + "\n");
}
this.collected_items[this.collected_index++] = match[i];
hit = true;
if (this.add_sound) {
rv = this.react(this.add_sound + " " + match[i], this.add_sound + "," + match[i]);
}
}
}
}
}
}
}
if (!rv) {
rv = this.asr.session.collectInput(this.asr.onInput, this.asr, 500);
}
if (!rv && !hit && !dup) {
rv = this.react(this.bad_sound, this.bad_sound);
}
}
return this.collected_items;
}
}