File: voicerec.cpp

package info (click to toggle)
freespace2 3.7.4%2Brepack-1.1
  • links: PTS, VCS
  • area: non-free
  • in suites: bullseye
  • size: 22,268 kB
  • sloc: cpp: 393,535; ansic: 4,106; makefile: 1,091; xml: 181; sh: 137
file content (510 lines) | stat: -rw-r--r-- 13,387 bytes parent folder | download | duplicates (2)
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
 * 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 "cfile/cfile.h"
#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
CComPtr<ISpAudio>				cpAudio;         // Pointer for Audio Input Device

const bool DEBUG_ON = false;

extern int button_function(int n);
extern void hud_squadmsg_msg_all_fighters();
extern void hud_squadmsg_shortcut( int command );
extern bool hud_squadmsg_ship_valid(ship *shipp, object *objp = nullptr);
extern int hud_squadmsg_wing_valid(wing *wingp);

extern int Msg_instance;;
extern int Msg_shortcut_command;
extern int Squad_msg_mode;

void doVid_Action(int action)
{
	// If menu is up
	if(Player->flags & PLAYER_FLAGS_MSG_MODE)
	{
		switch(action)
		{					
			case VID_DestroyTarget: 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 )
		{
			hud_squadmsg_send_to_all_fighters(Msg_shortcut_command);
		}
		else if(Squad_msg_mode == SM_MODE_SHIP_COMMAND)
		{
			// skip if player cannot order this ship
			if (hud_squadmsg_ship_valid(&Ships[Msg_instance]))
				hud_squadmsg_send_ship_command( Msg_instance, Msg_shortcut_command, 1 );
		}
		else if(Squad_msg_mode == SM_MODE_WING_COMMAND)
		{
			// skip if player cannot order this wing
			if (hud_squadmsg_wing_valid(&Wings[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_DestroyTarget: 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;
		}
	}
}


bool VOICEREC_init(HWND hWnd, int event_id, int grammar_id, int command_resource)
{
	HRESULT hr = S_OK;

	while (true)
	{
		// create a recognition engine
		hr = p_recogEngine.CoCreateInstance(CLSID_SpInprocRecognizer);
		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;
		}

		// Create a grammar
		hr = p_recogContext->CreateGrammar(grammar_id, &p_grammarObject);
		if (FAILED(hr))
		{
			MessageBox(hWnd,"Failed to create grammar\n","Error",MB_OK);
			break;
		}

		// Load our grammar from data\phrases.xml, or if that doesn't exist, from the compiled in 
		// user defined ("SRGRAMMAR") resource type.
		hr = p_grammarObject->LoadCmdFromFile(L"data\\phrases.xml", SPLO_STATIC);
		if (FAILED(hr))
		{
			hr = p_grammarObject->LoadCmdFromResource(NULL, MAKEINTRESOURCEW(command_resource),
												L"SRGRAMMAR", MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
												SPLO_STATIC);
			if (FAILED(hr))
			{
				MessageBox(hWnd,"Failed to load resource SRGRAMMAR\n","Error",MB_OK);
				break;
			}
		}

		hr = SpCreateDefaultObjectFromCategoryId(SPCAT_AUDIOIN, &cpAudio);
		if (FAILED(hr))
		{
			MessageBox(hWnd,"Failed to get default audio input\n", "Error", MB_OK);
			break;
		}

		// Set the audio input to our token.
		hr = p_recogEngine->SetInput(cpAudio, TRUE);
		if (FAILED(hr))
		{
			MessageBox(hWnd,"Failed to set audio input\n", "Error", MB_OK);
		}

		// 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()
{
	if ( cpAudio )
	{
		cpAudio.Release();
	}
	// 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;

		}
	}
}

/******************************************************************************
* ExecuteCommand *
*----------------*
*   Description:
*       Called to Execute commands that have been identified by the speech engine.
*
******************************************************************************/

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)))
	{
#ifndef NDEBUG
		if(DEBUG_ON)
		{
			WCHAR *pwszText;
			char szText[255];
			int i;

			pPhrase->GetText(static_cast<ULONG>(SP_GETWHOLEPHRASE), static_cast<ULONG>(SP_GETWHOLEPHRASE), TRUE, &pwszText, NULL);

			memset(szText, 0, 255);
			for (i=0;i<254;i++) 
			{
				if (*(pwszText + i) == 0)
				{
					break;
				}
				szText[i] = (char)(*(pwszText + i));
				szText[i+1] = 0;
			}

			mprintf(( "recognized speech : %s \n", szText ));
			mprintf(( "speech Rule.ulId : %d \n", pElements->Rule.ulId ));
			mprintf(( "confidence: %f \n", pElements->pProperties->SREngineConfidence));

			::CoTaskMemFree(pwszText);
		}
#endif
		int part1, part2, part3;
		part1 = part2 = part3 = -1;
		switch ( pElements->Rule.ulId )
		{

			case VID_WingName: 
			{

				part1 = pElements->pProperties->vValue.ulVal;
				if (pElements->pProperties->pNextSibling) 
				{
					part2 = pElements->pProperties->pNextSibling->vValue.ulVal;
					if (pElements->pProperties->pNextSibling->pNextSibling) 
					{
						part3 = pElements->pProperties->pNextSibling->pNextSibling->vValue.ulVal;
					}
				}
				if (part2 == -1) 
					break; // no ship number or wing

				const wchar_t* valstr = pElements->pProperties->pszValue;
				char* wing_name = new char[wcslen(valstr)+1];

				size_t j = 0;

				for (size_t i = 0; i < wcslen(valstr); i++) {
					int c = wctob(valstr[i]);
					if (c != EOF) {
						wing_name[j++] = static_cast<char>(c);
					}
				}
				wing_name[j] = '\0';

				if (part2 == 0) 
				{
					Msg_instance = wing_lookup(wing_name);
					delete[] wing_name;

					if (Msg_instance < 0)
						break;

					if(!(Player->flags & PLAYER_FLAGS_MSG_MODE))
					{
						hud_squadmsg_toggle();
					}

					if (hud_squadmsg_wing_valid(&Wings[Msg_instance]))
						hud_squadmsg_do_mode(SM_MODE_WING_COMMAND);
				}
				else
				{
					char shipName[NAME_LENGTH];
					wing_bash_ship_name(shipName, wing_name, part2);
					delete[] wing_name;

					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();
					}

					if (hud_squadmsg_ship_valid(&Ships[Msg_instance]))
						hud_squadmsg_do_mode(SM_MODE_SHIP_COMMAND);

				}

				if (part3 == -1)
					break;
			}

			case VID_Action:
			{
				int action;
				if (part3 == -1)
				{
					action = pElements->pProperties->vValue.ulVal;
				}
				else
				{
					action = part3;
				}

				doVid_Action(action);
		
				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:
						hud_squadmsg_msg_all_fighters();
						// can have the action to perform spoken directly afterwards
						if (pElements->pProperties->pFirstChild) {
							doVid_Action(pElements->pProperties->pFirstChild->vValue.ulVal);
						}
						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;
			}
			// phrases for transferring shield energy to different locations
			case VID_shields:
			{
				int action = pElements->pProperties->vValue.ulVal;

				switch(action)
				{
					case 0: button_function( SHIELD_XFER_TOP ); break;
					case 1: button_function( SHIELD_XFER_LEFT ); break;
					case 2: button_function( SHIELD_XFER_RIGHT ); break;
					case 3: button_function( SHIELD_XFER_BOTTOM ); break;
					case 4: button_function( SHIELD_EQUALIZE ); break;

				}

				break;
			}
			// basic cheat, phrase as a value equivalent to the defines in ControlConfig/ControlsConfig.h
			// it just calls the button_function with this value
			case VID_speed:
			case VID_targeting:
			case VID_other:
			{
				int action = pElements->pProperties->vValue.ulVal;

				if (action > -1)
				{
					button_function( action );
				}
				break;
			}
			// nearly the same as the previous except it has some extra entries for
			// maximising/minimising energy
			case VID_power:
			{
				int action = pElements->pProperties->vValue.ulVal;

				if (action >= INCREASE_WEAPON && action <= ETS_EQUALIZE)
				{
					button_function( action );
				}
				else
				{
					// this is for the max engines etc.
					for (int i=1; i<7; i++)
					{
						button_function( action - 132 );
					}
				}
				break;
			}
		}
		// Free the pElements memory which was allocated for us
		::CoTaskMemFree(pElements);
	}

}


#endif // VOICER
#endif // _WIN32