|
| 1 | +import com.alibaba.dashscope.multimodal.tingwu.TingWuRealtime; |
| 2 | +import com.alibaba.dashscope.multimodal.tingwu.TingWuRealtimeCallback; |
| 3 | +import com.alibaba.dashscope.multimodal.tingwu.TingWuRealtimeParam; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import lombok.extern.slf4j.Slf4j; |
| 6 | + |
| 7 | +import java.io.FileInputStream; |
| 8 | +import java.nio.ByteBuffer; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.nio.file.Paths; |
| 11 | + |
| 12 | +@Slf4j |
| 13 | +public class TingWuRealtimeUsage { |
| 14 | + |
| 15 | + public static TingWuRealtimeCallback tingWuRealtimeCallback = new TingWuRealtimeCallback() { |
| 16 | + |
| 17 | + @Override |
| 18 | + public void onStarted(String taskId) { |
| 19 | + log.debug("onStarted: {}", taskId); |
| 20 | + } |
| 21 | + |
| 22 | + @Override |
| 23 | + public void onStopped(String taskId) { |
| 24 | + log.debug("onStopped: {}", taskId); |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public void onError(String errorCode, String errorMsg) { |
| 29 | + log.debug("onError: {}, {}", errorCode, errorMsg); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public void onAiResult(String taskId, JsonObject content) { |
| 34 | + log.debug("onAiResult: {}, {}", taskId, content.toString()); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public void onRecognizeResult(String taskId, JsonObject content) { |
| 39 | + log.debug("onRecognizeResult: {}, {}", taskId, content.toString()); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void onSpeechListen(String taskId, String dataId) { |
| 44 | + log.debug("onSpeechListen: {}", taskId); |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public void onClosed() { |
| 49 | + log.debug("onClosed"); |
| 50 | + } |
| 51 | + }; |
| 52 | + |
| 53 | + public static void main(String[] args) { |
| 54 | + TingWuRealtimeParam tingWuRealtimeParam = TingWuRealtimeParam.builder() |
| 55 | + .model("tingwu-industrial-instruction") |
| 56 | + .format("pcm") |
| 57 | + .sampleRate(16000) |
| 58 | + .terminology("terminology") //please replace with your terminology |
| 59 | + .appId("your-app-id") |
| 60 | + .apiKey("your-api-key") |
| 61 | + .build(); |
| 62 | + |
| 63 | + Path filePath = Paths.get("local-path/test.pcm"); |
| 64 | + // 创建任务 |
| 65 | + TingWuRealtime tingWuRealtime = new TingWuRealtime(); |
| 66 | + // 启动任务 |
| 67 | + tingWuRealtime.call(tingWuRealtimeParam, tingWuRealtimeCallback); |
| 68 | + try { |
| 69 | + Thread.sleep(100); |
| 70 | + } catch (InterruptedException e) { |
| 71 | + throw new RuntimeException(e); |
| 72 | + } |
| 73 | + // 发送音频 |
| 74 | + try (FileInputStream fis = new FileInputStream(filePath.toFile())) { |
| 75 | + // chunk size set to 100 ms for 16KHz sample rate |
| 76 | + byte[] buffer = new byte[3200]; |
| 77 | + int bytesRead; |
| 78 | + // Loop to read chunks of the file |
| 79 | + while ((bytesRead = fis.read(buffer)) != -1) { |
| 80 | + ByteBuffer byteBuffer; |
| 81 | + if (bytesRead < buffer.length) { |
| 82 | + byteBuffer = ByteBuffer.wrap(buffer, 0, bytesRead); |
| 83 | + } else { |
| 84 | + byteBuffer = ByteBuffer.wrap(buffer); |
| 85 | + } |
| 86 | + // Send the ByteBuffer to the translation and recognition instance |
| 87 | + tingWuRealtime.sendAudioFrame(byteBuffer); |
| 88 | + Thread.sleep(100); |
| 89 | + buffer = new byte[3200]; |
| 90 | + } |
| 91 | + tingWuRealtime.stop(); |
| 92 | + Thread.sleep(1000 * 10); // wait for 10 seconds |
| 93 | + tingWuRealtime.getDuplexApi().close(1000, "bye"); |
| 94 | + } catch (Exception e) { |
| 95 | + e.printStackTrace(); |
| 96 | + tingWuRealtime.getDuplexApi().close(1000, "bye"); |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | + System.exit(0); |
| 101 | + } |
| 102 | +} |
0 commit comments