© Copyright 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
© Copyright 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 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Java Speech API (Cont.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Downloading and Installing FreeTTS Figure 28.1 Locations for downloading FreeTTS. Click the symbol to download
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Downloading and Installing FreeTTS (Cont.) Figure 28.2 Decompressing freetts-1_1_2.tar.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Downloading and Installing FreeTTS (Cont.) Figure 28.5 Accepting the license agreement.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Downloading and Installing FreeTTS (Cont.) Figure 28.6 Completing the Java Speech API implementation installation.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Test-Driving the Phone Book Application Figure 28.7 Phone Book application.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.)
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) Figure 28.9 Importing the Java Speech API packages. Importing the Java Speech API packages
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) Figure Declaring an instance variable for the speech synthesizer. Declaring an instance variable for the speech synthesizer
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) Figure Initializing the speech synthesizer. Create SynthesizerModeDesc for FreeTTS synthesizer Create a synthesizer
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) allocate method –Allocate resources required by the speech engine resume method –Readies the speech synthesizer to speak
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) Figure Getting the Synthesizer object ready to speak. Get speech synthesizer ready to speak
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved 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 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) Figure Setting the properties of the Synthesizer object. Get synthesizer property Set up speaking rate of the speech synthesizer
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) Figure 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
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) Figure Cleaning up the synthesizer. deallocate method –Free resources that are allocated to the speech engine Deallocate speech synthesizer
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Constructing the Phone Book Application (Cont.) Figure Running the completed application.
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; // JComboBox for names 17 private JComboBox nameJComboBox; // JButton to get phone number 20 private JButton getPhoneNumberJButton; // Synthesizer to speak phone number 23 private Synthesizer speechSynthesizer; 24 Import Java Speech API packages Declare the instance variable speechSynthesizer
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" }; // fill array with people's phone numbers 29 private String[] numbersArray = 30 { "(555) ", "(555) ", "(555) " }; // 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); // create a Synthesizer 44 speechSynthesizer = Central.createSynthesizer( descriptor ); 45 Create a SynthesizerMode- Desc for FreeTTS synthesizer Create a synthesizer object
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(); // get synthesizer properties 54 SynthesizerProperties properties = 55 speechSynthesizer.getSynthesizerProperties(); // set up speaking rate 58 properties.setSpeakingRate( 100.0f ); } // end if else 63 { 64 System.err.println( "Synthesizer creation failed." ); 65 System.exit( 1 ); 66 } } // 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
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(); } // end constructor // 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(); // enable explicit positioning of GUI components 86 contentPane.setLayout( null ); // 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
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 ); // set up nameJComboBox 103 nameJComboBox = new JComboBox( namesArray ); 104 nameJComboBox.setBounds( 50, 65, 150, 23 ); 105 contentPane.add( nameJComboBox ); // 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( new ActionListener() // anonymous inner class 115 { 116 // event handler called when getPhoneNumberJButton 117 // is clicked 118 public void actionPerformed( ActionEvent event ) 119 {
2004 Prentice Hall, Inc. All rights reserved. Outline 32 PhoneBook.java (6 of 8) 120 getPhoneNumberJButtonActionPerformed( event ); 121 } } // end anonymous inner class ); // end call to addActionListener // 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 // ensure synthesizer is cleaned up 133 // when user closes application 134 addWindowListener( new WindowAdapter() // anonymous inner class 137 { 138 public void windowClosing( WindowEvent event ) 139 { 140 frameWindowClosing( event ); 141 } } // end anonymous inner class 144
2004 Prentice Hall, Inc. All rights reserved. Outline 33 PhoneBook.java (7 of 8) 145 ); // end addWindowListener } // end method createUserInterface // 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(); // declare text to speak 157 String phoneNumberString = namesArray[ selectedName ] "'s phone number is " + numbersArray[ selectedName ]; // speak the person's name and phone number 161 speechSynthesizer.speakPlainText( phoneNumberString, null ); } // end method getPhoneNumberJButtonActionPerformed // 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
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 } } // end method frameWindowClosed // main method 185 public static void main( String[] args ) 186 { 187 PhoneBook application = new PhoneBook(); } // end method main } // end class PhoneBook Deallocate synthesizer