Toggle navigation
Toggle navigation
此项目
正在载入...
Sign in
xuning
/
sherpaonnx
转到一个项目
Toggle navigation
项目
群组
代码片段
帮助
Toggle navigation pinning
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Authored by
Fangjun Kuang
2024-07-30 17:21:33 +0800
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Committed by
GitHub
2024-07-30 17:21:33 +0800
Commit
9e02f88dbb332554a1566ce6742dc0ff52affded
9e02f88d
1 parent
06fd50f5
Non-streaming WebSocket client for Java. (#1190)
显示空白字符变更
内嵌
并排对比
正在显示
2 个修改的文件
包含
107 行增加
和
0 行删除
java-api-examples/NonStreamingWebsocketClient.java
java-api-examples/run-non-streaming-websocket-client.sh
java-api-examples/NonStreamingWebsocketClient.java
0 → 100644
查看文件 @
9e02f88
// 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
);
}
}
}
...
...
java-api-examples/run-non-streaming-websocket-client.sh
0 → 100755
查看文件 @
9e02f88
#!/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
...
...
请
注册
或
登录
后发表评论