Windows Speech Recognition Programming

Source: Internet
Author: User
Speech
1) Before constructing a speech class, you must set up the engineering environment:
A. download and install the Windows speech SDK from the Microsoft official website, and set it in Visual Studio 6.0. On the C ++ tab of the Project setings option, click "category: preprocessor "add" ,__ win32_dcom "(indicating that the COM component is successfully initialized );
B. Select the pre-processing header file option "automatic use of pre-compensation Header ";
C. On the General tab, select "Practical MFC static Connection Library ";
Figure: (1)

2) encapsulate voice
Because of the object-oriented programming concept, the cspeech class is constructed using UML (Unified Modeling Language) as follows:

Cspeech class

+ Void initsr (); // initialize the voice
+ Void recoevent (); // identify command functions

+ Bool B _initsr;
+ Bool B _cmd_grammar;

// Three voice Interfaces
+ Ccomptr M_cprecocontxt;
+ Ccomptr M_cprecogrammar;
+ Ccomptr M_cprecoengine;

Then, add the voice class. Note that the header file of the custom voice class contains "sphelper. H" and the custom voice recognition message and type.
# Define gid_1__gr 333333
# Define wm_recoevent wm_user+ 102
The rest is to supplement the function body to the functions initsr () and recoevent () of the CPP file.

3) For details, see the following: Code :
(1) void cspeech: initsr ()
{
Hresult hR = s_ OK;
HR = m_cprecoengine.cocreateinstance (clsid_spinprocrecognizer); // create a com instance of the recognition engine
If (succeeded (HR ))
{
HR = m_cprecoengine-> createrecocontext (& m_cprecoctxt); // create a recognition Context
}
Else
MessageBox (hwnd, "error1", "error", s_ OK );
If (succeeded (HR ))
{
HR = m_cprecoctxt-> setpolicywindowmessage (hwnd, wm_recoevent, 0, 0 );
} // Message mechanism settings to enable the computer to listen to voice messages at any time
Else
MessageBox (hwnd, "error2", "error", s_ OK );
If (succeeded (HR ))
{
Ulonglong ullmyevents = spfei (spei_recognition) | spfei (spei_hypothesis );
HR = m_cprecoctxt-> setinterest (ullmyevents, ullmyevents );
}
Else
MessageBox (hwnd, "error3", "error", s_ OK );
// Set the default audio
Ccomptr M_cpaudio;
HR = spcreatedefaultobjectfromcategoryid (spcat_audioin, & m_cpaudio); // create a default audio input object
HR = m_cprecoengine-> setinput (m_cpaudio, true); // sets the recognition engine Input Source
HR = m_cprecoctxt-> creategrammar (gid_1__gr, & m_cpcmdgrammar); // CREATE command syntax
B _1__grammar = true;
If (failed (HR ))
{
MessageBox (hwnd, "error 4", "error", s_ OK );
}
HR = m_cpcmdgrammar-> load1_fromresource (null, makeintresourcew (idr_1_ctrl), L "srgrammar", makelangid (lang_neutral, clerk), splo_dynamic); // load the command syntax File
If (failed (HR ))
{
MessageBox (hwnd, "error5", "error", s_ OK );
}
B _initsr = true;
}

(2) bool cspeech: recoevent ()
{
Uses_conversion;
Cspevent event;
While (event. getfrom (m_cprecoctxt) = s_ OK)
{
Switch (event. eeventid)
{
Case spei_recognition:
{
Static const wchar wszunrecognized [] = l" ";
Cspdynamicstring dstrtext;
If (failed (event. recoresult ()-> gettext (sp_getwholephrase, sp_getwholephrase, true, & dstrtext, null )))
{
Dstrtext = wszunrecognized;
}
Dstrtext. copytobstr (& srout );
Recstring. Empty ();
Recstring = srout;
If (B _1__grammar)
{
If (recstring = "Left ")
{
Ispvoice * pvoice = NULL;
If (failed (coinitialize (null )))
{
MessageBox (hwnd, "error to initialize com", "error", s_ OK );
Return false;
}
Hresult hR = cocreateinstance (clsid_spvoice, null, clsctx_all, iid_ispvoice, (void **) & pvoice );
If (succeeded (HR ))
{
HR = pvoice-> speak (L "Left turn", 0, null );
Pvoice-> release ();
Pvoice = NULL;
}
Couninitialize ();
M_opengl-> m_baiscobj-> left = 1;
Return true;
}

If (recstring = "Go Down ")
{
Ispvoice * pvoice = NULL;
If (failed (coinitialize (null )))
{
MessageBox (hwnd, "error to initialize com", "error", s_ OK );
Return false;
}
Hresult hR = cocreateinstance (clsid_spvoice, null, clsctx_all, iid_ispvoice, (void **) & pvoice );
If (succeeded (HR ))
{
HR = pvoice-> speak (L "Start backward", 0, null );
Pvoice-> release ();
Pvoice = NULL;
}
Couninitialize ();
M_opengl-> m_baiscobj-> back = 1;
Return true;
}
If (recstring = "minimal ")
{
Ispvoice * pvoice = NULL;
If (failed (coinitialize (null )))
{
MessageBox (hwnd, "error to initialize com", "error", s_ OK );
Return false;
}
Hresult hR = cocreateinstance (clsid_spvoice, null, clsctx_all, iid_ispvoice, (void **) & pvoice );
If (succeeded (HR ))
{
HR = pvoice-> speak (L "minimized", 0, null );
Pvoice-> release ();
Pvoice = NULL;
}
Couninitialize ();
Sendmessage (hwnd, wm_syscommand, SC _minimize, makelparam (0, 0 ));

Return true;
}
If (recstring = "right ")
{
Ispvoice * pvoice = NULL;
If (failed (coinitialize (null )))
{
MessageBox (hwnd, "error to initialize com", "error", s_ OK );
Return false;
}
Hresult hR = cocreateinstance (clsid_spvoice, null, clsctx_all, iid_ispvoice, (void **) & pvoice );
If (succeeded (HR ))
{
HR = pvoice-> speak (L "start to turn right", 0, null );
Pvoice-> release ();
Pvoice = NULL;
}
Couninitialize ();
M_opengl-> m_baiscobj-> right = 1;
Return true;
}
If (recstring = "stop ")
{
Ispvoice * pvoice = NULL;
If (failed (coinitialize (null )))
{
MessageBox (hwnd, "error to initialize com", "error", s_ OK );
Return false;
}
Hresult hR = cocreateinstance (clsid_spvoice, null, clsctx_all, iid_ispvoice, (void **) & pvoice );
If (succeeded (HR ))
{
HR = pvoice-> speak (L "action started", 0, null );
Pvoice-> release ();
Pvoice = NULL;
}
Couninitialize ();
M_opengl-> m_baiscobj-> move = 0;
M_opengl-> m_baiscobj-> back = 0;
M_opengl-> m_baiscobj-> left = 0;
M_opengl-> m_baiscobj-> right = 0;
Return true;
}

If (recstring = "running ")
{
Ispvoice * pvoice = NULL;
If (failed (coinitialize (null )))
{
MessageBox (hwnd, "error to initialize com", "error", s_ OK );
Return false;
}
Hresult hR = cocreateinstance (clsid_spvoice, null, clsctx_all, iid_ispvoice, (void **) & pvoice );
If (succeeded (HR ))
{
HR = pvoice-> speak (L "action started", 0, null );
Pvoice-> release ();
Pvoice = NULL;
}
Couninitialize ();
M_opengl-> m_baiscobj-> move = 1;
Return true;
}
If (recstring = "exit ")
{
M_opengl-> cleanup (); // End Processing
Postquitmessage (0 );
Return true;
}
}
}
}
} Return true;
}
Note that the recoevent () must be able to process the roaming of characters and cameras. Therefore, the control variables move, back, left, and right are added to the behavior functions of characters and cameras; with initial value 1 attached, when the behavior function is 1, the behavior function body is executed, so it must also
# Include "OpenGL. H"
# Include "baiscobj. H"
In the meantime, we use pointer variables to cleverly enable the voice to control the behavior, but it does not affect the animation refresh. However, the disadvantage is that the OpenGL animation md2 model cannot be imported successfully, so that the player can play the game, function functions such as jump wooden box are not completed, so as long as the behavior function is available, speech control can be implemented through the same method as above.

4) how to execute speech in the winmain () function Program ?
First, it contains the voice header file <SAPI. h> 〉
Then (# define cspeech speech) defines the Speech Class Object

Int winapi winmain (hinstance hinst, hinstance, lpstr, INT) // winmain program entry
{
: Coinitializeex (null, coinit_apartmentthreaded); // initialize com
......
Char CC [] = "TMl ";
Wndclassex WC = {sizeof (wndclassex), cs_classdc, msgproc, 0l, 0l,
Getmodulehandle (null), null, null, (lpctstr) idr_menu1,
CC, null };
Registerclassex (& WC );
M_opengl = new OpenGL ();//
Hwnd = createdomainwex (null, CC, "smart genie keyboard (← In and Out → right ← up left up dowm down )",
Dwstyle | ws_clipchildren | ws_clipsiblings, NX, NY, width, height, null, null, hinst, null); // create a window
Showwindow (hwnd, sw_showdefault); // display window
Updatewindow (hwnd); // refresh the window
Speech. B _cmd_grammar = false;
Speech. initsr ();
Gameloop (); // enters the message loop
Return 0;
}
Through speech. initsr (): executes speech initialization. To set a Simple Speech Recognition switch, simply add a taskbar with only voice as a menu resource, and then use the message mechanism, add the following in switch (Message) in the message processing function:
Case wm_command:
Switch (loword (wparam ))
{
Case idm_speech: speech. startcmd ();
}
Return 0; break;
That is, when you click the voice menu, the voice function is fully activated. The following is the entity of this function:

Void cspeech: startcmd ()
{
If (B _initsr)
{
Hresult hR = m_cpcmdgrammar-> setrulestate (null, null, sprs_active );
Ispvoice * pvoice = NULL;
If (failed (coinitialize (null )))
{
MessageBox (hwnd, "error to initialize com", "error", s_ OK );
Return;
}
HR = cocreateinstance (clsid_spvoice, null, clsctx_all, iid_ispvoice, (void **) & pvoice );
If (succeeded (HR ))
{
HR = pvoice-> speak (L "syntax activated", 0, null );
Pvoice-> release ();
Pvoice = NULL;
}
Couninitialize ();
}
}
5) before all the work is completed, you must add an XML syntax file under the project and load it using the loadcyclfromresource () function in initsr; the XML file can be opened and edited in Notepad format. The details are as follows:
- - -

Lower

Left

Right

Go up

Go down

Hop

Stop

Running

Recognition

Speech

Restore

File

Playing football

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.