ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

The Java Speech API, Part 1
Pages: 1, 2, 3

Next, the initSpeechSynthesisEngine() method is called from the init() method. The initSpeechSynthesisEngine() method initializes the FreeTTS speech synthesis engine using the Central class and selects the voice to be used for the speech output. A valid initialization returns the reference to the speech synthesis engine in the Synthesizer object. This reference will be used throughout the life of the VoicePad application to provide speech synthesis functionality.

private void initSpeechSynthesisEngine()
{
    String message = "";
    String synthesizerName = System.getProperty("synthesizerName",
        "Unlimited domain FreeTTS Speech Synthesizer from Sun Labs");

    // Create a new SynthesizerModeDesc that will match the FreeTTS synthesizer.
    SynthesizerModeDesc desc = new SynthesizerModeDesc
                                    (synthesizerName,
                                    null,
                                    Locale.US,
                                    Boolean.FALSE,
                                    null);
    // obtain the Speech Synthesizer instance
    try
    {
        synthesizer = Central.createSynthesizer(desc);

        if (synthesizer == null)
        {
            message = "Make sure that there is a \"speech.properties\" file " +
                        "at either of these locations: \n" +
                        "user.home : " +
                        System.getProperty("user.home") + "\n" +
                        "java.home/lib: " + System.getProperty("java.home") +
                        File.separator + "lib\n";

            System.out.println("NOVUSWARE : ERROR! Synthesizer not found!");
            System.out.println(message);

            throw new Exception("Synthesizer not found!");
        }

        System.out.println("NOVUSWARE : Speech synthesizer obtained.");

        voiceName = System.getProperty("voiceName", VOICE_SELECTED);
        voice     = new Voice(voiceName, Voice.GENDER_DONT_CARE,
                        Voice.AGE_DONT_CARE, null);

        if (voice == null)
        {
            System.out.println("NOVUSWARE : ERROR! No voice selected!");
            throw new Exception("No voice selected!");
        }

        System.out.println("NOVUSWARE : Voice " + VOICE_SELECTED
                         + " selected.");
        synthesizer.allocate();
        synthesizer.resume();
        synthesizer.getSynthesizerProperties().setVoice(voice);
    }
    catch(Exception e)
    {
        e.printStackTrace();
        System.out.println("NOVUSWARE : ERROR!" + e);
        closeSpeechSynthesisEngine();
    }

    System.out.println("NOVUSWARE : Speech engine initialized.");
}

The different UI items of the application will be initialized from the init() method of the VoicePad class. We will wrap the code to create all of the menu UI items and their action listeners in simple getXXX() methods. First, the menu bar for the VoicePad application is created, upon which the two sets of user navigation menus will be attached, using the getVoicePadMenuBar() method. The next step is to create the file and speech menus using getFileMenu() and getSpeechMenu(), respectively.

// menu bar creation
private JMenuBar getVoicePadMenuBar()
{
    if (menuBar == null)
    {
        menuBar = new JMenuBar();

        menuBar.add(getFileMenu());
        menuBar.add(getSpeechMenu());

        System.out.println("NOVUSWARE : Menubar added.");
    }

    return menuBar;
}

// file menu and file menu items begin
private JMenu getFileMenu()
{
    if (fileMenu == null)
    {
        // create menu
        fileMenu = new JMenu("File");
        myActionListener =
            new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    System.out.println("NOVUSWARE : " 
                                     + "File menu action performed.");
                }
            };
        fileMenu.setMnemonic('f');
        fileMenu.addActionListener(myActionListener);

        fileMenu.add(getNewMenuItem());
        fileMenu.add(getOpenMenuItem());
        fileMenu.add(getSaveMenuItem());
        fileMenu.add(getExitMenuItem());

        System.out.println("NOVUSWARE : File menu created.");
    }

    return fileMenu;
}

// speech menu and speech menu items begin
private JMenu getSpeechMenu()
{
    if (speechMenu == null)
    {
        // create menu
        speechMenu = new JMenu("Speech");
        myActionListener =
            new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    System.out.println("NOVUSWARE : "
                                     + "Speech menu action performed.");
                }
            };
        speechMenu.setMnemonic('s');
        speechMenu.addActionListener(myActionListener);

        speechMenu.add(getPlayMenuItem());
        speechMenu.add(getPauseMenuItem());
        speechMenu.add(getResumeMenuItem());
        speechMenu.add(getStopMenuItem());

        System.out.println("NOVUSWARE : Speech menu created.");
    }
    return speechMenu;
}

Menu items for the file menu to enable user actions (creating a new file, opening an existing file, saving the contents of an edited file, and closing a file) will be created using the getNewMenuItem(), getOpenMenuItem(), getSaveMenuItem(), and getExitMenuItem() methods. Since this is a Swing application, the application will respond to different user-navigation events. We will define action listeners for each of the menu items that generate these user-navigation events in the VoicePad application. The text editing functionality will be handled by the action listeners of the menu items of the file menu.

private JMenuItem getNewMenuItem()
{
    if (newMenuItem == null)
    {
        newMenuItem = new JMenuItem("New");
        myActionListener =
            new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    textArea.setText("");
                    System.out.println("NOVUSWARE : "
                                     + "New menu item action performed.");
                }
            };
        newMenuItem.setMnemonic('n');
        newMenuItem.addActionListener(myActionListener);

        System.out.println("NOVUSWARE : New menu item created.");
    }

    return newMenuItem;
}

private JMenuItem getOpenMenuItem()
{
    if (openMenuItem == null)
    {
        openMenuItem = new JMenuItem("Open");
        myActionListener =
            new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    VoicePad.this.repaint();
                    if (fileChooser.showOpenDialog(VoicePad.this) ==
                        JFileChooser.APPROVE_OPTION)
                    {
                        File fSelected = fileChooser.getSelectedFile();
                        try
                        {
                            FileReader in = new FileReader(fSelected);
                            textArea.read(in, null);
                            in.close();
                        }
                        catch(IOException ioe)
                        {
                            ioe.printStackTrace();
                        }
                    }

                    System.out.println("NOVUSWARE : "
                                     + "Open menu item action performed.");
                }
            };
        openMenuItem.setMnemonic('o');
        openMenuItem.addActionListener(myActionListener);

        System.out.println("NOVUSWARE : Open menu item created.");
    }

    return openMenuItem;
}

private JMenuItem getSaveMenuItem()
{
    if (saveMenuItem == null)
    {
        saveMenuItem = new JMenuItem("Save");
        myActionListener =
            new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    VoicePad.this.repaint();
                    if (fileChooser.showSaveDialog(VoicePad.this) ==
                        JFileChooser.APPROVE_OPTION)
                    {
                        File fSelected = fileChooser.getSelectedFile();
                        try
                        {
                            FileWriter out = new FileWriter(fSelected);
                            VoicePad.this.textArea.write(out);
                            out.close();
                        }
                        catch(IOException ioe)
                        {
                            ioe.printStackTrace();
                        }
                    }

                    System.out.println("NOVUSWARE : "
                                     + "Save menu item action performed.");
                }
            };
        saveMenuItem.setMnemonic('s');
        saveMenuItem.addActionListener(myActionListener);

        System.out.println("NOVUSWARE : Save menu item created.");
    }

    return saveMenuItem;
}

private JMenuItem getExitMenuItem()
{
    if (exitMenuItem == null)
    {
        exitMenuItem = new JMenuItem("Exit");
        myActionListener =
            new ActionListener()
            {
                public void actionPerformed(ActionEvent ae)
                {
                    closeSpeechSynthesisEngine();
                    System.out.println("NOVUSWARE : "
                                     + "Exit menu item action performed.");
                    System.exit(0);
                }
            };
        exitMenuItem.setMnemonic('x');
        exitMenuItem.addActionListener(myActionListener);

        System.out.println("NOVUSWARE : Exit menu item created.");
    }

    return exitMenuItem;
}

Pages: 1, 2, 3

Next Pagearrow