package net.tutorial.service;

import com.ibm.watson.developer_cloud.text_to_speech.v1.TextToSpeech;
import net.tutorial.misc.FilenameInputStreamPair;
import net.tutorial.service.credential.TextToSpeechCredential;
import net.tutorial.utility.HTTPUtility;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;

/**
 * Created by pongpantola.
 */
public class TextToSpeechService {

    private TextToSpeech t2s;

    public TextToSpeechService(){
        TextToSpeechCredential t2sCred = new TextToSpeechCredential();
        t2s = new TextToSpeech();
        t2s.setUsernameAndPassword(t2sCred.username, t2sCred.password);
    }

    public InputStream convert(String text){
        String format = "audio/wav";

        InputStream speech = t2s.synthesize(text, format);

        return speech;
    }


    public void downloadFile(String text, String fName, HttpServletResponse response) {
        InputStream inStream = convert(text);
        FilenameInputStreamPair fiPair = new FilenameInputStreamPair(fName, inStream, "audio/wav");
        HTTPUtility.download(fiPair, response);
    }
}

@@ -589,24 +589,27 @@
 ();%0D%0A       
+ //
  t2s.setUser
@@ -658,24 +658,117 @@
 password);%0D%0A
+        t2s.setUsernameAndPassword(%22a3a4eabb-ad65-43fa-baf0-d14ce6b1f5c5%22, %22lxwAkqSmoL14%22);%0D%0A
     %7D%0D%0A%0D%0A   

update creds