Initializing a Speech Port

A speech port is a connection between your application and the Speech Engine. It is required for recognition; you must have a speech port open before you can do anything else with the Engine. For every port that is open, you will need one available Speech Engine license.

The only thing you must do to initialize a speech port is to have the Speech Engine service running on your machine, and call OpenPort:

C Code

  1. HPORT port;
  2. long error_code;
  3. port = LV_SRE_OpenPort2(&error_code,NULL,NULL,0);
  4.  
  5. switch(error_code)
  6. {
  7. case LV_OPEN_PORT_FAILED__LICENSES_EXCEEDED:
  8. printf("licenses exceeded");
  9. break;
  10. case LV_OPEN_PORT_FAILED__PRIMARY_SERVER_NOT_RESPONDING:
  11. case LV_NO_SERVER_RESPONDING
  12. printf("Speech Engine server unavailable");
  13. break;
  14. case LV_SUCCESS:
  15. printf("port opened");
  16. break;
  17. }

C++ Code

  1. LVSpeechPort port;
  2. port.OpenPort( );
  3. int error_code = port.GetOpenPortStatus();
  4.  
  5. switch(error_code)
  6. {
  7. case LV_FAILURE:
  8. cout <<"failed to open port";
  9. break;
  10. case LV_OPEN_PORT_FAILED__PRIMARY_SERVER_NOT_RESPONDING:
  11. case LV_NO_SERVER_RESPONDING
  12. cout << "Speech Engine server unavailable";
  13. break;
  14. case LV_SUCCESS:
  15. cout << "port opened";
  16. break;
  17. }

Other things you can do besides opening a port include:


C Code

  1. /* a structure to hold logfile info */
  2. typedef struct logdata_s
  3. {
  4. long file;
  5. long message_count;
  6.  
  7. }logdata_t;
  8.  
  9. void logdata_callback(const char* message, void* userdata)
  10. {
  11. logdata_t* mydata = (logdata_t*)userdata;
  12. fprintf(mydata->file,"%s\n",message);
  13. ++(mydata->message_count;
  14. }
  15.  
  16. int init_port (HPORT* port, logdata_t* app_message, logdata_t* log_message )
  17. {
  18. long error_code;
  19.  
  20. // Register a callback to accept messages from the server
  21. // or client library, at warning level 3  
  22. LV_SRE_RegisterAppLogMsg(logdata_callback,app_message, 3);
  23.  
  24. // point the client library to a local server and a remote server
  25. // Note that this must be done before opening a port */
  26. LV_SRE_SetPropertyEx(NULL,PROP_EX_SRE_SERVERS,
  27. PROP_EX_VALUE_TYPE_STRING,
  28. "127.0.0.1,10.0.0.1",
  29. PROP_EX_TARGET_CLIENT, 0);
  30.  
  31. // open the port, registering a callback to accept messages
  32. // from the port at warning level 3
  33.  port = LV_SRE_OpenPort2(error_code, logdata_callback,log_message,3);
  34.  
  35. // turn on sound and response file logging
  36. int save_sound_files=1;
  37. LV_SRE_SetPropertyEx(port,PROP_EX_SAVE_SOUND_FILES,
  38. PROP_EX_VALUE_TYPE_INT_PTR,
  39. &save_sound_files,
  40. PROP_EX_TARGET_PORT,0);
  41.  
  42. return error_code;
  43. }

C++ Code

  1. // a class to hold logfile info
  2. struct logdata
  3. {
  4. ofstream file;
  5. long message_count;
  6. staticvoid callback(constchar* message, void* userdata)
  7. {
  8. logdata* self =(logdata*)userdata;
  9. mydata->file << message << endl;
  10. ++(mydata->message_count;
  11.  }
  12. };
  13.  
  14. int init_port (LVSpeechPort& port, logdata* app_message, logdata* log_message )
  15. {
  16. long error_code;
  17.  
  18. // Register a callback to accept messages from the server
  19. // or client library, at warning level 3.
  20. LVSpeechPort::RegisterAppLogMsg(logdata_callback,app_message, 3);
  21.  
  22. // point the client library to a local server and a remote server
  23. LVSpeechPort::SetClientPropertyEx(PROP_EX_SRE_SERVERS,
  24. PROP_EX_TYPE_STRING,
  25. "127.0.0.1,10.0.0.1");
  26.  
  27. // open the port, registering a callback to accept messages
  28. // from the port at warning level 3.
  29. port.OpenPort(logdata_callback,log_message,3);
  30.  
  31. // turn on sound and response file logging
  32. port.SetPropertyEx(PROP_EX_SAVE_SOUND_FILES,
  33. PROP_EX_VALUE_TYPE_INT_PTR,
  34. &save_sound_files);
  35.  
  36. return port.GetOpenPortStatus();
  37. }

© 2010 LumenVox LLC. All rights reserved.