Created
December 5, 2020 18:52
-
-
Save JoeGlines/447358606824aa95db92a66076d6c9a5 to your computer and use it in GitHub Desktop.
Voice Recognition with AutoHotkey
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;~ 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() | |
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") | |
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 | |
;~ 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