Speech Enabled Python
Last Modified: October 14, 2000
One of the goals of NatLink was to allow Python programmers to write Python scripts which support speech as an input mechanism. To demonstrate how this might work, I have written a sample program called winspch.py which is included in the NatLink distribution.
In my sample program, I speech enabled a Python GUI program which was written using the win32 (PythonWin) package by Mark Hammond (http://www.python.org/windows/win32). In writing the sample program, I tried to separate the speech recognition stuff from the Python win32 stuff to make it obvious what you have to add to an existing program to make it speech-aware.
You can puzzle out the complete technique by studying the example. However, here are some hints.
- You must be inside a Windows GetMessage/DispatchMessage loop in order to receive callbacks when speech is recognized. If you use the win32 package then the DoModal() function call enters a Windows message loop. If you need to force your program to enter a Windows message loop in order to listen for speech then you can call natlink.waitForSpeech() as well.
- You must call natlink.natConnect() before interfacing with Dragon NaturallySpeaking. This connects with NatSpeak and grabs pointers to the necessary COM interface pointers. You must call natlink.natDisconnect() when you are done. This releases the COM interface pointers.
If you forget to call natlink.natConnect(), NatLink will raise an exception when you try to use some other NatLink function. If you forget to call natlink.natDisconnect(), your Python program may hang on exit.
- When using the win32 package, you must pass a "1" parameter to natlink.natConnect(). This enables threading support inside natlink.dll. Normally, Python does not use threads but the win32 package requires threading support so when using win32 with NatLink you must pass "1" to natlink.natConnect().
- I was unsuccessful in getting NatLink to work with Tkinter, another popular Python GUI system. Because I do not have the Tkinter source code, I was not able to diagnose the problem. If anyone else can track this down and let me know, I would appreciate it. Also, I have not tested NatLink with any other Python GUI subsystems (like wxPython).
This web page (http://www.synapseadaptive.com/joel/SpeechEnabledPython.html) was last modified on October 14, 2000.
The contents of this page are (c) Copyright 1998-1999 by Joel Gould. All Rights Reserved.
See Copyright Information for more details.