1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
|
/*
* Code created by Thomas Whittaker (Random Tiger) for a FreeSpace 2 source code project
*
* You may not sell or otherwise commercially exploit the source or things you
* created based on the source.
*
*/
#ifdef _WIN32
#ifndef FS2_VOICER
#if NDEBUG
#pragma message( "WARNING: You have not compiled voice recognition into this build (use FS2_VOICER)" )
#endif // NDEBUG
#else // to end-of-file ...
#ifdef LAUNCHER
#include "stdafx.h"
#endif //LAUNCHER
#include <sphelper.h> // Contains definitions of SAPI functions
#include <stdio.h>
#include "voicerec.h"
#include "grammar.h"
// FreeSpace includes
#include "hud/hudsquadmsg.h"
#include "io/keycontrol.h"
#include "playerman/player.h"
#include "ship/ship.h"
CComPtr<ISpRecoGrammar> p_grammarObject; // Pointer to our grammar object
CComPtr<ISpRecoContext> p_recogContext; // Pointer to our recognition context
CComPtr<ISpRecognizer> p_recogEngine; // Pointer to our recognition engine instance
const bool DEBUG_ON = false;
bool VOICEREC_init(HWND hWnd, int event_id, int grammar_id, int command_resource)
{
HRESULT hr = S_OK;
CComPtr<ISpAudio> cpAudio;
while ( 1 )
{
// create a recognition engine
hr = p_recogEngine.CoCreateInstance(CLSID_SpSharedRecognizer);
if ( FAILED( hr ) )
{
MessageBox(hWnd,"Failed to create a recognition engine\n","Error",MB_OK);
printf("Failed to create a recognition engine\n");
break;
}
// create the command recognition context
hr = p_recogEngine->CreateRecoContext( &p_recogContext );
if ( FAILED( hr ) )
{
MessageBox(hWnd,"Failed to create the command recognition context\n","Error",MB_OK);
printf("Failed to create the command recognition context\n");
break;
}
// Let SR know that window we want it to send event information to, and using
// what message
hr = p_recogContext->SetNotifyWindowMessage( hWnd, event_id, 0, 0 );
if ( FAILED( hr ) )
{
MessageBox(hWnd,"Failed to SetNotifyWindowMessage\n","Error",MB_OK);
break;
}
// Tell SR what types of events interest us. Here we only care about command
// recognition.
hr = p_recogContext->SetInterest( SPFEI(SPEI_RECOGNITION), SPFEI(SPEI_RECOGNITION) );
if ( FAILED( hr ) )
{
MessageBox(hWnd,"Failed to set events\n","Error",MB_OK);
break;
}
// Load our grammar, which is the compiled form of simple.xml bound into this executable as a
// user defined ("SRGRAMMAR") resource type.
hr = p_recogContext->CreateGrammar(grammar_id, &p_grammarObject);
if (FAILED(hr))
{
MessageBox(hWnd,"Failed to load grammar\n","Error",MB_OK);
break;
}
hr = p_grammarObject->LoadCmdFromResource(NULL, MAKEINTRESOURCEW(command_resource),
L"SRGRAMMAR", MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
SPLO_DYNAMIC);
if ( FAILED( hr ) )
{
MessageBox(hWnd,"Failed to load resource\n","Error",MB_OK);
break;
}
// Set rules to active, we are now listening for commands
hr = p_grammarObject->SetRuleState(NULL, NULL, SPRS_ACTIVE );
if ( FAILED( hr ) )
{
MessageBox(hWnd,"Failed to set listening for commands\n","Error",MB_OK);
break;
}
break;
}
// if we failed and have a partially setup SAPI, close it all down
if ( FAILED( hr ) )
{
VOICEREC_deinit();
}
return ( hr == S_OK);
}
void VOICEREC_deinit()
{
// Release grammar, if loaded
if ( p_grammarObject )
{
p_grammarObject.Release();
}
// Release recognition context, if created
if ( p_recogContext )
{
p_recogContext->SetNotifySink(NULL);
p_recogContext.Release();
}
// Release recognition engine instance, if created
if ( p_recogEngine )
{
p_recogEngine.Release();
}
}
void VOICEREC_execute_command(ISpPhrase *pPhrase, HWND hWnd);
void VOICEREC_process_event(HWND hWnd)
{
CSpEvent event; // Event helper class
// Loop processing events while there are any in the queue
while (event.GetFrom(p_recogContext) == S_OK)
{
// Look at recognition event only
switch (event.eEventId)
{
case SPEI_RECOGNITION:
VOICEREC_execute_command(event.RecoResult(), hWnd);
break;
}
}
}
// Its not enough to update this, phrases.xml must have this info as well
char *wing_names[] =
{
"Alpha",
"Beta",
"Gamma",
"Delta",
"Epsilon",
"Zeta",
"Eta",
"Theta",
"Iota",
"Kappa",
"Lambda",
"Mu",
"Nu",
"Xi",
"Omicron",
"Pi",
"Rho",
"Sigma",
"Tau",
"Upsilon",
"Phi",
"Chi",
"Psi",
"Omega",
};
/******************************************************************************
* ExecuteCommand *
*----------------*
* Description:
* Called to Execute commands that have been identified by the speech engine.
*
******************************************************************************/
extern int button_function(int n);
extern void hud_squadmsg_msg_all_fighters();
extern void hud_squadmsg_shortcut( int command );
//extern void hud_squadmsg_ship_command();
extern int Msg_instance;;
extern int Msg_shortcut_command;
extern int Squad_msg_mode;
char VOICEREC_lastCommand[30];
void VOICEREC_execute_command(ISpPhrase *pPhrase, HWND hWnd)
{
SPPHRASE *pElements;
// Get the phrase elements, one of which is the rule id we specified in
// the grammar. Switch on it to figure out which command was recognized.
if (SUCCEEDED(pPhrase->GetPhrase(&pElements)))
{
if(DEBUG_ON)
{
WCHAR *pwszText;
pPhrase->GetText(SP_GETWHOLEPHRASE, SP_GETWHOLEPHRASE, TRUE, &pwszText, NULL);
MessageBoxW(NULL,pwszText,NULL,MB_OK);
}
switch ( pElements->Rule.ulId )
{
case VID_ShipName:
{
int wingType = pElements->pProperties->vValue.ulVal;
int shipNum = pElements->pProperties->pNextSibling->vValue.ulVal;
char shipName[NAME_LENGTH];
sprintf(shipName,"%s %d", wing_names[wingType], shipNum);
Msg_instance = ship_name_lookup(shipName);
// Can't issue commands to yourself or to nobody
if(Msg_instance < 0 || Msg_instance == Player_obj->instance)
{
break;
}
if(!(Player->flags & PLAYER_FLAGS_MSG_MODE))
{
hud_squadmsg_toggle();
}
hud_squadmsg_do_mode(SM_MODE_SHIP_COMMAND);
break;
}
case VID_WingName:
{
int wingType = pElements->pProperties->vValue.ulVal;
Msg_instance = wing_lookup(wing_names[wingType]);
if (Msg_instance < 0)
break;
if(!(Player->flags & PLAYER_FLAGS_MSG_MODE))
{
hud_squadmsg_toggle();
}
hud_squadmsg_do_mode(SM_MODE_WING_COMMAND);
break;
}
case VID_Action:
{
int action = pElements->pProperties->vValue.ulVal;
// If menu is up
if(Player->flags & PLAYER_FLAGS_MSG_MODE)
{
switch(action)
{
case VID_DestoryTarget: Msg_shortcut_command = ATTACK_TARGET_ITEM; break;
case VID_DisableTarget: Msg_shortcut_command = DISABLE_TARGET_ITEM; break;
case VID_DisarmTarget: Msg_shortcut_command = DISARM_TARGET_ITEM; break;
case VID_DestroySubsys: Msg_shortcut_command = DISABLE_SUBSYSTEM_ITEM; break;
case VID_ProtectTarget: Msg_shortcut_command = PROTECT_TARGET_ITEM; break;
case VID_IgnoreTarget: Msg_shortcut_command = IGNORE_TARGET_ITEM; break;
case VID_FormWing: Msg_shortcut_command = FORMATION_ITEM; break;
case VID_CoverMe: Msg_shortcut_command = COVER_ME_ITEM; break;
case VID_EngageEnemy: Msg_shortcut_command = ENGAGE_ENEMY_ITEM; break;
case VID_Depart: Msg_shortcut_command = DEPART_ITEM; break;
default: Msg_shortcut_command = -1; break;
}
if(Msg_instance == MESSAGE_ALL_FIGHTERS || Squad_msg_mode == SM_MODE_ALL_FIGHTERS )
{
// nprintf(("warning", "VOICER hud_squadmsg_send_to_all_fighters\n"));
hud_squadmsg_send_to_all_fighters(Msg_shortcut_command);
}
else if(Squad_msg_mode == SM_MODE_SHIP_COMMAND)
{
// nprintf(("warning", "VOICER msg ship %d\n", Msg_instance));
hud_squadmsg_send_ship_command( Msg_instance, Msg_shortcut_command, 1 );
}
else if(Squad_msg_mode == SM_MODE_WING_COMMAND)
{
// nprintf(("warning", "VOICER msg wing %d\n", Msg_instance));
hud_squadmsg_send_wing_command( Msg_instance, Msg_shortcut_command, 1 );
}
else if(Squad_msg_mode == SM_MODE_REINFORCEMENTS )
{
}
hud_squadmsg_toggle();
}
else
{
switch(action)
{
case VID_DestoryTarget: button_function(ATTACK_MESSAGE); break;
case VID_DisableTarget: button_function(DISABLE_MESSAGE); break;
case VID_DisarmTarget: button_function(DISARM_MESSAGE); break;
case VID_DestroySubsys: button_function(ATTACK_SUBSYSTEM_MESSAGE); break;
case VID_ProtectTarget: button_function(PROTECT_MESSAGE); break;
case VID_IgnoreTarget: button_function(IGNORE_MESSAGE); break;
case VID_FormWing: button_function(FORM_MESSAGE); break;
case VID_CoverMe: button_function(COVER_MESSAGE); break;
case VID_EngageEnemy: button_function(ENGAGE_MESSAGE); break;
case VID_Depart: button_function(WARP_MESSAGE); break;
}
}
break;
}
// These commands run no matter what, and will even bring up the menu
case VID_TopMenu:
{
int action = pElements->pProperties->vValue.ulVal;
bool msgWindow = false;
if (Player->flags & PLAYER_FLAGS_MSG_MODE)
{
msgWindow = true;
}
// If the command window is not up, or it is and its a cancel request toggle
if((msgWindow && action == VID_Cancel) || (!msgWindow && action != VID_Cancel))
{
hud_squadmsg_toggle();
}
switch(action)
{
case VID_Ships:
hud_squadmsg_do_mode( SM_MODE_SHIP_SELECT );
break;
case VID_Wings:
hud_squadmsg_do_mode( SM_MODE_WING_SELECT );
break;
case VID_AllFighters:
case VID_AllWings:
// Msg_instance = MESSAGE_ALL_FIGHTERS;
// Squad_msg_mode == SM_MODE_ALL_FIGHTERS
hud_squadmsg_msg_all_fighters();
// if(Msg_shortcut_command == -1)
// {
// hud_squadmsg_do_mode( SM_MODE_ALL_FIGHTERS );
// }
break;
case VID_Reinforcements:
hud_squadmsg_do_mode( SM_MODE_REINFORCEMENTS );
break;
case VID_SupportShip:
hud_squadmsg_do_mode( SM_MODE_REPAIR_REARM );
break;
case VID_AbortSupport:
hud_squadmsg_do_mode( SM_MODE_REPAIR_REARM_ABORT );
break;
case VID_More:
break;
}
break;
}
/*
hud_squadmsg_do_mode( SM_MODE_SHIP_COMMAND ); // and move to a new mode
hud_squadmsg_do_mode( SM_MODE_WING_COMMAND ); // and move to a new mode
hud_squadmsg_do_mode( SM_MODE_SHIP_COMMAND );
*/
}
// Free the pElements memory which was allocated for us
::CoTaskMemFree(pElements);
}
}
#endif // VOICER
#endif // _WIN32
|