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

📄 ch20.htm

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

<head>
<title>Chapter 20 -- Building the Voice-Activated Text Reader</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 20</font></h1>

<h1><b><font SIZE="5" COLOR="#FF0000">Building the Voice-Activated Text Reader</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="#DesigningtheApplication">Designing the Application</a> <ul>
      <li><a HREF="#AddingTTSServices">Adding TTS Services</a> </li>
      <li><a HREF="#AddingSRServices">Adding SR Services</a> </li>
    </ul>
  </li>
  <li><a HREF="#CodingtheMDISpeechModule">Coding the MDISpeech Module</a> <ul>
      <li><a HREF="#DeclaringtheGlobalVariables">Declaring the Global Variables</a> </li>
      <li><a HREF="#CodingtheInitSAPIandUnInitSAPIRouti">Coding the InitSAPI and UnInitSAPI 
        Routines</a> </li>
      <li><a HREF="#CodingtheInitVoiceRoutine">Coding the InitVoice Routine</a> </li>
      <li><a HREF="#CodingtheInitVTextRoutine">Coding the InitVText Routine</a> </li>
      <li><a HREF="#CodingtheVTextActionRoutine">Coding the VTextAction Routine</a> </li>
    </ul>
  </li>
  <li><a HREF="#ModifyingtheMDINoteForms">Modifying the MDINote Forms</a> <ul>
      <li><a HREF="#ModifyingtheMDIForm">Modifying the MDI Form</a> </li>
      <li><a HREF="#ModifyingtheNotePadForm">Modifying the NotePad Form</a> </li>
    </ul>
  </li>
  <li><a HREF="#TestingtheSAPIEnabledMDINotePad">Testing the SAPI-Enabled MDI NotePad</a> </li>
  <li><a HREF="#Summary">Summary</a> </li>
</ul>

<hr>

<p><font COLOR="#000000">In this chapter, you'll build a complete Visual Basic 4.0 
application that uses both speech recognition and text-to-speech services. You'll use the </font><tt><font
FACE="Courier">MDINOTE</font></tt> project that ships with Visual Basic 4.0 as a starting 
framework for creating a voice-activated text reader. This application will allow users to 
use voice commands to load text documents and then tell the workstation to play back the 
loaded documents using TTS services. </p>

<p>You'll learn how to declare your own custom voice commands and how to use the voice 
commands automatically built by Windows each time you load an application. You'll also 
learn how to add menu options and toolbar buttons that provide TTS services for your 
application. When you are done with the example project in this chapter, you'll know how 
to build SAPI-enabled applications using Visual Basic 4.0 or any other VBA-compliant 
language. </p>

<h2><a NAME="DesigningtheApplication"><font SIZE="5" COLOR="#FF0000">Designing the 
Application</font></a></h2>

<p>The first stage in the process of building a SAPI-enabled Windows application is 
designing the SAPI interface. Adding SAPI services to an application is much like adding 
other Windows extensions (such as messaging, telephony, and so on). Usually, you can start 
from an existing application framework and add SAPI services where appropriate. </p>

<p>In order for you to focus on the process of incorporating SAPI services into an 
application, the project in this chapter starts from a working Visual Basic 4.0 
application-the <tt><font FACE="Courier">MDINOTE.VBP</font></tt> project. This application 
allows users to create, edit, and save text files. </p>
<div align="center"><center>

<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
  <tr>
    <td><b>Note</b></td>
  </tr>
  <tr>
    <td><blockquote>
      <p>This project ships with Visual Basic 4.0 and can be found in the <tt><font
      FACE="Courier">SAMPLES\MDI</font></tt> folder of the Visual Basic home directory. You can 
      also find it on the CD-ROM that ships with this book. </p>
    </blockquote>
    </td>
  </tr>
</table>
</center></div>

<p>For the example in this chapter, you'll add both speech recognition and text-to-speech 
services to this application. When you complete the project outlined here, you'll have a 
fully functional text editor that provides both SR and TTS services to users. </p>

<h3><a NAME="AddingTTSServices">Adding TTS Services</a></h3>

<p>Adding TTS services to the <tt><font FACE="Courier">MDINOTE.VBP</font></tt> project is 
really quite simple. You will need to provide an option that allows users to tell the TTS 
engine to read the selected text. You should also allow users to fast-forward the reader, 
rewind it, and stop it as needed. The easiest way to do this is to provide a set of menu 
options that correspond to the SAPI TTS <tt><font FACE="Courier">Speak</font></tt>, <tt><font
FACE="Courier">FastForward</font></tt>, <tt><font FACE="Courier">Rewind</font></tt>, and <tt><font
FACE="Courier">StopSpeaking</font></tt> services. In addition to adding menu options, 
you'll also add command buttons to appear on the toolbar. </p>

<p>Adding TTS services also requires some initialization code to declare and initialize 
the <tt><font FACE="Courier">Voice Text</font></tt> object. </p>

<h3><a NAME="AddingSRServices">Adding SR Services</a></h3>

<p>Adding speech recognition services to Windows 95 applications is very easy. First, as 
soon as the SAPI services are installed and activated, all the menu options of a Windows 
application are valid voice commands. In other words, even if you have done no coding at 
all, your Windows 95 applications are ready to receive and respond to voice commands. </p>
<div align="center"><center>

<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
  <tr>
    <td><b>Tip</b></td>
  </tr>
  <tr>
    <td><blockquote>
      <p>As soon as you load a Windows application, the menu options are activated as valid 
      voice commands. You can view the valid voice commands at any moment by saying <i>What Can 
      I Say?</i> or by selecting the alternate mouse button while pointing to the Microsoft 
      Voice balloon icon in the system tray. </p>
    </blockquote>
    </td>
  </tr>
</table>
</center></div>

<p>In addition to the default menu option commands, you'll also add a set of custom 
commands for the <tt><font FACE="Courier">MDINOTE.VBP</font></tt> project. These are 
one-word commands that the user can speak. They correspond to the set of toolbar buttons 
that appear on the form while the project is running. </p>

<p>Along with the code that declares the menu commands and their objects, you'll also 
write code to check the <tt><font FACE="Courier">CommandSpoken</font></tt> property and 
respond to the voice commands as needed. </p>

<h2><a NAME="CodingtheMDISpeechModule"><font SIZE="5" COLOR="#FF0000">Coding the </font><tt><font
SIZE="5" COLOR="#FF0000" FACE="Courier">MDISpeech</font></tt><font SIZE="5"
COLOR="#FF0000"> Module</font></a></h2>

<p>The first job in adding speech services to a project is declaring global variables and 
adding the initialization routines for starting and ending the application. If you are 
adding TTS services, you need to add code that will respond to the various TTS engine 
requests (<tt><font FACE="Courier">Speak</font></tt>, <tt><font FACE="Courier">FastForward</font></tt>, 
<tt><font FACE="Courier">Rewind</font></tt>, and <tt><font FACE="Courier">Stop</font></tt>). 
If you are enabling custom SR voice commands, you'll need to add code that registers the 
new commands. </p>
<div align="center"><center>

<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
  <tr>
    <td><b>Note</b></td>
  </tr>
  <tr>
    <td><blockquote>
      <p>You'll also need code that checks the <tt><font FACE="Courier">CommandSpoken</font></tt> 
      property of the <tt><font FACE="Courier">Voice Command</font></tt> object to see if one of 
      your custom commands was uttered by the user. You'll add that code to the main form later 
      in this chapter. </p>
    </blockquote>
    </td>
  </tr>
</table>
</center></div>

<h3><a NAME="DeclaringtheGlobalVariables">Declaring the Global Variables</a></h3>

<p>For the project described in this chapter, you'll need to add both SR and TTS services. 
First, load the <tt><font FACE="Courier">MDINOTE</font></tt> project and add a new <tt><font
FACE="Courier">BAS</font></tt> module. Set its <tt><font FACE="Courier">Name</font></tt> 
property to <tt><font FACE="Courier">modSpeech</font></tt> and save it as <tt><font
FACE="Courier">MDISP.BAS</font></tt>. Next add the code shown in Listing 20.1 to the 
general declaration section of the module. </p>

<hr>

<blockquote>
  <b><p>Listing 20.1. Adding code to the declaration section.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Option Explicit<br>
  '<br>
  ' *********************************************<br>
  ' This module adds SAPI support to the program.<br>
  ' *********************************************<br>
  <br>
  '<br>
  ' VCmd speech stuff<br>
  Global objVCmd As Object ' SR Command object<br>
  Global objVMenu As Object ' SR Menu object<br>
  Global cVCmdMenu() As String ' SR command strings<br>
  Global lSRCmd As Long ' SR command ID<br>
  '<br>
  ' VTxt Speech stuff<br>
  Global objVText As Object ' TTS object<br>
  Global bVText As Boolean ' TTS flag<br>
  Global Const vTxtSpeak = 0<br>
  Global Const vTxtForward = 1<br>
  Global Const vTxtRewind = 2<br>
  Global Const vTxtStop = 3</font></tt> </p>
</blockquote>

<hr>

<p>The first several code lines declare the variables and objects needed to provide SR 
services. The SAPI voice command object library requires two different objects: the <tt><font
FACE="Courier">Voice Command</font></tt> object and the <tt><font FACE="Courier">Voice 
Menu</font></tt> object. You'll also need an array to hold the commands to be added to the 
menu and a variable to hold the menu ID returned in the <tt><font FACE="Courier">CommandSpoken</font></tt> 
property. </p>

<p>The TTS service requires only one object. You'll use a Boolean flag to hold the 
activity status of the TTS engine returned by the <tt><font FACE="Courier">IsSpeaking</font></tt> 
property. The last four constants are used by Visual Basic to keep track of menu and 
button options selected by the user. </p>

<p>Save the code module as <tt><font FACE="Courier">MDISP.BAS</font></tt> and the project 
as <tt><font FACE="Courier">MDINOTE.VBP</font></tt> before continuing on to the next 
section. </p>

<h3><a NAME="CodingtheInitSAPIandUnInitSAPIRouti">Coding the <tt><font SIZE="4"
FACE="Courier">InitSAPI</font></tt><font SIZE="4"> and </font><tt><font SIZE="4"
FACE="Courier">UnInitSAPI</font></tt><font SIZE="4"> Routines</font></a></h3>

<p>The next code section you need to add is the code that will initialize and uninitialize 
SAPI services for the application. The first routine (<tt><font FACE="Courier">InitSAPI</font></tt>) 
will be called when the program first starts. The last routine (<tt><font FACE="Courier">UnInitSAPI</font></tt>) 
will be called when the project ends. Add a new subroutine called <tt><font FACE="Courier">InitSAPI</font></tt> 
to the <tt><font FACE="Courier">modSpeech</font></tt> module. Enter the code shown in 
Listing 20.2. </p>

<hr>

<blockquote>
  <b><p>Listing 20.2. Adding the <font SIZE="1" FACE="MCPdigital-B">InitSAPI</font> routine.<br>
  </b></p>
</blockquote>

<blockquote>
  <tt><font FACE="Courier"><p>Public Sub InitSAPI()<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' voice command objects<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Set objVCmd = CreateObject(&quot;Speech.VoiceCommand&quot;) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;objVCmd.Register &quot;&quot; ' use default location<br>
  &nbsp;&nbsp;&nbsp;&nbsp;objVCmd.Awake = True ' awaken speech services <br>
  &nbsp;&nbsp;&nbsp;&nbsp;Set objVMenu = objVCmd.MenuCreate(App.EXEName, &quot;MDI&quot;, 
  1033, &quot;&quot;, vcmdmc_CREATE_TEMP)<br>
  &nbsp;&nbsp;&nbsp;&nbsp;InitVoice ' go build command list<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  &nbsp;&nbsp;&nbsp;&nbsp;' voice text objects<br>
  &nbsp;&nbsp;&nbsp;&nbsp;Set objVText = CreateObject(&quot;Speech.VoiceText&quot;) <br>
  &nbsp;&nbsp;&nbsp;&nbsp;objVText.Register &quot;&quot;, App.EXEName <br>
  &nbsp;&nbsp;&nbsp;&nbsp;objVText.Enabled = True<br>
  &nbsp;&nbsp;&nbsp;&nbsp;InitVText ' go build menu/buttons<br>
  &nbsp;&nbsp;&nbsp;&nbsp;'<br>
  End Sub</font></tt> </p>
</blockquote>

<hr>

<p>This routine first initializes the <tt><font FACE="Courier">Voice Command</font></tt> 
objects, registers the application, and starts the SR engine. The call to the <tt><font
FACE="Courier">InitVoice</font></tt> routine adds the custom voice commands to the 
declared menu. You'll see that code in the next section. </p>

⌨️ 快捷键说明

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