© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights...

34
1 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Outline 28.1 Java Speech API 28.2 Downloading and Installing FreeTTS 28.3 Test-Driving the Phone Book Application 28.4 Constructing the Phone Book Application 28.5 Wrap-Up Tutorial 28 – Phone Book Application Introducing the Java Speech API

description

© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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

Transcript of © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights...

Page 1: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

1

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

Outline28.1 Java Speech API28.2 Downloading and Installing FreeTTS 28.3 Test-Driving the Phone Book Application 28.4 Constructing the Phone Book Application 28.5 Wrap-Up

Tutorial 28 – Phone Book ApplicationIntroducing the Java Speech API

Page 2: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

2

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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.

Page 3: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

3

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

Page 4: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

4

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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 cli-ents’ 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.

Page 5: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

5

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

Page 6: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

6

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

28.2 Downloading and Installing FreeTTS

Figure 28.1 Locations for downloading FreeTTS.

Click the symbol to download

Page 7: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

7

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

28.2 Downloading and Installing FreeTTS (Cont.)

Figure 28.2 Decompressing freetts-1_1_2.tar.

Page 8: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

8

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

Page 9: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

9

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

28.2 Downloading and Installing FreeTTS (Cont.)

Figure 28.4 Specifying the extract destination.

Extract the content in freetts-1_1_1.tar

to the C:\ directory

Page 10: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

10

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

28.2 Downloading and Installing FreeTTS (Cont.)

Figure 28.5 Accepting the license agreement.

Page 11: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

11

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

28.2 Downloading and Installing FreeTTS (Cont.)

Figure 28.6 Completing the Java Speech API implementation installation.

Page 12: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

12

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

28.3 Test-Driving the Phone Book Application

Figure 28.7 Phone Book application.

Page 13: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

13

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

Page 14: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

14

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

28.4 Constructing the Phone Book Application (Cont.)

Action Component Event/Method Display instructions to the user

instruction1Jlabel, instruction2JLabel

User clicks the Get Phone Number JButton

getPhoneNumberJButton User clicks the Get Phone Number JButton

Get the selected name nameJComboBox JButton is clicked Speak the phone number Synthesizer speakPlainText

Figure 28.8 ACE table for the Phone Book application.

Page 15: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

15

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

28.4 Constructing the Phone Book Application (Cont.)

Figure 28.9 Importing the Java Speech API packages.

Importing the Java Speech API packages

Page 16: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

16

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

28.4 Constructing the Phone Book Application (Cont.)

Figure 28.10 Declaring an instance variable for the speech synthesizer.

Declaring an instance variable for the speech

synthesizer

Page 17: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

17

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

Page 18: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

18

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

Page 19: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

19

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

28.4 Constructing the Phone Book Application (Cont.)

Figure 28.11 Initializing the speech synthesizer.

Create SynthesizerModeDesc for FreeTTS synthesizer

Create a synthesizer

Page 20: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

20

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

Page 21: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

21

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

28.4 Constructing the Phone Book Application (Cont.)

Figure 28.12 Getting the Synthesizer object ready to speak.

Get speech synthesizer ready to speak

Page 22: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

22

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

Page 23: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

23

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

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

Page 24: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

24

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

28.4 Constructing the Phone Book Application (Cont.)

Figure 28.14 Coding the event handler for getPhoneNumberJButton.

• speakPlainText method– Used to speak text

Get index of the selected person

Declare text to speak

Speak the person’s name and phone number

Page 25: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

25

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

28.4 Constructing the Phone Book Application (Cont.)

Figure 28.15 Cleaning up the synthesizer.

• deallocate method– Free resources that are allocated to the speech engine

Deallocate speech synthesizer

Page 26: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

26

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.

28.4 Constructing the Phone Book Application (Cont.)

Figure 28.16 Running the completed application.

Page 27: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

2004 Prentice Hall, Inc.All rights reserved.

Outline27

PhoneBook.java(1 of 8)

1 // Tutorial 28: PhoneBook.java2 // An application announces phone number with FreeTTS.3 import java.awt.*;4 import java.awt.event.*;5 import java.util.*;6 import javax.swing.*;7 import javax.speech.*; 8 import javax.speech.synthesis.*;9 10 public class PhoneBook extends JFrame 11 {12 // JLabels to display instructions13 private JLabel instruction1JLabel;14 private JLabel instruction2JLabel;15 16 // JComboBox for names17 private JComboBox nameJComboBox;18 19 // JButton to get phone number20 private JButton getPhoneNumberJButton;21 22 // Synthesizer to speak phone number 23 private Synthesizer speechSynthesizer;24

Import Java Speech API packages

Declare the instance variable speechSynthesizer

Page 28: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

2004 Prentice Hall, Inc.All rights reserved.

Outline28

PhoneBook.java(2 of 8)

25 // fill array with people's names26 private String[] namesArray = { "John", "Jennifer", "Howard" };27 28 // fill array with people's phone numbers29 private String[] numbersArray = 30 { "(555) 555-9876", "(555) 555-1234", "(555) 555-4567" };31 32 // no-argument constructor33 public PhoneBook()34 {35 // initialize Synthesizer 36 try 37 { 38 // create SynthesizerModeDesc for FreeTTS synthesizer 39 SynthesizerModeDesc descriptor = new SynthesizerModeDesc( 40 "Unlimited domain FreeTTS Speech Synthesizer " + 41 "from Sun Labs", null, Locale.US, Boolean.FALSE, null);42 43 // create a Synthesizer 44 speechSynthesizer = Central.createSynthesizer( descriptor );45

Create a SynthesizerMode-Desc for FreeTTS synthesizer

Create a synthesizer object

Page 29: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

2004 Prentice Hall, Inc.All rights reserved.

Outline29

PhoneBook.java(3 of 8)

46 // Synthesizer created successfully47 if ( speechSynthesizer != null ) 48 { 49 // prepare synthesizer to speak 50 speechSynthesizer.allocate(); 51 speechSynthesizer.resume(); 52 53 // get synthesizer properties 54 SynthesizerProperties properties = 55 speechSynthesizer.getSynthesizerProperties();56 57 // set up speaking rate 58 properties.setSpeakingRate( 100.0f );59 60 } // end if61 62 else 63 { 64 System.err.println( "Synthesizer creation failed." );65 System.exit( 1 ); 66 } 67 68 } // end try69

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

Page 30: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

2004 Prentice Hall, Inc.All rights reserved.

Outline30

PhoneBook.java(4 of 8)

70 catch ( Exception myException )71 {72 myException.printStackTrace();73 } 74 // set up GUI75 createUserInterface();76 77 } // end constructor78 79 // create and position GUI components; register event handler80 private void createUserInterface()81 {82 // get content pane for attaching GUI components83 Container contentPane = getContentPane();84 85 // enable explicit positioning of GUI components86 contentPane.setLayout( null );87 88 // set up instruction1JLabel89 instruction1JLabel = new JLabel();90 instruction1JLabel.setBounds( 16, 8, 264, 23 );91 instruction1JLabel.setText( 92 "Select a name from the combo box." );93 contentPane.add( instruction1JLabel );94

Catch exceptions

Page 31: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

2004 Prentice Hall, Inc.All rights reserved.

Outline31

PhoneBook.java(5 of 8)

95 // set up instruction2JLabel96 instruction2JLabel = new JLabel();97 instruction2JLabel.setBounds( 16, 35, 264, 23 );98 instruction2JLabel.setText( 99 "Click the button to listen to the phone number." );100 contentPane.add( instruction2JLabel );101 102 // set up nameJComboBox103 nameJComboBox = new JComboBox( namesArray );104 nameJComboBox.setBounds( 50, 65, 150, 23 );105 contentPane.add( nameJComboBox );106 107 // set up getPhoneNumberJButton108 getPhoneNumberJButton = new JButton( "Get Phone Number" );109 getPhoneNumberJButton.setBounds( 50, 100, 150, 23 );110 getPhoneNumberJButton.setText( "Get Phone Number" );111 contentPane.add( getPhoneNumberJButton );112 getPhoneNumberJButton.addActionListener(113 114 new ActionListener() // anonymous inner class115 {116 // event handler called when getPhoneNumberJButton117 // is clicked118 public void actionPerformed( ActionEvent event )119 {

Page 32: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

2004 Prentice Hall, Inc.All rights reserved.

Outline32

PhoneBook.java(6 of 8)

120 getPhoneNumberJButtonActionPerformed( event );121 }122 123 } // end anonymous inner class124 125 ); // end call to addActionListener126 127 // set properties of application's window128 setTitle( "Phone Book" ); // set title bar string129 setSize( 300, 160 ); // set window size130 setVisible( true ); // display window131 132 // ensure synthesizer is cleaned up133 // when user closes application134 addWindowListener(135 136 new WindowAdapter() // anonymous inner class137 {138 public void windowClosing( WindowEvent event )139 {140 frameWindowClosing( event );141 }142 143 } // end anonymous inner class144

Page 33: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

2004 Prentice Hall, Inc.All rights reserved.

Outline33

PhoneBook.java(7 of 8)

145 ); // end addWindowListener146 147 } // end method createUserInterface148 149 // speak a person's phone number150 private void getPhoneNumberJButtonActionPerformed( 151 ActionEvent event )152 {153 // get index of the selected person 154 int selectedName = nameJComboBox.getSelectedIndex();155 156 // declare text to speak 157 String phoneNumberString = namesArray[ selectedName ] + 158 "'s phone number is " + numbersArray[ selectedName ];159 160 // speak the person's name and phone number 161 speechSynthesizer.speakPlainText( phoneNumberString, null );162 163 } // end method getPhoneNumberJButtonActionPerformed164 165 // cleanup synthesizer166 private void frameWindowClosing( WindowEvent event )167 {168 // deallocate synthesizer 169 try

Get index of the selected person

Speak the person’s name and phone number

Declare text to speak

Page 34: © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved.…

2004 Prentice Hall, Inc.All rights reserved.

Outline34

PhoneBook.java(8 of 8)

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 method185 public static void main( String[] args ) 186 {187 PhoneBook application = new PhoneBook();188 189 } // end method main190 191 } // end class PhoneBook

Deallocate synthesizer