⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch19.htm

📁 MAPI__SAPI__TAPI
💻 HTM
📖 第 1 页 / 共 3 页
字号:
  = wszSpeak;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gpITTSCentral-&gt;TextData 
  (chARSET_TEXT, 0,<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data, 
  NULL,<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IID_ITTSBufNotifySinkW); 
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} 
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 
  TRUE;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case IDCAncEL:<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;EndDialog 
  (hWnd, IDCAncEL);<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 
  TRUE;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br>
  &nbsp;&nbsp;&nbsp;};<br>
  <br>
  return FALSE;&nbsp;&nbsp;// didn't handle<br>
  }</font></tt> </p>
</blockquote>

<hr>

<p>Note that there are a couple of extra steps involved in sending the text to the TTS 
engine. The whole process involves filling an <tt><font FACE="Courier">SDATA</font></tt> 
structure to pass to the TTS engine. First, the <tt><font FACE="Courier">GetDlgItemText</font></tt> 
copies the text in the window into a memory location. The size of the string is inserted 
into the <tt><font FACE="Courier">SDATA</font></tt> structure. Next, the <tt><font
FACE="Courier">MultiByteToWideChar</font></tt> function is used to convert the data to 
wide character format, and the results are loaded into the <tt><font FACE="Courier">SDATA</font></tt> 
structure. Finally, the structure is passed to the TTS engine using the <tt><font
FACE="Courier">TextData</font></tt> method of the <tt><font FACE="Courier">TTSCentral</font></tt> 
interface. </p>

<p>That's all there is to it. You can test the application by compiling it yourself or by 
loading the <tt><font FACE="Courier">TTSDEMO.EXE</font></tt> application from the CD-ROM 
that ships with this book. </p>

<h2><a NAME="TheVCMDDemoProject"><font SIZE="5" COLOR="#FF0000">The VCMD Demo Project</font></a></h2>

<p>Building SR applications in C++ is not much different. The biggest change is that you 
need to create a menu object and load it with commands that will be spoken to the SR 
engine. In fact, the process of loading the commands and then checking the spoken phrase 
against the command list is the largest part of the code. </p>

<p>The C++ example reviewed here is part of the Microsoft Speech SDK. The project <tt><font
FACE="Courier">VCMDDEMO.MAK</font></tt> is installed when you install the SDK. If you have 
the SDK installed and own a copy of C++, you can load the project and review the source 
code while you read this chapter. </p>
<div align="center"><center>

<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
  <tr>
    <td><b>Note</b></td>
  </tr>
  <tr>
    <td><blockquote>
      <p>If you do not have a copy of the SDK or C++, you can still learn a lot by reviewing the 
      code shown here.</p>
    </blockquote>
    </td>
  </tr>
</table>
</center></div>

<p>There are two main parts to the project. The first is the <tt><font FACE="Courier">DEMO.CPP</font></tt> 
source code. This file contains the main C++ code for the project. The second part of the 
project is the <tt><font FACE="Courier">VCMDDEMO.RC</font></tt> resource file. This file 
contains the definitions of two dialog boxes used in the project. </p>

<h4>The Initial Header and Declarations for the <tt><font FACE="Courier">VCMDDEMO</font></tt> 
Project</h4>

<p>The first thing that must be done is to add the include and declaration statements to 
the source code. These will be used throughout the project. Most of the include files are 
a part of the VC++ system. However, the last two items (<tt><font FACE="Courier">speech.h</font></tt> 
and <tt><font FACE="Courier">resource.h</font></tt>) are unique. The <tt><font
FACE="Courier">speech.h</font></tt> file ships with the SDK. The <tt><font FACE="Courier">resource.h</font></tt> 
file contains information about the two dialog boxes used in the project. The <tt><font
FACE="Courier">define</font></tt> statements establish some constant values that will be 
used to track timer events later in the project. Listing 19.9 shows the header and include 
code for the project. </p>

<hr>

<blockquote>
  <b><p>Listing 19.9. The include and header code for the <tt><font FACE="Courier">VCMDDEMO</font></tt> 
  project.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>/*********************************************************************** 
  <br>
  Demo.Cpp - Code to quickly demo voice commands.<br>
  <br>
  Copyright c. 1995 by Microsoft Corporation<br>
  <br>
  */<br>
  <br>
  <br>
  #include &lt;windows.h&gt;<br>
  #include &lt;string.h&gt;<br>
  #include &lt;stdio.h&gt;<br>
  #include &lt;mmsystem.h&gt;<br>
  #include &lt;initguid.h&gt;<br>
  #include &lt;objbase.h&gt;<br>
  #include &lt;objerror.h&gt;<br>
  #include &lt;ole2ver.h&gt;<br>
  <br>
  #include &lt;speech.h&gt;<br>
  #include &quot;resource.h&quot;<br>
  <br>
  #define&nbsp;&nbsp;TIMER_chANGECOMMAND&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(52) <br>
  #define&nbsp;&nbsp;TIMER_CLEARRESULT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(53)</font></tt> 
  </p>
</blockquote>

<hr>

<p>The <tt><font FACE="Courier">VCMDDEMO</font></tt> project uses notification callbacks 
to receive messages from the SR engine. Since the SAPI system is based on the Component 
Object Model (COM), you'll need to include some code that creates the needed class object 
and associated methods. Listing 19.10 shows the code needed to declare the class and 
methods of the event notification routine. </p>

<hr>

<blockquote>
  <b><p>Listing 19.10. Declaring the event notification class and methods. <br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>//&nbsp;Voice Command notifications<br>
  class CIVCmdNotifySink : public IVCmdNotifySink {<br>
  &nbsp;&nbsp;&nbsp;&nbsp;private:<br>
  &nbsp;&nbsp;&nbsp;&nbsp;DWORD&nbsp;&nbsp;&nbsp;m_dwMsgCnt;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;HWND&nbsp;&nbsp;&nbsp;&nbsp;m_hwnd;<br>
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;public:<br>
  &nbsp;&nbsp;&nbsp;&nbsp;CIVCmdNotifySink(void);<br>
  &nbsp;&nbsp;&nbsp;&nbsp;~CIVCmdNotifySink(void);<br>
  <br>
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;// IUnknown members that delegate to m_punkOuter <br>
  &nbsp;&nbsp;&nbsp;&nbsp;// Non-delegating object IUnknown<br>
  &nbsp;&nbsp;&nbsp;&nbsp;STDMETHODIMP&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;QueryInterface 
  (REFIID, LPVOID FAR *);<br>
  &nbsp;&nbsp;&nbsp;&nbsp;STDMETHODIMP_(ULONG) AddRef(void);<br>
  &nbsp;&nbsp;&nbsp;&nbsp;STDMETHODIMP_(ULONG) Release(void);<br>
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;// IVCmdNotifySink members<br>
  &nbsp;&nbsp;&nbsp;&nbsp;STDMETHODIMP CommandRecognize (DWORD, PVCMDNAME, DWORD, DWORD, 
  PVOID,<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;DWORD,PSTR, 
  PSTR);<br>
  &nbsp;&nbsp;&nbsp;&nbsp;STDMETHODIMP CommandOther&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(PVCMDNAME, 
  PSTR);<br>
  &nbsp;&nbsp;&nbsp;&nbsp;STDMETHODIMP MenuActivate&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(PVCMDNAME, 
  BOOL);<br>
  &nbsp;&nbsp;&nbsp;&nbsp;STDMETHODIMP UtteranceBegin&nbsp;&nbsp;&nbsp;(void); <br>
  &nbsp;&nbsp;&nbsp;&nbsp;STDMETHODIMP UtteranceEnd&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void); <br>
  &nbsp;&nbsp;&nbsp;&nbsp;STDMETHODIMP CommandStart&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void); <br>
  &nbsp;&nbsp;&nbsp;&nbsp;STDMETHODIMP VUMeter&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(WORD); 
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;STDMETHODIMP AttribChanged&nbsp;&nbsp;&nbsp;&nbsp;(DWORD); <br>
  &nbsp;&nbsp;&nbsp;&nbsp;STDMETHODIMP Interference&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(DWORD); <br>
  };<br>
  typedef CIVCmdNotifySink * pcIVCmdNotifySink;</font></tt> </p>
</blockquote>

<hr>

<p>There is one more step needed as part of the initial declarations. The <tt><font
FACE="Courier">VCMDDEMO</font></tt> project must declare a list of commands to be loaded 
into the menu. These are the commands that the SR engine will be able to recognize when a 
user speaks. The project also uses a handful of other global-level declarations in the 
project. Listing 19.11 shows the final set of declarations for the project. </p>

<hr>

<blockquote>
  <b><p>Listing 19.11. Declaring the menu commands for the project. <br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>/************************************************************************* 
  <br>
  Globals */<br>
  <br>
  HINSTAncE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ghInstance;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;instance 
  handle<br>
  CIVCmdNotifySink&nbsp;&nbsp;gVCmdNotifySink;<br>
  HWND&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ghwndResultsDisplay 
  = NULL;<br>
  HWND&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ghwndDialog 
  = NULL;<br>
  PIVOICECMD&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gpIVoiceCommand = NULL;<br>
  PIVCMDDIALOGS&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gpIVCmdDialogs = NULL; <br>
  PIVCMDMENU&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gpIVCmdMenu = NULL;<br>
  char&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*gpszCommands 
  = NULL; // Commands<br>
  char&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*gpszCurCommand 
  = NULL;&nbsp;&nbsp;// current command that looking at<br>
  <br>
  char&nbsp;&nbsp;gszDefaultSet[] = // default command set<br>
  &nbsp;&nbsp;&nbsp;&quot;Help\r\n&quot;<br>
  &nbsp;&nbsp;&nbsp;&quot;Minimize window.\r\n&quot;<br>
  &nbsp;&nbsp;&nbsp;&quot;Maximize window.\r\n&quot;<br>
  &nbsp;&nbsp;&nbsp;&quot;What time is it?\r\n&quot;<br>
  &nbsp;&nbsp;&nbsp;&quot;What day is it?\r\n&quot;<br>
  &nbsp;&nbsp;&nbsp;&quot;Create a new file.\r\n&quot;<br>
  &nbsp;&nbsp;&nbsp;&quot;Delete the current file\r\n&quot;<br>
  &nbsp;&nbsp;&nbsp;&quot;Open a file\r\n&quot;<br>
  &nbsp;&nbsp;&nbsp;&quot;Switch to Word.\r\n&quot;<br>
  &nbsp;&nbsp;&nbsp;&quot;Switch to Excel.\r\n&quot;<br>
  &nbsp;&nbsp;&nbsp;&quot;Switch to calculator.\r\n&quot;<br>
  &nbsp;&nbsp;&nbsp;&quot;Change the background color.\r\n&quot; <br>
  &nbsp;&nbsp;&nbsp;&quot;Go to sleep.\r\n&quot;<br>
  &nbsp;&nbsp;&nbsp;&quot;Wake up.\r\n&quot;<br>
  &nbsp;&nbsp;&nbsp;&quot;Print the document.\r\n&quot;<br>
  &nbsp;&nbsp;&nbsp;&quot;Speak the text.\r\n&quot;<br>
  &nbsp;&nbsp;&nbsp;&quot;Paste\r\n&quot;<br>
  &nbsp;&nbsp;&nbsp;&quot;Copy\r\n&quot;;<br>
  <br>
  &nbsp;BOOL&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bNonFatalShutDown = FALSE;<br>
  &nbsp;int CheckNavigator(void);<br>
  &nbsp;DWORD VCMDState(PIVCMDATTRIBUTES);</font></tt> </p>
</blockquote>

<hr>

<h4>The <tt><font FACE="Courier">WinMain</font></tt> Procedure of the <tt><font
FACE="Courier">VCMDDEMO</font></tt> Project</h4>

<p>The main routine of the project is quite simple. First, the project performs basic 
initialization by starting the OLE session, initializing the SR engine, and making sure i </p>
</body>
</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -