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

📄 ch19.htm

📁 MAPI__SAPI__TAPI
💻 HTM
📖 第 1 页 / 共 3 页
字号:
  BOOL BeginOLE (void)<br>
  {<br>
  &nbsp;&nbsp;&nbsp;DWORD&nbsp;&nbsp;&nbsp;&nbsp;dwVer;<br>
  <br>
  //&nbsp;Initialize OLE<br>
  <br>
  &nbsp;&nbsp;&nbsp;SetMessageQueue(96);<br>
  &nbsp;&nbsp;&nbsp;dwVer = CoBuildVersion();<br>
  <br>
  &nbsp;&nbsp;&nbsp;if (rmm != HIWORD(dwVer)) return FALSE;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
  error<br>
  <br>
  &nbsp;&nbsp;&nbsp;if (FAILED(CoInitialize(NULL))) return FALSE; <br>
  <br>
  &nbsp;&nbsp;&nbsp;return TRUE;<br>
  }<br>
  <br>
  <br>
  /************************************************************************* <br>
  EndOLE - This closes up the OLE.<br>
  <br>
  inputs<br>
  &nbsp;&nbsp;&nbsp;none<br>
  returns<br>
  &nbsp;&nbsp;&nbsp;BOOL - TRUE if succede<br>
  */<br>
  <br>
  BOOL EndOLE (void)<br>
  {<br>
  //&nbsp;Free up all of OLE<br>
  <br>
  &nbsp;&nbsp;&nbsp;CoUninitialize ();<br>
  <br>
  &nbsp;&nbsp;&nbsp;return TRUE;<br>
  }</font></tt> </p>
</blockquote>

<hr>

<p>The code in Listing 19.3 shows three steps in the <tt><font FACE="Courier">BeginOLE</font></tt> 
routine. The first line creates a message queue that can hold up to 96 messages. The next 
two lines check the OLE version, and the last line actually initializes the OLE session. 
The <tt><font FACE="Courier">EndOLE</font></tt> session simply releases the OLE session 
you created in <tt><font FACE="Courier">BeginOLE</font></tt>. </p>

<h4>Selecting the TTS Engine Object</h4>

<p>After creating the OLE session, you need to locate and select a valid TTS engine 
object. There are just a few steps to the process. First, you need a few local variables 
to keep track of your progress. Listing 19.4 shows the initial declarations for the <tt><font
FACE="Courier">FindAndSelect</font></tt> procedure. </p>

<hr>

<blockquote>
  <b><p>Listing 19.4. Declarations for the <tt><font FACE="Courier">FindAndSelect</font></tt> 
  procedure.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>/************************************************************************* 
  <br>
  FindAndSelect - This finds and selects according to the specific TTSMODEINFOW.<br>
  <br>
  inputs<br>
  &nbsp;&nbsp;&nbsp;PTTSMODEINFOW&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pTTSInfo - desired mode<br>
  returns<br>
  &nbsp;&nbsp;&nbsp;PITTSCENTRAL - ISRCentral interface to TTS engine <br>
  sets:<br>
  <br>
  */<br>
  <br>
  PITTSCENTRALW FindAndSelect (PTTSMODEINFOW pTTSInfo)<br>
  {<br>
  &nbsp;&nbsp;&nbsp;HRESULT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hRes; <br>
  &nbsp;&nbsp;&nbsp;TTSMODEINFOW&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ttsResult;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;final 
  result<br>
  &nbsp;&nbsp;&nbsp;WchAR&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Zero = 
  0;<br>
  &nbsp;&nbsp;&nbsp;PITTSFINDW&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pITTSFind;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;find 
  interface<br>
  &nbsp;&nbsp;&nbsp;PIAUDIOMULTIMEDIADEVICE&nbsp;&nbsp;&nbsp;&nbsp;pIAMM;&nbsp;&nbsp;//&nbsp;multimedia 
  device interface for audio-dest<br>
  &nbsp;&nbsp;&nbsp;PITTSCENTRALW&nbsp;&nbsp;pITTSCentral;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;central 
  interface</font></tt> </p>
</blockquote>

<hr>

<p>Next, you need to create an instance of the <tt><font FACE="Courier">TTSFind</font></tt> 
object. This will be used to select an available TTS engine. Listing 19.5 shows how this 
is done. </p>

<hr>

<blockquote>
  <b><p>Listing 19.5. Creating the <tt><font FACE="Courier">TTSFind</font></tt> object.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>hRes = CoCreateInstance(CLSID_TTSEnumerator, NULL, CLSCTX_ALL, 
  IID_ITTSFindW,<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;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&amp;nbs 
  p;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(void**)&amp;pITTSFind); <br>
  &nbsp;&nbsp;&nbsp;if (FAILED(hRes)) return NULL;<br>
  <br>
  &nbsp;&nbsp;&nbsp;hRes = pITTSFind-&gt;Find(pTTSInfo, NULL, &amp;ttsResult); <br>
  <br>
  &nbsp;&nbsp;&nbsp;if (hRes)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pITTSFind-&gt;Release();<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return NULL;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;error 
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}</font></tt> </p>
</blockquote>

<hr>

<p>The next step is to locate and select an audio output object. This will be used by the 
TTS engine for playback of the text. Listing 19.6 shows the code needed to select an 
available audio device. </p>

<hr>

<blockquote>
  <b><p>Listing 19.6. Selecting an available audio device.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>//&nbsp;Get the audio dest<br>
  &nbsp;&nbsp;&nbsp;hRes = CoCreateInstance(CLSID_MMAudioDest, NULL, CLSCTX_ALL, <font
  FACE="ZAPFDINGBATS">&Acirc;</font>IID_IAudioMultiMediaDevice,(void**)&amp;pIAMM); <br>
  &nbsp;&nbsp;&nbsp;if (hRes)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pITTSFind-&gt;Release;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return NULL;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// error<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
  &nbsp;&nbsp;&nbsp;&nbsp;pIAMM-&gt;DeviceNumSet (WAVE_MappeR);</font></tt> </p>
</blockquote>

<hr>

<p>The code in Listing 19.6 uses the <tt><font FACE="Courier">DeviceNumSet</font></tt> 
method of the <tt><font FACE="Courier">MMAudioDest</font></tt> interface to find the 
available WAVE output device for the pc. </p>

<p>Once you have successfully created the <tt><font FACE="Courier">TSFind</font></tt> 
object and the <tt><font FACE="Courier">MMAudioDest</font></tt> object, you're ready to 
use the <tt><font FACE="Courier">Select</font></tt> method of <tt><font FACE="Courier">TTSFind</font></tt> 
to return a handle to a valid TTS engine object. After getting the handle, you can release 
the <tt><font FACE="Courier">TTSFind</font></tt> object because it was needed only to 
locate a valid TTS engine object. Listing 19.7 shows how this is done. </p>

<hr>

<blockquote>
  <b><p>Listing 19.7. Selecting the TTS engine and releasing <tt><font FACE="Courier">TTSFind</font></tt>. 
  <br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>//&nbsp;Pass off the multi-media-device interface as an 
  IUnknown (since it is one)<br>
  <br>
  //&nbsp;Should do select now<br>
  <br>
  &nbsp;&nbsp;&nbsp;hRes = pITTSFind-&gt;Select(ttsResult.gModeID, &amp;pITTSCentral, 
  (LPUNKNOWN) pIAMM);<br>
  <br>
  &nbsp;&nbsp;&nbsp;if (hRes) {<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pITTSFind-&gt;Release();<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return NULL;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<br>
  <br>
  //&nbsp;free random stuff up<br>
  <br>
  &nbsp;&nbsp;&nbsp;pITTSFind-&gt;Release();<br>
  <br>
  &nbsp;&nbsp;&nbsp;return pITTSCentral;<br>
  }</font></tt> </p>
</blockquote>

<hr>

<p>After getting a valid TTS engine and a valid audio output, you can start sending text 
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>

<h4>Sending Text to the TTS Engine</h4>

<p>The <tt><font FACE="Courier">TTSDEMO</font></tt> project uses a simple dialog box to 
accept text from the user and send it to the TTS engine. Figure 19.1 shows the dialog box 
in design mode. </p>

<p><a HREF="f19-1.gif"><b>Figure 19.1 : </b><i>The dialog box from the TTSDEMO project.</i></a> 
</p>

<p>This dialog box has a single text window and two command buttons-<tt><font
FACE="Courier">Speak</font></tt> and <tt><font FACE="Courier">Exit</font></tt>. When the 
user presses the <tt><font FACE="Courier">OK</font></tt> button, the text typed into the 
window is sent to the TTS engine for playback. The code in Listing 19.8 shows the <tt><font
FACE="Courier">CallBack</font></tt> routine that handles the dialog box events. </p>

<hr>

<blockquote>
  <b><p>Listing 19.8. Handling the dialog box callback.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>/************************************************************************* 
  <br>
  DialogProc<br>
  */<br>
  BOOL CALLBACK DialogProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)<br>
  {<br>
  switch (uMsg) {<br>
  &nbsp;&nbsp;&nbsp;case WM_COMMAND:<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch (LOWORD(wParam)) <br>
  &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;case IDOK:<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;char&nbsp;&nbsp;szSpeak[1024]; 
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;WchAR 
  wszSpeak[1024];<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SDATA 
  data;<br>
  <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// 
  Speak<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;GetDlgItemText 
  (hWnd, IDC_EDIT, szSpeak, sizeof(szSpeak));<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data.dwSize 
  = (DWORD)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MultiByteToWideChar(CP_ACP, 
  0, szSpeak, -1, wszSpeak,<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sizeof(wszSpeak) 
  / sizeof(WchAR)) * sizeof(WchAR);<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data.pData 

⌨️ 快捷键说明

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