-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpeechRecogClient.java
More file actions
88 lines (74 loc) · 3.57 KB
/
SpeechRecogClient.java
File metadata and controls
88 lines (74 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package speechRecognition;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.stream.Collectors;
import com.eclipsesource.json.Json;
import speechRecognition.SpeechAPI.*;
public class SpeechRecogClient {
private static final String REQUEST_URI = "https://speech.platform.bing.com/speech/recognition/%s/cognitiveservices/v1";
private static final String PARAMETERS = "language=%s&format=%s";
private RecognitionMode mode = RecognitionMode.Interactive;
private Language language = Language.en_US;
private OutputFormat format = OutputFormat.Simple;
private URL buildRequestURL() throws MalformedURLException {
String url = String.format(REQUEST_URI, mode.name().toLowerCase());
String params = String.format(PARAMETERS, language.name().replace('_', '-'), format.name().toLowerCase());
return new URL(String.format("%s?%s", url, params));
// return new URL("https://speech.platform.bing.com/speech/recognition/dictation/cognitiveservices/v1?language=en-US");
}
private HttpURLConnection connect() throws MalformedURLException, IOException {
HttpURLConnection connection = (HttpURLConnection) buildRequestURL().openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-type", "audio/wav; codec=\"audio/pcm\"; samplerate=16000");
connection.setRequestProperty("Accept", "application/json;text/xml");
connection.setRequestProperty("Ocp-Apim-Subscription-Key", "1d2e4ef44b5842a9897cefa570aa9cdd");
// connection.setRequestProperty("Host", "speech.platform.bing.com");
connection.setChunkedStreamingMode(1024); // 0 == default chunk size
connection.connect();
return connection;
}
private String getResponse(HttpURLConnection connection) throws IOException {
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new RuntimeException(String.format("Something went wrong, server returned: %d (%s)",
connection.getResponseCode(), connection.getResponseMessage()));
}
try (BufferedReader reader =
new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
String jsonResult = reader.lines().collect(Collectors.joining());
return Json.parse(jsonResult).asObject().get("DisplayText").asString();
}
}
private HttpURLConnection upload(InputStream is, HttpURLConnection connection) throws IOException {
try (OutputStream output = connection.getOutputStream()) {
byte[] buffer = new byte[1024];
int length;
while ((length = is.read(buffer)) != -1) {
output.write(buffer, 0, length);
}
output.flush();
}
return connection;
}
private HttpURLConnection upload(Path filepath, HttpURLConnection connection) throws IOException {
try (OutputStream output = connection.getOutputStream()) {
Files.copy(filepath, output);
}
return connection;
}
public String process(InputStream is) throws IOException {
return getResponse(upload(is, connect()));
}
public String process(Path filepath) throws IOException {
return getResponse(upload(filepath, connect()));
}
}