Keyword Spotting
The simplest voice-controlled applications map exact phrases to page actions. This is what the KeywordSpotter is for: it triggers different callbacks when a voice command EXACTLY matches one of the commands that you specify. This KeywordSpotter will fire an action for “turn on” and “turn off”:
// command handlers map voice commands to the actions they should trigger
const commandHandlers = {
"turn on": () => { // this function triggers when the user says "turn on"...
document.querySelector("body").style.backgroundColor = "white"
},
"turn off": () => { // ... and this one triggers when they say "turn off"
document.querySelector("body").style.backgroundColor = "black"
}
}
const keywordSpotter = Moonshine.KeywordSpotter(commandHandlers)
Then we simply pass in the KeywordSpotter when we create a transcriber:
var transcriber = new Moonshine.MicrophoneTranscriber(
"model/tiny",
keywordSpotter,
true // VAD enabled
)
transcriber.start();
Note that we enabled voice activity detection—this is highly recommended when you are using voice control.