!7

p {
  // Try coughing
  //S text = [[<spurt audio="g0001_004">cough</spurt> I can now cough]];
  
  S text = [[<spurt audio="g0001_028">cough</spurt>]];

  //S text = [[Today, <voice emotion='happy'>the sun is shining.</voice>]];
  
  //S text = [[When people leave a tip they want to know it will
  //<voice emotion='cross'> not be used</voice> to make up the minimum wage.]];
  
  File credentialsFile = newFile(javaxSecretDir(), "cereproc.txt");
  L<S> secret = tlft(credentialsFile);
  assertEquals("Please give me " + f2s(credentialsFile), 2, l(secret));
  
  String xml = "<?xml version='1.0'?><speakSimple>"
      +"<accountID>" + first(secret) + "</accountID>"
      +"<password>" + second(secret) + "</password>"
      +"<voice>Jess</voice>"
      +"<text>" + text + "</text></speakSimple>";
      
  URL u = new URL("https://cerevoice.com/rest/rest_1_1.php");
  URLConnection uc = openConnection(u);
  HttpURLConnection connection = (HttpURLConnection) uc;
  connection.setDoOutput(true);
  connection.setDoInput(true);
  connection.setRequestMethod("POST");
  connection.setRequestProperty("Content-Type","text/xml; charset=utf-8");
  connection.setRequestProperty("Content-Length", String.valueOf(xml));
  OutputStream out = connection.getOutputStream();
  Writer wout = new OutputStreamWriter(out);
  wout.write(xml);  
  wout.flush();
  wout.close();
  S result = utf8streamToString(connection.getInputStream());
  printIndent(result);
  /* 
     Using your favourite Java XML Parser,
     parse the XML output, check that resultCode is
     equal to 1 and act accordingly 
  */
  assertEquals("Voice Synthesis Result", "1", xml_contentsOfTag(result, "resultCode"));
  S fileUrl = xml_contentsOfTag(result, "fileUrl");
  print("URL: " + fileUrl);
  byte[] data = loadBinaryPage(fileUrl);
  print("Got " + l(data) + " bytes");
  saveBinaryProgramFile("jess.ogg", data);
  playOgg(byteArrayInputStream(data));
}