simplyjava1_28

Download Report

Transcript simplyjava1_28

1
Tutorial 28 – Phone Book Application
Introducing the Java Speech API
Outline
28.1
28.2
28.3
28.4
28.5
Java Speech API
Downloading and Installing FreeTTS
Test-Driving the Phone Book Application
Constructing the Phone Book Application
Wrap-Up
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
2
Objectives
• In this tutorial, you will learn to:
– Download and install FreeTTS to run the Java Speech
application.
– Enhance Java applications using multimedia.
– Use the Java Speech API in a Java application.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
3
28.1
Java Speech API
• Multimedia
– Graphics, animation, video and sound
• Java Speech API
– Adds speech capabilities to applications
– Supports two core speech technologies
• Speech synthesis
• Speech recognition
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
4
28.1
Java Speech API (Cont.)
Application Requirements
A software company’s customer service representatives are responsible for
calling clients. The representatives need a convenient way to access their clients’ phone numbers and have asked you to develop an application that stores
and retrieves the names and phone numbers of their clients. The service
representatives want an application that employs speech synthesis (using the
Java Speech API) to allow them to retrieve the phone numbers by selecting
clients’ names with the mouse.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
5
28.1
Java Speech API (Cont.)
• Speech synthesis or text-to-speech technology
– Produces synthetic speech from text
• Speech recognition
– Produces text from audio input that contains speech
• Speech recognition engine
• Text-to-speech engine
• Open source
– Software freely available for use and modification
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
6
28.2
Downloading and Installing FreeTTS
Figure 28.1
Click the symbol to
download
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Locations for downloading FreeTTS.
7
28.2
Downloading and Installing FreeTTS
(Cont.)
Figure 28.2
Decompressing freetts-1_1_2.tar.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
8
28.2
Downloading and Installing FreeTTS
(Cont.)
Figure 28.3
Extracting the content in freetts-1_1_2.tar.
Click the Extract button
to extract the content in
freetts-1_1_1.tar
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
9
28.2
Downloading and Installing FreeTTS
(Cont.)
Figure 28.4
Extract the content in
freetts-1_1_1.tar
to the C:\ directory
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Specifying the extract destination.
10
28.2
Downloading and Installing FreeTTS
(Cont.)
Figure 28.5
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Accepting the license agreement.
11
28.2
Downloading and Installing FreeTTS
(Cont.)
Figure 28.6
Completing the Java Speech API implementation installation.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
12
28.3
Test-Driving the Phone Book Application
Figure 28.7
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Phone Book application.
13
28.4
Constructing the Phone Book
Application
When the Get Phone Number JButton is clicked
Get the selected name
Speak the phone number of the selected person
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
14
28.4
Constructing the Phone Book
Application (Cont.)
Action
Display instructions to the
user
Component
instruction1Jlabel,
instruction2JLabel
getPhoneNumberJButton
User clicks the Get Phone
Number JButton
nameJComboBox
Get the selected name
Synthesizer
Speak the phone number
Figure 28.8 ACE table for the Phone Book application.
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Event/Method
User clicks the Get Phone
Number JButton
JButton is clicked
speakPlainText
15
28.4
Constructing the Phone Book
Application (Cont.)
Figure 28.9
Importing the Java
Speech API packages
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Importing the Java Speech API packages.
16
28.4
Figure 28.10
Constructing the Phone Book
Application (Cont.)
Declaring an instance variable for the speech synthesizer.
Declaring an instance
variable for the speech
synthesizer
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
17
28.4
Constructing the Phone Book
Application (Cont.)
• SynthesizerModeDesc
– A descriptor
– Specifies the properties of the speech synthesizer
•
•
•
•
•
Name
Mode of operation
Language supported
Running state
Voice
• Locale
– Represents a specific region
– Locale.US represents a Locale object for the United States
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
18
28.4
Constructing the Phone Book
Application (Cont.)
• Central
– Provides access to all speech input and output capabilities
• Synthesizer
– Provides speech synthesis capabilities
– Use the createSynthesizer method of the Central
class to create
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
19
28.4
Constructing the Phone Book
Application (Cont.)
Figure 28.11
Create
SynthesizerModeDesc
for FreeTTS synthesizer
Create a synthesizer
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Initializing the speech synthesizer.
20
28.4
Constructing the Phone Book
Application (Cont.)
• allocate method
– Allocate resources required by the speech engine
• resume method
– Readies the speech synthesizer to speak
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
21
28.4
Constructing the Phone Book
Application (Cont.)
Figure 28.12
Getting the Synthesizer object ready to speak.
Get speech synthesizer
ready to speak
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
22
28.4
Constructing the Phone Book
Application (Cont.)
• SynthesizerProperties object
– Contains various properties of a speech synthesizer
– Use the getSynthesizerProperties method to get this
object
• speakingRate property
– Specifies the speaking rate in words per minute
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
23
28.4
Constructing the Phone Book
Application (Cont.)
Figure 28.13 Setting the properties of the Synthesizer object.
Get synthesizer
property
Set up speaking rate of
the speech synthesizer
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
24
28.4
Constructing the Phone Book
Application (Cont.)
Figure 28.14
Coding the event handler for getPhoneNumberJButton.
Get index of the
selected person
Declare text to speak
Speak the person’s name
and phone number
• speakPlainText method
– Used to speak text
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
25
28.4
Constructing the Phone Book
Application (Cont.)
Figure 28.15
Cleaning up the synthesizer.
Deallocate speech synthesizer
• deallocate method
– Free resources that are allocated to the speech engine
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
26
28.4
Constructing the Phone Book
Application (Cont.)
Figure 28.16
© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson
Education Inc. All Rights Reserved.
Running the completed application.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// Tutorial 28: PhoneBook.java
// An application announces phone number with FreeTTS.
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.speech.*;
import javax.speech.synthesis.*;
27
Outline
PhoneBook.java
(1 of 8)
Import Java Speech
API packages
public class PhoneBook extends JFrame
{
// JLabels to display instructions
private JLabel instruction1JLabel;
private JLabel instruction2JLabel;
// JComboBox for names
private JComboBox nameJComboBox;
// JButton to get phone number
private JButton getPhoneNumberJButton;
// Synthesizer to speak phone number
private Synthesizer speechSynthesizer;
Declare the instance variable
speechSynthesizer
 2004 Prentice Hall, Inc.
All rights reserved.
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// fill array with people's names
private String[] namesArray = { "John", "Jennifer", "Howard" };
// fill array with people's phone numbers
private String[] numbersArray =
{ "(555) 555-9876", "(555) 555-1234", "(555) 555-4567" };
// no-argument constructor
public PhoneBook()
{
// initialize Synthesizer
try
{
// create SynthesizerModeDesc for FreeTTS synthesizer
SynthesizerModeDesc descriptor = new SynthesizerModeDesc(
"Unlimited domain FreeTTS Speech Synthesizer " +
"from Sun Labs", null, Locale.US, Boolean.FALSE, null);
// create a Synthesizer
speechSynthesizer = Central.createSynthesizer( descriptor );
28
Outline
PhoneBook.java
(2 of 8)
Create a
SynthesizerModeDesc for FreeTTS
synthesizer
Create a
synthesizer object
 2004 Prentice Hall, Inc.
All rights reserved.
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Synthesizer created successfully
if ( speechSynthesizer != null )
{
// prepare synthesizer to speak
speechSynthesizer.allocate();
speechSynthesizer.resume();
// get synthesizer properties
SynthesizerProperties properties =
speechSynthesizer.getSynthesizerProperties();
// set up speaking rate
properties.setSpeakingRate( 100.0f );
} // end if
else
{
System.err.println( "Synthesizer creation failed." );
System.exit( 1 );
}
29
Outline
PhoneBook.java
(3 of 8)
Prepare the
synthesizer to speak
Get synthesizer
properties
Set up speaking rate
of speech synthesizer
If synthesizer is not
created successfully,
print error message
and terminate the
application
} // end try
 2004 Prentice Hall, Inc.
All rights reserved.
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
catch ( Exception myException )
{
myException.printStackTrace();
}
// set up GUI
createUserInterface();
30
Outline
PhoneBook.java
(4 of 8)
Catch exceptions
} // end constructor
// create and position GUI components; register event handler
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
// enable explicit positioning of GUI components
contentPane.setLayout( null );
// set up instruction1JLabel
instruction1JLabel = new JLabel();
instruction1JLabel.setBounds( 16, 8, 264, 23 );
instruction1JLabel.setText(
"Select a name from the combo box." );
contentPane.add( instruction1JLabel );
 2004 Prentice Hall, Inc.
All rights reserved.
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// set up instruction2JLabel
instruction2JLabel = new JLabel();
instruction2JLabel.setBounds( 16, 35, 264, 23 );
instruction2JLabel.setText(
"Click the button to listen to the phone number." );
contentPane.add( instruction2JLabel );
31
Outline
PhoneBook.java
(5 of 8)
// set up nameJComboBox
nameJComboBox = new JComboBox( namesArray );
nameJComboBox.setBounds( 50, 65, 150, 23 );
contentPane.add( nameJComboBox );
// set up getPhoneNumberJButton
getPhoneNumberJButton = new JButton( "Get Phone Number" );
getPhoneNumberJButton.setBounds( 50, 100, 150, 23 );
getPhoneNumberJButton.setText( "Get Phone Number" );
contentPane.add( getPhoneNumberJButton );
getPhoneNumberJButton.addActionListener(
new ActionListener() // anonymous inner class
{
// event handler called when getPhoneNumberJButton
// is clicked
public void actionPerformed( ActionEvent event )
{
 2004 Prentice Hall, Inc.
All rights reserved.
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
getPhoneNumberJButtonActionPerformed( event );
}
} // end anonymous inner class
); // end call to addActionListener
32
Outline
PhoneBook.java
(6 of 8)
// set properties of application's window
setTitle( "Phone Book" ); // set title bar string
setSize( 300, 160 );
// set window size
setVisible( true );
// display window
// ensure synthesizer is cleaned up
// when user closes application
addWindowListener(
new WindowAdapter() // anonymous inner class
{
public void windowClosing( WindowEvent event )
{
frameWindowClosing( event );
}
} // end anonymous inner class
 2004 Prentice Hall, Inc.
All rights reserved.
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
); // end addWindowListener
33
Outline
} // end method createUserInterface
// speak a person's phone number
private void getPhoneNumberJButtonActionPerformed(
ActionEvent event )
{
// get index of the selected person
int selectedName = nameJComboBox.getSelectedIndex();
// declare text to speak
String phoneNumberString = namesArray[ selectedName ] +
"'s phone number is " + numbersArray[ selectedName ];
// speak the person's name and phone number
speechSynthesizer.speakPlainText( phoneNumberString, null );
PhoneBook.java
(7 of 8)
Get index of the
selected person
Declare text to speak
Speak the person’s name
and phone number
} // end method getPhoneNumberJButtonActionPerformed
// cleanup synthesizer
private void frameWindowClosing( WindowEvent event )
{
// deallocate synthesizer
try
 2004 Prentice Hall, Inc.
All rights reserved.
170
{
171
speechSynthesizer.deallocate();
172
}
173
catch ( Exception myException )
174
{
175
myException.printStackTrace();
176
}
177
finally
178
{
179
System.exit( 0 );
180
}
181
182
} // end method frameWindowClosed
183
184
// main method
185
public static void main( String[] args )
186
{
187
PhoneBook application = new PhoneBook();
188
189
} // end method main
190
191 } // end class PhoneBook
34
Outline
PhoneBook.java
(8 of 8)
Deallocate synthesizer
 2004 Prentice Hall, Inc.
All rights reserved.