Speech Recognition on Mac OS X in Java

When I got my first Mac, I discovered the voice-controlled Chess app. Well, sort of voice-controlled. “Pawn e2 to e4”, I would say, and my bishop would move. I don’t even enjoy chess but I was in Mac-awe, and showed anyone who would look how amazing it was that I could play voice-controlled chess.

Due to a recent e-mail conversation I got curious as to what it takes to add voice control to a Mac OS X app. I played around this evening for fun and was surprised how simple it is. My aim was to create an app that listens to basic poker commands, such as “Call”, “Fold”, and “Raise”.

With help from Google, I managed to do it in Java, via the Rococoa bridge between Cocoa and Java. Here’s pretty much the entire program, once the Rococoa boiler-plate code is removed:

   
private static void setUpSpeechRecognition() {
NSSpeechRecognizer recog = NSSpeechRecognizer.CLASS.alloc().init();
recog.setDelegate(new NSSpeechRecognizer.NSSpeechRecognizerDelegate() {
public void speechRecognizer_didRecognizeCommand(
NSSpeechRecognizer sender, NSString command) {
System.out.println("You said " + command);
}
});
NSArray commands = NSArray.CLASS.arrayWithObjects(
NSString.stringWithString("Check"),
NSString.stringWithString("Raise"),
NSString.stringWithString("Call"));
recog.setCommands(commands);
recog.setListensInForegroundOnly(false);
recog.setDisplayedCommandsTitle("Poker Copilot");
recog.startListening();
}

Now I too can have an app that responds haphazardly to voice control. Here’s some evidence:

Screen shot 2009-12-17 at 9.46.17 PM.png