Voice RSS Logo
 
Voice RSS Home Voice RSS Contacts Voice RSS Site Map Login  

Follow VoiceRSS on LinkedIn Follow VoiceRSS on Facebook Follow VoiceRSS on Twitter  
 
 

Text-to-speech Java SDK

The Voice RSS Text-to-Speech Java SDK wraps Voice RSS Text-to-Speech API.

The Voice RSS Text-to-Speech Java SDKs will help to make integration with our Text-to-Speech API faster and easer.

If you have any questions or suggestions please feel free to contact us via e-mail.



Documentation

To integrate an application with the Voice RSS Text-to-Speech Java SDK it needs to add reference to the package voicerss_tts.jar. The Voice RSS Text-to-Speech Java SDK implements converting text-to-speech synchronously and asynchronously. The Voice RSS Text-to-Speech Java SDK provides possibility to get speech in binary or Base64 string formats.

SDK specifications

Entity Description
VoiceProvider The SDK main class which provides text-to-speech converting functionality
VoiceParameters The class that contains voice parameters which defines text-to-speech converting parameters like text, language, and etc.
Languages The enumeration which contains languages list for text-to-speech converting
AudioCodec The enumeration which contains audio codecs list for text-to-speech converting
AudioFormat The enumeration which contains audio formats list for text-to-speech converting

Convert text-to-speech synchronously

The following example demonstraits synchronous converting text-to-speech as a binary array:

import java.io.FileOutputStream;
import com.voicerss.tts.AudioCodec;
import com.voicerss.tts.AudioFormat;
import com.voicerss.tts.Languages;
import com.voicerss.tts.SpeechDataEvent;
import com.voicerss.tts.SpeechDataEventListener;
import com.voicerss.tts.SpeechErrorEvent;
import com.voicerss.tts.SpeechErrorEventListener;
import com.voicerss.tts.VoiceParameters;
import com.voicerss.tts.VoiceProvider;

public class Example {
    public static void main (String args[]) throws Exception {
        VoiceProvider tts = new VoiceProvider("<API key>");
		
        VoiceParameters params = new VoiceParameters("Hello, world!", Languages.English_UnitedStates);
        params.setCodec(AudioCodec.WAV);
        params.setFormat(AudioFormat.Format_44KHZ.AF_44khz_16bit_stereo);
        params.setBase64(false);
        params.setSSML(false);
        params.setRate(0);
		
        byte[] voice = tts.speech(params);
		
        FileOutputStream fos = new FileOutputStream("voice.mp3");
        fos.write(voice, 0, voice.length);
        fos.flush();
        fos.close();
    }
}
                        

Convert text-to-speech asynchronously

The following example demonstraits asynchronous converting text-to-speech as a binary array based on event pattern:

import java.io.FileOutputStream;
import com.voicerss.tts.AudioCodec;
import com.voicerss.tts.AudioFormat;
import com.voicerss.tts.Languages;
import com.voicerss.tts.SpeechDataEvent;
import com.voicerss.tts.SpeechDataEventListener;
import com.voicerss.tts.SpeechErrorEvent;
import com.voicerss.tts.SpeechErrorEventListener;
import com.voicerss.tts.VoiceParameters;
import com.voicerss.tts.VoiceProvider;

public class Example {
    public static void main (String args[]) throws Exception {
        VoiceProvider tts = new VoiceProvider("<API key>");
		
        VoiceParameters params = new VoiceParameters("Hello, world!", Languages.English_UnitedStates);
        params.setCodec(AudioCodec.WAV);
        params.setFormat(AudioFormat.Format_44KHZ.AF_44khz_16bit_stereo);
        params.setBase64(false);
        params.setSSML(false);
        params.setRate(0);
		
        tts.addSpeechErrorEventListener(new SpeechErrorEventListener() {
            @Override
            public void handleSpeechErrorEvent(SpeechErrorEvent e) {
                System.out.print(e.getException().getMessage());
            }
        });
		
        tts.addSpeechDataEventListener(new SpeechDataEventListener() {
            @Override
            public void handleSpeechDataEvent(SpeechDataEvent e) {
                try {
                    byte[] voice = (byte[])e.getData();
					
                    FileOutputStream fos = new FileOutputStream("voice.mp3");
                    fos.write(voice, 0, voice.length);
                    fos.flush();
                    fos.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
		
        tts.speechAsync(params);
    }
}
                        

Convert text-to-speech as a Base64 string

The following example demonstraits synchronous converting text-to-speech as a Base64 string and prints HTML audio tag with audio contant to play in an internet browser:

import java.io.FileOutputStream;
import com.voicerss.tts.AudioCodec;
import com.voicerss.tts.AudioFormat;
import com.voicerss.tts.Languages;
import com.voicerss.tts.SpeechDataEvent;
import com.voicerss.tts.SpeechDataEventListener;
import com.voicerss.tts.SpeechErrorEvent;
import com.voicerss.tts.SpeechErrorEventListener;
import com.voicerss.tts.VoiceParameters;
import com.voicerss.tts.VoiceProvider;

public class Example {
    public static void main (String args[]) throws Exception {
        VoiceProvider tts = new VoiceProvider("<API key>");
		
        VoiceParameters params = new VoiceParameters("Hello, world!", Languages.English_UnitedStates);
        params.setCodec(AudioCodec.WAV);
        params.setFormat(AudioFormat.Format_44KHZ.AF_44khz_16bit_stereo);
        params.setBase64(false);
        params.setSSML(false);
        params.setRate(0);
		
        String voice = tts.speech(params);
		
        System.out.print(String.format("<audio src='%s' autoplay='autoplay'></audio>", voice));
    }
}