Presentation is loading. Please wait.

Presentation is loading. Please wait.

© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and.

Similar presentations


Presentation on theme: "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and."— Presentation transcript:

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

2 © 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.

3 © 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

4 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 4 28.1 Java Speech API (Cont.)

5 © 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

6 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 6 28.2 Downloading and Installing FreeTTS Figure 28.1 Locations for downloading FreeTTS. Click the symbol to download

7 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 7 28.2 Downloading and Installing FreeTTS (Cont.) Figure 28.2 Decompressing freetts-1_1_2.tar.

8 © 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

9 © 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 Specifying the extract destination. Extract the content in freetts-1_1_1.tar to the C:\ directory

10 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 10 28.2 Downloading and Installing FreeTTS (Cont.) Figure 28.5 Accepting the license agreement.

11 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 11 28.2 Downloading and Installing FreeTTS (Cont.) Figure 28.6 Completing the Java Speech API implementation installation.

12 © 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 Phone Book application.

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

14 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 14 28.4 Constructing the Phone Book Application (Cont.)

15 © Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 15 28.4 Constructing the Phone Book Application (Cont.) Figure 28.9 Importing the Java Speech API packages. Importing the Java Speech API packages

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

17 © 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

18 © 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

19 © 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 Initializing the speech synthesizer. Create SynthesizerModeDesc for FreeTTS synthesizer Create a synthesizer

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

21 © 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

22 © 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

23 © 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

24 © 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. speakPlainText method –Used to speak text Get index of the selected person Declare text to speak Speak the person ’ s name and phone number

25 © 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 method –Free resources that are allocated to the speech engine Deallocate speech synthesizer

26 © 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 Running the completed application.

27  2004 Prentice Hall, Inc. All rights reserved. Outline 27 PhoneBook.java (1 of 8) 1 // Tutorial 28: PhoneBook.java 2 // 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 instructions 13 private JLabel instruction1JLabel; 14 private JLabel instruction2JLabel; 15 16 // JComboBox for names 17 private JComboBox nameJComboBox; 18 19 // JButton to get phone number 20 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

28  2004 Prentice Hall, Inc. All rights reserved. Outline 28 PhoneBook.java (2 of 8) 25 // fill array with people's names 26 private String[] namesArray = { "John", "Jennifer", "Howard" }; 27 28 // fill array with people's phone numbers 29 private String[] numbersArray = 30 { "(555) 555-9876", "(555) 555-1234", "(555) 555-4567" }; 31 32 // no-argument constructor 33 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

29  2004 Prentice Hall, Inc. All rights reserved. Outline 29 PhoneBook.java (3 of 8) 46 // Synthesizer created successfully 47 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 if 61 62 else 63 { 64 System.err.println( "Synthesizer creation failed." ); 65 System.exit( 1 ); 66 } 67 68 } // end try 69 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

30  2004 Prentice Hall, Inc. All rights reserved. Outline 30 PhoneBook.java (4 of 8) 70 catch ( Exception myException ) 71 { 72 myException.printStackTrace(); 73 } 74 // set up GUI 75 createUserInterface(); 76 77 } // end constructor 78 79 // create and position GUI components; register event handler 80 private void createUserInterface() 81 { 82 // get content pane for attaching GUI components 83 Container contentPane = getContentPane(); 84 85 // enable explicit positioning of GUI components 86 contentPane.setLayout( null ); 87 88 // set up instruction1JLabel 89 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

31  2004 Prentice Hall, Inc. All rights reserved. Outline 31 PhoneBook.java (5 of 8) 95 // set up instruction2JLabel 96 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 nameJComboBox 103 nameJComboBox = new JComboBox( namesArray ); 104 nameJComboBox.setBounds( 50, 65, 150, 23 ); 105 contentPane.add( nameJComboBox ); 106 107 // set up getPhoneNumberJButton 108 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 class 115 { 116 // event handler called when getPhoneNumberJButton 117 // is clicked 118 public void actionPerformed( ActionEvent event ) 119 {

32  2004 Prentice Hall, Inc. All rights reserved. Outline 32 PhoneBook.java (6 of 8) 120 getPhoneNumberJButtonActionPerformed( event ); 121 } 122 123 } // end anonymous inner class 124 125 ); // end call to addActionListener 126 127 // set properties of application's window 128 setTitle( "Phone Book" ); // set title bar string 129 setSize( 300, 160 ); // set window size 130 setVisible( true ); // display window 131 132 // ensure synthesizer is cleaned up 133 // when user closes application 134 addWindowListener( 135 136 new WindowAdapter() // anonymous inner class 137 { 138 public void windowClosing( WindowEvent event ) 139 { 140 frameWindowClosing( event ); 141 } 142 143 } // end anonymous inner class 144

33  2004 Prentice Hall, Inc. All rights reserved. Outline 33 PhoneBook.java (7 of 8) 145 ); // end addWindowListener 146 147 } // end method createUserInterface 148 149 // speak a person's phone number 150 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 getPhoneNumberJButtonActionPerformed 164 165 // cleanup synthesizer 166 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

34  2004 Prentice Hall, Inc. All rights reserved. Outline 34 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 method 185 public static void main( String[] args ) 186 { 187 PhoneBook application = new PhoneBook(); 188 189 } // end method main 190 191 } // end class PhoneBook Deallocate synthesizer


Download ppt "© Copyright 1992-2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 28.1 Java Speech API 28.2 Downloading and."

Similar presentations


Ads by Google