Accessing Dragon NaturallySpeaking (DNS) from Java |
|
This example demonstrates how to access Dragon NaturallySpeaking (DNS) from Java. J-Integra® for COM is a Java interoperability component that bridges Java and Dragon NaturallySpeaking (DNS). It provides bi-directional access of Java objects and COM components.
// Example of interfacing to Dragon NaturallySpeaking (DNS) 4.02
// from pure Java, using J-Integra®.
//
// This Java program sets up a Dragon voice command menu with
// the voice commands, "Top" and "Bottom". For a 15 second period,
// the program displays events received from Dragon NaturallySpeaking
// corresponding to usage of these voice commands.
//
// Authors: Sven Wiener, Ahmad Hashemi-Sakhtsari, Oliver Carr, Feb 2001.
//
// Procedure for making Java wrappers to access COM components:
// ------------------------------------------------------------
// Java wrappers were made using the J-Integra® com2java utility
// on the DNS DLL installed at \speech\dragon\dnstk10.dll
// with com2java selected options of:
// "Lowercase method names"
// "Prompt for names for imported tlbs"
// "Generate deprecated constructors"
// "Ignore interfaces that may conflict"
//
// Of these options, "Prompt for names for imported tlbs" was found
// to be critical for correct generation of Java wrappers.
// In particular when run, com2java prompted for a Java package name for
// the type library, \system32\StdOle2.tlb which was
// referenced by \speech\dragon\dnstk10.dll .
// "StdOle2" was entered into com2java as the Java package name to be
// used for references to types from the imported StdOle2.tlb type library.
//
// Having made Java wrappers for calling COM components within
// \speech\dragon\dnstk10.dll, another set of Java wrappers
// was generated using com2java on \system32\StdOle2.tlb
// and specifying "StdOle2" as the Java package name. The package name
// used here had to have the same case as when entered earlier during
// generation of the dnstk10.dll Java wrappers.
//
// Files needed to compile and run test_drag application :
// test_drag.java (this file)
// jintegra.jar (J-Integra® Java archive file, version 1.3.8)
// drag.jar (Built from com2java-generated *.java wrapper files
// using dnstk10.dll as input)
// stdole2.jar (Built from com2java-generated *.java wrapper files
// using \system32\stdole2.tlb as input)
//
// Compile test_drag using:
// javac -classpath ".;jintegra.jar;drag.jar" test_drag.java
//
// Run test_drag using:
// java -cp ".;jintegra.jar;drag.jar;stdole2.jar" test_drag
//
//
import com.linar.jintegra.*;
import drag.*;
class Listener extends _DDgnVCmdEventsAdapter {
/**
* commandRecognize. Occurs when a command associated with this
* voice-command object is recognized.
*
* @param theEvent The
event
* @exception java.io.IOException If there are communications problems.
* @exception com.linar.jintegra.AutomationException If the remote server
* throws an exception.
*/
public void commandRecognize(_DDgnVCmdEventsCommandRecognizeEvent theEvent)
throws java.io.IOException, com.linar.jintegra.AutomationException {
System.out.println("Recognised command \"" + theEvent.getCommand() + "\"");
eventCount++; // Increment count of voice commands received.
}
public Listener() {
super();
eventCount = 0;
}
public int eventCount; // Counts voice command events received.
} // of class Listener
public class test_drag {
public static void main(String[] args) {
try {
System.out.println(
"Please wait while the Dragon speech engine starts up ...");
// Construct a DgnVoiceCmd object
// (as distinct from the Dragon Voice Command COM object)
// on this machine.
DgnVoiceCmd VCmd = new DgnVoiceCmd();
// Construct a DgnMicBtn object on this machine.
DgnMicBtn MicBtn = new DgnMicBtn();
// Register this application with the Dragon Microphone Button COM object
// and connect to the Dragon NaturallySpeaking speech engine.
// This step takes about five seconds on a Pentium III-866.
MicBtn.register(null);
/////////////////////////////////////////////////
// A two voice-command menu is set up so that Dragon
// will generate command recognize events. The design
// of this voice menu is based on that used in the
// standard "Simple" example in the Dragon SDK 4.0,
// that demonstrates interfacing to Dragon via COM.
// Register this application with the Dragon Voice Command COM object.
VCmd.register("", new Integer(DgnRegisterConstants.dgnregGlobalCM));
// Create a voice menu on the Dragon Voice Command COM object.
// The new menu is temporary and is discarded when the voice-menu object
// is released implicitly, upon going out of scope.
IVMenuAuto myMenu =
VCmd.menuCreate("Simple App", "Simple Menu",
DgnLanguageConstants.dgnlangUSEnglish, "",
CREATEFLAGS.vcmdmc_CREATE_TEMP);
// Add commands, "Top" (command Id = 1)
// and "Bottom" (command Id=2) to the new voice menu.
myMenu.add(1, "Top", "Simple Navigation Commands",
"Sets focus to top box", null, null);
myMenu.add(2, "Bottom", "Simple Navigation Commands",
"Sets focus to bottom box", null, null);
//////////////////////////////////////////////////
// This line is critical. No command recognize events will be sent by the
// Dragon Voice Command COM object unless the new voice menu is
// set active.
myMenu.setActive(true);
// Construct a voice command event listener, customised to display
// details of received command recognize events,
// and register it with the Dragon Voice Command COM object.
Listener myListener = new Listener();
VCmd.add_DDgnVCmdEventsListener(myListener);
// Switch on the Dragon microphone.
MicBtn.setMicState(DgnMicStateConstants.dgnmicOn);
System.out.println("Now accepting voice commands, \"Top\" or \"Bottom\""
+ " for the next 15 seconds");
// This thread now sleeps for 15 seconds while the
// listener receives command recognize events and
// displays their contents.
Thread.sleep(15000);
// Stop listening to voice command events from the
// voice command object.
VCmd.remove_DDgnVCmdEventsListener(myListener);
// Deactivate voice menu.
myMenu.setActive(false);
// Switch off Dragon microphone.
MicBtn.setMicState(DgnMicStateConstants.dgnmicOff);
System.out.print("Total events received by Listener : ");
System.out.println(myListener.eventCount);
// Unregister this application from the Dragon Microphone Button
// and Dragon Voice Command COM objects. Disconnects from the
// Dragon speech engine.
MicBtn.unRegister();
VCmd.unRegister();
// Release any COM objects that have not been garbage collected.
// Otherwise, the 60MB Dragon process stays loaded in memory for some
// period of time after this function has ended.
com.linar.jintegra.Cleaner.releaseAll();
} catch (Exception e) {
e.printStackTrace();
}
} // of main().
} // of class test_drag |