Browse
 
Tools
Rss Categories

SimpleRecognizer.cpp

Reference Number: AA-01514 Views: 3117 0 Rating/ Voters

C++ Code

  1. #include "SimpleRecognizer.h"
  2. #include <sstream>

  3. //=============================================================================

  4. // Callback for messages from the speech port
  5. void logger(const char* msg, void* userdata)
  6. {
  7.  std::cout << msg << std::endl;
  8. }

  9. //=============================================================================

  10. // Code to plug LVSemanticData into any standard stream
  11. std::ostream& operator << (std::ostream& os, const LVSemanticData& Data)
  12. {
  13.  int i;
  14.  LVSemanticObject Obj;

  15.  switch (Data.Type())
  16.  {
  17.  case SI_TYPE_BOOL:
  18.   os << Data.GetBool() << "\n";
  19.   break;
  20.  case SI_TYPE_INT:
  21.   os << Data.GetInt() << "\n";
  22.   break;
  23.  case SI_TYPE_DOUBLE:
  24.   os << Data.GetDouble() << "\n";
  25.   break;
  26.  case SI_TYPE_STRING:
  27.   os << Data.GetString() << "\n";
  28.   break;
  29.  case SI_TYPE_OBJECT:
  30.   Obj = Data.GetSemanticObject();
  31.   for (i = 0; i < Obj.NumberOfProperties(); ++i)
  32.   {
  33. os << "<property name=" << Obj.PropertyName(i) << ">\n";
  34. os << Obj.PropertyValue(i);
  35. os << "</property>\n";
  36.   }
  37.   break;
  38.  case SI_TYPE_ARRAY:
  39.   for (i = 0; i < Data.GetSemanticArray().Size(); ++i)
  40.   {
  41. os << "<element>\n";
  42. os << Data.GetArray().At(i);
  43. os << "</element>\n";
  44.   }
  45.   break;
  46.  }
  47.  return os;
  48. }

  49. //=============================================================================

  50. // Code to plug LVInterpretation into any standard stream
  51. std::ostream& operator << (std::ostream& os, const LVInterpretation& Interp)
  52. {
  53.  os << "<interpretation grammar=\"" << Interp.GrammarLabel()
  54.    <<  "\" score=\"" << Interp.Score() << "\">" << std::endl;
  55.  os << "<result name=\"" << Interp.ResultName() << "\">" << std::endl;
  56.  os << Interp.ResultData();
  57.  os << "</result>" << std::endl;
  58.  os << "<input>" << std::endl;
  59.  os << Interp.InputSentence() << std::endl;
  60.  os << "</input>" << std::endl;
  61.  os << "</interpretation>";
  62.  return os;
  63. }

  64. //=============================================================================

  65. void SimpleRecognizer::WaitUntilDone()
  66. {
  67.  while (!finished_decode) Sleep(50);
  68. }

  69. //=============================================================================

  70. SimpleRecognizer::SimpleRecognizer() :
  71. voiceChannel(1),
  72. finished_decode(true),
  73. AudioThread(NULL)
  74. {
  75. LVSpeechPort::RegisterAppLogMsg(logger,NULL,6);
  76. int v = port.CreateClient(logger,NULL,6);
  77. if (v != LV_SUCCESS)
  78. {
  79. std::cout << LVSpeechPort::ReturnErrorString(port.GetOpenPortStatus())
    << std::endl;
  80. exit(-1);
  81. }
  82.  
  83. // Turn on frequency based voice activity detector
  84. port.StreamSetParameter(STREAM_PARM_DETECT_BARGE_IN, 1);
  85. port.StreamSetParameter(STREAM_PARM_DETECT_END_OF_SPEECH, 1);
  86. port.StreamSetParameter(STREAM_PARM_VOICE_CHANNEL, voiceChannel);
  87. port.StreamSetParameter(STREAM_PARM_GRAMMAR_SET, LV_ACTIVE_GRAMMAR_SET);
  88.  
  89. //Let the port handle the decode process
  90. port.StreamSetParameter(STREAM_PARM_AUTO_DECODE, 1);
  91.  
  92. //and use semantic interpretation processor
  93. port.StreamSetParameter(STREAM_PARM_DECODE_FLAGS, LV_DECODE_SEMANTIC_INTERPRETATION);
  94. port.StreamSetStateChangeCallBack(PortCB, this);
  95. }

  96. //=============================================================================

  97. SimpleRecognizer::~SimpleRecognizer()
  98. {
  99. port.DestroyClient();
  100. }

  101. //=============================================================================

  102. void SimpleRecognizer::PortCB(int NewState, unsigned int TotalBytes,
    unsigned int RecordedBytes, void* UserData)
  103. {
  104. SimpleRecognizer* self = (SimpleRecognizer*)UserData;
  105. switch (NewState)
  106. {
  107. case STREAM_STATUS_END_SPEECH:
  108. if (!self->finished_decode)
  109. {
  110. self->AudioThread->StopStream();
  111. self->GetAnswers();
  112. self->finished_decode = true;
  113. }
  114. break;
  115. case STREAM_STATUS_STOPPED:
  116. if (!self->finished_decode)
  117. {
  118. self->AudioThread->StopStream();
  119. self->GetAnswers();
  120. self->finished_decode = true;
  121. }
  122. break;
  123. case STREAM_STATUS_NOT_READY:
  124. break;
  125. case STREAM_STATUS_READY:
  126. self->finished_decode = false;
  127. self->AudioThread->StartStream(AudioCB,self);
  128. break;
  129. }
  130. }

  131. //=============================================================================

  132. void SimpleRecognizer::LoadGrammar(const std::string& grammar_name,
    const std::string& grammar_location)
  133. {
  134. port.LoadGrammar(grammar_name.c_str(), grammar_location.c_str());
  135. }

  136. //=============================================================================

  137. bool SimpleRecognizer::AudioCB(char* audio_data, int audio_data_size,
    void* user_data)
  138. {
  139. SimpleRecognizer* self = (SimpleRecognizer*)user_data;
  140. self->port.StreamSendData(audio_data, audio_data_size);
  141. return true;
  142. }

  143. //=============================================================================

  144. void SimpleRecognizer::Recognize(AudioStreamer* Audio, const std::string& grammar_name)
  145. {
  146. finished_decode = false;
  147. AudioThread = Audio;

  148. // Clear out old grammars.
  149. port.DeactivateGrammars();
  150. port.ActivateGrammar(grammar_name.c_str());
  151. port.AddEvent(EVENT_START_DECODE_SEQ);
  152. port.StreamSetParameter(STREAM_PARM_SOUND_FORMAT, ULAW_8KHZ);
  153. port.StreamStart();
  154. }

  155. //=============================================================================

  156. void SimpleRecognizer::GetAnswers()
  157. {
  158. int val;
  159. val = port.WaitForEngineToIdle(3000,voiceChannel);
  160. if (val < 0)
  161. {
  162. result = "<noanswer/>";
  163. return;
  164. }
  165.  
  166. //View the results of the decode:
  167. std::stringstream ss;
  168. int numInterp = port.GetNumberOfInterpretations(voiceChannel);
  169. for (int t = 0; t < numInterp; ++t)
  170. {
  171. ss << port.GetInterpretation(voiceChannel, t);
  172. }
  173. result = ss.str();
  174. }

  175. //=============================================================================

  176. const std::string& SimpleRecognizer::GetResult() {return result;}

  177. //=============================================================================