- ///////////////////////////////////////////////////////////////////////////////
- // This C example is meant as a training vehicle for our C API.
- // This will initialize the SpeechPort, load up some sample grammar concepts
- // and a SRGS grammar, then open up an audio file for streaming to our Engine
- // for decoding, and display the results. The raw audio file is in 8k u-law.
- // This example will detect the end of speech then get the results
- // from the Engine.
- ///////////////////////////////////////////////////////////////////////////////
- #include <LV_SRE.h>
- #include <stdio.h>
-
#include <iostream.h>
-
#include <winsock.h>
-
#include <fcntl.h>
#include <io.h>
-
#define IN_BUFFER_SIZE 1024
-
#define NUM_IN_BUFFERS 6
// A speech port is a connection between your application and the SRE engine. // It must be initialized in main before the engine can do anything.
// HPORT creates a handle for that port.
-
HPORT hport;
-
int VOICECHANNEL = 1;
-
// The call back function will check the various states of the engine.
-
// For this example, we only care about the STREAM_STATUS_STOPPED.
-
// When this event occurs then we will wait for the engine to idle,
-
// which tells us that the decode has been performed.
-
// When the decode is done, we then get the results and display them.
- void port_callback(long StreamStatus, unsigned long total_bytes, unsigned long recorded_bytes, void* userdata)
-
{
-
int iDecodeStatus;
-
int i = 0;
-
printf ("Port Callback.\n");
-
switch (StreamStatus)
-
{
-
case STREAM_STATUS_NOT_READY:
-
printf ("not ready\n");
-
break;
-
case STREAM_STATUS_READY:
-
printf ("ready\n");
-
break;
-
case STREAM_STATUS_BARGE_IN:
-
printf ("Barge In\n");
break;
case STREAM_STATUS_END_SPEECH:
printf("end of speech\n");
iDecodeStatus = LV_SRE_WaitForEngineToIdle(hport, VOICECHANNEL,7000);
if(iDecodeStatus!= LV_TIME_OUT)
{
printf("\nDecoded Transcript:\t%s\nConfidence Score:\t%d\n
Concept Count:\t\t%d\n\n",
LV_SRE_GetInterpretationString(hport, VOICECHANNEL, i),
LV_SRE_InterpretationScore(hport, VOICECHANNEL, i),
LV_SRE_GetNumberOfInterpretations(hport, VOICECHANNEL));
}
break;
case STREAM_STATUS_STOPPED:
printf("stream stopped\n");
break;
case STREAM_STATUS_BARGE_IN_TIMEOUT:
printf("barge in time out\n");
break;
case STREAM_STATUS_END_SPEECH_TIMEOUT:
printf("EOS time out\n");
break;
}
}
// This function will open the audio file for streaming,
// fill up the stream buffers, and send the buffers to the engine
void Audio_Stream(char* filename)
{
int error_code;
const char* error_string;
double realtime_factor = 1.0;
int audio_buffer_size = 0;
int increment_ms = 300;
int bytes_per_sample = 1;
int samples_per_second = 8000;
int chunk_size = 2400;
char end_buffer[2400];
char* audio_buffer;
int audio_length = 0;
// Opens specified audio file
int audio_handle = _open(filename, _O_BINARY | _O_RDONLY);
audio_buffer_size = _lseek(audio_handle, 0L, SEEK_END);
audio_buffer =(char*)malloc(audio_buffer_size);
_lseek(audio_handle, 0L, SEEK_SET);
char* current_pos = audio_buffer;
_read(audio_handle, audio_buffer, audio_buffer_size);
_close(audio_handle);
chunk_size = bytes_per_sample*samples_per_second*increment_ms/1000;
memset(end_buffer,0, chunk_size);
error_code = LV_SRE_StreamStart(hport);
error_string = LV_SRE_ReturnErrorString(error_code);
printf("Streaming the audio.\n");
// Filling up the buffers
while(current_pos != audio_buffer+ audio_buffer_size)
{
if(audio_length + chunk_size > audio_buffer_size)
{
chunk_size = audio_buffer_size - audio_length;
}
audio_length += chunk_size;
// Sending the filled buffers to our Engine
error_code = LV_SRE_StreamSendData(hport, current_pos, chunk_size);
current_pos += chunk_size;
Sleep((DWORD)((double)increment_ms/realtime_factor));
}
}
void LoadGrammar(char* grammar_name,char* grammar_file)
{
int error_code;
printf("Loading the grammmar and activating it.\n");
// Here we are loading up a SRGS grammar. error_code = LV_SRE_LoadGrammar(hport, grammar_name, grammar_file);
// Once you load up a grammar you can activate and deactivate it
// at any point during the callflow.
// For this example we will activate it immediately.
error_code = LV_SRE_ActivateGrammar(hport, grammar_name);
error_code = LV_SRE_StreamSetParameter(hport,
STREAM_PARM_GRAMMAR_SET,
LV_ACTIVE_GRAMMAR_SET);
}
void InitializePort()
{
int error_code;
hport = LV_SRE_CreateClient(&error_code,NULL,NULL,0);
printf("Initializing the port.\n");
// Engine properties
error_code = LV_SRE_SetPropertyEx(hport,
PROP_EX_CHOOSE_MODEL,
PROP_EX_VALUE_TYPE_INT,
(void*)1,
PROP_EX_TARGET_CLIENT,
VOICECHANNEL);
error_code = LV_SRE_SetPropertyEx(hport,
PROP_EX_SRE_SERVERS,
PROP_EX_VALUE_TYPE_STRING,
"127.0.0.1:5730",
PROP_EX_TARGET_CLIENT,
VOICECHANNEL);
error_code = LV_SRE_SetPropertyEx(hport,
PROP_EX_SAVE_SOUND_FILES,
PROP_EX_VALUE_TYPE_INT,
(void*)0,
PROP_EX_TARGET_PORT,
VOICECHANNEL);
error_code = LV_SRE_SetPropertyEx(hport,
PROP_EX_NOISE_REDUCTION_ENABLE,
PROP_EX_VALUE_TYPE_INT,
(void*)1,
PROP_EX_TARGET_CHANNEL,
VOICECHANNEL);
// Frequency based Voice Activated Detection(VAD) parameters
error_code = LV_SRE_StreamSetParameter(hport, STREAM_PARM_DETECT_BARGE_IN,1);
error_code = LV_SRE_StreamSetParameter(hport,
STREAM_PARM_DETECT_END_OF_SPEECH,1);
error_code = LV_SRE_StreamSetParameter(hport, STREAM_PARM_VOICE_CHANNEL,
VOICECHANNEL);
error_code = LV_SRE_StreamSetParameter(hport, STREAM_PARM_AUTO_DECODE,1);
error_code = LV_SRE_StreamSetParameter(hport, STREAM_PARM_DECODE_FLAGS,
LV_DECODE_SEMANTIC_INTERPRETATION);
error_code = LV_SRE_StreamSetParameter(hport,
STREAM_PARM_VAD_STREAM_INIT_DELAY,100);
error_code = LV_SRE_StreamSetParameter(hport, STREAM_PARM_VAD_EOS_DELAY,800);
error_code = LV_SRE_StreamSetParameter(hport, STREAM_PARM_VAD_WIND_BACK,256);
error_code = LV_SRE_StreamSetParameter(hport, STREAM_PARM_SOUND_FORMAT,
ULAW_8KHZ);
LV_SRE_StreamSetStateChangeCallBack(hport, port_callback, NULL);
}
void main()
{
InitializePort();
LoadGrammar(grammar_label, grammar_file_path);
Audio_Stream(audio_file_path);
LV_SRE_DestroyClient(hport);
}