📄 ch35.htm
字号:
<html>
<head>
<title>Chapter 35 -- Creating the Voice Phone Application</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 35</font></h1>
<h1><b><font SIZE="5" COLOR="#FF0000">Creating the Voice Phone Application</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="#ProjectResources">Project Resources</a> </li>
<li><a HREF="#CodingtheLibraryModules">Coding the Library Modules</a> <ul>
<li><a HREF="#TheAssistedTAPIModule">The AssistedTAPI Module</a> </li>
<li><a HREF="#TheCallBackModules">The CallBack Modules</a> </li>
</ul>
</li>
<li><a HREF="#BuildingtheLibVPhoneModule">Building the LibVPhone Module</a> <ul>
<li><a HREF="#AddingtheVoiceCommandRoutines">Adding the Voice Command Routines</a> </li>
<li><a HREF="#AddingtheVoiceTextRoutines">Adding the Voice Text Routines</a> </li>
<li><a HREF="#AddingtheDatabaseEngineRoutines">Adding the Database Engine Routines</a> </li>
<li><a HREF="#AddingtheTAPIandFormSupportRoutine">Adding the TAPI and Form Support Routines</a>
</li>
</ul>
</li>
<li><a HREF="#LayingOuttheVPhoneForm">Laying Out the VPhone Form</a> </li>
<li><a HREF="#CodingtheVPhoneForm">Coding the VPhone Form</a> </li>
<li><a HREF="#LayingOuttheSupportForms">Laying Out the Support Forms</a> <ul>
<li><a HREF="#TheVRecForm">The VRec Form</a> </li>
<li><a HREF="#TheVHelpForm">The VHelp Form</a> </li>
<li><a HREF="#TheVAboutForm">The VAbout Form</a> </li>
</ul>
</li>
<li><a HREF="#TestingVoicePhone">Testing Voice Phone</a> </li>
<li><a HREF="#Summary">Summary</a> </li>
</ul>
<hr>
<p>The example program in this chapter uses SAPI and TAPI services to create a true
"hands-free" telephone. With this program and a pc speaker phone, you can lookup
and dial telephone numbers by simply giving voice commands to your pc. You'll be able to
initiate database adds, edits, and deletes; issue a voice command that will search the
database for a name; and tell the program to <i>Dial Mike</i>. The Voice Phone will locate
the record in the database, pull up the phone number, place the call, and then prompt you
to begin speaking. </p>
<p>As an added bonus, this program gives audible responses to help requests and speaks the
names and telephone numbers of selected records in the database. Even the About box is
"read" to you! Figure 35.1 shows a completed version of Voice Phone in action. </p>
<p><a HREF="f35-1.gif"><b>Figure 35.1 : </b><i>The completed version of Voice Phone.</i></a>
</p>
<h2><a NAME="ProjectResources"><b><font SIZE="5" COLOR="#FF0000">Project Resources</font></b></a></h2>
<p>There is one main form for the project and three support forms:
<ul>
<li><i>VPhone</i>-This is the main form. All primary operations are performed here, and it
calls all other forms. </li>
<li><i>VRec</i>-This is the form used to add and edit database records. </li>
<li><i>VHelp</i>-This is a small help screen that displays and speaks help hints on using
Voice Phone. </li>
<li><i>VAbout</i>-This is the standard About box. It also speaks the application information
to the user. </li>
</ul>
<p>In addition to the forms, there are four support modules for the Voice Phone project:
<ul>
<li><tt><font FACE="Courier">VTAPI.BAS</font></tt>-This holds the Assisted TAPI API calls,
two user-defined types, and two functions that are used to request Assisted TAPI services.
</li>
<li><tt><font FACE="Courier">SRCBK.CLS</font></tt>-This is the speech recognition callback
class module. This module will process the commands spoken to Voice Phone by users. </li>
<li><tt><font FACE="Courier">TTSCBK.CLS</font></tt>-This is the text-to-speech callback
class module. The Voice Phone project does not use this module, but it is a good idea to
include it in all projects that use the TTS engine. </li>
<li><tt><font FACE="Courier">VPHONE.BAS</font></tt>-This module contains the bulk of the
project code. All supporting subroutines and functions are kept here. </li>
</ul>
<p>Before you begin coding the project, you'll need to make sure you have the following
resources loaded available on your workstation:
<ul>
<li><font COLOR="#000000">An Assisted TAPI compatible modem (almost any modem will do) with
a telephone handset attached.</font> </li>
<li><font COLOR="#000000">A sound card, a microphone, and speakers for speech recognition
and text-to-speech services.</font> </li>
</ul>
<p>When you first start this project, you'll need to make sure you have loaded the proper
support libraries for Visual Basic. Select <tt><font FACE="Courier">Tools | References</font></tt>
and load the following:
<ul>
<li><font COLOR="#000000">Voice Command 1.0 type library</font> </li>
<li><font COLOR="#000000">Voice Text 1.0 type library</font> </li>
<li><font COLOR="#000000">Microsoft DAO 3.0 object library</font> </li>
</ul>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><b>Note</b></td>
</tr>
<tr>
<td><blockquote>
<p>If you don't have these libraries available from your <tt><font FACE="Courier">References</font></tt>
dialog box, you'll need to use the Browse button to find them. Usually they can be found
in the <tt><font FACE="Courier">SYSTEM</font></tt> folder of the <tt><font FACE="Courier">WINDOWS</font></tt>
directory. The Voice libraries can also be found on the CD-ROM that ships with this book.
You probably have other resources loaded for your programs. That's fine. Just be sure you
have the three resources listed above. </p>
</blockquote>
</td>
</tr>
</table>
</center></div>
<h2><a NAME="CodingtheLibraryModules"><b><font SIZE="5" COLOR="#FF0000">Coding the Library
Modules</font></b></a></h2>
<p>The first step in the process of building the Voice Phone project is creating the
library modules. Three of the modules exist to provide simple support for the Windows
extension services (SAPI and TAPI). The fourth module holds most of the project code. </p>
<h3><b><a NAME="TheAssistedTAPIModule">The <tt><font SIZE="4" FACE="Courier">AssistedTAPI</font></tt><font
SIZE="4"> Module</font></a></b></h3>
<p>The <tt><font FACE="Courier">AssistedTAPI</font></tt> module holds the Telephony API
declarations, two type definitions, and two Visual Basic wrapper functions. With this one
code module you can provide any Visual Basic or VBA-compatible program with basic TAPI
services. </p>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><b>Tip</b></td>
</tr>
<tr>
<td><blockquote>
<p>You might think it a better idea to implement Assisted TAPI support using a Visual
Basic class module instead of a simple code module. However, the code module can be loaded
into any VBA-compatible environment (including Visual Basic 3.0). The class module could
only be used for Visual Basic 4.0.</p>
</blockquote>
</td>
</tr>
</table>
</center></div>
<p>Add a BAS module to your project (<tt><font FACE="Courier">Insert | Module</font></tt>)
and set its Name property to <tt><font FACE="Courier">AssistedTAPI</font></tt>. Save the
module as <tt><font FACE="Courier">VTAPI.BAS</font></tt>. Now you're ready to start
coding. </p>
<p>The first things to add to this module are the Assisted TAPI API declarations. Listing
35.1 shows the code that imports the <tt><font FACE="Courier">tapiRequestMakeCall</font></tt>
and <tt><font FACE="Courier">tapiGetLocationInfo</font></tt> API calls. </p>
<hr>
<blockquote>
<b><p>Listing 35.1. Adding the Assisted Telephony API Declarations. <br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>'<br>
'</font></tt> <tt><font FACE="Courier">declare assisted tapi functions<br>
'<br>
#If Win32 Then<br>
Declare Function tapiRequestMakeCall Lib "TAPI32.DLL"
(ByVal lpszDestAddress As <font FACE="ZAPFDINGBATS">Â</font>String, ByVal
lpszAppName As String, ByVal lpszCalledParty As String, ByVal <font FACE="ZAPFDINGBATS">Â</font>lpszComment
As String) As Long<br>
Declare Function tapiGetLocationInfo Lib "TAPI32.DLL"
(ByVal lpszCountryCode As <font FACE="ZAPFDINGBATS">Â</font>String, ByVal
lpszCityCode As String) As Long<br>
#Else<br>
Declare Function tapiRequestMakeCall Lib "TAPI.DLL"
(ByVal lpszDestAddress As <font FACE="ZAPFDINGBATS">Â</font>String, ByVal
lpszAppName As String, ByVal lpszCalledParty As String, ByVal <font FACE="ZAPFDINGBATS">Â</font>lpszComment
As String) As Long<br>
Declare Function tapiGetLocationInfo Lib "TAPI.DLL"
(ByVal lpszCountryCode As <font FACE="ZAPFDINGBATS">Â</font>String, ByVal
lpszCityCode As String) As Long<br>
#End If</font></tt> </p>
</blockquote>
<hr>
<p>Notice that the code in Listing 35.1 uses the conditional compilation directives along
with the code for both 16- and 32-bit Visual Basic 4.0. This ensures that the code will
work in either version of Visual Basic 4.0 that you use. </p>
<p>Next you need to add two user-defined types to the module. These types encapsulate the
parameters needed for the two TAPI calls. Using UDTs in this way reduces the complexity of
your code and makes it easier to read. Add the code shown in Listing 35.2 to the general
section of the module. </p>
<hr>
<blockquote>
<b><p>Listing 35.2. Adding the user-defined types.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>'<br>
'</font></tt> <tt><font FACE="Courier">TAPILocation Type <br>
Type TAPILocation<br>
Country As String * 1<br>
City As String * 3<br>
End Type<br>
'<br>
' TAPIMakeCall Type<br>
Type TAPICall<br>
Address As String<br>
AppName As String<br>
CalledParty As String<br>
Comment As String<br>
End Type</font></tt> </p>
</blockquote>
<hr>
<p>After declaring the API routines and defining the type variables, you're ready to add
the wrapper functions that will encapsulate the API calls. Listing 35.3 shows the function
that supports the <tt><font FACE="Courier">tapiGetLocationInfo</font></tt> API call. Add
this to your module. </p>
<hr>
<blockquote>
<b><p>Listing 35.3. Adding the <tt><font FACE="Courier">TAPIGetLocation</font></tt>
function.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>Public Function TAPIGetLocation() As TAPILocation<br>
'<br>
' returns UDT w/ city and country<br>
'<br>
Dim lTapi As Long<br>
Dim cCountry As String * 1<br>
Dim cCity As String * 3<br>
'<br>
lTapi = tapiGetLocationInfo(cCountry, cCity)<br>
If lTapi >= 0 Then<br>
TAPIGetLocation.Country = cCountry<br>
TAPIGetLocation.City = cCity<br>
End If<br>
'<br>
End Function</font></tt> </p>
</blockquote>
<hr>
<p>Notice that this function <i>returns</i> a UDT that contains both the country code and
the city code for the workstation. </p>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><b>Note</b></td>
</tr>
<tr>
<td><blockquote>
<p>The <tt><font FACE="Courier">TAPIGetLocation</font></tt> function is not called in this
project. It is included here for completeness. When you use this module for other TAPI
projects, you'll have this function call already defined. </p>
</blockquote>
</td>
</tr>
</table>
</center></div>
<p>The second API wrapper function is the <tt><font FACE="Courier">TAPIMakeCall</font></tt>
function. This function accepts the <tt><font FACE="Courier">TAPICall</font></tt>
user-defined type as input and places an outbound call. Listing 35.4 shows the code for
this function. </p>
<hr>
<blockquote>
<b><p>Listing 35.4. Adding the <tt><font FACE="Courier">TAPIMakeCall</font></tt> function.<br>
</b></p>
</blockquote>
<blockquote>
<tt><font FACE="Courier"><p>Public Function TAPIMakeCall(tapiRec As TAPICall) As Long<br>
'<br>
' make an assisted TAPI call<br>
' returns 0 if OK, <0 if error<br>
'<br>
TAPIMakeCall = tapiRequestMakeCall(tapiRec.Address,
tapiRec.AppName, <font FACE="ZAPFDINGBATS">Â</font>tapiRec.CalledParty,
tapiRec.Comment) <br>
'<br>
End Function</font></tt> </p>
</blockquote>
<hr>
<p>This function returns a value that indicates the results of the TAPI request. If the
value is less than <tt><font FACE="Courier">0</font></tt>, you've got an error condition.
Only the first parameter is required (<tt><font FACE="Courier">Address</font></tt>). </p>
<div align="center"><center>
<table BORDERCOLOR="#000000" BORDER="1" WIDTH="80%">
<tr>
<td><b>Tip</b></td>
</tr>
<tr>
<td><blockquote>
<p>For a more detailed look at Assisted Telephony, refer to <a HREF="ch29.htm">Chapter 29</a>,
"Writing TAPI-Assisted Applications." </p>
</blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -