The Artima Developer Community
Sponsored Link

Java Buzz Forum
Java speaks back

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Sam Dalton

Posts: 143
Nickname: samd
Registered: Jun, 2003

Sam Dalton is a Java Developer with ThoughtWorks in teh UK
Java speaks back Posted: Mar 15, 2004 10:15 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Sam Dalton.
Original Post: Java speaks back
Feed Title: import java.*;
Feed URL: http://www.samjdalton.com/pebble/rss.xml
Feed Description: Random, Infrequent Bloggings of a Techie
Latest Java Buzz Posts
Latest Java Buzz Posts by Sam Dalton
Latest Posts From import java.*;

Advertisement

While perusing my friend Gerard's blog today I noticed he has been playing around with FreeTTS which is a speech synthesis system written entirely in Java.

FreeTTS provides some support for the Java Speech API v1.0 (JSAPI).

One day I will find a good use for this (when I ever have an assignment that allows me to write client side code!), but for now, I put together a small demo. All this does is speaks back anything that you type. You too can have endless (almost!) fun findong out how it pronounces various swear words (I am such a child somethimes ;)) Anyhow, the code is as follows:


package com.samjdalton.speakie;

import com.sun.speech.freetts.jsapi.FreeTTSEngineCentral;

import javax.speech.EngineCreate;
import javax.speech.EngineList;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;


public class JavaSpeak implements Runnable {
    private Synthesizer synthesizer;

    public JavaSpeak() {
        initialise();
    }


    private void initialise() {
        try {
            SynthesizerModeDesc desc =
                    new SynthesizerModeDesc(null,
                            "general",
                            Locale.US,
                            Boolean.FALSE,
                            null);

            FreeTTSEngineCentral central = new FreeTTSEngineCentral();
            EngineList list = central.createEngineList(desc);

            if (list.size() > 0) {
                EngineCreate creator = (EngineCreate) list.get(0);
                this.synthesizer = (Synthesizer) creator.createEngine();

            }
            if (this.synthesizer == null) {
                System.exit(1);
            }
            this.synthesizer.allocate();
            this.synthesizer.resume();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void run() {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        try {
            String line = reader.readLine();
            while (!line.equals("")) {
                synthesizer.speakPlainText(line, null);
                line = reader.readLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        System.exit(0);
    }


    public static void main(String[] args) {
        Runnable runnable = new JavaSpeak();
        Thread t = new Thread(runnable);
        t.start();
     }

}


Simply put freetts.jar and jsapi.jar from the distribution into your classpath, and type

java com.samjdalton.speakie.JavaSpeak

to run the demo (an empty line followed by a carriage return will terminate the program)

Read: Java speaks back

Topic: AOP Differences Previous Topic   Next Topic Topic: JavaOne blues, verse 2

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use