📄 ch19.htm
字号:
<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 <windows.h><br>
#include <string.h><br>
#include <stdio.h><br>
#include <mmsystem.h><br>
#include <initguid.h><br>
#include <objbase.h><br>
#include <objerror.h><br>
#include <ole2ver.h><br>
<br>
#include <speech.h><br>
#include "resource.h"<br>
<br>
<br>
/************************************************************************* <br>
Globals */<br>
<br>
HINSTAncE ghInstance; // instance
handle<br>
PITTSCENTRALW 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>
LPSTR
lpszCmdLine, int nCmdShow)<br>
{<br>
TTSMODEINFOW ModeInfo;<br>
<br>
// try to begin ole<br>
<br>
if (!BeginOLE())<br>
{<br>
MessageBox (NULL, "Can't create OLE.", NULL,
MB_OK);<br>
return 1;<br>
}<br>
<br>
// find the right object<br>
memset (&ModeInfo, 0, sizeof(ModeInfo)); <br>
gpITTSCentral = FindAndSelect (&ModeInfo); <br>
if (!gpITTSCentral) {<br>
MessageBox (NULL, "Can't create a TTS
engine.", NULL, MB_OK);<br>
return 1;<br>
};<br>
<br>
// Bring up the dialog box<br>
DialogBox (hInstance, MAKEINTRESOURCE(IDD_TTS), <br>
NULL, (FARPROC) DialogProc); <br>
<br>
// try to close ole<br>
gpITTSCentral->Release();<br>
<br>
if (!EndOLE())<br>
MessageBox (NULL, "Can't shut down OLE.",
NULL, MB_OK);<br>
<br>
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>
none<br>
returns<br>
BOOL - TRUE if is succedes<br>
*/<br>
<br>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -