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

📄 ch19.htm

📁 MAPI__SAPI__TAPI
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<html>

<head>
<title>Chapter 19 -- Creating SAPI Applications with C++</title>
<meta NAME="GENERATOR" CONTENT="Microsoft FrontPage 3.0">
</head>

<body TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<!-- Spidersoft WebZIP Ad Banner Insert -->
<!-- End of Spidersoft WebZIP Ad Banner Insert-->

<h1><font COLOR="#FF0000">Chapter 19</font></h1>

<h1><b><font SIZE="5" COLOR="#FF0000">Creating SAPI Applications with C++</font></b> </h1>

<hr WIDTH="100%">

<h3 ALIGN="CENTER"><font SIZE="+2" COLOR="#000000">CONTENTS<a NAME="CONTENTS"></a> </font></h3>

<ul>
  <li><a HREF="#TheTTSDemoProject">The TTS Demo Project</a> </li>
  <li><a HREF="#TheVCMDDemoProject">The VCMD Demo Project</a> </li>
  <li><a HREF="#Summary">Summary</a> </li>
</ul>

<hr>

<p>Most of the things you'll want to add to Windows applications can be handled using the 
OLE automation libraries for speech recognition (<tt><font FACE="Courier">VCMDAUTO.TLB</font></tt>) 
and text-to-speech (<tt><font FACE="Courier">VTXTAUTO.TLB</font></tt>). These libraries 
can be called from Visual Basic or any VBA-compliant application such as Excel, Access, 
Word, and so on. However, there are some things that you can only do using C or C++ 
languages. This includes building permanently stored grammars, calling SAPI dialog boxes, 
and other low-level functions. For this reason, it is a good idea to know how to perform 
some basic SAPI functions in C. Even if you do not regularly program in C or C++, you can 
still learn a lot by reading through the code in these examples. </p>

<p>In this chapter, you'll get a quick tour of a TTS demonstration application and an SR 
demonstration application. The applications reviewed here are part of the Microsoft Speech 
SDK. If you have the Speech SDK installed on your workstation, you can follow along with 
the code and compile and test the resulting application. If you do not have the SDK 
installed or do not have a copy of C++ with which to compile the application, you can 
still follow along with the review of the various functions used in the C++ programs. </p>

<p>When you are finished with this chapter, you will know how to build simple TTS and SR 
programs using C++ and the Microsoft Speech SDK. </p>
<div align="center"><center>

<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
  <tr>
    <td><b>Note</b></td>
  </tr>
  <tr>
    <td><blockquote>
      <p>All the examples in this chapter are shipped with the Microsoft Speech SDK. The 
      compiler used in this chapter is Microsoft Visual C++ 4.1 (VC++). If you are using another 
      compiler, you may need to modify some of the code in order for it to work for you.</p>
    </blockquote>
    </td>
  </tr>
</table>
</center></div>

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

<p>Creating a TTS application with C++ involves just a few basic steps. To see how this is 
done, you can look at the <tt><font FACE="Courier">TTSDEMO.CPP</font></tt> source code 
that ships with the Microsoft Speech SDK. You can find this file in the <tt><font
FACE="Courier">\SPEEchSDK\SAMPLES\TTSDEMO</font></tt> folder that was created when you 
installed the Microsoft Speech SDK. </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 the Microsoft Speech SDK installed or do not have Visual C++, you 
      can still follow along with the code examples shown in this chapter.</p>
    </blockquote>
    </td>
  </tr>
</table>
</center></div>

<p>The rest of this section reviews the contents of the <tt><font FACE="Courier">TTSDEMO.MAK</font></tt> 
project. There are two components to review: the <tt><font FACE="Courier">DEMO.CPP</font></tt> 
source code file and the <tt><font FACE="Courier">DEMO.RC</font></tt> file. The <tt><font
FACE="Courier">DEMO.CPP</font></tt> source code file contains all the code needed to 
implement TTS services for Windows using C++. The <tt><font FACE="Courier">DEMO.RC</font></tt> 
file contains a simple dialog box that you can use to accept text input from the user and 
then send that text to the TTS engine for playback. </p>

<h4>The Initial Header and Declarations for <tt><font FACE="Courier">DEMO.CPP</font></tt> </h4>

<p>Before you code the various routines for the TTS demo, there are a handful of include 
statements and a couple of global declarations that must be added. Listing 19.1 shows the 
include statements needed to implement TTS services in VC++. </p>

<hr>

<blockquote>
  <b><p>Listing 19.1. The include statements and global declarations for <tt><font
  FACE="Courier">DEMO.CPP</font></tt>. <br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>/*********************************************************************** 
  <br>
  Demo.Cpp - Code to demo tts.<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>
  <br>
  /************************************************************************* <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>
  PITTSCENTRALW&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;gpITTSCentral = NULL;</font></tt> </p>
</blockquote>

<hr>

<p>Most of the include files are part of VC++. The <tt><font FACE="Courier">speech.h</font></tt> 
header file is shipped as part of the Microsoft Speech SDK. And the <tt><font
FACE="Courier">resource.h</font></tt> header file is created when you build the dialog box 
for the project. </p>

<h4>The <tt><font FACE="Courier">WinMain</font></tt> Procedure of <tt><font FACE="Courier">DEMO.CPP</font></tt></h4>

<p>Since the SAPI model is implemented using the Component Object Model (COM) interface, 
you need to begin and end an OLE session as part of normal processing. After starting the 
OLE session, you need to use the <tt><font FACE="Courier">TTSCentral</font></tt> interface 
to locate and initialize an available TTS engine. Once you have a session started with a 
valid TTS engine, you can use a simple dialog box to accept text input and send that 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. After exiting the dialog box, you need to 
release the connection to <tt><font FACE="Courier">TTSCentral</font></tt> and then end the 
OLE session. </p>

<p>The code in Listing 19.2 shows the <tt><font FACE="Courier">WinMain</font></tt> 
procedure for the <tt><font FACE="Courier">TTSDEMO.CPP</font></tt> file. </p>

<hr>

<blockquote>
  <b><p>Listing 19.2. The <tt><font FACE="Courier">WinMain</font></tt> procedure of the <tt><font
  FACE="Courier">TTSDEMO</font></tt> project.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>/************************************************************************* 
  <br>
  winmain - Windows main code.<br>
  */<br>
  <br>
  int PASCAL WinMain(HINSTAncE hInstance, HINSTAncE hPrevInstance, <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;LPSTR 
  lpszCmdLine, int nCmdShow)<br>
  {<br>
  TTSMODEINFOW&nbsp;&nbsp;&nbsp;ModeInfo;<br>
  <br>
  // try to begin ole<br>
  <br>
  &nbsp;&nbsp;&nbsp;if (!BeginOLE())<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MessageBox (NULL, &quot;Can't create OLE.&quot;, NULL, 
  MB_OK);<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br>
  <br>
  //&nbsp;find the right object<br>
  &nbsp;&nbsp;&nbsp;memset (&amp;ModeInfo, 0, sizeof(ModeInfo)); <br>
  &nbsp;&nbsp;&nbsp;gpITTSCentral = FindAndSelect (&amp;ModeInfo); <br>
  &nbsp;&nbsp;&nbsp;if (!gpITTSCentral) {<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MessageBox (NULL, &quot;Can't create a TTS 
  engine.&quot;, NULL, MB_OK);<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 1;<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;};<br>
  <br>
  //&nbsp;Bring up the dialog box<br>
  &nbsp;&nbsp;&nbsp;DialogBox (hInstance, MAKEINTRESOURCE(IDD_TTS), <br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NULL, (FARPROC) DialogProc); <br>
  <br>
  //&nbsp;try to close ole<br>
  &nbsp;&nbsp;&nbsp;gpITTSCentral-&gt;Release();<br>
  <br>
  &nbsp;&nbsp;&nbsp;if (!EndOLE())<br>
  &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MessageBox (NULL, &quot;Can't shut down OLE.&quot;, 
  NULL, MB_OK);<br>
  <br>
  &nbsp;&nbsp;&nbsp;return 0;<br>
  }</font></tt> </p>
</blockquote>

<hr>

<p>You can see the basic steps mentioned earlier: start the OLE session, get a TTS object, 
and start the dialog box. Once the dialog is completed, you need to release the TTS object 
and end the OLE session. The rest of the code all supports the code in <tt><font
FACE="Courier">WinMain</font></tt>. </p>

<h4>Starting and Ending the OLE Session</h4>

<p>The code needed to start and end the OLE session is pretty basic. The code in Listing 
19.3 shows both the <tt><font FACE="Courier">BeginOLE</font></tt> and <tt><font
FACE="Courier">EndOLE</font></tt> procedures. </p>

<hr>

<blockquote>
  <b><p>Listing 19.3. The <tt><font FACE="Courier">BeginOLE</font></tt> and <tt><font
  FACE="Courier">EndOLE</font></tt> procedures.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>/************************************************************************* 
  <br>
  BeginOLE - This begins the OLE.<br>
  <br>
  inputs<br>
  &nbsp;&nbsp;&nbsp;none<br>
  returns<br>
  &nbsp;&nbsp;&nbsp;BOOL - TRUE if is succedes<br>
  */<br>
  <br>

⌨️ 快捷键说明

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