Examples

This document contains examples of the usage of the cross platform JSAPI API and also of the JSpeechLib Macintosh specific API. Please see Sun's Java™ Speech API Programmer's Guide for more examples.

JSAPI Example


import javax.speech.*;
import javax.speech.synthesis.*;
import java.util.Locale;


public class TrivialJSAPI {
        public static void main(String args[]) {
                try {
                        // Create a synthesizer for English
                        Synthesizer synth = Central.createSynthesizer(
                                new SynthesizerModeDesc(Locale.ENGLISH));

                        // Get it ready to speak
                        synth.allocate();
                        synth.resume();

                        // Speak the "Hello world" string
                        synth.speakPlainText("Hello, world!", null);

                        // Wait till speaking is done
                        synth.waitEngineState(Synthesizer.QUEUE_EMPTY);

                        // Clean up
                        synth.deallocate();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
}
                    

JSpeechLib Examples

A simple hello world 



import lordpixel.macspeech.*;

public class Trivial {

        public static void main(String args[]) {
                //speak with the voice the user set in the Speech control panel
                SpeechChannel.speakString("Say hello world");
        }
        
}
                    

Something a little fancier... 


import lordpixel.macspeech.*;
import lordpixel.jdirect.macspeechmanager.SpeechSynthConstants;

public class Fancy{

    public static void main(String args[]) {
    
	    SpeechChannel vicky, male;
	    
	    //is MacinTalk installed?
	    if (!SpeechChannel.isSpeechAvailable()) {
	    	return;
	    }
	    	    
	    try { 
	   		vicky = new SpeechChannel(new Voice("Victoria"));
		
			//name,sex,age and language - or set to don't care
			//this gets a middle aged, male American English voice.
			male = new SpeechChannel(
			 new Voice(  new Integer(SpeechSynthConstants.c_kMale),
                         Voice.kMiddleAdult,Voice.kUSEnglish));
			
			vicky.speakText("And its goodnight from me");
		
			while (vicky.isChannelBusy()) {;}
			
			
			male.speakText("And its goodnight from her");
			
			
			while (SpeechChannel.isSpeechBusyApplicationWide()) {;}
			
			
			vicky.dispose(); //finalisers will clean these up, but we're neat and tidy
			male.dispose();


		} catch ( NoAppropriateVoiceException NAVE) {
			//do something sensible
			System.out.println("A voice was not found: " + NAVE);
		}	
	}
}
                    

Copyright and License

The Java™ Speech API was developed by Sun Microsystems

The original JSAPI for Macintosh implementation is Copyright ©2000 Brendan Burns, except changes made to produce version 0.5 which are Copyright ©2000 Andrew Thompson.

JSpeechLib and associated documentation is Copyright ©1997-2000 Andrew Thompson

You rights with respect to this library, both binary and source code and are defined by the GNU Lesser General Public License. A copy has also been included with this distribution. You should read the license carefully as it governs your rights with respect to this library.