Java code for Text to speech in female voice

828 Views Asked by At

I want java code for text to speech in both male and female voice and after that I want to save that audio to wav file. I tried using this code but its only giving male voice. Please suggest me a way to get female voice without downloading a jar file (just by adding maven dependency or so).

Currently using this code -

     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 queue is empty.
     synthesizer.speakPlainText("Hi, this is me. Fine.", null);
     synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);

     // Deallocate the Synthesizer.
     synthesizer.deallocate();
     }

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

and this code -

  try {
        System.setProperty("FreeTTSSynthEngineCentral", "com.sun.speech.freetts.jsapi.FreeTTSEngineCentral");
        System.setProperty("freetts.voices", "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");
        Central.registerEngineCentral("com.sun.speech.freetts.jsapi.FreeTTSEngineCentral");

        SynthesizerModeDesc desc = new SynthesizerModeDesc(null, "general", Locale.US, null, null);

        Synthesizer synth = Central.createSynthesizer(desc);
        synth.allocate();
        desc = (SynthesizerModeDesc) synth.getEngineModeDesc();
        Voice voice = new Voice();
        // "business", "casual", "robotic", "breathy"
        voice.setAge(Voice.AGE_TEENAGER);
        voice.setGender(Voice.GENDER_FEMALE);
        voice.setStyle("breathy");
        synth.getSynthesizerProperties().setVoice(voice);
        synth.resume();
        synth.speakPlainText("Hi, this is me. Fine.", null);
        synth.waitEngineState(Synthesizer.QUEUE_EMPTY);
        synth.deallocate();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

but both are giving male voices (And there isn't any female voice available in KevinVoiceDirectory).

1

There are 1 best solutions below

0
Yuriy N. On

Use cloud providers: Amazon, Asure, Google, IBM. They all have text to speech services with tutorials and examples available.

Below example of using Asure:

import com.javaghost.scrapperutil.ReadWriteUtil;
import com.microsoft.cognitiveservices.speech.CancellationReason;
import com.microsoft.cognitiveservices.speech.ResultReason;
import com.microsoft.cognitiveservices.speech.SpeechConfig;
import com.microsoft.cognitiveservices.speech.SpeechSynthesisCancellationDetails;
import com.microsoft.cognitiveservices.speech.SpeechSynthesisOutputFormat;
import com.microsoft.cognitiveservices.speech.SpeechSynthesisResult;
import com.microsoft.cognitiveservices.speech.SpeechSynthesizer;
import com.microsoft.cognitiveservices.speech.audio.AudioConfig;
import java.io.File;

/**
 *
 */
public class AsureTts {

    private static String speechKey = "";
    private static String speechRegion = "";


    public static void generateAudio(String ssml,
            boolean isMp3, String pathToOutputFile) throws Exception {

        SpeechConfig speechConfig = SpeechConfig.fromSubscription(speechKey, speechRegion);
        if (isMp3) {
            speechConfig.setSpeechSynthesisOutputFormat(SpeechSynthesisOutputFormat.Audio48Khz192KBitRateMonoMp3);
        }

        AudioConfig fileOutput = AudioConfig.fromWavFileOutput(pathToOutputFile);

        SpeechSynthesizer speechSynthesizer = new SpeechSynthesizer(speechConfig, fileOutput);

        SpeechSynthesisResult speechSynthesisResult = speechSynthesizer.SpeakSsmlAsync(ssml).get();

        if (speechSynthesisResult.getReason() == ResultReason.SynthesizingAudioCompleted) {
            System.out.println("Speech synthesis completed!");
        } else if (speechSynthesisResult.getReason() == ResultReason.Canceled) {
            SpeechSynthesisCancellationDetails cancellation = SpeechSynthesisCancellationDetails.fromResult(speechSynthesisResult);
            System.out.println("CANCELED: Reason=" + cancellation.getReason());

            if (cancellation.getReason() == CancellationReason.Error) {
                System.out.println("CANCELED: ErrorCode=" + cancellation.getErrorCode());
                System.out.println("CANCELED: ErrorDetails=" + cancellation.getErrorDetails());
                System.out.println("CANCELED: Did you set the speech resource key and region values?");
            }
        }

    }
}