Categories
|
|
|
Initializing a Speech Port
Reference Number: AA-00604 Views: 13044 |
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 -
HPORT port; -
int error_code; -
const char *error_desc; -
port = LV_SRE_CreateClient(&error_code, NULL, NULL, 0); -
if (port == NULL) -
{ -
// Initializing a port failed, handle error -
error_desc = LV_SRE_ReturnErrorString(error_code); -
printf("Initializing a port failed [%d: %s]\n", error_code, error_desc); -
return; -
} -
// Use the initialized port here C++ Code -
LVSpeechPort port; -
int retval = port.CreateClient(NULL, NULL, 0); -
if (retval != LV_SUCCESS) -
{ -
// Initializing a port failed, handle error -
int error_code = port.GetOpenPortStatus(); -
std::cout << "Initializing a port failed [" << error_code << ": " -
<< LVSpeechPort::ReturnErrorString(error_code) << "]" << std::endl; -
return; -
} -
// 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 -
// a structure to hold logfile info -
typedef struct logdata_s -
{ -
FILE* file; -
long message_count; -
} logdata_t; -
void logdata_callback (const char* message, void* userdata) -
{ -
logdata_t* mydata = (logdata_t*)userdata; -
fprintf(mydata -> file, "%s \n", message); -
++(mydata -> message_count); -
} -
int init_port(HPORT** port, -
logdata_t* app_log_msg_handler, -
logdata_t* port_log_msg_handler) -
{ -
int error_code; -
int save_sound_files = 1; -
// Register a callback to accept messages from the speech -
// port client library, at warning level 3 -
LV_SRE_RegisterAppLogMsg(logdata_callback, app_log_msg_handler, 3); -
// Point the client library to a local server and a remote server -
// Note that this must be done before opening a port */ -
LV_SRE_SetPropertyEx(NULL, PROP_EX_SRE_SERVERS, -
PROP_EX_VALUE_TYPE_STRING, -
"127.0.0.1;10.0.0.1", -
PROP_EX_TARGET_CLIENT, 0); -
// Open the port, registering a callback to accept messages -
// from the port at warning level 3 -
**port = LV_SRE_CreateClient(&error_code, -
logdata_callback, -
port_log_msg_handler, -
3); -
// turn on audio and response file logging -
LV_SRE_SetPropertyEx(port, PROP_EX_SAVE_SOUND_FILES, -
PROP_EX_VALUE_TYPE_INT_PTR, -
&save_sound_files, -
PROP_EX_TARGET_PORT, 0); -
return error_code; -
} C++ Code -
// A class to hold logfile info -
struct logdata -
{ -
std::ofstream file; -
long message_count; -
static void callback(const char* message, void* userdata) -
{ -
logdata* mydata = (logdata*)userdata; -
mydata -> file << message << std::endl; -
++(mydata -> message_count); -
} -
}; -
int init_port(LVSpeechPort & port, -
logdata* app_log_msg_handler, -
logdata* port_log_msg_handler) -
{ -
int error_code; -
// Register a callback to accept messages from the speech -
// port client library, at warning level 3. -
LVSpeechPort::RegisterAppLogMsg(logdata::callback, app_log_msg_handler, 3); -
// Point the client library to a local server and a remote server -
LVSpeechPort::SetClientPropertyEx(PROP_EX_SRE_SERVERS, -
PROP_EX_VALUE_TYPE_STRING, -
"127.0.0.1;10.0.0.1"); -
// Open the port, registering a callback to accept messages -
// from the port at warning level 3. -
port.CreateClient(logdata::callback, port_log_msg_handler, 3); -
// Turn on audio and response file logging -
int save_sound_files = 1; -
port.SetPropertyEx(PROP_EX_SAVE_SOUND_FILES, -
PROP_EX_VALUE_TYPE_INT_PTR, -
&save_sound_files); -
return port.GetOpenPortStatus(); -
}
|
|
|
|