Advertisement
MageKing17

voicerec.cpp.patch

Apr 23rd, 2015
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 15.88 KB | None | 0 0
  1.  code/sound/phrases.xml  |  66 +++++------
  2.  code/sound/voicerec.cpp | 299 ++++++++++++++++++++++++------------------------
  3.  2 files changed, 184 insertions(+), 181 deletions(-)
  4.  
  5. diff --git a/code/sound/phrases.xml b/code/sound/phrases.xml
  6. index 0e93614..45207a2 100644
  7. --- a/code/sound/phrases.xml
  8. +++ b/code/sound/phrases.xml
  9. @@ -1,6 +1,6 @@
  10.  <GRAMMAR>
  11.     <DEFINE>
  12. -       <ID NAME="VID_Counter"          VAL="80"/>
  13. +       <ID NAME="VID_Counter"          VAL="80"/>
  14.         <ID NAME="VID_DestroyTarget"    VAL="10"/>
  15.         <ID NAME="VID_DisableTarget"    VAL="11"/>
  16.         <ID NAME="VID_DisarmTarget"     VAL="12"/>
  17. @@ -35,30 +35,30 @@
  18.  
  19.     <RULE ID="VID_WingName" TOPLEVEL="ACTIVE">
  20.         <L PROPID="VID_WingName">
  21. -           <P VAL="0">Alpha</P>
  22. -           <P VAL="1">Beta</P>
  23. -           <P VAL="2">Gamma</P>   
  24. -           <P VAL="3">Delta</P>
  25. -           <P VAL="4">Epsilon</P>
  26. -           <P VAL="5">Zeta</P>
  27. -           <P VAL="6">Eta</P>
  28. -           <P VAL="7">Theta</P>
  29. -           <P VAL="8">Iota</P>
  30. -           <P VAL="9">Kappa</P>
  31. -           <P VAL="10">Lambda</P>
  32. -           <P VAL="11">Mu</P>
  33. -           <P VAL="12">Nu</P>
  34. -           <P VAL="13">Xi</P>
  35. -           <P VAL="14">Omicron</P>
  36. -           <P VAL="15">Pi</P>
  37. -           <P VAL="16">Rho</P>
  38. -           <P VAL="17">Sigma</P>
  39. -           <P VAL="18">Tau</P>
  40. -           <P VAL="19">Upsilon</P>
  41. -           <P VAL="20">Phi</P>
  42. -           <P VAL="21">Chi</P>
  43. -           <P VAL="22">Psi</P>
  44. -           <P VAL="23">Omega</P>      
  45. +           <P VALSTR="Alpha">Alpha</P>
  46. +           <P VALSTR="Beta">Beta</P>
  47. +           <P VALSTR="Gamma">Gamma</P>
  48. +           <P VALSTR="Delta">Delta</P>
  49. +           <P VALSTR="Epsilon">Epsilon</P>
  50. +           <P VALSTR="Zeta">Zeta</P>
  51. +           <P VALSTR="Eta">Eta</P>
  52. +           <P VALSTR="Theta">Theta</P>
  53. +           <P VALSTR="Iota">Iota</P>
  54. +           <P VALSTR="Kappa">Kappa</P>
  55. +           <P VALSTR="Lambda">Lambda</P>
  56. +           <P VALSTR="Mu">Mu</P>
  57. +           <P VALSTR="Nu">Nu</P>
  58. +           <P VALSTR="Xi">Xi</P>
  59. +           <P VALSTR="Omicron">Omicron</P>
  60. +           <P VALSTR="Pi">Pi</P>
  61. +           <P VALSTR="Rho">Rho</P>
  62. +           <P VALSTR="Sigma">Sigma</P>
  63. +           <P VALSTR="Tau">Tau</P>
  64. +           <P VALSTR="Upsilon">Upsilon</P>
  65. +           <P VALSTR="Phi">Phi</P>
  66. +           <P VALSTR="Chi">Chi</P>
  67. +           <P VALSTR="Psi">Psi</P>
  68. +           <P VALSTR="Omega">Omega</P>
  69.         </L>
  70.         <L PROPID="VID_ShipName">
  71.             <P VAl="0">Wing</P>
  72. @@ -87,26 +87,26 @@
  73.             <P VAL="VID_IgnoreTarget">Ignore my target</P>
  74.             <P VAL="VID_FormWing">Form on my wing</P>
  75.             <P VAL="VID_CoverMe">Cover me</P>
  76. -           <P VAL="VID_EngageEnemy">Engage Enemy</P>
  77. +           <P VAL="VID_EngageEnemy">Engage the Enemy</P>
  78.             <P VAL="VID_EngageEnemy">Attack</P>
  79.             <P VAL="VID_Depart">Depart</P>
  80.         </L>
  81.     </RULE>
  82.  
  83.     <RULE ID="VID_TopMenu" TOPLEVEL="ACTIVE">
  84. -           <L PROPID="VID_TopMenu">
  85. +       <L PROPID="VID_TopMenu">
  86.             <P VAL="VID_Ships">Ships</P>
  87.             <P VAL="VID_Wings">Wings</P>
  88.             <P VAL="VID_AllFighters">All Fighters<O><RULEREF REFID="VID_Action"/></O></P>
  89.             <P VAL="VID_AllWings">All Wings<O><RULEREF REFID="VID_Action"/></O></P>
  90.             <P VAL="VID_Reinforcements">Reinforcements</P>
  91.             <P VAL="VID_SupportShip">Support ship</P>
  92. -           <P VAL="VID_SupportShip">Rearm</P>
  93. -           <P VAL="VID_SupportShip">Repair</P>
  94. -           <P VAL="VID_AbortSupport">Abort</P>
  95. +           <P VAL="VID_SupportShip">Rearm <O>and repair</O></P>
  96. +           <P VAL="VID_SupportShip">Repair <O>and rearm</O></P>
  97. +           <P VAL="VID_AbortSupport">Abort <L><P>support <O>ship</O></P><P>rearm <O>and repair</O></P><P>repair <O>and rearm</O></P></L></P>
  98.             <P VAL="VID_Cancel">Cancel</P>
  99.             <P VAL="VID_Command">Command</P>
  100. -           <P VAL="VID_More">More</P>        
  101. +           <P VAL="VID_More">More</P>
  102.         </L>
  103.     </RULE>
  104.  
  105. @@ -144,9 +144,9 @@
  106.             <P VAL="91">Target previous uninspected</P>
  107.             <P VAL="92">Target newest</P>
  108.             <P VAL="93">Target next turret</P>
  109. -           <P VAL="94">Target previous turrent</P>
  110. +           <P VAL="94">Target previous turret</P>
  111.             <P VAL="95">Target next bomb</P>
  112. -           <P VAL="96">Target previous bomb</P>               
  113. +           <P VAL="96">Target previous bomb</P>
  114.         </L>
  115.     </RULE>
  116.  
  117. diff --git a/code/sound/voicerec.cpp b/code/sound/voicerec.cpp
  118. index 70e847d..80a5230 100644
  119. --- a/code/sound/voicerec.cpp
  120. +++ b/code/sound/voicerec.cpp
  121. @@ -112,104 +112,118 @@ void doVid_Action(int action)
  122.  
  123.  bool VOICEREC_init(HWND hWnd, int event_id, int grammar_id, int command_resource)
  124.  {
  125. -    HRESULT hr = S_OK;
  126. -    CComPtr<ISpAudio> cpAudio;
  127. -
  128. -    while (true)
  129. -    {
  130. -        // create a recognition engine
  131. -        hr = p_recogEngine.CoCreateInstance(CLSID_SpSharedRecognizer);
  132. -        if (FAILED(hr))
  133. -        {
  134. +   HRESULT hr = S_OK;
  135. +   CComPtr<ISpAudio> cpAudio;
  136. +
  137. +   while (true)
  138. +   {
  139. +       // create a recognition engine
  140. +       hr = p_recogEngine.CoCreateInstance(CLSID_SpInprocRecognizer);
  141. +       if (FAILED(hr))
  142. +       {
  143.             MessageBox(hWnd,"Failed to create a recognition engine\n","Error",MB_OK);
  144.             printf("Failed to create a recognition engine\n");
  145. -            break;
  146. -        }
  147. -      
  148. -        // create the command recognition context
  149. -        hr = p_recogEngine->CreateRecoContext( &p_recogContext );
  150. -        if (FAILED(hr))
  151. -        {
  152. +           break;
  153. +       }
  154. +
  155. +       // create the command recognition context
  156. +       hr = p_recogEngine->CreateRecoContext( &p_recogContext );
  157. +       if (FAILED(hr))
  158. +       {
  159.             MessageBox(hWnd,"Failed to create the command recognition context\n","Error",MB_OK);
  160.             printf("Failed to create the command recognition context\n");
  161. -            break;
  162. -        }
  163. -
  164. -        // Let SR know that window we want it to send event information to, and using
  165. -        // what message
  166. -        hr = p_recogContext->SetNotifyWindowMessage( hWnd, event_id, 0, 0 );
  167. -        if (FAILED(hr))
  168. -        {
  169. +           break;
  170. +       }
  171. +
  172. +       // Let SR know that window we want it to send event information to, and using
  173. +       // what message
  174. +       hr = p_recogContext->SetNotifyWindowMessage( hWnd, event_id, 0, 0 );
  175. +       if (FAILED(hr))
  176. +       {
  177.             MessageBox(hWnd,"Failed to SetNotifyWindowMessage\n","Error",MB_OK);
  178. -            break;
  179. -        }
  180. -
  181. -       // Tell SR what types of events interest us.  Here we only care about command
  182. -        // recognition.
  183. -        hr = p_recogContext->SetInterest( SPFEI(SPEI_RECOGNITION), SPFEI(SPEI_RECOGNITION) );
  184. -        if (FAILED(hr))
  185. -        {
  186. +           break;
  187. +       }
  188. +
  189. +       // Tell SR what types of events interest us.  Here we only care about command
  190. +       // recognition.
  191. +       hr = p_recogContext->SetInterest( SPFEI(SPEI_RECOGNITION), SPFEI(SPEI_RECOGNITION) );
  192. +       if (FAILED(hr))
  193. +       {
  194.             MessageBox(hWnd,"Failed to set events\n","Error",MB_OK);
  195. -            break;
  196. -        }
  197. +           break;
  198. +       }
  199.  
  200.         // Create a grammar
  201. -        hr = p_recogContext->CreateGrammar(grammar_id, &p_grammarObject);
  202. -        if (FAILED(hr))
  203. -        {
  204. +       hr = p_recogContext->CreateGrammar(grammar_id, &p_grammarObject);
  205. +       if (FAILED(hr))
  206. +       {
  207.             MessageBox(hWnd,"Failed to create grammar\n","Error",MB_OK);
  208. -            break;
  209. -        }
  210. +           break;
  211. +       }
  212.  
  213. -        // Load our grammar from data\phrases.xml, or if that doesn't exist, from the compiled in
  214. -        // user defined ("SRGRAMMAR") resource type.
  215. +       // Load our grammar from data\phrases.xml, or if that doesn't exist, from the compiled in
  216. +       // user defined ("SRGRAMMAR") resource type.
  217.         hr = p_grammarObject->LoadCmdFromFile(L"data\\phrases.xml", SPLO_STATIC);
  218. -        if (FAILED(hr))
  219. -        {
  220. +       if (FAILED(hr))
  221. +       {
  222.             hr = p_grammarObject->LoadCmdFromResource(NULL, MAKEINTRESOURCEW(command_resource),
  223. -                                                 L"SRGRAMMAR", MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
  224. -                                                 SPLO_DYNAMIC);
  225. +                                               L"SRGRAMMAR", MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
  226. +                                               SPLO_STATIC);
  227.             if (FAILED(hr))
  228.             {
  229.                 MessageBox(hWnd,"Failed to load resource\n","Error",MB_OK);
  230. -               break;
  231. +               break;
  232.             }
  233. -        }
  234. +       }
  235. +
  236. +       hr = SpCreateDefaultObjectFromCategoryId(SPCAT_AUDIOIN, &cpAudio);
  237. +       if (FAILED(hr))
  238. +       {
  239. +           MessageBox(hWnd,"Failed to get default audio input\n", "Error", MB_OK);
  240. +           break;
  241. +       }
  242.  
  243. -        // Set rules to active, we are now listening for commands
  244. -        hr = p_grammarObject->SetRuleState(NULL, NULL, SPRS_ACTIVE );
  245. -        if (FAILED(hr))
  246. -        {
  247. +       // Set the audio input to our token.
  248. +       hr = p_recogEngine->SetInput(cpAudio, TRUE);
  249. +       if (FAILED(hr))
  250. +       {
  251. +           MessageBox(hWnd,"Failed to set audio input\n", "Error", MB_OK);
  252. +       }
  253. +
  254. +       // Set rules to active, we are now listening for commands
  255. +       hr = p_grammarObject->SetRuleState(NULL, NULL, SPRS_ACTIVE );
  256. +       if (FAILED(hr))
  257. +       {
  258.             MessageBox(hWnd,"Failed to set listening for commands\n","Error",MB_OK);
  259. -            break;
  260. -        }
  261. +           break;
  262. +       }
  263.  
  264. -        break;
  265. -    }
  266. +       break;
  267. +   }
  268.  
  269. -    // if we failed and have a partially setup SAPI, close it all down
  270. -    if (FAILED(hr))
  271. -    {
  272. -        VOICEREC_deinit();
  273. -    }
  274. +   // if we failed and have a partially setup SAPI, close it all down
  275. +   if (FAILED(hr))
  276. +   {
  277. +       VOICEREC_deinit();
  278. +   }
  279.  
  280. -    return ( hr == S_OK);
  281. +   return ( hr == S_OK);
  282.  }
  283.  
  284.  void VOICEREC_deinit()
  285.  {
  286. -    // Release grammar, if loaded
  287. -    if ( p_grammarObject )
  288. -    {
  289. -        p_grammarObject.Release();
  290. -    }
  291. -    // Release recognition context, if created
  292. -    if ( p_recogContext )
  293. -    {
  294. -        p_recogContext->SetNotifySink(NULL);
  295. -        p_recogContext.Release();
  296. -    }
  297. -    // Release recognition engine instance, if created
  298. +   // Release grammar, if loaded
  299. +   if ( p_grammarObject )
  300. +   {
  301. +       p_grammarObject.Release();
  302. +   }
  303. +   // Release recognition context, if created
  304. +   if ( p_recogContext )
  305. +   {
  306. +       p_recogContext->SetNotifySink(NULL);
  307. +       p_recogContext.Release();
  308. +   }
  309. +   // Release recognition engine instance, if created
  310.     if ( p_recogEngine )
  311.     {
  312.         p_recogEngine.Release();
  313. @@ -219,50 +233,21 @@ void VOICEREC_deinit()
  314.  void VOICEREC_execute_command(ISpPhrase *pPhrase, HWND hWnd);
  315.  void VOICEREC_process_event(HWND hWnd)
  316.  {
  317. -    CSpEvent event;  // Event helper class
  318. -
  319. -    // Loop processing events while there are any in the queue
  320. -    while (event.GetFrom(p_recogContext) == S_OK)
  321. -    {
  322. -        // Look at recognition event only
  323. -        switch (event.eEventId)
  324. -        {
  325. -            case SPEI_RECOGNITION:
  326. -                   VOICEREC_execute_command(event.RecoResult(), hWnd);
  327. -                break;
  328. -
  329. -        }
  330. -    }
  331. -}
  332. +   CSpEvent event;  // Event helper class
  333.  
  334. -// Its not enough to update this, phrases.xml must have this info as well
  335. -char *wing_names[] =
  336. -{
  337. -       "Alpha",
  338. -       "Beta",
  339. -       "Gamma",   
  340. -       "Delta",
  341. -       "Epsilon",
  342. -       "Zeta",
  343. -       "Eta",
  344. -       "Theta",
  345. -       "Iota",
  346. -       "Kappa",
  347. -       "Lambda",
  348. -       "Mu",
  349. -       "Nu",
  350. -       "Xi",
  351. -       "Omicron",
  352. -       "Pi",
  353. -       "Rho",
  354. -       "Sigma",
  355. -       "Tau",
  356. -       "Upsilon",
  357. -       "Phi",
  358. -       "Chi",
  359. -       "Psi",
  360. -       "Omega",
  361. -};
  362. +   // Loop processing events while there are any in the queue
  363. +   while (event.GetFrom(p_recogContext) == S_OK)
  364. +   {
  365. +       // Look at recognition event only
  366. +       switch (event.eEventId)
  367. +       {
  368. +           case SPEI_RECOGNITION:
  369. +               VOICEREC_execute_command(event.RecoResult(), hWnd);
  370. +               break;
  371. +
  372. +       }
  373. +   }
  374. +}
  375.  
  376.  /******************************************************************************
  377.  * ExecuteCommand *
  378. @@ -276,12 +261,12 @@ char VOICEREC_lastCommand[30];
  379.  
  380.  void VOICEREC_execute_command(ISpPhrase *pPhrase, HWND hWnd)
  381.  {
  382. -    SPPHRASE *pElements;
  383. +   SPPHRASE *pElements;
  384.  
  385. -    // Get the phrase elements, one of which is the rule id we specified in
  386. -    // the grammar.  Switch on it to figure out which command was recognized.
  387. -    if (SUCCEEDED(pPhrase->GetPhrase(&pElements)))
  388. -    {    
  389. +   // Get the phrase elements, one of which is the rule id we specified in
  390. +   // the grammar.  Switch on it to figure out which command was recognized.
  391. +   if (SUCCEEDED(pPhrase->GetPhrase(&pElements)))
  392. +   {
  393.  #ifndef NDEBUG
  394.         if(DEBUG_ON)
  395.         {
  396. @@ -304,13 +289,15 @@ void VOICEREC_execute_command(ISpPhrase *pPhrase, HWND hWnd)
  397.  
  398.             mprintf(( "recognized speech : %s \n", szText ));
  399.             mprintf(( "speech Rule.ulId : %d \n", pElements->Rule.ulId ));
  400. -           //MessageBoxW(NULL,pwszText,NULL,MB_OK);   
  401. +           //MessageBoxW(NULL,pwszText,NULL,MB_OK);
  402. +
  403. +           ::CoTaskMemFree(pwszText);
  404.         }
  405. -#endif    
  406. +#endif
  407.         int part1, part2, part3;
  408.         part1 = part2 = part3 = -1;
  409. -        switch ( pElements->Rule.ulId )
  410. -        {
  411. +       switch ( pElements->Rule.ulId )
  412. +       {
  413.  
  414.             case VID_WingName:
  415.             {
  416. @@ -327,9 +314,24 @@ void VOICEREC_execute_command(ISpPhrase *pPhrase, HWND hWnd)
  417.                 if (part2 == -1)
  418.                     break; // no ship number or wing
  419.  
  420. +               const wchar_t* valstr = pElements->pProperties->pszValue;
  421. +               char* wing_name = new char[wcslen(valstr)+1];
  422. +
  423. +               size_t j = 0;
  424. +
  425. +               for (size_t i = 0; i < wcslen(valstr); i++) {
  426. +                   int c = wctob(valstr[i]);
  427. +                   if (c != EOF) {
  428. +                       wing_name[j++] = static_cast<char>(c);
  429. +                   }
  430. +               }
  431. +               wing_name[j] = '\0';
  432. +
  433.                 if (part2 == 0)
  434.                 {
  435. -                   Msg_instance = wing_lookup(wing_names[part1]);
  436. +                   Msg_instance = wing_lookup(wing_name);
  437. +                   delete[] wing_name;
  438. +
  439.                     if (Msg_instance < 0)
  440.                         break;
  441.  
  442. @@ -338,13 +340,14 @@ void VOICEREC_execute_command(ISpPhrase *pPhrase, HWND hWnd)
  443.                         hud_squadmsg_toggle();
  444.                     }
  445.  
  446. -                   hud_squadmsg_do_mode(SM_MODE_WING_COMMAND);            
  447. +                   hud_squadmsg_do_mode(SM_MODE_WING_COMMAND);
  448.                 }
  449. -               else
  450. +               else
  451.                 {
  452.                     char shipName[NAME_LENGTH];
  453. -                   wing_bash_ship_name(shipName, wing_names[part1], part2);
  454. -                  
  455. +                   wing_bash_ship_name(shipName, wing_name, part2);
  456. +                   delete[] wing_name;
  457. +
  458.                     Msg_instance = ship_name_lookup(shipName);
  459.  
  460.                     // Can't issue commands to yourself or to nobody
  461. @@ -358,20 +361,20 @@ void VOICEREC_execute_command(ISpPhrase *pPhrase, HWND hWnd)
  462.                         hud_squadmsg_toggle();
  463.                     }
  464.  
  465. -                   hud_squadmsg_do_mode(SM_MODE_SHIP_COMMAND);            
  466. +                   hud_squadmsg_do_mode(SM_MODE_SHIP_COMMAND);
  467.  
  468.                 }
  469.  
  470. -               if (part3 == -1)
  471. +               if (part3 == -1)
  472.                     break;
  473.             }
  474.  
  475.             case VID_Action:
  476.             {
  477.                 int action;
  478. -               if (part3 == -1)
  479. +               if (part3 == -1)
  480.                 {
  481. -                   action = pElements->pProperties->vValue.ulVal;                 
  482. +                   action = pElements->pProperties->vValue.ulVal;
  483.                 }
  484.                 else
  485.                 {
  486. @@ -401,7 +404,7 @@ void VOICEREC_execute_command(ISpPhrase *pPhrase, HWND hWnd)
  487.                
  488.                 switch(action)
  489.                 {
  490. -                   case VID_Ships:
  491. +                   case VID_Ships:
  492.                         hud_squadmsg_do_mode( SM_MODE_SHIP_SELECT );
  493.                         break;
  494.  
  495. @@ -409,22 +412,22 @@ void VOICEREC_execute_command(ISpPhrase *pPhrase, HWND hWnd)
  496.                         hud_squadmsg_do_mode( SM_MODE_WING_SELECT );
  497.                         break;
  498.  
  499. -                   case VID_AllFighters:
  500. +                   case VID_AllFighters:
  501.                     case VID_AllWings:
  502.                     //  Msg_instance = MESSAGE_ALL_FIGHTERS;
  503.                     //  Squad_msg_mode == SM_MODE_ALL_FIGHTERS
  504.                         hud_squadmsg_msg_all_fighters();
  505. -                       if (pElements->pProperties->pNextSibling) {
  506. -                           doVid_Action(pElements->pProperties->pNextSibling->vValue.ulVal);
  507. +                       if (pElements->pProperties->pFirstChild) {
  508. +                           doVid_Action(pElements->pProperties->pFirstChild->vValue.ulVal);
  509.                         }
  510.  
  511.                     //  if(Msg_shortcut_command == -1)
  512.                     //  {
  513. -                   //      hud_squadmsg_do_mode( SM_MODE_ALL_FIGHTERS );
  514. +                   //      hud_squadmsg_do_mode( SM_MODE_ALL_FIGHTERS );
  515.                     //  }
  516.                         break;
  517.  
  518. -                   case VID_Reinforcements:
  519. +                   case VID_Reinforcements:
  520.                         hud_squadmsg_do_mode( SM_MODE_REINFORCEMENTS );
  521.                         break;
  522.  
  523. @@ -466,7 +469,7 @@ void VOICEREC_execute_command(ISpPhrase *pPhrase, HWND hWnd)
  524.                 int action = pElements->pProperties->vValue.ulVal;
  525.                 mprintf(("Targeting/speed %d \n", action));
  526.  
  527. -               if (action > -1)
  528. +               if (action > -1)
  529.                 {
  530.                     button_function( action );
  531.                 }
  532. @@ -500,19 +503,19 @@ void VOICEREC_execute_command(ISpPhrase *pPhrase, HWND hWnd)
  533.             }
  534.    
  535.    
  536. -   /* 
  537. -      
  538. -      
  539. +   /*
  540. +
  541. +
  542.     hud_squadmsg_do_mode( SM_MODE_SHIP_COMMAND );               // and move to a new mode
  543.     hud_squadmsg_do_mode( SM_MODE_WING_COMMAND );               // and move to a new mode
  544.  hud_squadmsg_do_mode( SM_MODE_SHIP_COMMAND );
  545.  
  546.  */
  547.  
  548. -        }
  549. -        // Free the pElements memory which was allocated for us
  550. -        ::CoTaskMemFree(pElements);
  551. -    }
  552. +       }
  553. +       // Free the pElements memory which was allocated for us
  554. +       ::CoTaskMemFree(pElements);
  555. +   }
  556.  
  557.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement