Wouter Verweirder

My Personal Blog

AIR Native Extension for Simple Speech Recognition on OSX

Today I played around a bit with speech recognition on OSX. One of the things I did, was create a simple bridge between Adobe AIR and the NSSpeechRecognizer API on OSX. This api allows you to set a predefined list of commands, and listen for those spoken commands.

The API is quite simple, and so is the native extension api. After including the ANE file, you’ll create an instance of the bridge:

1
var nsSpeechRecognizerBridge:NSSpeechRecognizerBridge = new NSSpeechRecognizerBridge();

After that, you add a list of valid commands:

1
2
3
4
nsSpeechRecognizerBridge.setCommands(Vector.<String>([
    "square",
    "circle",
]));

You add an event handler, which is triggered when a command is recognized:

1
nsSpeechRecognizerBridge.addEventListener(CommandRecognizedEvent.COMMAND_RECOGNIZED, commandRecognizedHandler);

And you start the recognizer:

1
nsSpeechRecognizerBridge.startListening();

This will open the speech recognition widget of OSX, with your AIR application. The event handler is triggered when the bridge recognizes one of the commands. The CommandRecognizedEvent object, will contain the command that was recognized:

1
2
3
4
protected function commandRecognizedHandler(event:CommandRecognizedEvent):void
{
    trace("command recognized: " + event.command);
}

Note that this built-in speech recognition engine is quite sensitive to background noise, and only recognizes US-English spoken words.

Everything (as3 source, native source, demo & ane) is on github for your coding pleasure. Enjoy!

Comments