I cant seem to get OnRecognition() to trigger..
but first.. thank you to all you guys that have contributed to voice rec and the com lib, and have managed to get this stuff working to begin with...
I've been wrestling with this for days.. and nothing seems to work..
I'm on WinXP using SDK SAPI 5.1 with AHK 1.0.48.5. and the latest COM.ahk (
http://www.autohotkey.com/forum/topic22923.html)
The SAPI examples from the MS SDK all seem to work fine in their C++/VB format, and the mic is working fine (at least in the MS apps it is), but within AHK it is all fubar..
maybe a bad audioinput setting, no mic activation?
this line appears redundant..
Code:
COM_Invoke(plistener, "AudioInput", paudioin ? "+" . paudioin : "+0")
forcing the string "+0" is the only thing that the AudioInput property seems to accept without throwing up an error..
for example this will start up without an error and show the correct description..
Code:
plistener:= COM_CreateObject("SAPI.SpSharedRecognizer")
paudioin:= COM_Invoke(plistener, "AudioInput")
MsgBox % COM_Invoke(paudioin, "GetDescription")
COM_Invoke(plistener,"AudioInput", "+0")
But try this
Code:
plistener:= COM_CreateObject("SAPI.SpSharedRecognizer")
paudioin:= COM_Invoke(plistener, "AudioInput")
MsgBox % COM_Invoke(paudioin, "GetDescription")
COM_Invoke(plistener,"AudioInput", "+" . paudioin)
and you get
Quote:
Function name "AudioInput"
ERROR: member not found (0x80020003)
A little Googling and it looks like whatever we are doing here with COM_Invoke to write this AudioInput property could be getting mangled somehow by the COM.ahk library. bit lost on this one though. It's as if the script is trying to read the value, not write it..
actually how does COM_invoke() know to write to a property and not read it anyway? it looks like in other parts of the code invoke is using "+" . 0 to tell the script to write to properties.. but not so with this audio input code..in fact if it is writing a value, it's a null value.. ahh.. but then the MS SDK docs say..
Quote:
AudioInput Property ....
SpObjectToken
Set: An SpObjectToken object that sets the property. If this parameter is Nothing, the default audio input device will be used.
so .. okay the null value here in paudioin causes a "+" . 0 with no 3rd parameter, so a blank write? and the default mic is activated... Still.. I doubt that if paudioin is ever set that it will ever work... I think this bit of code, will always fail... at least it does for me..
Code:
paudioin ? "+" . paudioin
ok, so I hard code it now with..
COM_Invoke(plistener,"AudioInput", "+0")
but still it's not triggering OnRecognition()
next...
I can get the Text to Speech to work fine from within AHK, but no voice recognition. The ms docs say ..
Quote:
To create an ISpRecoContext for a shared ISpRecognizer, an application need only call COM's CoCreateInstance on the component CLSID_SpSharedRecoContext. In this case, SAPI will set up the audio input stream, setting it to SAPI's default audio input stream.
SpSharedRecoContext???? Nobody is using this anywhere on this forum? Is this a new thing?
And the MS docs are saying it will set up the audioinput for me, if I use SpSharedRecoContext.
so I tried coding a version using spSharedRecoContext...
Code:
COM_Init()
pspeaker := COM_CreateObject("SAPI.SpVoice")
pcontext := COM_CreateObject("SAPI.SpSharedRecoContext")
pgrammar := COM_Invoke(pcontext , "CreateGrammar")
COM_Invoke(pgrammar, "DictationSetState", 0)
prules := COM_Invoke(pgrammar, "Rules")
prulec := COM_Invoke(prules, "Add", "wordsRule", 0x1|0x20)
COM_Invoke(prulec, "Clear")
pstate := COM_Invoke(prulec, "InitialState")
I would bet for most of you, this would work, no need for redundant mic and volume setting, paudio and plistener are all gone, and it just goes direct to an established recoContext with pcontext:=
well at least it doesnt trigger any errors for me, but it doesnt do anything else either.
thought maybe dictationSetState =1 would make a differnece.. but no..
maybe it was the sharedReco? so I tried...
Code:
pcontext := COM_CreateObject("SAPI.SpRecoContext")
Ahh, and now we get errors, that we probably should get with spSharedRecoContext too...
upon createGrammar we get an invalid dispatch object error..
so abort on that idea
I moved on to
Code:
plistener:= COM_CreateObject("SAPI.SpInProcRecognizer")
COM_Invoke(plistener,"AudioInput", "+0")
pcontext := COM_Invoke(plistener, "CreateRecoContext")
again, no errors but also no activation of onRecognition()
So maybe you guys can see what is wrong in here?? coz I'm lost..
Code:
#include c:\program files\autohotkey\COM.ahk
#Persistent
OnExit, CleanUp
COM_Init()
pspeaker := COM_CreateObject( "SAPI.SpVoice")
plistener:= COM_CreateObject( "SAPI.SpInProcRecognizer")
COM_Invoke(plistener, "AudioInput", "+0")
pcontext := COM_Invoke(plistener, "CreateRecoContext")
pgrammar := COM_Invoke(pcontext , "CreateGrammar")
COM_Invoke(pgrammar, "DictationSetState", 0 )
prules := COM_Invoke(pgrammar, "Rules")
prulec := COM_Invoke(prules, "Add", "wordsRule", 0x1|0x20)
COM_Invoke(prulec, "Clear")
pstate := COM_Invoke(prulec, "InitialState")
; Add here the words to be recognized!
COM_Invoke(pstate, "AddWordTransition", "+" . 0, "One")
COM_Invoke(pstate, "AddWordTransition", "+" . 0, "Two")
COM_Invoke(pstate, "AddWordTransition", "+" . 0, "hello")
COM_Invoke(pstate, "AddWordTransition", "+" . 0, "kill")
COM_Invoke(prules, "Commit")
COM_Invoke(pgrammar, "CmdSetRuleState", "wordsRule", 1)
COM_Invoke(prules, "Commit")
pevent := COM_ConnectObject(pcontext,"On")
If (pspeaker && pcontext && pgrammar && prules && prulec && pstate) {
COM_Invoke(pspeaker, "Speak", "Starting Succeeded")
}Else{
COM_Invoke(pspeaker, "Speak", "Starting Failed")
}
Return
OnRecognition(prms, this){
msgBox do anything ???
presult := COM_DispGetParam(prms, 3, 9)
pphrase := COM_Invoke(presult, "PhraseInfo")
sText := COM_Invoke(pphrase, "GetText")
COM_Release(pphrase)
; Add custom operations from here!
if IsLabel(sText){
GoSub,%sText%
}else{
COM_Invoke(pspeaker, "Speak", "You Said" . sText)
}
msgBox %sText% anything ???
}
kill:
CleanUp:
COM_Invoke(pspeaker, "Speak", "Shutting down")
COM_Release(pspeaker)
COM_Release(pevent)
COM_Release(pstate)
COM_Release(prulec)
COM_Release(prules)
COM_Release(pgrammar)
COM_Release(pcontext)
COM_Release(plistener)
COM_Term()
ExitApp
hello:
msgBox do something for goodness sake!!!
return
If it would just talk to me, or pop a message box, or show me that it is triggering OnRecognition.... does this work for anyone on XP...
oh another thing, if I use
plistener:= COM_CreateObject("SAPI.SpSharedRecognizer")
then it sets my mic input volume to 0 every time I start it up... turns off the mic, and I have to turn it back on manually..
please take a look at that code there, someone, and tell me what I'm missing.