Tag Archive for 'OSX'

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:

var nsSpeechRecognizerBridge:NSSpeechRecognizerBridge = new NSSpeechRecognizerBridge();

After that, you add a list of valid commands:

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

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

nsSpeechRecognizerBridge.addEventListener(CommandRecognizedEvent.COMMAND_RECOGNIZED, commandRecognizedHandler);

And you start the recognizer:

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:

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!

Access the mac sudden motion sensor with an AIR native extension

AIR on mobile enables you to access the accelerometer of the mobile device. But what about the motion sensor in your macbook / macbook pro computer on the desktop? All macbooks come with a “sudden motion sensor”, which shuts down the hard disk when the laptop moves too much.

I wrote an AIR native extension, to access this sensor information in AIR on OSX. I tried to mimic the Accelerometer API as much as possible, and make it easy to use in your AIR application.

You can check if the SuddenMotionSensor is supported on your mac, set the update interval and listen for accelerometer events:

if(SuddenMotionSensor.isSupported)
{
	suddenMotionSensor = new SuddenMotionSensor();
	suddenMotionSensor.setRequestedUpdateInterval(50);
	suddenMotionSensor.addEventListener(AccelerometerEvent.UPDATE, accelerometerUpdateHandler, false, 0, true);
}

You can then access the accelerometer info in the event handler:

private function accelerometerUpdateHandler(event:AccelerometerEvent):void
{
	trace(event.accelerationX, event.accelerationY, event.accelerationZ);
}

You can download the sources & demos on github: https://github.com/wouterverweirder/AIR-Sudden-Motion-Sensor-Extension. Happy coding!

including dylib in air native extension for OSX

I’ve been struggling a bit to create a framework file to use in an air native extension on OSX. The issue was that I was using 3rd party dylib libraries (intel building blocks), which aren’t normally installed on somebody’s computer. The framework compiled and everything, but for some reason the application crashed when using the native extension.

When air loaded the native extension, it could no longer find the necessary dylib files. Solution was:

- add a copy files phase to your build phases, which should copy your dylib file to the Resources directory of your framework
- after compilation, open up a terminal window and navigate to your build folder. Use the otool -L command to get a listing of the linked libraries:

otool -L HelloThreadsAndUSB.framework/HelloThreadsAndUSB

Output is something like this:

HelloThreadsAndUSB:
	/Library/Frameworks/HelloThreadsAndUSB.framework/Versions/A/HelloThreadsAndUSB (compatibility version 1.0.0, current version 1.0.0)
	@rpath/Adobe AIR.framework/Versions/1.0/Adobe AIR (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 15.0.0)
	libtbb.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.9.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.11)

In my case, the libtbb.dylib file could not be found when the application launched. You need to change the lookup path with the install_name_tool command:

install_name_tool -change libtbb.dylib @loader_path/Resources/libtbb.dylib HelloThreadsAndUSB.framework/HelloThreadsAndUSB

When you type in the otool command again, you should see the adjusted path:

HelloThreadsAndUSB:
	/Library/Frameworks/HelloThreadsAndUSB.framework/Versions/A/HelloThreadsAndUSB (compatibility version 1.0.0, current version 1.0.0)
	@rpath/Adobe AIR.framework/Versions/1.0/Adobe AIR (compatibility version 1.0.0, current version 1.0.0)
	/System/Library/Frameworks/Cocoa.framework/Versions/A/Cocoa (compatibility version 1.0.0, current version 15.0.0)
	@loader_path/Resources/libtbb.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.9.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.11)

Now you should be able to use this framework in your air native extension :-)

(Note: I tried the other options @executable_path and @rpath, but those didn’t do the trick)