Friday, 12 August 2016

Convert Text to Audio using FreeTTS

Converting a text to audio may have multiple use cases in automation like creating the audio files for any data feed, voice driven tests etc.

FreeTTS[Free Text To Speech] is a library based on Java Speech API which is built on core technologies like Speech Synthesis and Speech Recognition.

Follow the below steps to configure & code to CONVERT TEXT TO AUDIO



Step 1: 

Download the FreeTTS library from the below link.
Download Link to FreeTTS Libs


Step 2: 

Create the Java Project & add the downloaded FreeTTS files under lib folder [freetts-1.2\lib] to build path.
namely: jsapi.jar, freetts-jsapi10.jar, en_us.jar, mbrola.jar etc..

Steps 3: 

Create a class by name 'TextToSpeech' & create a method called 'Speak'

Add the below code to the method 'Speak' & call it in main.

String voiceName = "kevin16";
String speaktext= "This is Text to Speech Test";

//Create Synthesizer
SynthesizerModeDesc mode = new SynthesizerModeDesc(null, "general",Locale.US, null, null);
Synthesizer szr = Central.createSynthesizer(mode);
szr.allocate(); szr.resume();
mode = (SynthesizerModeDesc) szr.getEngineModeDesc();
//Get the Required voice name
Voice[] voices = mode.getVoices();
Voice voice = null;
for (int i = 0; i < voices.length; i++) {
if (voices[i].getName().equals(voiceName)) {
         voice = voices[i];
break;
}
}
szr.getSynthesizerProperties().setVoice(voice);

//Speak out the given Text
szr.speakPlainText(speaktext, null);
szr.waitEngineState(Synthesizer.QUEUE_EMPTY);
szr.deallocate();


NOTE: 


  1. 'jsapi.exe' to be executed to get the 'jsapi.jar' in the lib folder. Add the same jar to the project references.
  2. 'speech.properties' file to be placed in the system HOME directory. i.e., C:\users\<Username>

No comments:

Post a Comment