Converting Text to Speech in Java - GeeksforGeeks

19 Aug, 2019
Java Speech API: The Java Speech API allows Java applications to incorporate speech technology into their user interfaces. It defines a cross-platform API to support command and control recognizers, dictation systems and speech synthesizers.
Java Speech supports speech synthesis which means the process of generating spoken the language by machine on the basis of written input.
It is important to keep in mind that Java Speech is only a specification i.e. no implementation is included. Thus third-parties provide the implementations. The javax.speech package defines the common functionality of recognizers, synthesizers, and other speech engines. The package javax.speech.synthesis extends this basic functionality for synthesizers.
We will understand that what is required for java API to convert text to speech
Engine: The Engine interface is available inside the speech package.”Speech engine” is the generic term for a system designed to deal with either speech input or speech output.import javax.speech.Engine;Central: Central provides the ability to locate, select and create speech recognizers and speech synthesizers.import javax.speech.Central;SynthesizerModeDesc: SynthesizerModeDesc extends the EngineModeDesc with the properties that are specific to speech synthesizers.import javax.speech.synthesis.SynthesizerModeDesc;Synthesizer: The Synthesizer interface provides primary access to speech synthesis capabilities.SynthesizerModeDesc adds two properties: List of voices provided by the synthesizer Voice to be loaded when the synthesizer is started.import javax.speech.synthesis.Synthesizer;
Engine: The Engine interface is available inside the speech package.”Speech engine” is the generic term for a system designed to deal with either speech input or speech output.import javax.speech.Engine;
import javax.speech.Engine;
Central: Central provides the ability to locate, select and create speech recognizers and speech synthesizers.import javax.speech.Central;
import javax.speech.Central;
SynthesizerModeDesc: SynthesizerModeDesc extends the EngineModeDesc with the properties that are specific to speech synthesizers.import javax.speech.synthesis.SynthesizerModeDesc;
import javax.speech.synthesis.SynthesizerModeDesc;
Synthesizer: The Synthesizer interface provides primary access to speech synthesis capabilities.SynthesizerModeDesc adds two properties: List of voices provided by the synthesizer Voice to be loaded when the synthesizer is started.import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.Synthesizer;
Below is an open-source implementation of Java Speech Synthesis called FreeTTS in the form of steps:
Download the FreeTTS in the form of zip folder from here
Extract the zip file and go tofreetts-1.2.2-bin/freetts-1.2/lib/jsapi.exe
freetts-1.2.2-bin/freetts-1.2/lib/jsapi.exe
Open the jsapi.exe file and install it.
This will create a jar file by the name jsapi.jar. This is the JAR library that contains the FreeTTS library to be included in the project.
Create a new Java project in your IDE.
Include this jsapi.jar file into your project.
Now copy the below code into your project
Execute the project to get the below expected output.
Below is the code for the above project:
// Java code to convert text to speech  import java.util.Locale;import javax.speech.Central;import javax.speech.synthesis.Synthesizer;import javax.speech.synthesis.SynthesizerModeDesc;  public class TextSpeech {      public static void main(String[] args)    {          try {            // Set property as Kevin Dictionary            System.setProperty(                "freetts.voices",                "com.sun.speech.freetts.en.us"                    + ".cmu_us_kal.KevinVoiceDirectory");              // Register Engine            Central.registerEngineCentral(                "com.sun.speech.freetts"                + ".jsapi.FreeTTSEngineCentral");              // Create a Synthesizer            Synthesizer synthesizer                = Central.createSynthesizer(                    new SynthesizerModeDesc(Locale.US));              // Allocate synthesizer            synthesizer.allocate();              // Resume Synthesizer            synthesizer.resume();              // Speaks the given text            // until the queue is empty.            synthesizer.speakPlainText(                "GeeksforGeeks", null);            synthesizer.waitEngineState(                Synthesizer.QUEUE_EMPTY);              // Deallocate the Synthesizer.            synthesizer.deallocate();        }          catch (Exception e) {            e.printStackTrace();        }    }}
Output:YouTubeWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosVideo unavailableThis video is unavailableMore videos on YouTubeSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 0:00•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage">Unable to execute JavaScript.</div></div>
References:
https://docs.oracle.com/cd/E17802_01/products/products/java-media/speech/forDevelopers/jsapi-doc/javax/speech/package-summary.html
https://www.javatpoint.com/q/5931/java-code-for-converting-audio-to-text-and-video-to-audio
http://www.oracle.com/technetwork/java/jsapifaq-135248.html
Related article: Convert Text to Speech in Python
This article is contributed by Akash Sharan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
RishabhPrabhu
GBlog
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
DSA Sheet by Love Babbar
GET and POST requests using Python
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Types of Software Testing
Working with csv files in Python
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Object Oriented Programming (OOPs) Concept in Java
HashMap in Java with Examples