-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathTingWuRealtimeUsage.java
More file actions
102 lines (88 loc) · 3.44 KB
/
TingWuRealtimeUsage.java
File metadata and controls
102 lines (88 loc) · 3.44 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
import com.alibaba.dashscope.multimodal.tingwu.TingWuRealtime;
import com.alibaba.dashscope.multimodal.tingwu.TingWuRealtimeCallback;
import com.alibaba.dashscope.multimodal.tingwu.TingWuRealtimeParam;
import com.google.gson.JsonObject;
import lombok.extern.slf4j.Slf4j;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.file.Path;
import java.nio.file.Paths;
@Slf4j
public class TingWuRealtimeUsage {
public static TingWuRealtimeCallback tingWuRealtimeCallback = new TingWuRealtimeCallback() {
@Override
public void onStarted(String taskId) {
log.debug("onStarted: {}", taskId);
}
@Override
public void onStopped(String taskId) {
log.debug("onStopped: {}", taskId);
}
@Override
public void onError(String errorCode, String errorMsg) {
log.debug("onError: {}, {}", errorCode, errorMsg);
}
@Override
public void onAiResult(String taskId, JsonObject content) {
log.debug("onAiResult: {}, {}", taskId, content.toString());
}
@Override
public void onRecognizeResult(String taskId, JsonObject content) {
log.debug("onRecognizeResult: {}, {}", taskId, content.toString());
}
@Override
public void onSpeechListen(String taskId, String dataId) {
log.debug("onSpeechListen: {}", taskId);
}
@Override
public void onClosed() {
log.debug("onClosed");
}
};
public static void main(String[] args) {
TingWuRealtimeParam tingWuRealtimeParam = TingWuRealtimeParam.builder()
.model("tingwu-industrial-instruction")
.format("pcm")
.sampleRate(16000)
.terminology("terminology") //please replace with your terminology
.appId("your-app-id")
.apiKey("your-api-key")
.build();
Path filePath = Paths.get("local-path/test.pcm");
// 创建任务
TingWuRealtime tingWuRealtime = new TingWuRealtime();
// 启动任务
tingWuRealtime.call(tingWuRealtimeParam, tingWuRealtimeCallback);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// 发送音频
try (FileInputStream fis = new FileInputStream(filePath.toFile())) {
// chunk size set to 100 ms for 16KHz sample rate
byte[] buffer = new byte[3200];
int bytesRead;
// Loop to read chunks of the file
while ((bytesRead = fis.read(buffer)) != -1) {
ByteBuffer byteBuffer;
if (bytesRead < buffer.length) {
byteBuffer = ByteBuffer.wrap(buffer, 0, bytesRead);
} else {
byteBuffer = ByteBuffer.wrap(buffer);
}
// Send the ByteBuffer to the translation and recognition instance
tingWuRealtime.sendAudioFrame(byteBuffer);
Thread.sleep(100);
buffer = new byte[3200];
}
tingWuRealtime.stop();
Thread.sleep(1000 * 10); // wait for 10 seconds
tingWuRealtime.getDuplexApi().close(1000, "bye");
} catch (Exception e) {
e.printStackTrace();
tingWuRealtime.getDuplexApi().close(1000, "bye");
}
System.exit(0);
}
}