Activate AHK with voice recongnition

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Guill
Posts: 139
Joined: 09 Jun 2016, 22:00

Activate AHK with voice recongnition

12 Jul 2016, 20:21

I would like to know how you can do that Autohotkey obey a command of spoken text -that is with voice recognition-instead of having to press a key or hotstring.

I suppose we need an expression similar to a hotstring (when I speak this phrase "open notepad", it'll write it, and it'll active the script), but I tried something like that, and it doesn't work.
The microphone is right, of course.

Code: Select all

:*?:open notepad::
Run notepad
Return
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Activate AHK with voice recongnition

12 Jul 2016, 20:31

With windows 10, you could simply have Cortana run them, however that works (I don't use W10). If you need an action on an already running script, you can make use of SendMessage to do pretty much anything you need.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
Guill
Posts: 139
Joined: 09 Jun 2016, 22:00

Re: Activate AHK with voice recongnition

12 Jul 2016, 21:32

Thanks
(I use Windows 8.1)
can you write exemples with SendMessage?
User avatar
Masonjar13
Posts: 1555
Joined: 20 Jul 2014, 10:16
Location: Не Россия
Contact:

Re: Activate AHK with voice recongnition

12 Jul 2016, 22:23

Well, I could if I had something to do with them. I meant, if using Cortana to run a script, you could have that script affect an already running script by using SendMessage/PostMessage.

As far as doing so otherwise, try looking up a third-party software, or maybe someone even ported Cortana (?), to be able to run scripts through voice. A Voice2Text might also work, if you can get it to actually type the keys out, then you could use a hotstring.
OS: Windows 10 Pro | Editor: Notepad++
My Personal Function Library | Old Build - New Build
Elgin
Posts: 124
Joined: 30 Sep 2013, 09:19

Re: Activate AHK with voice recongnition

13 Jul 2016, 07:18

See here for the probably simplest to use code:
https://autohotkey.com/board/topic/9645 ... cognition/

Searching for "autohotkey speech recognition" will give you more results depending on what you need.
Guill
Posts: 139
Joined: 09 Jun 2016, 22:00

Re: Activate AHK with voice recongnition

13 Jul 2016, 07:49

Thanks to both


For Elgin: do you know how it works? I downloaded and run it, but I dont know what another thing I have to do.
Besides, I need that it works with Spanish recognition, because it is my mother language. Do you know if it works in other languages?

Thanks again
Elgin
Posts: 124
Joined: 30 Sep 2013, 09:19

Re: Activate AHK with voice recongnition

13 Jul 2016, 14:51

It's a library. Running it alone does nothing; you need to write code that uses it. Some examples are given in the library comments.

One example would be:

Code: Select all

#Include Speech Recognition.ahk

TrayTip, Speech Recognition, Say something (press Escape to close)

s := new CustomSpeech ;create the custom speech recognizer
s.Recognize(["hola", "que pasa", "me entiendes"])

Esc::ExitApp

class CustomSpeech extends SpeechRecognizer
{
    OnRecognize(Text)
    {
        Msgbox, % Text
    }
}
If you want to use the code with the Windows speech recognition widget to be able to use all the normal speech commands you need to replace the following three lines:

Code: Select all

            this.cListener := ComObjCreate("SAPI.SpInprocRecognizer") ;obtain speech recognizer (ISpeechRecognizer object)
            cAudioInputs := this.cListener.GetAudioInputs() ;obtain list of audio inputs (ISpeechObjectTokens object)
            this.cListener.AudioInput := cAudioInputs.Item(0) ;set audio device to first input
by this line:

Code: Select all

            this.cListener := ComObjCreate("SAPI.SpSharedRecognizer") ;obtain speech recognizer (ISpeechRecognizer object)
Guill
Posts: 139
Joined: 09 Jun 2016, 22:00

Re: Activate AHK with voice recongnition

13 Jul 2016, 16:44

I' affraid I will need more specific information, then. My level is basic in AHK and I dont know anything about programming.

Thank you very much anyway.
User avatar
YoucefHam
Posts: 372
Joined: 24 Aug 2015, 12:56
Location: Algeria
Contact:

Re: Activate AHK with voice recongnition

13 Jul 2016, 19:42

Elgin wrote:It's a library. Running it alone does nothing; you need to write code that uses it. Some examples are given in the library comments.

One example would be:

Code: Select all

#Include Speech Recognition.ahk

TrayTip, Speech Recognition, Say something (press Escape to close)

s := new CustomSpeech ;create the custom speech recognizer
s.Recognize(["hola", "que pasa", "me entiendes"])

Esc::ExitApp

class CustomSpeech extends SpeechRecognizer
{
    OnRecognize(Text)
    {
        Msgbox, % Text
    }
}
If you want to use the code with the Windows speech recognition widget to be able to use all the normal speech commands you need to replace the following three lines:

Code: Select all

            this.cListener := ComObjCreate("SAPI.SpInprocRecognizer") ;obtain speech recognizer (ISpeechRecognizer object)
            cAudioInputs := this.cListener.GetAudioInputs() ;obtain list of audio inputs (ISpeechObjectTokens object)
            this.cListener.AudioInput := cAudioInputs.Item(0) ;set audio device to first input
by this line:

Code: Select all

            this.cListener := ComObjCreate("SAPI.SpSharedRecognizer") ;obtain speech recognizer (ISpeechRecognizer object)

Thanks for sharing

I tried this and it's working perfectly

Code: Select all

#Include Speech Recognition.ahk

TrayTip, Speech Recognition, Say a number between 0 and 9 inclusive

s := new SpeechRecognizer
s.Recognize(["Note","Cal"])
Text := s.Prompt()

TrayTip, Speech Recognition, You said: %Text%
if Text = Cal
	Run, calc.exe
else if Text = Note
	Run, notepad.exe
Sleep, 2000
ExitApp
https://autohotkey.com/board/topic/9645 ... cognition/
:wave: There is always more than one way to solve a problem. ;)
Elgin
Posts: 124
Joined: 30 Sep 2013, 09:19

Re: Activate AHK with voice recongnition

14 Jul 2016, 04:45

Guill,

from your first post I was afraid you are a beginner... You'll need to read up on functions and objects in the AHK documentation and understand the concepts. Speech recognition in AHK involves COM-programming which is not exactly beginners stuff, so be prepared for a steep learning curve.

Also you'll need to ask more specific questions and tell us what exactly you want to do and where you get stuck otherwise it's hard to give you useful advice...

Depending on what you want to do it might be even be simpler for you to use something else than AHK:
There is Vocola http://vocola.net/v3/ which is a scripting system for speech recognition which can do quite a lot; the windows speech recognition itself has a scripting system called Windows Speech Regognition Macros https://www.microsoft.com/en-us/downloa ... a7468=True; and there's a growing heap of free (and shareware) programs that allow you to define speech commands and do something like sending keystrokes on recognition without need for programming skills (but I haven't tried any of them).
Guill
Posts: 139
Joined: 09 Jun 2016, 22:00

Re: Activate AHK with voice recongnition

14 Jul 2016, 17:05

Elgin,
Thank you for your thorough answer. We can try one last attempt.
Windows detects the voice and converts it into text.
In turn, Windows can do that a typed text activates an command by a hotstring.
What we need is that windows detects that the text written by order of voice recognition be as if it had been typed.
Is this possible?

Thank you very much again.
Elgin
Posts: 124
Joined: 30 Sep 2013, 09:19

Re: Activate AHK with voice recongnition

15 Jul 2016, 02:52

You're making it more complicated than it is...no hotstrings involved at all.

I've expanded and commented the example to get you a bit further on the way. Save this code to a new file in the same folder where you saved "Speech Recognition.ahk" and run it.

Code: Select all

#Include Speech Recognition.ahk         ; import the speech library so we can use its functions

TrayTip, Speech Recognition, Say something (press Escape to close)  ; display a welcome message

s := new CustomSpeech ; create the custom speech recognizer object; don't worry about this for now
s.Recognize(["hola", "que pasa", "me entiendes"])   ; <- with this command AHK tells the Windows speech recognition which speech commands you want recognized; you need to edit this to the speech commands you need; make sure you keep your commands in quotes and separated by commas

Esc::ExitApp        ; Exit the application when escape is pressed

class CustomSpeech extends SpeechRecognizer     ; defines a custom speech recognizer class...don't worry about this for now
{
    OnRecognize(Text)   ; <- when the Windows speech recognition recognizes one of your speech commands it calls this function and places the command in the variable Text
    {
        ; inside this function you need to determine which of your commands was recognized and do the corresponding action
        If (Text="hola")    ; <- compare variable Text to your first speech command; if it is the same, execute the code below inside the brackets
        {
            ; edit this to whatever you want done 
            Run, notepad.exe    ; just an example change as needed... opens Notepad
        }
        else
        If (Text="que pasa") ; <- compare variable Text to your second speech command
        {
            ; edit this to whatever you want done 
            SendInput, I heard "que pasa"    ; just an example change as needed... types a message into the notepad you opened by saying "hola"
        }
        else
        If (Text="me entiendes") ; add more if/else statements as necessary...
        {
            ; edit this to whatever you want done 
            SendInput, Sure I understand you.    ; just an example change as needed... types a message into the notepad you opened by saying "hola"
        }
        else
        Msgbox, % Text  ; throw a message box in case you missed a command or made a typo...
    }
}
Guill
Posts: 139
Joined: 09 Jun 2016, 22:00

Re: Activate AHK with voice recongnition

15 Jul 2016, 11:58

Thanks again for your polite patience and your time.
Best wishes
trey ton

Re: Activate AHK with voice recongnition

12 Dec 2017, 05:34

Elgin wrote:You're making it more complicated than it is...no hotstrings involved at all.

I've expanded and commented the example to get you a bit further on the way. Save this code to a new file in the same folder where you saved "Speech Recognition.ahk" and run it.

Code: Select all

#Include Speech Recognition.ahk         ; import the speech library so we can use its functions

TrayTip, Speech Recognition, Say something (press Escape to close)  ; display a welcome message

s := new CustomSpeech ; create the custom speech recognizer object; don't worry about this for now
s.Recognize(["hola", "que pasa", "me entiendes"])   ; <- with this command AHK tells the Windows speech recognition which speech commands you want recognized; you need to edit this to the speech commands you need; make sure you keep your commands in quotes and separated by commas

Esc::ExitApp        ; Exit the application when escape is pressed

class CustomSpeech extends SpeechRecognizer     ; defines a custom speech recognizer class...don't worry about this for now
{
    OnRecognize(Text)   ; <- when the Windows speech recognition recognizes one of your speech commands it calls this function and places the command in the variable Text
    {
        ; inside this function you need to determine which of your commands was recognized and do the corresponding action
        If (Text="hola")    ; <- compare variable Text to your first speech command; if it is the same, execute the code below inside the brackets
        {
            ; edit this to whatever you want done 
            Run, notepad.exe    ; just an example change as needed... opens Notepad
        }
        else
        If (Text="que pasa") ; <- compare variable Text to your second speech command
        {
            ; edit this to whatever you want done 
            SendInput, I heard "que pasa"    ; just an example change as needed... types a message into the notepad you opened by saying "hola"
        }
        else
        If (Text="me entiendes") ; add more if/else statements as necessary...
        {
            ; edit this to whatever you want done 
            SendInput, Sure I understand you.    ; just an example change as needed... types a message into the notepad you opened by saying "hola"
        }
        else
        Msgbox, % Text  ; throw a message box in case you missed a command or made a typo...
    }
}
can you please help and tell me how I can reference a variable in this list?

mysaying = tourist sale
s.Recognize([%mysaying%, "que pasa", "me entiendes"])

that did not work
I tried
mysaying = tourist sale
s.Recognize([mysaying, "que pasa", "me entiendes"])

also did not work
Elgin
Posts: 124
Joined: 30 Sep 2013, 09:19

Re: Activate AHK with voice recongnition

12 Dec 2017, 06:17

Code: Select all

mysaying = tourist sale	; or: mysaying := "tourist sale"
s.Recognize([mysaying, "que pasa", "me entiendes"])
works as expected for me and displays a message box "tourist sale".
trey ton

Re: Activate AHK with voice recongnition

12 Dec 2017, 08:18

Elgin wrote:

Code: Select all

mysaying = tourist sale	; or: mysaying := "tourist sale"
s.Recognize([mysaying, "que pasa", "me entiendes"])
works as expected for me and displays a message box "tourist sale".
it works for me too now, appreciate that

can you give me an example on how to stop the listening?

here is from the documentation
"### Recognizer.Recognize(Values = True)

Set the values that can be recognized by the recognizer.

If `Values` is an array of strings, the array is interpreted as a list of possibile phrases to recognize. Phrases not in the array will not be recognized. This provides a relatively high degree of recognition accuracy compared to dictation mode.

If `Values` is otherwise truthy, dictation mode is enabled, which means that the speech recognizer will attempt to recognize any phrases spoken.

If `Values` is falsy, the speech recognizer will be disabled and will stop listening if currently doing so."

Can you tell me what it means by falsy? I am assuming false, however, what do I write?
I tried
Recognizer.Recognize(Values = False)

I knew its nonsense for me to do that but im trying
Elgin
Posts: 124
Joined: 30 Sep 2013, 09:19

Re: Activate AHK with voice recongnition

12 Dec 2017, 09:31

Code: Select all

s.Recognize(False)
or

Code: Select all

s.Listen(False)
should do it.
trey ton

Re: Activate AHK with voice recongnition

12 Dec 2017, 10:19

Elgin wrote:

Code: Select all

s.Recognize(False)
or

Code: Select all

s.Listen(False)
should do it.
I tried both but it does not work, :-(((
trey ton

Re: Activate AHK with voice recongnition

12 Dec 2017, 11:48

Elgin wrote:

Code: Select all

s.Recognize(False)
or

Code: Select all

s.Listen(False)
should do it.
your code s.Recognize(False) worked it was my mistake
JKnight_xbt33
Posts: 135
Joined: 18 Sep 2019, 02:06

Re: Activate AHK with voice recongnition

24 Dec 2020, 10:49

@Elgin

I've been looking at this thread as I am trying to do something similar.

Where is this speech recognition library located with all its functions?

Is it somewhere on the forums?

regards
J

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 197 guests