Skip to content

Instantly share code, notes, and snippets.

@rhulha
Created February 16, 2017 18:08
Show Gist options
  • Save rhulha/00d36c190f8944fd5ca935332fb10968 to your computer and use it in GitHub Desktop.
Save rhulha/00d36c190f8944fd5ca935332fb10968 to your computer and use it in GitHub Desktop.
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JMenu;
import javax.swing.JTextField;
import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider;
import com.ivona.services.tts.IvonaSpeechCloudClient;
import com.ivona.services.tts.model.CreateSpeechRequest;
import com.ivona.services.tts.model.CreateSpeechResult;
import com.ivona.services.tts.model.Input;
import com.ivona.services.tts.model.Voice;
import net.raysforge.easyswing.EasySwing;
import net.raysforge.easyswing.EasyTextArea;
public class Main implements ActionListener {
EasySwing es = new EasySwing("TTS", 1024, 768);
EasyTextArea eta = new EasyTextArea();
JTextField tf;
private IvonaSpeechCloudClient speechCloud;
public Main() {
speechCloud = new IvonaSpeechCloudClient(new ClasspathPropertiesFileCredentialsProvider("resources/IvonaCredentials.properties"));
speechCloud.setEndpoint("https://tts.eu-west-1.ivonacloud.com");
JMenu mi = es.addMenuItem("File");
es.addMenuItem(mi, "Save", "save", this);
tf = es.addToolBarTextField();
tf.setText("test42");
es.addToolBarItem("Convert English", "en", this);
es.addToolBarItem("Convert German", "de", this);
es.add(eta);
es.show();
}
public static void main(String[] args) {
new Main();
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("en")||e.getActionCommand().equals("de")) {
CreateSpeechRequest createSpeechRequest = new CreateSpeechRequest();
Input input = new Input();
Voice voice = new Voice();
String lang = e.getActionCommand();
if( lang.equals("de")) {
voice.setName("Marlene");
} else {
voice.setName("Salli");
}
String text = eta.getText();
for (int i = 0, n=0; i < text.length(); i += 8000, n++) {
String part = text.substring(i, Math.min(i+8000, text.length()));
input.setData(part);
createSpeechRequest.setInput(input);
createSpeechRequest.setVoice(voice);
CreateSpeechResult createSpeechResult = speechCloud.createSpeech(createSpeechRequest);
System.out.println(" content type:\t" + createSpeechResult.getContentType());
System.out.println(" request id:\t" + createSpeechResult.getTtsRequestId());
System.out.println(" request chars:\t" + createSpeechResult.getTtsRequestCharacters());
System.out.println(" request units:\t" + createSpeechResult.getTtsRequestUnits());
InputStream in = null;
FileOutputStream out = null;
try {
in = createSpeechResult.getBody();
out = new FileOutputStream(new File(tf.getText()+"."+lang+"."+n+".mp3"));
byte[] buffer = new byte[2 * 1024];
int readBytes;
while ((readBytes = in.read(buffer)) > 0) {
// In the example we are only printing the bytes counter,
// In real-life scenario we would operate on the buffer
//System.out.println(" received bytes: " + readBytes);
out.write(buffer, 0, readBytes);
}
System.out.println("done");
in.close();
out.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment