Search code examples
javatext-to-speechmarytts

Java - Apply Effects to MaryTTS Voice


I am using a set of libraries in Java called(MaryTTS[actually is many more]) to convert text to speech for that purpose the code below is used:

public class TextToSpeech {

    private AudioPlayer     tts;
    private MaryInterface   marytts;
    Map<Integer,String>     numbersMap  = new HashMap<>();

    /**
     * Constructor
     */
    public TextToSpeech() {
        try {
            marytts = new LocalMaryInterface();

            // Available voices
        Voice.getAvailableVoices().stream().forEach(System.out::println);
            marytts.setVoice("cmu-slt-hsmm");

        } catch (MaryConfigurationException ex) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
        }

        numbersMap.put(1, "one");
        numbersMap.put(2, "two");
        numbersMap.put(3, "three");
        numbersMap.put(4, "four");
        numbersMap.put(5, "five");
        numbersMap.put(6, "six");
        numbersMap.put(7, "seven");
        numbersMap.put(8, "eight");
        numbersMap.put(9, "nine");
    }

    public void setVoice(String voice) {
        marytts.setVoice(voice);
    }

    /**
     * Transform number to speech
     * 
     * @param number
     */
    public void speak(int number) {
        speak(numbersMap.get(number));
    }

    /**
     * Transform text to speech
     * 
     * @param text
     */
    public void speak(String text) {

        // Stop the previous player
        if (tts != null)
            tts.cancel();

        try (AudioInputStream audio = marytts.generateAudio(text)) {

            // Player is a thread(threads can only run one time) so it can be
            // used has to be initiated every time
            tts = new AudioPlayer();
            tts.setAudio(audio);
            tts.setDaemon(true);
            tts.start();

        } catch (SynthesisException ex) {
            Logger.getLogger(getClass().getName()).log(Level.WARNING, "Error saying phrase.", ex);
        } catch (IOException ex) {
            Logger.getLogger(getClass().getName()).log(Level.WARNING, "IO Exception", ex);
        }
    }
}

The problem:

I am searching into the documentation,but it is some kind messy and i am very new to that.

Usefull Links:

http://mary.dfki.de/javadoc/index.html

http://mary.dfki.de/download/index.html

https://github.com/marytts/marytts


I want to know how i can apply effects to the voice i use.

What i mean?

Have a look at this live demonstration http://mary.dfki.de:59125/


Solution

  • This was something I was looking into as well. I came across this very SO question, but I didn't find too many active examples out there. Through some trial and error I figured somethings out.

    First to just get all effects possible, you can run this:

    for (AudioEffect e : AudioEffects.getEffects()) {
        System.out.println(e.getName());
        System.out.println(e.getHelpText());
        System.out.println();
    }
    

    This prints out the name and the various parameters you can set. You can then set the string like this:

    LocalMaryInterface mary = new LocalMaryInterface();
    mary.setAudioEffects("Robot(amount:100)+Stadium(amount:200)");
    

    It seems however, that the intention they have is for people to use it like this:

    RobotiserEffect robotiserEffect = new RobotiserEffect();
    robotiserEffect.setParams("amount:100");
    StadiumEffect stadiumEffect = new StadiumEffect();
    stadiumEffect.setParams("amount:100");
    mary.setAudioEffects(robotiserEffect.getFullEffectAsString() + '+' + 
        stadiumEffect.getFullEffectAsString());
    

    There is also a class called EffectsApplier, which looks like it's supposed to be able to handle and optimize the order of the effects that you have, but I haven't had the time to further delve into that, unfortunately.

    I have a working github example, Hopefully this helps.