Converting Text to Speech


The SpeakText.java file converts the specified date, day, and time values to speech.

Listing 2-3 shows the contents of the SpeakText.java file:

Listing 2-3: The SpeakText.java File
start example
 /*Import required packages*/ import java.io.File; import java.util.Locale; import java.util.Vector; import javax.speech.Central; import javax.speech.Engine; import javax.speech.EngineList; import javax.speech.synthesis.Synthesizer; import javax.speech.synthesis.SynthesizerModeDesc; import javax.speech.synthesis.SynthesizerProperties; import javax.speech.synthesis.Voice; /* Class: SpeakText-Converts the text value to speech. Methods: listAllVoices(): Retrieves all the voices that the end user's system supports */ public class SpeakText  {    /*    listAllVoices():List all the system voices.    Parameters:    modeName:An object of String class.    Return Type: NA    */    public static void listAllVoices(String modeName)     {       System.out.println();       /*        Print the mode name and locale for the voice synthesizer       */       System.out.println(       "All " + modeName + " Mode JSAPI Synthesizers and Voices:");       SynthesizerModeDesc required = new SynthesizerModeDesc(       null,       modeName,       Locale.US,       null,       null);       /*       Retrieve all the available synthesizers        */       EngineList engineList = Central.availableSynthesizers(required);       /*       Start a loop from 0 to the total number of available synthesizers       */       for (int i = 0; i < engineList.size(); i++)        {          SynthesizerModeDesc desc = (SynthesizerModeDesc) engineList.get(i);          /*          Print the name, mode, and locale          */          System.out.println(" " + desc.getEngineName()          + " (mode=" + desc.getModeName()          + ", locale=" + desc.getLocale() + "):");          /*          Retrieve all the available voices in an array          */          Voice[] voices = desc.getVoices();          /*          Print all the available voices on console          */          for (int j = 0; j < voices.length; j++)           {             System.out.println(" " + voices[j].getName());          }       }    }    /*          SpeakText(): Default constructor of SpeakText class    Parameter:    k: An object of String class.    Return Type:NA    */    public SpeakText(String k)     {       String l=k;       /*       List all the available voices by calling the ListAllVoices() method       */       listAllVoices("general");       /*       Set the voice to kevin16       */       String voiceName = "kevin16";       System.out.println();       System.out.println("Using voice: " + voiceName);       try        {          SynthesizerModeDesc desc = new SynthesizerModeDesc          (             null,                       "general",                  Locale.US,                  null,                       null);                      Synthesizer synthesizer = Central.createSynthesizer(desc);             /*             Check if the value of synthesizer is null             */             if (synthesizer == null)              {                /*                Print the error message                */                String message = "\nCan't find synthesizer.\n"                + "Make sure that there is a \"speech.properties\"file"                + "at either of these locations: \n";                message += "user.home: "                + System.getProperty("user.home") + "\n";                message += "java.home/lib: " + System.getProperty("java.home")                + File.separator + "lib\n";                System.err.println(message);                System.exit(1);              }             /*             Allocate the synthesizer             */             synthesizer.allocate();             synthesizer.resume();             /*             Retrieve the engine mode description of the synthesizer             */             desc = (SynthesizerModeDesc) synthesizer.getEngineModeDesc();             /*             Retrieve the voices for the synthesizer             */             Voice[] voices = desc.getVoices();             Voice voice = null;             for (int i = 0; i < voices.length; i++)              {                if (voices[i].getName().equals(voiceName))                 {                   voice = voices[i];                   break;                }             }             /*             Checks if no voice exists             */             if (voice == null)              {                /*                Print a message that the voice selected does not exists                */                System.err.println("Synthesizer does not have a voice named "+ voiceName + ".");                System.exit(1);             }             /*             Set the voice for the synthesizer             */             synthesizer.getSynthesizerProperties().setVoice(voice);             /*             Speak the text using the speakPlainText() method             */             synthesizer.speakPlainText(l, null);             synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);          }           catch (Exception e)           {             e.printStackTrace();          }       }    } 
end example
 

Download this listing .

In the above code, the constructor of the SpeakText class takes the modeName string as an input parameter. This constructor calls the listAllVoices() method to list all the available voices and sets the voice for the synthesizer. Then, the synthesizer converts the date, day, or time to speech. The listAllVoices() method is a static method, which receives the modeName string as an input parameter. The listAllVoices() method returns the list of all available voices in the computer.




Java InstantCode. Developing Applications using Java Speech API
Java InstantCode. Developing Applications using Java Speech API
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 46

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net