Using MaryTTS or OpenMary in Java

« Back to list of posts

So now I've started another project (Again). This ones called Aimie and happens to be a robot with an AI coded in Java.

For the voice portion of Aimie, I needed a voice synthesiser that sounded good but I also didnt want to reinvent the wheel, so I'm using a free TTS client/server called MaryTTS or OpenMary.

I've found there's not much documentation for getting a standalone mary server/client running in your own application. The good news? It's easier than you thought.

Take a look at the code below to get an idea of what I mean:

package Speech;

import javax.sound.sampled.AudioInputStream;
import marytts.LocalMaryInterface;
import marytts.MaryInterface;
import marytts.exceptions.MaryConfigurationException;
import marytts.exceptions.SynthesisException;
import marytts.util.data.audio.AudioPlayer;

public class Voice
{
    private MaryInterface marytts;
    private AudioPlayer ap;
            
    public Voice(String voiceName)
    {
        try
        {
            marytts = new LocalMaryInterface();
            marytts.setVoice(voiceName);
            ap = new AudioPlayer();
        }
        catch (MaryConfigurationException ex)
        {
            ex.printStackTrace();
        }
    }
    
    public void say(String input)
    {
        try
        {
            AudioInputStream audio = marytts.generateAudio(input);
            
            ap.setAudio(audio);
            ap.start();
        }
        catch (SynthesisException ex)
        {
            System.err.println("Error saying phrase.");
        }
    }
}

As you can see, you only have to set up a 'LocalMaryInterface' to get all the benefits of Mary.

Ignore all the people saying you need a server but beware, the startup time can be a few seconds.


By Luke Alderton at 23 Dec 2013, 23:01 PM

Tags: Robot,Java

Comments

Danilo de sousa
how was the implementation of LocalMaryInterface, followed some kind of marytts?
22/04/2015
Luke
What do you mean by followed?
22/04/2015
Danilo
has adapted some MaryTTS class to create the LocalMaryInterface?
22/04/2015
Luke
Nope, it is part of marytts.
22/04/2015
Ben
Hi Luke, im playing around with the marytts runtime. everything is working well as it seemt to be for you. The only thing i cant get to work are setting different audioeffects like Robot or whisper. Can you help ? :)
28/11/2015
Luke
Hi Ben, unfortunately I haven't used audio effects within my setup, sorry.
29/11/2015
Ben
Oh, that is a pity. Thanks for your fast respond.
01/12/2015
Nietoperz
This requires a running Mary server.
29/09/2017
faiza
how can i setup LocalMaryInterface?
22/10/2017

Post a comment

Please correct the following:
Share with