Skip to content

Instantly share code, notes, and snippets.

@JoeGlines
Created December 7, 2020 11:59
Show Gist options
  • Save JoeGlines/ac75cca907e2188a0a7ba8055c7879ba to your computer and use it in GitHub Desktop.
Save JoeGlines/ac75cca907e2188a0a7ba8055c7879ba to your computer and use it in GitHub Desktop.
;~ https://www.autohotkey.com/boards/viewtopic.php?t=35737 by scriptor2016 ; For voice recognition to work you need Microsoft SAPI installed in your PC, some versions of Windows don't support voice recognition though.
; You may also need to train voice recognition in Windows so that it will understand your voice.
#Persistent
#SingleInstance, Force
global pspeaker := ComObjCreate("SAPI.SpVoice") ;plistener := ComObjCreate("SAPI.SpSharedRecognizer")
plistener:= ComObjCreate("SAPI.SpInprocRecognizer") ; For not showing Windows Voice Recognition widget.
paudioinputs := plistener.GetAudioInputs() ; For not showing Windows Voice Recognition widget.
plistener.AudioInput := paudioinputs.Item(0) ; For not showing Windows Voice Recognition widget.
ObjRelease(paudioinputs) ; Release object from memory, it is not needed anymore.
pcontext := plistener.CreateRecoContext() ;create event listener
pgrammar := pcontext.CreateGrammar()
pgrammar.DictationSetState(0)
prules := pgrammar.Rules()
prulec := prules.Add("wordsRule", 0x1|0x20)
prulec.Clear()
pstate := prulec.InitialState()
;Object of Text responses and their Labels to jump to when detected
global responses:={"run Notepad":"RunNotepad"
,"Turn volume down":"TurnVolumeDown"
,"Turn volume up":"TurnVolumeUp"
,"Turn Screen Off":"TurnScreenOff"
,"Turn Screen On":"TurnscreenOn"
,"Launch the Automator":"lta"}
for Text, v in Responses ;Need to add each text to the pstate object and watch for them
pstate.AddWordTransition( ComObjParameter(13,0),Text) ; ComObjParemeter(13,0) is value Null for AHK_L
prules.Commit()
pgrammar.CmdSetRuleState("wordsRule",1)
prules.Commit()
ComObjConnect(pcontext, "On") ;Event
If (pspeaker && plistener && pcontext && pgrammar && prules && prulec && pstate){
SplashTextOn,300,50,,Voice recognition initialisation succeeded
}Else {
MsgBox, Sorry, voice recognition initialisation FAILED ; pspeaker.speak("Starting voice recognition initialisation failed")
}
sleep, 2000
SplashTextOff
return
;********************On recognition function***********************************
OnRecognition(StreamNum,StreamPos,RecogType,Result ;
sText:= Result.PhraseInfo().GetText() ; Grab the text we just spoke and go to that subroutine
MsgBox hi
;~ pspeaker.Speak("You said " sText) ;~ MsgBox, Command is %sText%
if (Responses[sText]) ;If text is found as a key in the object then...
gosub % Responses[sText] ;jump to the gosub
ObjRelease(sText)
}
;********************Voice command labels***********************************
TurnVolumeUp:
Send {Volume_Up}
return
TurnVolumeDown:
Send {Volume_Down}
return
TurnScreenOff:
SendMessage, 0x112, 0xF170, 2,, Program Manager ; Use 2 to turn the monitors off.
return
TurnscreenOn:
SendMessage, 0x112, 0xF170, -1,, Program Manager ; Use -1 to turn the monitor on.
return
lta:
run https://the-Automator.com
return
RunNotepad:
Run Notepad
Return
^Escape::ExitApp ;Control Escape exits the program
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment