SpeechRecognizer

Description

Supported Script Types:

  • Interface Scripts
  • Client Entity Scripts
  • Avatar Scripts
The SpeechRecognizer API provides facilities to recognize voice commands.

Speech recognition is enabled or disabled via the Developer > Scripting > Enable Speech Control API menu item or the SpeechRecognizer.setEnabled method.

Methods

Name Return Value Summary
addCommand None

Adds a voice command to the speech recognizer.

removeCommand None

Removes a voice command from the speech recognizer.

setEnabled None

Enables or disables speech recognition.

Signals

Name Summary
commandRecognized

Triggered when a voice command has been recognized.

enabledUpdated

Triggered when speech recognition is enabled or disabled.

Method Details

(static) addCommand( command )

Adds a voice command to the speech recognizer.

Parameters

Name Type Description
command string

The voice command to recognize.

(static) removeCommand( command )

Removes a voice command from the speech recognizer.

Parameters

Name Type Description
command string

The voice command to stop recognizing.

(static) setEnabled( enabled )

Enables or disables speech recognition.

Parameters

Name Type Description
enabled boolean

true to enable speech recognition, false to disable.

Signal Details

commandRecognized( command )
Returns: Signal

Triggered when a voice command has been recognized.

Parameters

Name Type Description
command string

The voice command recognized.

Example

Turn your avatar upon voice command.

var TURN_LEFT = "turn left";
var TURN_RIGHT = "turn right";
var TURN_RATE = 0.5;
var TURN_DURATION = 1000; // ms
var turnRate = 0;

function getTurnRate() {
    return turnRate;
}

var MAPPING_NAME = "com.highfidelity.controllers.example.speechRecognizer";
var mapping = Controller.newMapping(MAPPING_NAME);

mapping.from(getTurnRate).to(Controller.Actions.Yaw);
Controller.enableMapping(MAPPING_NAME);

function onCommandRecognized(command) {
    print("Speech command: " + command);
    switch (command) {
        case TURN_LEFT:
            turnRate = -TURN_RATE;
            break;
        case TURN_RIGHT:
            turnRate = TURN_RATE;
            break;
    }
    Script.setTimeout(function () {
        turnRate = 0;
    }, TURN_DURATION);
}

SpeechRecognizer.addCommand(TURN_LEFT);
SpeechRecognizer.addCommand(TURN_RIGHT);
SpeechRecognizer.commandRecognized.connect(onCommandRecognized);

Script.scriptEnding.connect(function () {
    Controller.disableMapping(MAPPING_NAME);
    SpeechRecognizer.removeCommand(TURN_LEFT);
    SpeechRecognizer.removeCommand(TURN_RIGHT);
});
enabledUpdated( enabled )
Returns: Signal

Triggered when speech recognition is enabled or disabled.

Parameters

Name Type Description
enabled boolean

true if speech recognition is enabled, false if it is disabled.

Example

Report when speech recognition is enabled or disabled.

SpeechRecognizer.enabledUpdated.connect(function (enabled) {
    print("Speech recognition: " + (enabled ? "enabled" : "disabled"));
});