Non-streaming WebSocket client for Java. (#1190)
This commit is contained in:
71
java-api-examples/NonStreamingWebsocketClient.java
Normal file
71
java-api-examples/NonStreamingWebsocketClient.java
Normal file
@@ -0,0 +1,71 @@
|
||||
// Refer to
|
||||
// https://stackoverflow.com/questions/55380813/require-assistance-with-simple-pure-java-11-websocket-client-example
|
||||
//
|
||||
//
|
||||
// This is a WebSocketClient client for ../python-api-examples/non_streaming_server.py
|
||||
//
|
||||
// Please see ./run-non-streaming-websocket-client.sh
|
||||
import com.k2fsa.sherpa.onnx.*;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.WebSocket;
|
||||
import java.nio.*;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
|
||||
public class NonStreamingWebsocketClient {
|
||||
public static void main(String[] args) throws Exception {
|
||||
CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
WebSocket ws =
|
||||
HttpClient.newHttpClient()
|
||||
.newWebSocketBuilder()
|
||||
.buildAsync(URI.create("ws://localhost:6006"), new WebSocketClient(latch))
|
||||
.join();
|
||||
|
||||
// Please use a 16-bit, single channel wav for testing.
|
||||
// the sample rate does not need to be 16kHz
|
||||
String waveFilename = "./zh.wav";
|
||||
WaveReader reader = new WaveReader(waveFilename);
|
||||
int sampleRate = reader.getSampleRate();
|
||||
int numSamples = reader.getSamples().length;
|
||||
|
||||
// Here is the format of the message
|
||||
// byte 0-3 in little endian: sampleRate
|
||||
// byte 4-7 in little endian: number of bytes for samples
|
||||
// remaining bytes: samples. Each sample is a float32
|
||||
ByteBuffer buffer = ByteBuffer.allocate(8 + 4 * numSamples).order(ByteOrder.LITTLE_ENDIAN);
|
||||
buffer.putInt(sampleRate);
|
||||
buffer.putInt(numSamples * 4); // each sample has 4 bytes
|
||||
|
||||
for (float s : reader.getSamples()) {
|
||||
buffer.putFloat(s);
|
||||
}
|
||||
|
||||
buffer.rewind();
|
||||
buffer.flip();
|
||||
buffer.order(ByteOrder.LITTLE_ENDIAN);
|
||||
|
||||
ws.sendBinary(ByteBuffer.wrap(buffer.array()), true).join();
|
||||
|
||||
// Send Done to the server to indicate that we don't have new wave files to decode
|
||||
ws.sendText("Done", true).join();
|
||||
|
||||
latch.await();
|
||||
}
|
||||
|
||||
private static class WebSocketClient implements WebSocket.Listener {
|
||||
private final CountDownLatch latch;
|
||||
|
||||
public WebSocketClient(CountDownLatch latch) {
|
||||
this.latch = latch;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletionStage<?> onText(WebSocket webSocket, CharSequence data, boolean last) {
|
||||
System.out.println("Result is " + data);
|
||||
latch.countDown();
|
||||
return WebSocket.Listener.super.onText(webSocket, data, last);
|
||||
}
|
||||
}
|
||||
}
|
||||
36
java-api-examples/run-non-streaming-websocket-client.sh
Executable file
36
java-api-examples/run-non-streaming-websocket-client.sh
Executable file
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -ex
|
||||
|
||||
if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then
|
||||
mkdir -p ../build
|
||||
pushd ../build
|
||||
cmake \
|
||||
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \
|
||||
-DSHERPA_ONNX_ENABLE_TESTS=OFF \
|
||||
-DSHERPA_ONNX_ENABLE_CHECK=OFF \
|
||||
-DBUILD_SHARED_LIBS=ON \
|
||||
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
|
||||
-DSHERPA_ONNX_ENABLE_JNI=ON \
|
||||
..
|
||||
|
||||
make -j4
|
||||
ls -lh lib
|
||||
popd
|
||||
fi
|
||||
|
||||
if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then
|
||||
pushd ../sherpa-onnx/java-api
|
||||
make
|
||||
popd
|
||||
fi
|
||||
|
||||
if [ ! -f zh.wav ]; then
|
||||
# wget https://huggingface.co/csukuangfj/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/resolve/main/test_wavs/zh.wav
|
||||
wget https://hf-mirror.com/csukuangfj/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/resolve/main/test_wavs/zh.wav
|
||||
fi
|
||||
|
||||
java \
|
||||
-Djava.library.path=$PWD/../build/lib \
|
||||
-cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \
|
||||
NonStreamingWebsocketClient.java
|
||||
Reference in New Issue
Block a user