2

Im trying to use FreeTTS in my java program (from https://freetts.sourceforge.io/docs/index.php) and I am getting this error: java.lang.ClassCastException: com.sun.speech.freetts.en.us.cmu_time_awb.AlanVoiceDirectory cannot be cast to com.sun.speech.freetts.VoiceDirectory

Ive tried re-adding the Free TTS jar files to my project and this is my code:

import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;

public class TextToSpeech {
//String voiceName = "kevin16";
VoiceManager freeVM;
Voice voice;
public TextToSpeech(String words) {
    freeVM = VoiceManager.getInstance();
    voice = VoiceManager.getInstance().getVoice("kevin16");
    if (voice != null) {
        voice.allocate();//Allocating Voice
    }

    try {
        voice.setRate(190);//Setting the rate of the voice
        voice.setPitch(150);//Setting the Pitch of the voice
        voice.setVolume(3);//Setting the volume of the voice
        SpeakText(words);// Calling speak() method


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



}

public void SpeakText(String words) {
    voice.speak(words);
}

I call the TextToSpeech constructor from another class like this:

 new TextToSpeech("Hello World");

Would appreciate any help or advice!

1 Answers1

7

Change TextToSpeech contructor like this:

public TextToSpeech(String words) {
    System.setProperty("freetts.voices", "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");
    voice = VoiceManager.getInstance().getVoice("kevin16");
    if (voice != null) {
        voice.allocate();// Allocating Voice
        try {
            voice.setRate(190);// Setting the rate of the voice
            voice.setPitch(150);// Setting the Pitch of the voice
            voice.setVolume(3);// Setting the volume of the voice
            SpeakText(words);// Calling speak() method

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

    } else {
        throw new IllegalStateException("Cannot find voice: kevin16");
    }
}

The idea is to instruct freetts to use com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory class instead of AlanVoiceDirectory class.

  • Ah brilliant thanks! I've added my imports and Ill try this out –  Oct 26 '18 at 17:44
  • Thanks that worked! Really appreciate your help and the explaination! –  Oct 26 '18 at 17:48
  • I know we are not supposed to comment for thanks etc and just give a thumbs up, but you just saved my whole semester. Thank you so much. – Kostas Thanasis Mar 25 '20 at 14:45
  • I'd like to know why that works and why such a janky solution is needed. Is there a better way? – Andrew S Mar 28 '22 at 03:17
  • By adding System.setProperty("freetts.voices", "com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory"), my old java code is working again. My environment is Java 11 in Windows 10 and Ubuntu 20.04. – oraclesoon Sep 15 '22 at 12:05