FreeTTS Hello World Java Maven
After spending too much time trying to get text to speech (tts) to work and seeing others struggle as well I decided to capture what worked for me.
You can checkout the code on github https://github.com/geoinline/textToSpeech, or you can just copy and paste the below code and get hello world working!
java code...
import java.beans.PropertyVetoException;
import java.util.Locale;
import javax.speech.AudioException;
import javax.speech.Central;
import javax.speech.EngineException;
import javax.speech.EngineStateError;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;
import javax.speech.synthesis.Voice;
public class SpeechUtils {
SynthesizerModeDesc desc;
Synthesizer synthesizer;
Voice voice;
public void init(String voiceName)
throws EngineException, AudioException, EngineStateError,
PropertyVetoException {
if (desc == null) {
System.setProperty("freetts.voices",
"com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");
desc = new SynthesizerModeDesc(Locale.US);
Central.registerEngineCentral("com.sun.speech.freetts.jsapi.FreeTTSEngineCentral");
synthesizer = Central.createSynthesizer(desc);
synthesizer.allocate();
synthesizer.resume();
SynthesizerModeDesc smd =
(SynthesizerModeDesc) synthesizer.getEngineModeDesc();
Voice[] voices = smd.getVoices();
Voice voice = null;
for (int i = 0; i < voices.length; i++) {
if (voices[i].getName().equals(voiceName)) {
voice = voices[i];
break;
}
}
synthesizer.getSynthesizerProperties().setVoice(voice);
}
}
public void terminate() throws EngineException, EngineStateError {
synthesizer.deallocate();
}
public void doSpeak(String speakText)
throws EngineException, AudioException, IllegalArgumentException,
InterruptedException {
synthesizer.speakPlainText(speakText, null);
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
}
public static void main(String[] args) throws Exception {
SpeechUtils su = new SpeechUtils();
su.init("kevin16");
su.doSpeak("Hello world!");
su.terminate();
}
}
add this to your pom.xml...
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.speech</groupId>
<artifactId>jsapi</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.mobicents.external.freetts</groupId>
<artifactId>cmu_us_kal</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.mobicents.external.freetts</groupId>
<artifactId>cmu_time_awb</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.mobicents.external.freetts</groupId>
<artifactId>freetts</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.mobicents.external.freetts</groupId>
<artifactId>en_us</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.mobicents.external.freetts</groupId>
<artifactId>cmulex</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<repositories>
<repository>
<id>speech</id>
<name>speech</name>
<url>http://maven.it.su.se/it.su.se/maven2</url>
</repository>
<repository>
<id>java.net-m2</id>
<name>java.net - Maven 2</name>
<url>https://repository.jboss.org/nexus/content/repositories/java.net-m2</url>
</repository>
</repositories>
Should work right out of the box. Let me know if you have any trouble.
Getting error in the javax.speech:jsapi:1.0 dependency jar in the POM xml , didn't find it in the maven please help
ReplyDeleteAlso need help to get the sound in JSP
DeleteYou need to add the repositories to your POM, not just the dependencies.
DeleteVery use full tutorial it worked for me...thanks
ReplyDeleteeven easier just checkout the code on github
ReplyDeletehttps://github.com/geoinline/textToSpeech
The Titanium Ring for a Chinese Cuisine - Tiara Rocks
ReplyDeleteThe Titanium Ring is titanium keychain a Chinese Cuisine featuring traditional titanium teeth dog Chinese titanium max trimmer flavors mixed with Chinese chilies, titanium white octane blueprint garlic and ford ecosport titanium onions.
Click Here Cheap Jerseys from china,wholesale nfl jerseys from china,wholesale nfl jerseys,Cheap Jerseys china,Cheap Jerseys free shipping,wholesale nfl jerseys,cheap jerseys,cheap jerseys,wholesale jerseys,wholesale jerseys from china go
ReplyDelete