evilC / HotVoice

Adds Speech Recognition support to AutoHotkey, via a C# DLL
MIT License
61 stars 9 forks source link

Understanding Grammar and Choices #7

Open Laanan opened 1 year ago

Laanan commented 1 year ago

Hi there,

I am tinkering around with some initial scripting with Hot Voice and want to make sure I am understanding how it works. First script is to set a voice controlled volume up and volume down command, and I am using the Choices script as a template...I am aiming for the user to be able to say "Volume Up" to raise the volume and "Volume Down" to lower it. Later I am to add things like "Open Sesame" to open one app and "Open Banana" to open another.

It looks like grammar pieces are added to an array, and then choices are appended to that array. Does Hot Voice take the first input as item [0], and then wait for any further inputs [1,2,3,n], or do I need to make a function with conditionals that takes the inputs and checks them/parses them, like:

if "volume" --> if "up" --> do this; else if "down" --> do that

or, a bit closer to the actual syntax:

if (grammar[0] == "Volume") { if (grammar[1] == "Up") { SetSound, +5 } else if (grammar[1] == "Down") { SetSound, -5 } else { break } }

Something like that?

evilC commented 1 year ago

Something like

#SingleInstance force
#Persistent ; You will need this if your script creates no hotkeys or has no GUI

; Load the HotVoice Library
#include Lib\HotVoice.ahk

; Create a new HotVoice class
hv := new HotVoice()

; Initialize HotVoice and tell it what ID Recognizer to use
hv.Initialize(0)

; Create a new Grammar
testGrammar := hv.NewGrammar()
testGrammar.AppendString("Volume")

; Create a new Choices object with up / down choices
upDownChoices := hv.NewChoices("up, down")

; Add the choices to the Grammar
volumeGrammar.AppendChoices(upDownChoices)

; Load the Grammar
hv.LoadGrammar(volumeGrammar, "Volume", Func("VolumeFunc"))

hv.StartRecognizer()

return

VolumeFunc(grammarName, words){
    if (words[2] == "up"){
        SetSound, +5
    } else if (words[2] == "down) {
        SetSound, -5
    }
}