Browse
 
Tools
Rss Categories

A Working Example In C

Reference Number: AA-01516 Views: 6635 25 Rating/ 1 Voters

Included in this documentation is a working example that incorporates streaming audio, SRGS grammars, and SemanticInterpretation.It is written in C, is based on examples throughout this documentation, and compiles under Visual Studio 2013, although should run on most recent versions. Note that this sample is very simplistic, and does not include the required amount of error-detection reporting functionality would be for a production application.

C Code

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // This C example is meant as a training vehicle for our C API.
  3. // This will initialize the SpeechPort, load up some sample grammar concepts
  4. // and a SRGS grammar, then open up an audio file for streaming to our Engine
  5. // for decoding, and display the results. The raw audio file is in 8k u-law.
  6. // This example will detect the end of speech then get the results
  7. // from the Engine.
  8. ///////////////////////////////////////////////////////////////////////////////

  9. #include <LV_SRE.h>
  10. #include <stdio.h>
  11. #include <iostream.h>
  12. #include <winsock.h>
  13. #include <fcntl.h>
  14. #include <io.h>

  15. #define IN_BUFFER_SIZE 1024
  16. #define NUM_IN_BUFFERS 6

  17. // A speech port is a connection between your application and the SRE engine.
  18. // It must be initialized in main before the engine can do anything.
  19. // HPORT creates a handle for that port.
  20. HPORT hport;
  21.  
  22. int VOICECHANNEL = 1;
  23.  
  24. // The call back function will check the various states of the engine.
  25. // For this example, we only care about the STREAM_STATUS_STOPPED.
  26. // When this event occurs then we will wait for the engine to idle,
  27. // which tells us that the decode has been performed.
  28. // When the decode is done, we then get the results and display them.
  29. void port_callback(long StreamStatus, unsigned long total_bytes, unsigned long recorded_bytes, void* userdata)
  30. {
  31. int iDecodeStatus;
  32. int i = 0;

  33. printf ("Port Callback.\n");
  34. switch (StreamStatus)
  35. {
  36. case STREAM_STATUS_NOT_READY:
  37. printf ("not ready\n");
  38. break;
  39. case STREAM_STATUS_READY:
  40. printf ("ready\n");
  41. break;
  42. case STREAM_STATUS_BARGE_IN:
  43. printf ("Barge In\n");
  44. break;
  45. case STREAM_STATUS_END_SPEECH:
  46. printf("end of speech\n");
  47. iDecodeStatus = LV_SRE_WaitForEngineToIdle(hport, VOICECHANNEL,7000);

  48. if(iDecodeStatus!= LV_TIME_OUT)
  49. {
  50. printf("\nDecoded Transcript:\t%s\nConfidence Score:\t%d\n
  51. Concept Count:\t\t%d\n\n",
    LV_SRE_GetInterpretationString(hport, VOICECHANNEL, i),
    LV_SRE_InterpretationScore(hport, VOICECHANNEL, i),
    LV_SRE_GetNumberOfInterpretations(hport, VOICECHANNEL));
  52. }
  53. break;
  54. case STREAM_STATUS_STOPPED:
  55. printf("stream stopped\n");
  56. break;
  57. case STREAM_STATUS_BARGE_IN_TIMEOUT:
  58. printf("barge in time out\n");
  59. break;
  60. case STREAM_STATUS_END_SPEECH_TIMEOUT:
  61. printf("EOS time out\n");
  62. break;
  63. }
  64. }

  65. // This function will open the audio file for streaming,
  66. // fill up the stream buffers, and send the buffers to the engine
  67. void Audio_Stream(char* filename)
  68. {
  69. int error_code;
  70. const char* error_string;
  71. double realtime_factor 1.0;
  72. int audio_buffer_size 0;
  73. int increment_ms 300;
  74. int bytes_per_sample 1;
  75. int samples_per_second 8000;
  76. int chunk_size 2400;
  77. char end_buffer[2400];
  78. char* audio_buffer;
  79. int audio_length 0;

  80. // Opens specified audio file
  81. int audio_handle = _open(filename, _O_BINARY | _O_RDONLY);

  82. audio_buffer_size = _lseek(audio_handle, 0L, SEEK_END);
  83. audio_buffer =(char*)malloc(audio_buffer_size);

  84. _lseek(audio_handle, 0L, SEEK_SET);
  85. char* current_pos = audio_buffer;
  86. _read(audio_handle, audio_buffer, audio_buffer_size);
  87. _close(audio_handle);

  88. chunk_size = bytes_per_sample*samples_per_second*increment_ms/1000;
  89. memset(end_buffer,0, chunk_size);

  90. error_code = LV_SRE_StreamStart(hport);
  91. error_string = LV_SRE_ReturnErrorString(error_code);
  92. printf("Streaming the audio.\n");

  93. // Filling up the buffers
  94. while(current_pos != audio_buffer+ audio_buffer_size)
  95. {
  96. if(audio_length + chunk_size > audio_buffer_size)
  97. {
  98. chunk_size = audio_buffer_size - audio_length;
  99. }
  100. audio_length += chunk_size;
  101. // Sending the filled buffers to our Engine
  102. error_code = LV_SRE_StreamSendData(hport, current_pos, chunk_size);
  103. current_pos += chunk_size;
  104. Sleep((DWORD)((double)increment_ms/realtime_factor));
  105. }
  106. }

  107. void LoadGrammar(char* grammar_name,char* grammar_file)
  108. {
  109. int error_code;
  110. printf("Loading the grammmar and activating it.\n");

  111. // Here we are loading up a SRGS grammar.
  112. error_code = LV_SRE_LoadGrammar(hport, grammar_name, grammar_file);

  113. // Once you load up a grammar you can activate and deactivate it
  114. // at any point during the callflow.
  115. // For this example we will activate it immediately.
  116. error_code = LV_SRE_ActivateGrammar(hport, grammar_name);
  117. error_code = LV_SRE_StreamSetParameter(hport,
  118. STREAM_PARM_GRAMMAR_SET,
    LV_ACTIVE_GRAMMAR_SET);
  119. }

  120. void InitializePort()
  121. {
  122. int error_code;
  123. hport = LV_SRE_CreateClient(&error_code,NULL,NULL,0);
  124. printf("Initializing the port.\n");

  125. // Engine properties
  126. error_code = LV_SRE_SetPropertyEx(hport,
  127. PROP_EX_CHOOSE_MODEL,
    PROP_EX_VALUE_TYPE_INT,
    (void*)1,
    PROP_EX_TARGET_CLIENT,
    VOICECHANNEL);
  128. error_code = LV_SRE_SetPropertyEx(hport,
  129. PROP_EX_SRE_SERVERS,
    PROP_EX_VALUE_TYPE_STRING,
    "127.0.0.1:5730",
    PROP_EX_TARGET_CLIENT,
    VOICECHANNEL);
  130. error_code = LV_SRE_SetPropertyEx(hport,
  131. PROP_EX_SAVE_SOUND_FILES,
    PROP_EX_VALUE_TYPE_INT,
    (void*)0,
    PROP_EX_TARGET_PORT,
    VOICECHANNEL);
  132. error_code = LV_SRE_SetPropertyEx(hport,
  133. PROP_EX_NOISE_REDUCTION_ENABLE,
    PROP_EX_VALUE_TYPE_INT,
    (void*)1,
    PROP_EX_TARGET_CHANNEL,
    VOICECHANNEL);

  134. // Frequency based Voice Activated Detection(VAD) parameters
  135. error_code = LV_SRE_StreamSetParameter(hport, STREAM_PARM_DETECT_BARGE_IN,1);
  136. error_code = LV_SRE_StreamSetParameter(hport,
  137. STREAM_PARM_DETECT_END_OF_SPEECH,1);
  138. error_code = LV_SRE_StreamSetParameter(hport, STREAM_PARM_VOICE_CHANNEL,
  139. VOICECHANNEL);
  140. error_code = LV_SRE_StreamSetParameter(hport, STREAM_PARM_AUTO_DECODE,1);
  141. error_code = LV_SRE_StreamSetParameter(hport, STREAM_PARM_DECODE_FLAGS,
  142. LV_DECODE_SEMANTIC_INTERPRETATION);
  143. error_code = LV_SRE_StreamSetParameter(hport,
  144. STREAM_PARM_VAD_STREAM_INIT_DELAY,100);
  145. error_code = LV_SRE_StreamSetParameter(hport, STREAM_PARM_VAD_EOS_DELAY,800);
  146. error_code = LV_SRE_StreamSetParameter(hport, STREAM_PARM_VAD_WIND_BACK,256);
  147. error_code = LV_SRE_StreamSetParameter(hport, STREAM_PARM_SOUND_FORMAT,
    ULAW_8KHZ);
  148. LV_SRE_StreamSetStateChangeCallBack(hport, port_callback, NULL);
  149. }

  150. void main()
  151. {
  152. InitializePort();
  153. LoadGrammar(grammar_label, grammar_file_path);
  154. Audio_Stream(audio_file_path);
  155. LV_SRE_DestroyClient(hport);
  156. }