Skip to content

Instantly share code, notes, and snippets.

@JoeGlines
Last active April 26, 2021 15:38
Show Gist options
  • Save JoeGlines/dd36a8e4fe77a35eed0ecc67ea074b49 to your computer and use it in GitHub Desktop.
Save JoeGlines/dd36a8e4fe77a35eed0ecc67ea074b49 to your computer and use it in GitHub Desktop.
;*******************************************************
; Want a clear path for learning AutoHotkey; Take a look at our AutoHotkey Udemy courses. They're structured in a way to make learning AHK EASY
; Right now you can get a coupon code here: https://the-Automator.com/Learn
;*******************************************************
;~ 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.
;~ https://answers.microsoft.com/en-us/windows/forum/all/what-languages-does-microsoft-speech-recognition/cb5eaf9d-7391-4ab5-8ce9-f1e44096c853?auth=1
#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:={A_ComputerName " Mute":"ComputerName_Mute"
,A_ComputerName " lower volume":"ComputerName_TurnVolumeDown"
,A_ComputerName " Turn volume down":"ComputerName_TurnVolumeDown"
,A_ComputerName " Turn down volume":"ComputerName_TurnVolumeDown"
,A_ComputerName " Increase volume up":"ComputerName_TurnVolumeUp"
,A_ComputerName " Turn volume up":"ComputerName_TurnVolumeUp"
,A_ComputerName " Turn up volume":"ComputerName_TurnVolumeUp"}
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***********************************
ComputerName_TurnVolumeUp:
Send {Volume_Up}
return
ComputerName_TurnVolumeDown:
Send {Volume_Down}
return
ComputerName_Mute:
SendEvent {Volume_Mute}
Return
ComputerName_Suspend:
MsgBox here
;~ DllCall("PowrProf\SetSuspendState","int",0,"int",1,"int",1)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment