Browse
 
Tools
Rss Categories

Initializing a Speech Port

Reference Number: AA-00604 Views: 3530 0 Rating/ Voters

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. port = LV_SRE_CreateClient(&error_code, NULL, NULL, 0);
  5. if (port == NULL)
  6. {
  7. // Initializing a port failed, handle error
  8. error_desc = LV_SRE_ReturnErrorString(error_code);
  9. printf("Initializing a port failed [%d: %s]\n", error_code, error_desc);

  10. return;
  11. }

  12. // 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. std::cout << "Initializing a port failed [" << error_code << ": "
  8. << LVSpeechPort::ReturnErrorString(error_code) << "]"
  9. << std::endl;

  10. return;
  11. }

  12. // Use the initialized port here

Other things you can do besides opening a port include:

  • Register logging callback functions.
  • Specify multiple speech servers (useful when using distributed architecture, as the Speech Engine will send decodes to the least busy server).
  • Turn on audio file and result logging, for later application tuning.
  • Set various streaming parameters; see LV_SRE_StreamSetParameter and LVSpeechPort::StreamSetParameter.

C Code

  1. // a structure to hold logfile info
  2. typedef struct logdata_s
  3. {
  4. FILE* file;
  5. long message_count;
  6. }  logdata_t;

  7. void logdata_callback (const char* message, void* userdata)
  8. {
  9. logdata_t* mydata = (logdata_t*)userdata;
  10. fprintf(mydata -> file, "%s \n", message);
  11. ++(mydata -> message_count);
  12. }

  13. int init_port(HPORT** port,
  14. logdata_t* app_log_msg_handler,
  15. logdata_t* port_log_msg_handler)
  16. {
  17. int error_code;
  18. int save_sound_files = 1;

  19. // Register a callback to accept messages from the speech
  20. // port client library, at warning level 3
  21. LV_SRE_RegisterAppLogMsg(logdata_callback, app_log_msg_handler, 3);

  22. // Point the client library to a local server and a remote server
  23. // Note that this must be done before opening a port */
  24. LV_SRE_SetPropertyEx(NULL, PROP_EX_SRE_SERVERS,
  25. PROP_EX_VALUE_TYPE_STRING,
  26. "127.0.0.1;10.0.0.1",
  27. PROP_EX_TARGET_CLIENT, 0);

  28. // Open the port, registering a callback to accept messages
  29. // from the port at warning level 3
  30. **port = LV_SRE_CreateClient(&error_code,
  31. logdata_callback,
  32. port_log_msg_handler,
  33. 3);

  34. // turn on audio and response file logging
  35. LV_SRE_SetPropertyEx(port, PROP_EX_SAVE_SOUND_FILES,
  36. PROP_EX_VALUE_TYPE_INT_PTR,
  37. &save_sound_files,
  38. PROP_EX_TARGET_PORT, 0);

  39. return error_code;
  40. }

C++ Code

  1. // A class to hold logfile info
  2. struct logdata
  3. {
  4. std::ofstream file;
  5. long message_count;
  6. static void callback(const char* message, void* userdata)
  7. {
  8. logdata* mydata = (logdata*)userdata;
  9. mydata -> file << message << std::endl;
  10. ++(mydata -> message_count);
  11. }
  12. };

  13. int init_port(LVSpeechPort & port,
  14. logdata* app_log_msg_handler,
  15. logdata* port_log_msg_handler)
  16. {
  17. int error_code;

  18. // Register a callback to accept messages from the speech
  19. // port client library, at warning level 3.
  20. LVSpeechPort::RegisterAppLogMsg(logdata::callback, app_log_msg_handler, 3);

  21. // Point the client library to a local server and a remote server
  22. LVSpeechPort::SetClientPropertyEx(PROP_EX_SRE_SERVERS,
  23. PROP_EX_VALUE_TYPE_STRING,
  24. "127.0.0.1;10.0.0.1");

  25. // Open the port, registering a callback to accept messages
  26. // from the port at warning level 3.
  27. port.CreateClient(logdata::callback, port_log_msg_handler, 3);

  28. // Turn on audio and response file logging
  29. int save_sound_files = 1;
  30. port.SetPropertyEx(PROP_EX_SAVE_SOUND_FILES,
  31. PROP_EX_VALUE_TYPE_INT_PTR,
  32. &save_sound_files);

  33. return port.GetOpenPortStatus();
  34. }