Last active
June 9, 2016 13:04
-
-
Save joshuajnoble/194e477035e5625669f6d33127612399 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.URL; | |
import java.io.*; | |
import javax.net.ssl.HttpsURLConnection; | |
import java.net.URLEncoder; | |
String clientId = "[email protected]"; | |
String clientSecret = "8264ac9334dd47feb7a7ad9a26ddaccc"; | |
String speechHost = "https://speech.platform.bing.com"; | |
String post_data; | |
String AccessTokenUri = "https://oxford-speech.cloudapp.net/token/issueToken"; | |
String speechPath = "/recognize/query?scenarios=ulm&appid=D4D52672-91D7-4C74-8AD8-42B1D98141A5&locale=en-US&device.os=wp7&version=3.0" + | |
"&format=xml&requestid=1d4b6030-9099-11e0-91e4-0800200c9a66&instanceid=1d4b6030-9099-11e0-91e4-0800200c9a66"; | |
JSONObject json; | |
public void setup() | |
{ | |
size(400, 400); | |
smooth(); | |
post_data = "grant_type=client_credentials&client_id=" + URLEncoder.encode(clientId) + "&client_secret=" + | |
URLEncoder.encode(clientSecret) + "&scope=" + speechHost; | |
println(post_data); | |
try | |
{ | |
println(" starting "); | |
URL myurl = new URL(AccessTokenUri); | |
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection(); | |
con.setRequestMethod("POST"); | |
con.setRequestProperty("Content-type", "application/x-www-form-urlencoded"); | |
con.setRequestProperty("Content-Length", String.valueOf(post_data.length())); | |
con.setDoOutput(true); | |
con.setDoInput(true); | |
DataOutputStream output = new DataOutputStream(con.getOutputStream()); | |
output.writeBytes(post_data); | |
output.close(); | |
println(" closing "); | |
DataInputStream input = new DataInputStream( con.getInputStream() ); | |
println(" input back "); | |
String resp = ""; | |
for ( int c = input.read(); c != -1; c = input.read() ) { | |
System.out.print( (char)c ); | |
resp += (char) c; | |
} | |
input.close(); | |
System.out.println("Resp Code:"+con.getResponseCode()); | |
System.out.println("Resp Message:"+ con.getResponseMessage()); | |
json = JSONObject.parse(resp); | |
print(json.getString("access_token")); // make sure we're parsing the token correctly | |
try { | |
// now we can load our wav file | |
byte b[] = loadBytes("josh2.wav"); | |
URL myurl2 = new URL(speechHost + speechPath); | |
println(" ----------------- new url " ); | |
println(myurl2); | |
HttpsURLConnection con2 = (HttpsURLConnection)myurl2.openConnection(); | |
con2.setRequestMethod("POST"); | |
con2.setRequestProperty("Content-type", "audio/wav; samplerate=8000"); | |
println(" wave file is " + String.valueOf(b.length) + " long "); | |
con2.setRequestProperty("Content-Length", String.valueOf(b.length)); | |
con2.setRequestProperty("Authorization", "Bearer " + json.getString("access_token")); | |
con2.setDoOutput(true); | |
con2.setDoInput(true); | |
println(" now do output stream "); | |
OutputStream wr = con2.getOutputStream(); | |
println(" get output stream "); | |
wr.write(b); | |
println(" done writipng "); | |
wr.flush(); | |
wr.close(); | |
println( " closed "); | |
input = new DataInputStream( con2.getInputStream() ); | |
String resp2 = ""; | |
for ( int c = input.read(); c != -1; c = input.read() ) { | |
//System.out.print( (char)c ); | |
resp2 += (char) c; | |
} | |
input.close(); | |
XML response = parseXML(resp2); | |
println(response.getChild("results/result/lexical")); | |
System.out.println("Resp Code:"+con2.getResponseCode()); | |
System.out.println("Resp Message:"+ con2.getResponseMessage()); | |
//json = loadJSONObject(con2.getResponseMessage()); | |
} | |
catch (Exception e) | |
{ | |
println(" uh 2 "); | |
} | |
} | |
catch (Exception e) { | |
println(" uh" ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment