xyz.zaddrot.api.Speaker.java Source code

Java tutorial

Introduction

Here is the source code for xyz.zaddrot.api.Speaker.java

Source

package xyz.zaddrot.api;

import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider;
import com.ivona.services.tts.IvonaSpeechCloudClient;
import com.ivona.services.tts.model.CreateSpeechRequest;
import com.ivona.services.tts.model.Input;
import com.ivona.services.tts.model.Voice;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

/**
 * Created by night on 15.05.2016.
 */
public class Speaker {
    final public static int MALE = 0;
    final public static int FEMALE = 1;

    public static void SuperGirl(int sex, String message) {
        if (message.length() < 200) {
            BufferedInputStream bis = new BufferedInputStream(
                    getAudio(sex, message, "/IvonaCredentials.properties"));
            try {
                Player player = new Player(bis);
                player.play();
            } catch (JavaLayerException ex) {
            }
        }
    }

    static IvonaSpeechCloudClient speechCloud;

    private static void init(String path) {
        speechCloud = new IvonaSpeechCloudClient(new ClasspathPropertiesFileCredentialsProvider(path));
        speechCloud.setEndpoint("https://tts.eu-west-1.ivonacloud.com");
    }

    private static InputStream getAudio(int sex, String text, String License) {
        init(License);
        CreateSpeechRequest createSpeechRequest = new CreateSpeechRequest();
        Input input = new Input();
        Voice voice = new Voice();

        switch (sex) {
        case 0:
            voice.setName("Maxim");
            break;
        case 1:
            voice.setName("Tatyana");
            break;
        }
        input.setData(text);

        createSpeechRequest.setInput(input);
        createSpeechRequest.setVoice(voice);

        InputStream audioSrc = null;
        try {
            URL url = new URL(String.valueOf(speechCloud.getCreateSpeechUrl(createSpeechRequest)));
            URLConnection urlConn = url.openConnection();
            audioSrc = urlConn.getInputStream();
        } catch (Exception e) {
        }
        return audioSrc;
    }
}