[mary-users] client Java application

Nikolai Kouznetsov kzntsv at rogers.com
Wed Feb 22 05:40:22 CET 2012


Hello,

I am trying to see how I can run the TTS engine from within my Java 
application. To do this, I started with an example provided on MARy's 
website. When cling.dfki.uni-sb.de server is psecified, the clients works - 
it generates an utterance and plays it. When I use localhost, it does not 
work for me. When I run the provide maryclient from the bin directory it 
prodeuces the following:

java version "1.6.0_30"
Java(TM) SE Runtime Environment (build 1.6.0_30-b12)
Java HotSpot(TM) 64-Bit Server VM (build 20.5-b03, mixed mode)
Mary TTS client 4.3.0 (impl. 20101220)
Connected to localhost:59125, Mary TTS server 4.3.0 (impl. 20101220)


So, it looks like the maryclient can work with the localhost.




Please see the code and the generated output below.

========================== modified code code ===================

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;

import marytts.client.MaryClient;
import marytts.client.http.Address;
import marytts.util.data.audio.AudioPlayer;


public class MaryTTSClientExample {

      public static void main(String[] args) throws Exception
      {
         String serverHost = System.getProperty("server.host", "localhost");
          int serverPort = Integer.getInteger("server.port", 
59125).intValue();
          MaryClient mary = MaryClient.getMaryClient(new Address(serverHost, 
serverPort));
          String text = "Willkommen in der Welt der Sprachsynthese!";
          String locale = "de"; // or US English (en-US), Telugu (te), 
Turkish (tr), ...
          String inputType = "TEXT";
          String outputType = "AUDIO";
          String audioType = "WAVE";
          String defaultVoiceName = null;
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          mary.process(text, inputType, outputType, locale, audioType, 
defaultVoiceName, baos);
          // The byte array constitutes a full wave file, including the 
headers.
          // And now, play the audio data:
          AudioInputStream ais = AudioSystem.getAudioInputStream(
              new ByteArrayInputStream(baos.toByteArray()));
          LineListener lineListener = new LineListener() {
              public void update(LineEvent event) {
                  if (event.getType() == LineEvent.Type.START) {
                      System.err.println("Audio started playing.");
                  } else if (event.getType() == LineEvent.Type.STOP) {
                      System.err.println("Audio stopped playing.");
                  } else if (event.getType() == LineEvent.Type.OPEN) {
                      System.err.println("Audio line opened.");
                  } else if (event.getType() == LineEvent.Type.CLOSE) {
                      System.err.println("Audio line closed.");
                  }
              }
          };

          AudioPlayer ap = new AudioPlayer(ais, lineListener);
          ap.start();
      }

}

===================== the end of modified code 
=================================


%%%%%%%%%%%%%%%%%%%% output %%%%%%%%%%%%%%%%%%%%

C:\JavaCenter\JavaProjects\MaryTTSExample>java MaryTTSClientExample
Mary TTS client 4.3.0 (impl. 20101220)
Connected to localhost:59125, Mary TTS server 4.3.0 (impl. 20101220)
Exception in thread "main" java.io.IOException: Server returned HTTP 
response code: 500 for URL: http://localhost:59125/process
        at 
sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
        at 
marytts.client.http.MaryHttpClient.requestInputStream(MaryHttpClient.java:435)
        at 
marytts.client.http.MaryHttpClient._process(MaryHttpClient.java:481)
        at marytts.client.MaryClient.process(MaryClient.java:661)
        at marytts.client.MaryClient.process(MaryClient.java:668)
        at MaryTTSClientExample.main(MaryTTSClientExample.java:29)





=================== original code =========================
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;

import marytts.client.MaryClient;
import marytts.client.http.Address;
import marytts.util.data.audio.AudioPlayer;


public class MaryTTSClientExample {

      public static void main(String[] args) throws Exception
      {
         String serverHost = System.getProperty("server.host", 
"cling.dfki.uni-sb.de");
         //String serverHost = System.getProperty("server.host", 
"localhost");
          int serverPort = Integer.getInteger("server.port", 
59125).intValue();
          MaryClient mary = MaryClient.getMaryClient(new Address(serverHost, 
serverPort));
          String text = "Willkommen in der Welt der Sprachsynthese!";
          String locale = "de"; // or US English (en-US), Telugu (te), 
Turkish (tr), ...
          String inputType = "TEXT";
          String outputType = "AUDIO";
          String audioType = "WAVE";
          String defaultVoiceName = null;
          ByteArrayOutputStream baos = new ByteArrayOutputStream();
          mary.process(text, inputType, outputType, locale, audioType, 
defaultVoiceName, baos);
          // The byte array constitutes a full wave file, including the 
headers.
          // And now, play the audio data:
          AudioInputStream ais = AudioSystem.getAudioInputStream(
              new ByteArrayInputStream(baos.toByteArray()));
          LineListener lineListener = new LineListener() {
              public void update(LineEvent event) {
                  if (event.getType() == LineEvent.Type.START) {
                      System.err.println("Audio started playing.");
                  } else if (event.getType() == LineEvent.Type.STOP) {
                      System.err.println("Audio stopped playing.");
                  } else if (event.getType() == LineEvent.Type.OPEN) {
                      System.err.println("Audio line opened.");
                  } else if (event.getType() == LineEvent.Type.CLOSE) {
                      System.err.println("Audio line closed.");
                  }
              }
          };

          AudioPlayer ap = new AudioPlayer(ais, lineListener);
          ap.start();
      }

}

==================== the end of the original code 
=============================



%%%%%%%%%%%%%%  output %%%%%%%%%%%%%%%%%%%%%

C:\JavaCenter\JavaProjects\MaryTTSExample>java MaryTTSClientExample
Mary TTS client 4.3.0 (impl. 20101220)
Connected to cling.dfki.uni-sb.de:59125, Mary TTS server 4.3.0 (impl. 
20101220)
Audio line opened.
Audio started playing.
Audio stopped playing.
Audio line closed.

Please advise. I would like to understand why you client code does not work 
if I  specify localhost as the name of the sever.

My best regards,
Nikolai





More information about the Mary-users mailing list