-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStreamingFromLocalFile.java
More file actions
116 lines (95 loc) · 3.9 KB
/
Copy pathStreamingFromLocalFile.java
File metadata and controls
116 lines (95 loc) · 3.9 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package ai.rev;
import ai.rev.speechtotext.models.streaming.ConnectedMessage;
import ai.rev.speechtotext.models.streaming.Hypothesis;
import ai.rev.speechtotext.models.streaming.SessionConfig;
import ai.rev.speechtotext.models.streaming.StreamContentType;
import okhttp3.Response;
import okio.ByteString;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.Arrays;
public class StreamingFromLocalFile {
public static void main(String[] args) throws InterruptedException {
// Assign your access token to a String
String accessToken = "<YOUR_ACCESS_TOKEN>";
// Configure the streaming content type
StreamContentType streamContentType = new StreamContentType();
streamContentType.setContentType("audio/x-raw"); // audio content type
streamContentType.setLayout("interleaved"); // layout
streamContentType.setFormat("S16LE"); // format
streamContentType.setRate(16000); // sample rate
streamContentType.setChannels(1); // channels
// Setup the SessionConfig with any optional parameters
SessionConfig sessionConfig = new SessionConfig();
sessionConfig.setMetaData("Streaming from the Java SDK");
sessionConfig.setFilterProfanity(true);
sessionConfig.setRemoveDisfluencies(true);
sessionConfig.setDeleteAfterSeconds(2592000); // 30 days in seconds
sessionConfig.setDetailedPartials(false);
sessionConfig.setStartTs(0.0);
sessionConfig.setTranscriber("machine");
sessionConfig.setLanguage("en");
sessionConfig.setSkipPostprocessing(false);
// Initialize your client with your access token
StreamingClient streamingClient = new StreamingClient(accessToken);
// Initialize your WebSocket listener
WebSocketListener webSocketListener = new WebSocketListener();
// Begin the streaming session
streamingClient.connect(webSocketListener, streamContentType, sessionConfig);
// Read file from disk
File file = new File("path/to/file/foo.raw");
// Convert file into byte array
byte[] fileByteArray = new byte[(int) file.length()];
try (final FileInputStream fileInputStream = new FileInputStream(file)) {
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
try (final DataInputStream dataInputStream = new DataInputStream(bufferedInputStream)) {
dataInputStream.readFully(fileByteArray, 0, fileByteArray.length);
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
} catch (IOException e) {
throw new RuntimeException(e.getMessage());
}
// Set the number of bytes to send in each message
int chunk = 8000;
// Stream the file in the configured chunk size
for (int start = 0; start < fileByteArray.length; start += chunk) {
streamingClient.sendAudioData(
ByteString.of(
ByteBuffer.wrap(
Arrays.copyOfRange(
fileByteArray, start, Math.min(fileByteArray.length, start + chunk)))));
}
// Wait to make sure all responses are received
Thread.sleep(5000);
// Close the WebSocket
streamingClient.close();
}
// Your WebSocket listener for all streaming responses
private static class WebSocketListener implements RevAiWebSocketListener {
@Override
public void onConnected(ConnectedMessage message) {
System.out.println(message);
}
@Override
public void onHypothesis(Hypothesis hypothesis) {
System.out.println(hypothesis.toString());
}
@Override
public void onError(Throwable t, Response response) {
System.out.println(response);
}
@Override
public void onClose(int code, String reason) {
System.out.println(reason);
}
@Override
public void onOpen(Response response) {
System.out.println(response.toString());
}
}
}