/* ----------------------------------------------------------------------------- This source file is part of OpenSpace3D For the latest info, see http://www.openspace3d.com Copyright (c) 2012 I-maginer This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or go to http://www.gnu.org/copyleft/lesser.txt ----------------------------------------------------------------------------- */ struct PlugRec = [ PREC_instance : PInstance, PREC_XMLfile : XMLfile, PREC_ObjReco : ObjRecognition, PREC_input : ObjAudioInput, PREC_EnableDict : I, PREC_bListen : I, PREC_sLang : S ]mkPlugRec;; fun deleteOb(inst, instRec)= setPluginInstanceCbScenePreRender inst nil; _DSRecognition instRec.PREC_ObjReco; set instRec.PREC_ObjReco = nil; _DSAudioInput instRec.PREC_input; set instRec.PREC_input = nil; 0;; fun cbReadText(recognition, instRec, recoText)= let XMLgetMarkByValue instRec.PREC_XMLfile "wordlist" -> eventlistMark in let XMLgetMarkByValueAndParamValueFromMark eventlistMark "word" "value" (strlowercase recoText) -> wordmark in if wordmark == nil then nil else let XMLgetParam wordmark.XMLfather "value" -> event in ( SendPluginEvent instRec.PREC_instance event nil nil; SendPluginEvent instRec.PREC_instance "Word recognized" recoText nil; ); SendPluginEvent instRec.PREC_instance "Text" recoText nil; 0;; fun cbReadPartialText(recognition, instRec, recoText)= // [[S F] r1] SendPluginEvent instRec.PREC_instance "Partial text" recoText nil; 0;; fun createGrammar(instRec)= let XMLgetMarkByValue instRec.PREC_XMLfile "wordlist" -> eventlistMark in let XMLgetMarksByValueFromMark eventlistMark "event" -> levent in while levent != nil do ( let hd levent -> eventmark in let XMLgetParam eventmark "value" -> event in let XMLgetMarksByValueFromMark eventmark "word" -> lword in while lword != nil do ( let hd lword -> wordmark in let XMLgetParam wordmark "value" -> word in _AddRecognitionWord instRec.PREC_ObjReco event word; set lword = tl lword; ); set levent = tl levent; ); 0;; fun cbRecognitionEnd(recognition, instRec)= SendPluginEvent instRec.PREC_instance "End" nil nil; 0;; fun cbRecognitionStart(recognition, instRec)= SendPluginEvent instRec.PREC_instance "Start" nil nil; 0;; fun cbControlPreRender(inst, sessionstr, etime, instRec)= if (instRec.PREC_input == nil) then nil else ( let _AudioGetCaptureBuffer instRec.PREC_input -> buff in _FillRecognitionBuffer instRec.PREC_ObjReco buff; ); 0;; fun enableReco(inst, from, action, param, rep, instRec)= if instRec.PREC_ObjReco != nil then nil else ( if (!instRec.PREC_bListen) then nil else set instRec.PREC_input = _CRAudioInput _channel nil 16000 AUDIO_16BIT_MONO; let strcatn (getPluginDirectory (getInstancePlugin instRec.PREC_instance))::"/models/"::instRec.PREC_sLang::"/README"::nil -> binpath in set instRec.PREC_ObjReco = _CRRecognition2 _channel (_checkpack binpath) instRec.PREC_sLang; if !instRec.PREC_EnableDict then nil else createGrammar instRec; _CBRecognitionText instRec.PREC_ObjReco @cbReadText instRec; _CBRecognitionPartialText instRec.PREC_ObjReco @cbReadPartialText instRec; _CBRecognitionEnd instRec.PREC_ObjReco @cbRecognitionEnd instRec; _CBRecognitionStart instRec.PREC_ObjReco @cbRecognitionStart instRec; if (!instRec.PREC_bListen) then nil else ( _AudioStartCapture instRec.PREC_input; setPluginInstanceCbScenePreRender inst mkfun4 @cbControlPreRender instRec; ); ); 0;; fun disableReco(inst, from, action, param, rep, instRec)= setPluginInstanceCbScenePreRender inst nil; _DSRecognition instRec.PREC_ObjReco; set instRec.PREC_ObjReco = nil; _DSAudioInput instRec.PREC_input; set instRec.PREC_input = nil; 0;; fun enableDict(inst, from, action, param, rep, instRec)= if (instRec.PREC_EnableDict == 1) then nil else ( set instRec.PREC_EnableDict = 1; if instRec.PREC_ObjReco == nil then nil else createGrammar instRec; ); 0;; fun disableDict(inst, from, action, param, rep, instRec)= if (instRec.PREC_EnableDict == 0) then nil else ( set instRec.PREC_EnableDict = 0; if instRec.PREC_ObjReco == nil then nil else _ResetRecognitionWords instRec.PREC_ObjReco; ); 0;; fun setListen(inst, from, action, param, rep, p)= let p -> [instRec state] in ( set instRec.PREC_bListen = state; if (instRec.PREC_ObjReco == nil) then nil else ( if (state) then ( set instRec.PREC_input = _CRAudioInput _channel nil 16000 AUDIO_16BIT_MONO; _AudioStartCapture instRec.PREC_input; setPluginInstanceCbScenePreRender inst mkfun4 @cbControlPreRender instRec; 0; ) else ( setPluginInstanceCbScenePreRender inst nil; _DSAudioInput instRec.PREC_input; set instRec.PREC_input = nil; 0; ); ); ); 0;; fun newOb(inst)= let XMLloadString webtostr (getPluginInstanceParam inst "xmldata") -> xmldata in let atoi (getPluginInstanceParam inst "enableReco") -> initStartReco in let atoi (getPluginInstanceParam inst "enableDict") -> initStartDict in let if initStartReco == nil then 1 else initStartReco -> initStartReco in let if initStartDict == nil then 1 else initStartDict -> initStartDict in let if (!strcmpi currentLanguage "french") then "fr-fr" else "en-us" -> lang in let atoi (getPluginInstanceParam inst "listen") -> listen in let if listen == nil then 1 else listen -> listen in let mkPlugRec [inst xmldata nil nil initStartDict listen lang] -> instRec in ( if !initStartReco then nil else enableReco inst nil nil nil nil instRec; PluginRegisterAction inst "Enable" mkfun6 @enableReco instRec; PluginRegisterAction inst "Disable" mkfun6 @disableReco instRec; PluginRegisterAction inst "Enable dictionnary" mkfun6 @enableDict instRec; PluginRegisterAction inst "Disable dictionnary" mkfun6 @disableDict instRec; PluginRegisterAction inst "Enable" mkfun6 @enableReco instRec; PluginRegisterAction inst "Listen" mkfun6 @setListen [instRec 1]; PluginRegisterAction inst "Deaf" mkfun6 @setListen [instRec 0]; setPluginInstanceCbDel inst mkfun2 @deleteOb instRec; ); 0;; fun IniPlug(file)= PlugRegister @newOb nil; setPluginEditor @dynamicedit; 0;;