Using MaryTTS or OpenMary in Java

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.


Published at

Tags: Robot,Java

Luke Alderton

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
ever tried to change the audioeffects? Like Robot ?`
28/11/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
Share with
Tags
Latest Comments
By Mark Gentry on Windows Server 2019 - Change product key does nothing
20 Aug 2021, 03:30 AM
By Cathy on In-Place Upgrade for a Windows Server domain controller
31 Jul 2021, 18:28 PM
By Mr. Greymatter on Raspberry Pi - Running Java app on Raspbian
16 Feb 2021, 07:35 AM
By Mikko Seittenranta on Xamarin Forms multiple instances of same app open
16 Feb 2021, 04:34 AM
By Andrew on Auto/Custom height on Xamarin Forms WebView for Android and iOS
22 Jan 2021, 22:15 PM
By Nick on Raspberry Pi - Running Java app on Raspbian
14 Oct 2020, 19:37 PM
By Ivan on Fixed: Value cannot be null Parameter Name: source
15 Sep 2020, 19:47 PM
By Anand on Raspberry Pi - Bluetooth using Bluecove on Raspbian
7 Sep 2020, 16:53 PM
Categories
App Development
Event
Game Development
Mapping
Modelling
Programming
Review
Robotics
Tutorial
Web Development