2

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/

GOXR3PLUS
  • 6,877
  • 9
  • 44
  • 93

1 Answers1

5

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.

Niko
  • 4,158
  • 9
  • 46
  • 85
  • Thank you very much!!,i will run try all your examples... then i will mark it as answer if nothing goes wrong :) . I want to ask something more.. How you run the examples without the libraries?I am new to that, i mean you take them from the internet using Maven or something like this?Cause i have downloaded them and added into the project as jar files... – GOXR3PLUS Nov 15 '16 at 13:45
  • The `AudioPlayer` of the Library seems to be a little buggy so i implemented a new version . In case you are interested you can find it here : https://github.com/goxr3plus/Java-Text-To-Speech-Tutorial/blob/master/src/model/AudioPlayer.java – GOXR3PLUS Nov 15 '16 at 13:54
  • Yeah, I got the library with maven. If you look at my github project, you can see my [POM file here](https://github.com/paul-nelson-baker/marytts-hello-world-demo/blob/master/pom.xml). Make note of the repositories and dependencies. – Niko Nov 15 '16 at 17:33
  • Hm i read the POM file,so the project depends on internet connection in order to run :)? – GOXR3PLUS Nov 15 '16 at 17:56
  • It will need an internet connection at least once to download the libraries to your computer, but It won't need it to run afterwards. – Niko Nov 15 '16 at 18:07
  • Oou i need to have a look at `Maven` then thanks man!`Grandle` works the same but better ? I am asking those questions cause i want seriously to use one of these two in my projects and i am new to this logic of building... :) – GOXR3PLUS Nov 15 '16 at 18:10
  • 1
    "Better" depends on what you need. If you just need dependency management, than either will work arbitrarily. Look at compairisons and use the tool that fits your needs: https://gradle.org/maven_vs_gradle/ – Niko Nov 15 '16 at 18:15