FileDocCategorySizeDatePackage
SpeechListener.javaAPI DocExample5313Wed Jul 17 00:42:38 BST 2002com.wiverson.macosbook.speech

SpeechListener.java

package com.wiverson.macosbook.speech;

import javax.swing.JLabel;
import javax.swing.JComboBox;
import java.awt.BorderLayout;

public class SpeechListener extends javax.swing.JDialog implements
java.awt.event.ActionListener,
com.apple.speech.recognition.UnrecognizedEventListener,
com.apple.speech.recognition.DetectedEventListener,
com.apple.speech.recognition.DoneEventListener
{
    
    // Set up the speech recognition engine
    static com.apple.speech.recognition.Recognizer mySpeechRecognizer = null;
    static com.apple.speech.recognition.LanguageModel myLanguageModel = null;
    
    // Set up the text-to-speech engine
    static com.apple.speech.synthesis.Synthesizer mySynthesizer = null;
    
    
    public SpeechListener()
    {
        this.getContentPane().setLayout(new BorderLayout());
        statusLabel = new JLabel("Ready.");
        statusLabel.setHorizontalTextPosition(statusLabel.LEFT);
        this.getContentPane().add(statusLabel, BorderLayout.CENTER);
        
        manualCommandMenu = new JComboBox();
        manualCommandMenu.setModel(new javax.swing.DefaultComboBoxModel(tasks));
        manualCommandMenu.addActionListener(this);
        
        this.getContentPane().add(manualCommandMenu, BorderLayout.EAST);
        
        this.pack();
        this.setSize(300, 50);
        this.setTitle("Address me as " + computerName);
        
        // Set up to talk have the computer talk back.
        if(mySynthesizer == null)
            mySynthesizer = new com.apple.speech.synthesis.Synthesizer();
        
        try
        {
            // Hack for workaround of bug which
            // prevents Java apps from receiving
            // AppleEvents in Mac OS X 10.0
            com.apple.ae.AppleEventFunctions.initAE();
            
            // Create the SpeechRecoginizer.  Speech is activated lazily upon startup.
            mySpeechRecognizer = new com.apple.speech.recognition.Recognizer();
            
            // Create & setup the LanguageModel which we will add our phrases to.
            myLanguageModel = new com.apple.speech.recognition.LanguageModel();
            mySpeechRecognizer.setLanguageModel(myLanguageModel);
            
            // Add the phrases we are looking for.
            // Note that we need to add the computer's address first.
            // Still, easier than using the more complex API
            String[] full_tasks = new String[tasks.length];
            for(int i = 0; i < tasks.length; i++)
                full_tasks[i] = computerName + tasks[i];
            
            myLanguageModel.setPhrases(full_tasks);
            
            // Start the recoginizer
            mySpeechRecognizer.start();
            
            // Listen for speech events
            mySpeechRecognizer.addDoneEventListener(this);
            mySpeechRecognizer.addUnrecognizedEventListener(this);
            mySpeechRecognizer.addDetectedEventListener(this);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    
    private JLabel statusLabel;
    private JComboBox manualCommandMenu;
    private String computerName = "Computer ";
    
    static final private int DAY = 0;
    static final private int SONG = 1;
    static final private int QUIT = 2;
    static final private int BEEP = 3;
    
    private String[] tasks =
    {
        "what day is it",
        "sing a song",
        "quit",
        "beep"
    };
    
    static void main(String[] args)
    {
        (new SpeechListener()).show();
    }
    
    public void doCommand(String input)
    {
        statusLabel.setText("I heard " + input);
        
        if(input.compareTo(tasks[DAY]) == 0)
        {
            mySynthesizer.speakText(new java.util.Date().toString());
        }
        
        if(input.compareTo(tasks[SONG]) == 0)
        {
            mySynthesizer.speakText("Sorry, I'm shy");
        }
        
        if(input.compareTo(tasks[QUIT]) == 0)
        {
            System.exit(0);
        }
        
        if(input.compareTo(tasks[BEEP]) == 0)
        {
            java.awt.Toolkit.getDefaultToolkit().beep();
        }
    }
    
    public void handleDoneEvent(com.apple.speech.recognition.DoneEvent doneEvent)
    {
        String command = doneEvent.getPhraseRecognized();
        if(command != null)
        {
            command = command.substring(computerName.length(), command.length() );
            doCommand(command);
        } else
        {
            statusLabel.setText("Can't understand...?");
        }
    }
    
    public void actionPerformed(java.awt.event.ActionEvent actionEvent)
    {
        if(actionEvent.getSource() instanceof JComboBox)
        {
            doCommand(((JComboBox)actionEvent.getSource()).getSelectedItem().toString());
        }
    }
    
    public void handleDetectedEvent(com.apple.speech.recognition.DetectedEvent detectedEvent)
    {
        statusLabel.setText("Listening...");
    }
    
    public void handleUnrecognizedEvent(com.apple.speech.recognition.UnrecognizedEvent unrecognizedEvent)
    {
        statusLabel.setText("Unrecognized...");
    }
}