# TTS sketch not working, why?

**URL:** <https://discourse.processing.org/t/tts-sketch-not-working-why/5681>\
**Category:** Libraries\
**Created:** [November 18, 2018, 2:22pm UTC](https://discourse.processing.org/t/tts-sketch-not-working-why/5681 "2018-11-18T14:22:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![GeorgeJava](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/georgejava/32/7646_2.png) [@GeorgeJava](https://discourse.processing.org/u/GeorgeJava)\
**Post date:** [November 18, 2018, 2:22pm UTC](https://discourse.processing.org/t/tts-sketch-not-working-why/5681/1 "2018-11-18T14:22:03Z")

</div>

Hello this sketch dont working i want to write some text and spell it from google translator, do you have some working library or code for TTS ? text to speak ?

```auto
import java.net.*; 
File f;
import java.io.FileOutputStream;
String sketchPath;
import ddf.minim.*;
AudioPlayer player;
Minim minim;
 
String s = "Type here";
 
void setup() {
  size(1280, 720);
  minim = new Minim(this);
  textAlign(CENTER, CENTER);
  textSize(50);
  stroke(255);
}
 
void draw() {
  background(0);
  text(s, 0, 0, width, height);
  if (player != null) {
    translate(0, 250);
    for(int i = 0; i < player.left.size()-1; i++) {
      line(i, 50 + player.left.get(i)*50, i+1, 50 + player.left.get(i+1)*50);
      line(i, 150 + player.right.get(i)*50, i+1, 150 + player.right.get(i+1)*50);
    }
  }
}
 
void keyPressed() {
  if (keyCode == BACKSPACE) {
    if (s.length() > 0) {
      s = s.substring(0, s.length()-1);
    }
  } else if (keyCode == DELETE) {
    s = "";
  } else if (keyCode == ENTER) {
    googleTTS(s, "en");
    if (player != null) { player.close(); }
    player = minim.loadFile(s + ".mp3", 2048);
    player.loop();
    s = "";
  } else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT && s.length() < 100) {
    s += key;
  }
}
 
void googleTTS(String txt, String language) {
  String u = "http://translate.google.com/translate_tts?tl=";
  u = u + language + "&q=" + txt;
  u = u.replace(" ", "%20");
  try {
    URL url = new URL(u);
    try {
      URLConnection connection = url.openConnection();
      connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 1.2.30703)");
      connection.connect();
      InputStream is = connection.getInputStream();
      File f = new File(sketchPath + "/" + txt + ".mp3");
      OutputStream out = new FileOutputStream(f);
      byte buf[] = new byte[1024];
      int len;
      while ((len = is.read(buf)) > 0) {
        out.write(buf, 0, len);
      }
      out.close();
      is.close();
      println("File created: " + txt + ".mp3");
    } catch (IOException e) {
      e.printStackTrace();
    }
  } catch (MalformedURLException e) {
    e.printStackTrace();
  }
}
 
void stop() {
  player.close();
  minim.stop();
  super.stop();
}

```

---

<div class="post-metadata">

**Author:** ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)\
**Post date:** [November 19, 2018, 5:32am UTC](https://discourse.processing.org/t/tts-sketch-not-working-why/5681/2 "2018-11-19T05:32:32Z")

</div>

I might be wrong, but I do not think the translate offers tts, or not anymore I was browsing the google cloud platform, and you can try using the tts from GCP. However, before you attempt this route, you can explore the [ttslib](https://www.local-guru.net/blog/pages/ttslib) that you can find in the Contribution manager in the PDE. After you **install** this library, explore the examples that comes with it as it seems to do a good job.

If you still want to use google via GCP, you can follow this [link](https://cloud.google.com/text-to-speech/docs/)

Cloud Text-To-Speech provides a daily amount of characters that you can convert into speech before it starts charging you. If you need more information, review the documentation in their site.

I also would like to suggest the following change to your code: `u = u.replace(" ", "%20");` could be instead, be done by using `String urlEncoded= urlEncoder(u);` This function is a static function and it comes with Processing. Also notice you only need to encode the parameters of your query.

Kf
