Initializing a Speech Port

A speech port is a connection between your application and the Speech Recognition Engine. It is required for recognition; you must have a speech port open before you can do anything else with the Engine. You will need one license for every port that is open. The type of license required depends on the configuration settings.

C Code

  1. HPORT port;
  2. int error_code;
  3. const char *error_desc;
  4.  
  5. port = LV_SRE_CreateClient(&error_code, NULL, NULL, 0);
  6. if(port == NULL)
  7. {
  8. // Initializing a port failed, handle error
  9. error_desc = LV_SRE_ReturnErrorString(error_code);
  10. printf("Initializing a port failed [%d: %s]\n", error_code, error_desc);
  11.  
  12. return;
  13. }
  14.  
  15. // Use the initialized port here

C++ Code

  1. LVSpeechPort port;
  2. int retval = port.CreateClient(NULL, NULL, 0);
  3. if(retval != LV_SUCCESS)
  4. {
  5. // Initializing a port failed, handle error
  6. int error_code = port.GetOpenPortStatus();
  7. cout << "Initializing a port failed ["
  8. << error_code << ": "
  9. << LVSpeechPort::ReturnErrorString(error_code) << "]" << endl;
  10.  
  11. return;
  12. }
  13.  
  14. // Use the initialized port here

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. }logdata_t;
  7.  
  8. void logdata_callback(const char* message, void* userdata)
  9. {
  10. logdata_t* mydata = (logdata_t*)userdata;
  11. fprintf(mydata->file, "%s\n", message);
  12. ++(mydata->message_count);
  13. }
  14.  
  15. int init_port(HPORT** port,
  16. logdata_t* app_log_msg_handler,
  17. logdata_t* port_log_msg_handler)
  18. {
  19. int error_code;
  20. int save_sound_files = 1;
  21.  
  22. // Register a callback to accept messages from the speech
  23. // port client library, at warning level 3  
  24. LV_SRE_RegisterAppLogMsg(logdata_callback, app_log_msg_handler, 3);
  25.  
  26. // point the client library to a local server and a remote server
  27. // Note that this must be done before opening a port */
  28. LV_SRE_SetPropertyEx(NULL, PROP_EX_SRE_SERVERS,
  29. PROP_EX_VALUE_TYPE_STRING,
  30. "127.0.0.1;10.0.0.1",
  31. PROP_EX_TARGET_CLIENT, 0);
  32.  
  33. // open the port, registering a callback to accept messages
  34. // from the port at warning level 3
  35. *port = LV_SRE_CreateClient(&error_code,
  36. logdata_callback,
  37. port_log_msg_handler,
  38. 3);
  39.  
  40. // turn on audio and response file logging
  41. LV_SRE_SetPropertyEx(port, PROP_EX_SAVE_SOUND_FILES,
  42. PROP_EX_VALUE_TYPE_INT_PTR,
  43. &save_sound_files,
  44. PROP_EX_TARGET_PORT, 0);
  45.  
  46. return error_code;
  47. }

C++ Code

  1. // a class to hold logfile info
  2. struct logdata
  3. {
  4. ofstream file;
  5. long message_count;
  6. static void callback(const char* 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,
  15. logdata* app_log_msg_handler,
  16. logdata* port_log_msg_handler)
  17. {
  18. int error_code;
  19.  
  20. // Register a callback to accept messages from the speech
  21. // port client library, at warning level 3.
  22. LVSpeechPort::RegisterAppLogMsg(logdata::callback, app_log_msg_handler, 3);
  23.  
  24. // point the client library to a local server and a remote server
  25. LVSpeechPort::SetClientPropertyEx(PROP_EX_SRE_SERVERS,
  26. PROP_EX_VALUE_TYPE_STRING,
  27. "127.0.0.1;10.0.0.1");
  28.  
  29. // open the port, registering a callback to accept messages
  30. // from the port at warning level 3.
  31. port.CreateClient(logdata::callback, port_log_msg_handler, 3);
  32.  
  33. // turn on audio and response file logging
  34. int save_sound_files = 1;
  35. port.SetPropertyEx(PROP_EX_SAVE_SOUND_FILES,
  36. PROP_EX_VALUE_TYPE_INT_PTR,
  37. &save_sound_files);
  38.  
  39. return port.GetOpenPortStatus();
  40. }

© 2012 LumenVox LLC. All rights reserved.