Committed by
GitHub
Non-streaming WebSocket client for Java. (#1190)
正在显示
2 个修改的文件
包含
107 行增加
和
0 行删除
| 1 | +// Refer to | ||
| 2 | +// https://stackoverflow.com/questions/55380813/require-assistance-with-simple-pure-java-11-websocket-client-example | ||
| 3 | +// | ||
| 4 | +// | ||
| 5 | +// This is a WebSocketClient client for ../python-api-examples/non_streaming_server.py | ||
| 6 | +// | ||
| 7 | +// Please see ./run-non-streaming-websocket-client.sh | ||
| 8 | +import com.k2fsa.sherpa.onnx.*; | ||
| 9 | +import java.net.URI; | ||
| 10 | +import java.net.http.HttpClient; | ||
| 11 | +import java.net.http.WebSocket; | ||
| 12 | +import java.nio.*; | ||
| 13 | +import java.util.concurrent.CompletionStage; | ||
| 14 | +import java.util.concurrent.CountDownLatch; | ||
| 15 | + | ||
| 16 | +public class NonStreamingWebsocketClient { | ||
| 17 | + public static void main(String[] args) throws Exception { | ||
| 18 | + CountDownLatch latch = new CountDownLatch(1); | ||
| 19 | + | ||
| 20 | + WebSocket ws = | ||
| 21 | + HttpClient.newHttpClient() | ||
| 22 | + .newWebSocketBuilder() | ||
| 23 | + .buildAsync(URI.create("ws://localhost:6006"), new WebSocketClient(latch)) | ||
| 24 | + .join(); | ||
| 25 | + | ||
| 26 | + // Please use a 16-bit, single channel wav for testing. | ||
| 27 | + // the sample rate does not need to be 16kHz | ||
| 28 | + String waveFilename = "./zh.wav"; | ||
| 29 | + WaveReader reader = new WaveReader(waveFilename); | ||
| 30 | + int sampleRate = reader.getSampleRate(); | ||
| 31 | + int numSamples = reader.getSamples().length; | ||
| 32 | + | ||
| 33 | + // Here is the format of the message | ||
| 34 | + // byte 0-3 in little endian: sampleRate | ||
| 35 | + // byte 4-7 in little endian: number of bytes for samples | ||
| 36 | + // remaining bytes: samples. Each sample is a float32 | ||
| 37 | + ByteBuffer buffer = ByteBuffer.allocate(8 + 4 * numSamples).order(ByteOrder.LITTLE_ENDIAN); | ||
| 38 | + buffer.putInt(sampleRate); | ||
| 39 | + buffer.putInt(numSamples * 4); // each sample has 4 bytes | ||
| 40 | + | ||
| 41 | + for (float s : reader.getSamples()) { | ||
| 42 | + buffer.putFloat(s); | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + buffer.rewind(); | ||
| 46 | + buffer.flip(); | ||
| 47 | + buffer.order(ByteOrder.LITTLE_ENDIAN); | ||
| 48 | + | ||
| 49 | + ws.sendBinary(ByteBuffer.wrap(buffer.array()), true).join(); | ||
| 50 | + | ||
| 51 | + // Send Done to the server to indicate that we don't have new wave files to decode | ||
| 52 | + ws.sendText("Done", true).join(); | ||
| 53 | + | ||
| 54 | + latch.await(); | ||
| 55 | + } | ||
| 56 | + | ||
| 57 | + private static class WebSocketClient implements WebSocket.Listener { | ||
| 58 | + private final CountDownLatch latch; | ||
| 59 | + | ||
| 60 | + public WebSocketClient(CountDownLatch latch) { | ||
| 61 | + this.latch = latch; | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + @Override | ||
| 65 | + public CompletionStage<?> onText(WebSocket webSocket, CharSequence data, boolean last) { | ||
| 66 | + System.out.println("Result is " + data); | ||
| 67 | + latch.countDown(); | ||
| 68 | + return WebSocket.Listener.super.onText(webSocket, data, last); | ||
| 69 | + } | ||
| 70 | + } | ||
| 71 | +} |
| 1 | +#!/usr/bin/env bash | ||
| 2 | + | ||
| 3 | +set -ex | ||
| 4 | + | ||
| 5 | +if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then | ||
| 6 | + mkdir -p ../build | ||
| 7 | + pushd ../build | ||
| 8 | + cmake \ | ||
| 9 | + -DSHERPA_ONNX_ENABLE_PYTHON=OFF \ | ||
| 10 | + -DSHERPA_ONNX_ENABLE_TESTS=OFF \ | ||
| 11 | + -DSHERPA_ONNX_ENABLE_CHECK=OFF \ | ||
| 12 | + -DBUILD_SHARED_LIBS=ON \ | ||
| 13 | + -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ | ||
| 14 | + -DSHERPA_ONNX_ENABLE_JNI=ON \ | ||
| 15 | + .. | ||
| 16 | + | ||
| 17 | + make -j4 | ||
| 18 | + ls -lh lib | ||
| 19 | + popd | ||
| 20 | +fi | ||
| 21 | + | ||
| 22 | +if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then | ||
| 23 | + pushd ../sherpa-onnx/java-api | ||
| 24 | + make | ||
| 25 | + popd | ||
| 26 | +fi | ||
| 27 | + | ||
| 28 | +if [ ! -f zh.wav ]; then | ||
| 29 | + # wget https://huggingface.co/csukuangfj/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/resolve/main/test_wavs/zh.wav | ||
| 30 | + wget https://hf-mirror.com/csukuangfj/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/resolve/main/test_wavs/zh.wav | ||
| 31 | +fi | ||
| 32 | + | ||
| 33 | +java \ | ||
| 34 | + -Djava.library.path=$PWD/../build/lib \ | ||
| 35 | + -cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \ | ||
| 36 | + NonStreamingWebsocketClient.java |
-
请 注册 或 登录 后发表评论