speech-dispatcher: Python API

 
 4.2 Python API
 ==============
 
 There is a full Python API available in 'src/api/python/speechd/' in the
 source tree.  Please see the Python docstrings for full reference about
 the available objects and methods.
 
    Simple Python client:
      import speechd
      client = speechd.SSIPClient('test')
      client.set_output_module('festival')
      client.set_language('en-US')
      client.set_punctuation(speechd.PunctuationMode.SOME)
      client.speak("Hello World!")
      client.close()
 
    The Python API respects the environment variables SPEECHD_ADDRESS it
 the communication address is not specified explicitly (see 'SSIPClient'
 constructor arguments).
 
    Implementation of callbacks within the Python API tries to hide the
 low level details of SSIP callback handling and provide a convenient
 Pythonic interface.  You just pass a callable object (function) to the
 'speak()' method and this function will be called whenever an event
 occurs for the corresponding message.
 
    Callback example:
      import speechd, time
      called = []
      client = speechd.SSIPClient('callback-test')
      client.speak("Hi!", callback=lambda cb_type: called.append(cb_type))
      time.sleep(2) # Wait for the events to happen.
      print "Called callbacks:", called
      client.close()
 
    Real-world callback functions will most often need some sort of
 context information to be able to distinguish for which message the
 callback was called.  This can be simply done in Python.  The following
 example uses the actual message text as the context information within
 the callback function.
 
    Callback context example:
      import speechd, time
 
      class CallbackExample(object):
          def __init__(self):
              self._client = speechd.SSIPClient('callback-test')
 
          def speak(self, text):
              def callback(callback_type):
                  if callback_type == speechd.CallbackType.BEGIN:
                      print "Speech started:", text
                  elif callback_type == speechd.CallbackType.END:
                      print "Speech completed:", text
                  elif callback_type == speechd.CallbackType.CANCEL:
                      print "Speech interupted:", text
              self._client.speak(text, callback=callback,
                                 event_types=(speechd.CallbackType.BEGIN,
                                              speechd.CallbackType.CANCEL,
                                              speechd.CallbackType.END))
 
          def go(self):
              self.speak("Hi!")
              self.speak("How are you?")
              time.sleep(4) # Wait for the events to happen.
              self._client.close()
 
      CallbackExample().go()
 
    _Important notice:_ The callback is called in Speech Dispatcher
 listener thread.  No subsequent Speech Dispatcher interaction is allowed
 from within the callback invocation.  If you need to do something more
 complicated, do it in another thread to prevent deadlocks in SSIP
 communication.