From 7d49a7257d658f2a623c9c972a46401dd4c50e52 Mon Sep 17 00:00:00 2001
From: 雾聪 <wucong.lyb@alibaba-inc.com>
Date: 星期四, 17 八月 2023 17:14:28 +0800
Subject: [PATCH] Merge branch 'main' of https://github.com/alibaba-damo-academy/FunASR into main
---
funasr/runtime/wss-client/FunASRWSClient_Online/Program.cs | 255 +++++++++++++++
funasr/runtime/wss-client/confg/config.ini | 2
egs/callhome/TOLD/soap/conf/EAND_ResNet34_SAN_L4N512_None_FFN_FSMN_L6N512_bce_dia_loss_01.yaml | 3
egs/callhome/TOLD/soap/run.sh | 87 +++-
funasr/runtime/wss-client/FunASRWSClient_Online/WaveCollect.cs | 106 ++++++
funasr/runtime/wss-client/FunASRClient_CShape.sln | 31 +
funasr/runtime/wss-client/FunASRWSClient_Offline/WebScoketClient.cs | 120 +++++++
funasr/runtime/wss-client/FunASRWSClient_Online/README.md | 9
funasr/runtime/wss-client/FunASRWSClient_Offline/Program.cs | 85 +++++
egs/callhome/TOLD/soap/conf/EAND_ResNet34_SAN_L4N512_None_FFN_FSMN_L6N512_bce_dia_loss_01_phase3.yaml | 3
funasr/runtime/docs/SDK_advanced_guide_online_zh.md | 2
funasr/runtime/wss-client/FunASRClient_CShape.suo | 0
funasr/runtime/wss-client/FunASRWSClient_Online/FunASRWSClient_Online.csproj | 15
funasr/runtime/wss-client/FunASRWSClient_Offline/README.md | 9
egs/callhome/TOLD/soap/conf/EAND_ResNet34_SAN_L4N512_None_FFN_FSMN_L6N512_bce_dia_loss_01_phase2.yaml | 3
funasr/runtime/run_server.sh | 14
funasr/runtime/run_server_2pass.sh | 15
funasr/runtime/wss-client/FunASRWSClient_Online/WebScoketClient.cs | 219 ++++++++++++
funasr/runtime/wss-client/confg/tmp.wav | 0
funasr/runtime/wss-client/FunASRWSClient_Offline/FunASRWSClient_Offline.csproj | 14
20 files changed, 962 insertions(+), 30 deletions(-)
diff --git a/egs/callhome/TOLD/soap/conf/EAND_ResNet34_SAN_L4N512_None_FFN_FSMN_L6N512_bce_dia_loss_01.yaml b/egs/callhome/TOLD/soap/conf/EAND_ResNet34_SAN_L4N512_None_FFN_FSMN_L6N512_bce_dia_loss_01.yaml
index 4a63a65..ce5dd89 100644
--- a/egs/callhome/TOLD/soap/conf/EAND_ResNet34_SAN_L4N512_None_FFN_FSMN_L6N512_bce_dia_loss_01.yaml
+++ b/egs/callhome/TOLD/soap/conf/EAND_ResNet34_SAN_L4N512_None_FFN_FSMN_L6N512_bce_dia_loss_01.yaml
@@ -1,3 +1,4 @@
+init: xavier_uniform
model: sond
model_conf:
lsm_weight: 0.0
@@ -98,7 +99,7 @@
num_workers: 8
max_epoch: 20
num_iters_per_epoch: 10000
-keep_nbest_models: 20
+keep_nbest_models: 5
# optimization related
accum_grad: 1
diff --git a/egs/callhome/TOLD/soap/conf/EAND_ResNet34_SAN_L4N512_None_FFN_FSMN_L6N512_bce_dia_loss_01_phase2.yaml b/egs/callhome/TOLD/soap/conf/EAND_ResNet34_SAN_L4N512_None_FFN_FSMN_L6N512_bce_dia_loss_01_phase2.yaml
index bd55e67..7124034 100644
--- a/egs/callhome/TOLD/soap/conf/EAND_ResNet34_SAN_L4N512_None_FFN_FSMN_L6N512_bce_dia_loss_01_phase2.yaml
+++ b/egs/callhome/TOLD/soap/conf/EAND_ResNet34_SAN_L4N512_None_FFN_FSMN_L6N512_bce_dia_loss_01_phase2.yaml
@@ -1,3 +1,4 @@
+init: xavier_uniform
model: sond
model_conf:
lsm_weight: 0.0
@@ -98,7 +99,7 @@
num_workers: 8
max_epoch: 30
num_iters_per_epoch: 10000
-keep_nbest_models: 30
+keep_nbest_models: 5
# optimization related
accum_grad: 1
diff --git a/egs/callhome/TOLD/soap/conf/EAND_ResNet34_SAN_L4N512_None_FFN_FSMN_L6N512_bce_dia_loss_01_phase3.yaml b/egs/callhome/TOLD/soap/conf/EAND_ResNet34_SAN_L4N512_None_FFN_FSMN_L6N512_bce_dia_loss_01_phase3.yaml
index b3d9957..8691fe9 100644
--- a/egs/callhome/TOLD/soap/conf/EAND_ResNet34_SAN_L4N512_None_FFN_FSMN_L6N512_bce_dia_loss_01_phase3.yaml
+++ b/egs/callhome/TOLD/soap/conf/EAND_ResNet34_SAN_L4N512_None_FFN_FSMN_L6N512_bce_dia_loss_01_phase3.yaml
@@ -1,3 +1,4 @@
+init: xavier_uniform
model: sond
model_conf:
lsm_weight: 0.0
@@ -96,7 +97,7 @@
# 6 samples
batch_size: 6
num_workers: 8
-max_epoch: 12
+max_epoch: 10
num_iters_per_epoch: 300
keep_nbest_models: 5
diff --git a/egs/callhome/TOLD/soap/run.sh b/egs/callhome/TOLD/soap/run.sh
index 5c5cbd4..63b2e46 100644
--- a/egs/callhome/TOLD/soap/run.sh
+++ b/egs/callhome/TOLD/soap/run.sh
@@ -8,7 +8,7 @@
# [2] Speaker Overlap-aware Neural Diarization for Multi-party Meeting Analysis, EMNLP 2022
# We recommend you run this script stage by stage.
-# [developing] This recipe includes:
+# This recipe includes:
# 1. simulating data with switchboard and NIST.
# 2. training the model from scratch for 3 stages:
# 2-1. pre-train on simu_swbd_sre
@@ -18,6 +18,7 @@
# Finally, you will get a similar DER result claimed in the paper.
# environment configuration
+# path/to/kaldi
kaldi_root=
if [ -z "${kaldi_root}" ]; then
@@ -34,21 +35,35 @@
ln -s ${kaldi_root}/egs/callhome_diarization/v2/utils ./utils
fi
+# path to Switchboard and NIST including:
+# LDC98S75, LDC99S79, LDC2002S06, LDC2001S13, LDC2004S07
+data_root=
+if [ -z "${data_root}" ]; then
+ echo "We need Switchboard and NIST to simulate data for pretraining."
+ echo "If you can't get them, please use 'finetune.sh' to finetune a pretrained model."
+ exit;
+fi
+
+# path/to/NIST/LDC2001S97
+callhome_root=
+if [ -z "${callhome_root}" ]; then
+ echo "We need callhome corpus for training."
+ echo "If you want inference only, please refer https://www.modelscope.cn/models/damo/speech_diarization_sond-en-us-callhome-8k-n16k4-pytorch/summary"
+ exit;
+fi
+
+
# machines configuration
gpu_devices="4,5,6,7" # for V100-16G, use 4 GPUs
gpu_num=4
count=1
# general configuration
-stage=3
-stop_stage=3
+stage=0
+stop_stage=19
# number of jobs for data process
nj=16
sr=8000
-
-# dataset related
-data_root=
-callhome_root=path/to/NIST/LDC2001S97
# experiment configuration
lang=en
@@ -68,16 +83,16 @@
freeze_param=
# inference related
-inference_model=valid.der.ave_5best.pth
+inference_model=valid.der.ave_5best.pb
inference_config=conf/basic_inference.yaml
inference_tag=""
-test_sets="callhome1"
+test_sets="callhome2"
gpu_inference=true # Whether to perform gpu decoding, set false for cpu decoding
# number of jobs for inference
# for gpu decoding, inference_nj=ngpu*njob; for cpu decoding, inference_nj=njob
-njob=5
+njob=4
infer_cmd=utils/run.pl
-told_max_iter=2
+told_max_iter=4
. utils/parse_options.sh || exit 1;
@@ -127,6 +142,22 @@
# 3. Prepare the Callhome portion of NIST SRE 2000.
local/make_callhome.sh ${callhome_root} ${datadir}/
+ # 4. split ref.rttm
+ for dset in callhome1 callhome2; do
+ rm -rf ${datadir}/${dset}/ref.rttm
+ for name in `awk '{print $1}' ${datadir}/${dset}/wav.scp`; do
+ grep ${name} ${datadir}/callhome/fullref.rttm >> ${datadir}/${dset}/ref.rttm;
+ done
+
+ # filter out records which don't have rttm labels.
+ awk '{print $2}' ${datadir}/${dset}/ref.rttm | sort | uniq > ${datadir}/${dset}/uttid
+ mv ${datadir}/${dset}/wav.scp ${datadir}/${dset}/wav.scp.bak
+ awk '{if (NR==FNR){a[$1]=1}else{if (a[$1]==1){print $0}}}' ${datadir}/${dset}/uttid ${datadir}/${dset}/wav.scp.bak > ${datadir}/${dset}/wav.scp
+ mkdir ${datadir}/${dset}/raw
+ mv ${datadir}/${dset}/{reco2num_spk,segments,spk2utt,utt2spk,uttid,wav.scp.bak} ${datadir}/${dset}/raw/
+ awk '{print $1,$1}' ${datadir}/${dset}/wav.scp > ${datadir}/${dset}/utt2spk
+ done
+
fi
if [ ${stage} -le 1 ] && [ ${stop_stage} -ge 1 ]; then
@@ -156,10 +187,10 @@
mkdir -p ${dumpdir}/${dset}/nonoverlap_0s
python -Wignore script/extract_nonoverlap_segments.py \
${datadir}/${dset}/wav.scp ${datadir}/${dset}/ref.rttm ${dumpdir}/${dset}/nonoverlap_0s \
- --min_dur 0 --max_spk_num 8 --sr ${sr} --no_pbar --nj ${nj}
+ --min_dur 0.1 --max_spk_num 8 --sr ${sr} --no_pbar --nj ${nj}
mkdir -p ${datadir}/${dset}/nonoverlap_0s
- find `pwd`/${dumpdir}/${dset}/nonoverlap_0s | sort | awk -F'[/.]' '{print $(NF-1),$0}' > ${datadir}/${dset}/nonoverlap_0s/wav.scp
+ find ${dumpdir}/${dset}/nonoverlap_0s/ -iname "*.wav" | sort | awk -F'[/.]' '{print $(NF-1),$0}' > ${datadir}/${dset}/nonoverlap_0s/wav.scp
awk -F'[/.]' '{print $(NF-1),$(NF-2)}' ${datadir}/${dset}/nonoverlap_0s/wav.scp > ${datadir}/${dset}/nonoverlap_0s/utt2spk
echo "Done."
done
@@ -279,11 +310,16 @@
if [ ${stage} -le 6 ] && [ ${stop_stage} -ge 6 ]; then
echo "Stage 6: Extract speaker embeddings."
- git lfs install
- git clone https://www.modelscope.cn/damo/speech_xvector_sv-en-us-callhome-8k-spk6135-pytorch.git
- mv speech_xvector_sv-en-us-callhome-8k-spk6135-pytorch ${expdir}/
-
sv_exp_dir=exp/speech_xvector_sv-en-us-callhome-8k-spk6135-pytorch
+
+ if [ ! -e ${sv_exp_dir} ]; then
+ echo "start to download sv models"
+ git lfs install
+ git clone https://www.modelscope.cn/damo/speech_xvector_sv-en-us-callhome-8k-spk6135-pytorch.git
+ mv speech_xvector_sv-en-us-callhome-8k-spk6135-pytorch ${expdir}/
+ echo "Done."
+ fi
+
sed "s/input_size: null/input_size: 80/g" ${sv_exp_dir}/sv.yaml > ${sv_exp_dir}/sv_fbank.yaml
for dset in swbd_sre/none_silence callhome1/nonoverlap_0s callhome2/nonoverlap_0s; do
key_file=${datadir}/${dset}/feats.scp
@@ -301,6 +337,7 @@
${infer_cmd} --gpu "${_ngpu}" --max-jobs-run "${_nj}" JOB=1:"${_nj}" "${_logdir}"/sv_inference.JOB.log \
python -m funasr.bin.sv_inference_launch \
--batch_size 1 \
+ --njob ${njob} \
--ngpu "${_ngpu}" \
--gpuid_list ${gpuid_list} \
--data_path_and_name_and_type "${key_file},speech,kaldi_ark" \
@@ -321,7 +358,7 @@
python -Wignore script/calc_real_meeting_frame_labels.py \
${datadir}/${dset} ${dumpdir}/${dset}/labels \
--n_spk 8 --frame_shift 0.01 --nj 16 --sr 8000
- find `pwd`/${dumpdir}/${dset}/labels -iname "*.lbl.mat" | awk -F'[/.]' '{print $(NF-2),$0}' | sort > ${datadir}/${dset}/labels.scp
+ find `pwd`/${dumpdir}/${dset}/labels/ -iname "*.lbl.mat" | awk -F'[/.]' '{print $(NF-2),$0}' | sort > ${datadir}/${dset}/labels.scp
done
fi
@@ -362,7 +399,7 @@
echo "Stage 8: start to dump for callhome1."
python -Wignore script/dump_meeting_chunks.py --dir ${data_dir} \
- --out ${dumpdir}/callhome1/dumped_files/data --n_spk 16 --no_pbar --sr 8000 --mode test \
+ --out ${dumpdir}/callhome1/dumped_files/data --n_spk 16 --no_pbar --sr 8000 --mode train \
--chunk_size 1600 --chunk_shift 400 --add_mid_to_speaker true
mkdir -p ${datadir}/callhome1/dumped_files
@@ -507,8 +544,8 @@
done
fi
-# Scoring for pretrained model, you may get a DER like 13.73 16.25
-# 13.73: with oracle VAD, 16.25: with only SOND outputs, aka, system VAD.
+# Scoring for pretrained model, you may get a DER like 13.29 16.54
+# 13.29: with oracle VAD, 16.54: with only SOND outputs, aka, system VAD.
if [ ${stage} -le 12 ] && [ ${stop_stage} -ge 12 ]; then
echo "stage 12: Scoring phase-1 models"
if [ ! -e dscore ]; then
@@ -588,7 +625,7 @@
--valid_data_path_and_name_and_type ${datadir}/${valid_set}/dumped_files/profile.scp,profile,kaldi_ark \
--valid_data_path_and_name_and_type ${datadir}/${valid_set}/dumped_files/label.scp,binary_labels,kaldi_ark \
--valid_shape_file ${expdir}/${valid_set}_states/speech_shape \
- --init_param exp/${model_dir}/valid.der.ave_5best.pth \
+ --init_param exp/${model_dir}/valid.der.ave_5best.pb \
--unused_parameters true \
${init_opt} \
${freeze_opt} \
@@ -654,8 +691,8 @@
done
fi
-# Scoring for pretrained model, you may get a DER like 11.25 15.30
-# 11.25: with oracle VAD, 15.30: with only SOND outputs, aka, system VAD.
+# Scoring for pretrained model, you may get a DER like 11.54 15.41
+# 11.54: with oracle VAD, 15.41: with only SOND outputs, aka, system VAD.
if [ ${stage} -le 15 ] && [ ${stop_stage} -ge 15 ]; then
echo "stage 15: Scoring phase-2 models"
if [ ! -e dscore ]; then
@@ -733,7 +770,7 @@
--valid_data_path_and_name_and_type ${datadir}/${valid_set}/dumped_files/profile.scp,profile,kaldi_ark \
--valid_data_path_and_name_and_type ${datadir}/${valid_set}/dumped_files/label.scp,binary_labels,kaldi_ark \
--valid_shape_file ${expdir}/${valid_set}_states/speech_shape \
- --init_param exp/${model_dir}_phase2/valid.forward_steps.ave_5best.pth \
+ --init_param exp/${model_dir}_phase2/valid.forward_steps.ave_5best.pb \
--unused_parameters true \
${init_opt} \
${freeze_opt} \
diff --git a/funasr/runtime/docs/SDK_advanced_guide_online_zh.md b/funasr/runtime/docs/SDK_advanced_guide_online_zh.md
index 1e1d68c..fd8a257 100644
--- a/funasr/runtime/docs/SDK_advanced_guide_online_zh.md
+++ b/funasr/runtime/docs/SDK_advanced_guide_online_zh.md
@@ -28,6 +28,8 @@
--model-dir damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-onnx \
--online-model-dir damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-online-onnx \
--punc-dir damo/punc_ct-transformer_zh-cn-common-vad_realtime-vocab272727-onnx > log.out 2>&1 &
+
+# 濡傛灉鎯冲叧闂璼sl锛屽鍔犲弬鏁帮細--certfile "" --keyfile ""
```
鏈嶅姟绔缁嗗弬鏁颁粙缁嶅彲鍙傝�僛鏈嶅姟绔弬鏁颁粙缁峕(#鏈嶅姟绔弬鏁颁粙缁�)
### 瀹㈡埛绔祴璇曚笌浣跨敤
diff --git a/funasr/runtime/run_server.sh b/funasr/runtime/run_server.sh
index 2ab6491..5754447 100644
--- a/funasr/runtime/run_server.sh
+++ b/funasr/runtime/run_server.sh
@@ -12,6 +12,18 @@
. ../../egs/aishell/transformer/utils/parse_options.sh || exit 1;
cd /workspace/FunASR/funasr/runtime/websocket/build/bin
+if [ -z "$certfile" ] || [ "$certfile" -eq 0 ]; then
+./funasr-wss-server \
+ --download-model-dir ${download_model_dir} \
+ --model-dir ${model_dir} \
+ --vad-dir ${vad_dir} \
+ --punc-dir ${punc_dir} \
+ --decoder-thread-num ${decoder_thread_num} \
+ --io-thread-num ${io_thread_num} \
+ --port ${port} \
+ --certfile "" \
+ --keyfile ""
+else
./funasr-wss-server \
--download-model-dir ${download_model_dir} \
--model-dir ${model_dir} \
@@ -22,4 +34,4 @@
--port ${port} \
--certfile ${certfile} \
--keyfile ${keyfile}
-
+fi
diff --git a/funasr/runtime/run_server_2pass.sh b/funasr/runtime/run_server_2pass.sh
index 12c19be..2e3977c 100644
--- a/funasr/runtime/run_server_2pass.sh
+++ b/funasr/runtime/run_server_2pass.sh
@@ -13,6 +13,19 @@
. ../../egs/aishell/transformer/utils/parse_options.sh || exit 1;
cd /workspace/FunASR/funasr/runtime/websocket/build/bin
+if [ -z "$certfile" ] || [ "$certfile" -eq 0 ]; then
+./funasr-wss-server-2pass \
+ --download-model-dir ${download_model_dir} \
+ --model-dir ${model_dir} \
+ --online-model-dir ${online_model_dir} \
+ --vad-dir ${vad_dir} \
+ --punc-dir ${punc_dir} \
+ --decoder-thread-num ${decoder_thread_num} \
+ --io-thread-num ${io_thread_num} \
+ --port ${port} \
+ --certfile "" \
+ --keyfile ""
+else
./funasr-wss-server-2pass \
--download-model-dir ${download_model_dir} \
--model-dir ${model_dir} \
@@ -24,4 +37,4 @@
--port ${port} \
--certfile ${certfile} \
--keyfile ${keyfile}
-
+fi
diff --git a/funasr/runtime/wss-client/FunASRClient_CShape.sln b/funasr/runtime/wss-client/FunASRClient_CShape.sln
new file mode 100644
index 0000000..74eb6fa
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRClient_CShape.sln
@@ -0,0 +1,31 @@
+锘�
+Microsoft Visual Studio Solution File, Format Version 12.00
+# Visual Studio Version 17
+VisualStudioVersion = 17.6.33829.357
+MinimumVisualStudioVersion = 10.0.40219.1
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunASRWSClient_Offline", "FunASRWSClient_Offline\FunASRWSClient_Offline.csproj", "{E0986CC4-D443-44E2-96E8-F6E4B691CA57}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FunASRWSClient_Online", "FunASRWSClient_Online\FunASRWSClient_Online.csproj", "{11E80B4F-A838-4DFB-A0C8-9BAE6726BAC0}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {E0986CC4-D443-44E2-96E8-F6E4B691CA57}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {E0986CC4-D443-44E2-96E8-F6E4B691CA57}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {E0986CC4-D443-44E2-96E8-F6E4B691CA57}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {E0986CC4-D443-44E2-96E8-F6E4B691CA57}.Release|Any CPU.Build.0 = Release|Any CPU
+ {11E80B4F-A838-4DFB-A0C8-9BAE6726BAC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {11E80B4F-A838-4DFB-A0C8-9BAE6726BAC0}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {11E80B4F-A838-4DFB-A0C8-9BAE6726BAC0}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {11E80B4F-A838-4DFB-A0C8-9BAE6726BAC0}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(SolutionProperties) = preSolution
+ HideSolutionNode = FALSE
+ EndGlobalSection
+ GlobalSection(ExtensibilityGlobals) = postSolution
+ SolutionGuid = {E8483245-31D3-4C42-AAF9-B3195EAB97C2}
+ EndGlobalSection
+EndGlobal
diff --git a/funasr/runtime/wss-client/FunASRClient_CShape.suo b/funasr/runtime/wss-client/FunASRClient_CShape.suo
new file mode 100644
index 0000000..a2bf849
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRClient_CShape.suo
Binary files differ
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Offline/FunASRWSClient_Offline.csproj b/funasr/runtime/wss-client/FunASRWSClient_Offline/FunASRWSClient_Offline.csproj
new file mode 100644
index 0000000..0604316
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRWSClient_Offline/FunASRWSClient_Offline.csproj
@@ -0,0 +1,14 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <OutputType>Exe</OutputType>
+ <TargetFramework>net6.0</TargetFramework>
+ <ImplicitUsings>enable</ImplicitUsings>
+ <Nullable>enable</Nullable>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="Websocket.Client" Version="4.6.1" />
+ </ItemGroup>
+
+</Project>
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Offline/Program.cs b/funasr/runtime/wss-client/FunASRWSClient_Offline/Program.cs
new file mode 100644
index 0000000..a0039e7
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRWSClient_Offline/Program.cs
@@ -0,0 +1,85 @@
+锘縰sing System.Collections.Specialized;
+using WebSocketSpace;
+
+namespace FunASRWSClient_Offline
+{
+ /// <summary>
+ /// /涓荤▼搴忓叆鍙�
+ /// </summary>
+ public class Program
+ {
+ private static void Main()
+ {
+ WSClient_Offline m_funasrclient = new WSClient_Offline();
+ m_funasrclient.FunASR_Main();
+ }
+ }
+
+ public class WSClient_Offline
+ {
+ public static string host = "0.0.0.0";
+ public static string port = "10095";
+ private static CWebSocketClient m_websocketclient = new CWebSocketClient();
+ [STAThread]
+ public async void FunASR_Main()
+ {
+ loadconfig();
+ //鍒濆鍖栭�氫俊杩炴帴
+ string errorStatus = string.Empty;
+ string commstatus = ClientConnTest();
+ if (commstatus != "閫氫俊杩炴帴鎴愬姛")
+ errorStatus = commstatus;
+ //绋嬪簭鍒濆鐩戞祴寮傚父--鎶ラ敊銆侀��鍑�
+ if (errorStatus != string.Empty)
+ {
+ //鎶ラ敊鏂瑰紡寰呭姞
+ Environment.Exit(0);
+ }
+
+ //寰幆杈撳叆鎺ㄧ悊鏂囦欢
+ while (true)
+ {
+ Console.WriteLine("璇疯緭鍏ヨ浆褰曟枃浠惰矾寰勶細");
+ string filepath = Console.ReadLine();
+ if (filepath != string.Empty && filepath != null)
+ {
+ await m_websocketclient.ClientSendFileFunc(filepath);
+ }
+ }
+ }
+ private void loadconfig()
+ {
+ string filePath = "config.ini";
+ NameValueCollection settings = new NameValueCollection();
+ using (StreamReader reader = new StreamReader(filePath))
+ {
+ string line;
+ while ((line = reader.ReadLine()) != null)
+ {
+ // 蹇界暐绌鸿鍜屾敞閲�
+ if (string.IsNullOrEmpty(line) || line.StartsWith(";") || line.StartsWith("#"))
+ continue;
+ // 瑙f瀽閿�煎
+ int equalsIndex = line.IndexOf('=');
+ if (equalsIndex > 0)
+ {
+ string key = line.Substring(0, equalsIndex).Trim();
+ string value = line.Substring(equalsIndex + 1).Trim();
+ if (key == "host")
+ host = value;
+ else if (key == "port")
+ port = value;
+ }
+ }
+ }
+ }
+ private static string ClientConnTest()
+ {
+ //WebSocket杩炴帴鐘舵�佺洃娴�
+ Task<string> websocketstatus = m_websocketclient.ClientConnTest();
+ if (websocketstatus != null && websocketstatus.Result.IndexOf("鎴愬姛") == -1)
+ return websocketstatus.Result;
+ return "閫氫俊杩炴帴鎴愬姛";
+ }
+ }
+}
\ No newline at end of file
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Offline/README.md b/funasr/runtime/wss-client/FunASRWSClient_Offline/README.md
new file mode 100644
index 0000000..3563560
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRWSClient_Offline/README.md
@@ -0,0 +1,9 @@
+# cshape-client-offline
+
+杩欐槸涓�涓熀浜嶧unASR-Websocket鏈嶅姟鍣ㄧ殑CShape瀹㈡埛绔紝鐢ㄤ簬杞綍鏈湴闊抽鏂囦欢銆�
+
+灏嗛厤缃枃浠舵斁鍦ㄤ笌绋嬪簭鐩稿悓鐩綍涓嬬殑config鏂囦欢澶逛腑锛屽苟鍦╟onfig.ini涓厤缃湇鍔″櫒ip鍦板潃鍜岀鍙e彿銆�
+
+閰嶇疆濂芥湇鍔$ip鍜岀鍙e彿锛屽湪vs涓墦寮�闇�娣诲姞Websocket.Client鐨凬uget绋嬪簭鍖呭悗锛屽彲鐩存帴杩涜娴嬭瘯锛屾寜鐓ф帶鍒跺彴鎻愮ず鎿嶄綔鍗冲彲銆�
+
+娉細鏈鎴风鏆傛敮鎸亀av鏂囦欢锛屽湪win11涓嬪畬鎴愭祴璇曪紝缂栬瘧鐜VS2022銆�
\ No newline at end of file
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Offline/WebScoketClient.cs b/funasr/runtime/wss-client/FunASRWSClient_Offline/WebScoketClient.cs
new file mode 100644
index 0000000..9208524
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRWSClient_Offline/WebScoketClient.cs
@@ -0,0 +1,120 @@
+锘縰sing Websocket.Client;
+using System.Text.Json;
+using System.Reactive.Linq;
+using FunASRWSClient_Offline;
+
+namespace WebSocketSpace
+{
+ internal class CWebSocketClient
+ {
+ private static readonly Uri serverUri = new Uri($"ws://{WSClient_Offline.host}:{WSClient_Offline.port}"); // 浣犺杩炴帴鐨刉ebSocket鏈嶅姟鍣ㄥ湴鍧�
+ private static WebsocketClient client = new WebsocketClient(serverUri);
+ public async Task<string> ClientConnTest()
+ {
+ string commstatus = "WebSocket閫氫俊杩炴帴澶辫触";
+ try
+ {
+ client.Name = "funasr";
+ client.ReconnectTimeout = null;
+ client.ReconnectionHappened.Subscribe(info =>
+ Console.WriteLine($"Reconnection happened, type: {info.Type}, url: {client.Url}"));
+ client.DisconnectionHappened.Subscribe(info =>
+ Console.WriteLine($"Disconnection happened, type: {info.Type}"));
+
+ client
+ .MessageReceived
+ .Where(msg => msg.Text != null)
+ .Subscribe(msg =>
+ {
+ recmessage(msg.Text);
+ });
+
+ await client.Start();
+
+ if (client.IsRunning)
+ commstatus = "WebSocket閫氫俊杩炴帴鎴愬姛";
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine(ex.ToString());
+ client.Dispose();
+ }
+
+ return commstatus;
+ }
+
+ public async Task<Task> ClientSendFileFunc(string file_name)//鏂囦欢杞綍
+ {
+ try
+ {
+ if (client.IsRunning)
+ {
+ var exitEvent = new ManualResetEvent(false);
+ string path = Path.GetFileName(file_name);
+ string firstbuff = string.Format("{{\"mode\": \"offline\", \"wav_name\": \"{0}\", \"is_speaking\": true}}", Path.GetFileName(file_name));
+ client.Send(firstbuff);
+ showWAVForm(client, file_name);
+ }
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine(ex.ToString());
+ }
+ return Task.CompletedTask;
+ }
+
+ public void recmessage(string message)
+ {
+ if (message != null)
+ {
+ try
+ {
+ JsonDocument jsonDoc = JsonDocument.Parse(message);
+ JsonElement root = jsonDoc.RootElement;
+ string mode = root.GetProperty("mode").GetString();
+ string text = root.GetProperty("text").GetString();
+ string name = root.GetProperty("wav_name").GetString();
+ if(name == "asr_stream")
+ Console.WriteLine($"瀹炴椂璇嗗埆鍐呭: {text}");
+ else
+ Console.WriteLine($"鏂囦欢鍚嶇О:{name} 鏂囦欢杞綍鍐呭: {text}");
+ }
+ catch (JsonException ex)
+ {
+ Console.WriteLine("JSON 瑙f瀽閿欒: " + ex.Message);
+ }
+ }
+ }
+
+ private void showWAVForm(WebsocketClient client, string file_name)
+ {
+ byte[] getbyte = FileToByte(file_name).Skip(44).ToArray();
+
+ for (int i = 0; i < getbyte.Length; i += 1024000)
+ {
+ byte[] send = getbyte.Skip(i).Take(1024000).ToArray();
+ client.Send(send);
+ Thread.Sleep(5);
+ }
+ Thread.Sleep(10);
+ client.Send("{\"is_speaking\": false}");
+ }
+
+ public byte[] FileToByte(string fileUrl)
+ {
+ try
+ {
+ using (FileStream fs = new FileStream(fileUrl, FileMode.Open, FileAccess.Read))
+ {
+ byte[] byteArray = new byte[fs.Length];
+ fs.Read(byteArray, 0, byteArray.Length);
+ return byteArray;
+ }
+ }
+ catch
+ {
+ return null;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Online/FunASRWSClient_Online.csproj b/funasr/runtime/wss-client/FunASRWSClient_Online/FunASRWSClient_Online.csproj
new file mode 100644
index 0000000..75fc029
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRWSClient_Online/FunASRWSClient_Online.csproj
@@ -0,0 +1,15 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+ <PropertyGroup>
+ <OutputType>Exe</OutputType>
+ <TargetFramework>net6.0</TargetFramework>
+ <ImplicitUsings>enable</ImplicitUsings>
+ <Nullable>enable</Nullable>
+ </PropertyGroup>
+
+ <ItemGroup>
+ <PackageReference Include="NAudio" Version="2.1.0" />
+ <PackageReference Include="Websocket.Client" Version="4.6.1" />
+ </ItemGroup>
+
+</Project>
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Online/Program.cs b/funasr/runtime/wss-client/FunASRWSClient_Online/Program.cs
new file mode 100644
index 0000000..ec0c5e4
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRWSClient_Online/Program.cs
@@ -0,0 +1,255 @@
+锘縰sing AliFsmnVadSharp;
+using NAudio.Wave;
+using System.Collections.Concurrent;
+using WebSocketSpace;
+using NAudio.CoreAudioApi;
+using System.IO;
+using System.Collections.Specialized;
+
+namespace FunASRWSClient_Online
+{
+ /// <summary>
+ /// /涓荤▼搴忓叆鍙�
+ /// </summary>
+ public class Program
+ {
+ private static void Main()
+ {
+ WSClient_Online m_funasrclient = new WSClient_Online();
+ m_funasrclient.FunASR_Main();
+ }
+ }
+ /// <summary>
+ /// /涓荤嚎绋嬪叆鍙o紝鍒濆鍖栧悗璇诲彇鏁版嵁
+ /// </summary>
+ public class WSClient_Online
+ {
+ /// <summary>
+ /// FunASR瀹㈡埛绔蒋浠惰繍琛岀姸鎬�
+ /// </summary>
+ ///
+ public static string host = "0.0.0.0";
+ public static string port = "10095";
+ public static string onlineasrmode = string.Empty;
+ private static WaveCollect m_wavecollect = new WaveCollect();
+ private static CWebSocketClient m_websocketclient = new CWebSocketClient();
+ public static readonly ConcurrentQueue<byte[]> ActiveAudioSet = new ConcurrentQueue<byte[]>();
+ public static readonly ConcurrentQueue<string> AudioFileQueue = new ConcurrentQueue<string>();
+ [STAThread]
+ public void FunASR_Main()
+ {
+ loadconfig();
+ //楹﹀厠椋庣姸鎬佺洃娴�
+ string errorStatus = string.Empty;
+ if (GetCurrentMicVolume() == -2)
+ errorStatus = "娉ㄦ剰锛氶害鍏嬮琚缃负闈欓煶锛�";
+ else if (GetCurrentMicVolume() == -1)
+ errorStatus = "娉ㄦ剰锛氶害鍏嬮鏈繛鎺ワ紒";
+ else if (GetCurrentMicVolume() == 0)
+ errorStatus = "娉ㄦ剰锛氶害鍏嬮澹伴煶璁剧疆涓�0锛�";
+
+ //鍒濆鍖栭�氫俊杩炴帴
+ string commstatus = ClientConnTest();
+ if (commstatus != "閫氫俊杩炴帴鎴愬姛")
+ errorStatus = commstatus;
+ //绋嬪簭鍒濆鐩戞祴寮傚父--鎶ラ敊銆侀��鍑�
+ if (errorStatus != string.Empty)
+ {
+ Environment.Exit(0);//鎶ラ敊鏂瑰紡寰呭姞
+ }
+
+ //鍚姩瀹㈡埛绔悜鏈嶅姟绔彂閫侀煶棰戞暟鎹嚎绋�
+ Thread SendAudioThread = new Thread(SendAudioToSeverAsync);
+ SendAudioThread.Start();
+
+ //鍚姩闊抽鏂囦欢杞綍绾跨▼
+ Thread AudioFileThread = new Thread(SendAudioFileToSeverAsync);
+ AudioFileThread.Start();
+
+ while (true)
+ {
+ Console.WriteLine("璇烽�夋嫨璇煶璇嗗埆鏂瑰紡锛�1.绂荤嚎鏂囦欢杞啓锛�2.瀹炴椂璇煶璇嗗埆");
+ string str = Console.ReadLine();
+ if (str != string.Empty)
+ {
+ if (str == "1")//绂荤嚎鏂囦欢杞啓
+ {
+ onlineasrmode = "offline";
+ Console.WriteLine("璇疯緭鍏ヨ浆褰曟枃浠惰矾寰�");
+ str = Console.ReadLine();
+ if (!string.IsNullOrEmpty(str))
+ AudioFileQueue.Enqueue(str);
+ }
+ else if (str == "2")//瀹炴椂璇煶璇嗗埆
+ {
+ Console.WriteLine("璇疯緭鍏ュ疄鏃惰闊宠瘑鍒ā寮忥細1.online锛�2.2pass");
+ str = Console.ReadLine();
+ OnlineASR(str);
+ }
+ }
+ }
+ }
+ private void loadconfig()
+ {
+ string filePath = "config.ini";
+ NameValueCollection settings = new NameValueCollection();
+ using (StreamReader reader = new StreamReader(filePath))
+ {
+ string line;
+ while ((line = reader.ReadLine()) != null)
+ {
+ // 蹇界暐绌鸿鍜屾敞閲�
+ if (string.IsNullOrEmpty(line) || line.StartsWith(";") || line.StartsWith("#"))
+ continue;
+ // 瑙f瀽閿�煎
+ int equalsIndex = line.IndexOf('=');
+ if (equalsIndex > 0)
+ {
+ string key = line.Substring(0, equalsIndex).Trim();
+ string value = line.Substring(equalsIndex + 1).Trim();
+ if (key == "host")
+ host = value;
+ else if (key == "port")
+ port = value;
+ }
+ }
+ }
+ }
+ private void OnlineASR(string str)
+ {
+ if (!string.IsNullOrEmpty(str))
+ {
+ if (str == "1")//瀹炴椂璇煶璇嗗埆
+ onlineasrmode = "online";
+ else if (str == "2")//瀹炴椂璇煶璇嗗埆-鍔ㄦ�佷慨姝�
+ onlineasrmode = "2pass";
+ }
+ //寮�濮嬪綍鍒跺0闊炽�佸彂閫佽瘑鍒�
+ if (onlineasrmode != string.Empty)
+ {
+ m_wavecollect.StartRec();
+ m_websocketclient.ClientFirstConnOnline(onlineasrmode);
+ try
+ {
+ while (true)
+ {
+ if (!WaveCollect.voicebuff.IsEmpty)
+ {
+ byte[] buff;
+ int buffcnt = WaveCollect.voicebuff.Count;
+ WaveCollect.voicebuff.TryDequeue(out buff);
+ if (buff != null)
+ ActiveAudioSet.Enqueue(buff);
+ }
+ else
+ {
+ if (Console.KeyAvailable)
+ {
+ var key = Console.ReadKey(true);
+
+ // 妫�娴嬪埌鎸変笅Ctrl+C
+ if ((key.Modifiers & ConsoleModifiers.Control) != 0 && key.Key == ConsoleKey.C)
+ {
+ // 鎵ц鐩稿簲鐨勬搷浣�
+ Console.WriteLine("Ctrl+C Pressed!");
+ // 閫�鍑哄惊鐜垨鎵ц鍏朵粬鎿嶄綔
+ break;
+ }
+ }
+ else
+ {
+ Thread.Sleep(10);
+ }
+ }
+ }
+ }
+ catch
+ {
+ Console.WriteLine("瀹炴椂璇嗗埆鍑虹幇寮傚父锛�");
+ }
+ finally
+ {
+ m_wavecollect.StopRec();
+ m_websocketclient.ClientLastConnOnline();
+ }
+ }
+ }
+
+ private string ClientConnTest()
+ {
+ //WebSocket杩炴帴鐘舵�佺洃娴�
+ Task<string> websocketstatus = m_websocketclient.ClientConnTest();
+ if (websocketstatus != null && websocketstatus.Result.IndexOf("鎴愬姛") == -1)
+ return websocketstatus.Result;
+ return "閫氫俊杩炴帴鎴愬姛";
+ }
+ private void SendAudioFileToSeverAsync()
+ {
+ while (true)
+ {
+ Thread.Sleep(1000);
+ if (AudioFileQueue.Count > 0)
+ {
+ string filepath = string.Empty;
+ AudioFileQueue.TryDequeue(out filepath);
+ if (filepath != string.Empty && filepath != null)
+ {
+ m_websocketclient.ClientSendFileFunc(filepath);
+ }
+ }
+ else
+ {
+ Thread.Sleep(100);
+ }
+ }
+ }
+ private void SendAudioToSeverAsync()
+ {
+ while (true)
+ {
+ if (ActiveAudioSet.Count > 0)
+ {
+ byte[] audio;
+ ActiveAudioSet.TryDequeue(out audio);
+ if (audio == null)
+ continue;
+
+ byte[] mArray = new byte[audio.Length];
+ Array.Copy(audio, 0, mArray, 0, audio.Length);
+ if (mArray != null)
+ m_websocketclient.ClientSendAudioFunc(mArray);
+ }
+ else
+ {
+ Thread.Sleep(10);
+ }
+ }
+ }
+
+ private void SaveAsWav(byte[] pcmData, string fileName, int sampleRate, int bitsPerSample, int channels)
+ {
+ using (var writer = new WaveFileWriter(fileName, new WaveFormat(sampleRate, bitsPerSample, channels)))
+ {
+ writer.Write(pcmData, 0, pcmData.Length);
+ }
+ }
+
+ private int GetCurrentMicVolume() //鑾峰彇楹﹀厠椋庤缃�
+ {
+ int volume = -1;
+ var enumerator = new MMDeviceEnumerator();
+
+ //鑾峰彇闊抽杈撳叆璁惧
+ IEnumerable<MMDevice> captureDevices = enumerator.EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active).ToArray();
+ if (captureDevices.Count() > 0)
+ {
+ MMDevice mMDevice = captureDevices.ToList()[0];
+ if (mMDevice.AudioEndpointVolume.Mute)
+ return -2;
+ volume = (int)(mMDevice.AudioEndpointVolume.MasterVolumeLevelScalar * 100);
+
+ }
+ return volume;
+ }
+ }
+}
\ No newline at end of file
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Online/README.md b/funasr/runtime/wss-client/FunASRWSClient_Online/README.md
new file mode 100644
index 0000000..c828c93
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRWSClient_Online/README.md
@@ -0,0 +1,9 @@
+# cshape-client-online
+
+杩欐槸涓�涓熀浜嶧unASR-Websocket鏈嶅姟鍣ㄧ殑CShape瀹㈡埛绔紝鐢ㄤ簬瀹炴椂璇煶璇嗗埆鍜岃浆褰曟湰鍦伴煶棰戞枃浠躲��
+
+灏嗛厤缃枃浠舵斁鍦ㄤ笌绋嬪簭鐩稿悓鐩綍涓嬬殑config鏂囦欢澶逛腑锛屽苟鍦╟onfig.ini涓厤缃湇鍔″櫒ip鍦板潃鍜岀鍙e彿銆�
+
+閰嶇疆濂芥湇鍔$ip鍜岀鍙e彿锛屽湪vs涓墦寮�闇�娣诲姞NAudio鍜學ebsocket.Client鐨凬uget绋嬪簭鍖呭悗锛屽彲鐩存帴杩涜娴嬭瘯锛屾寜鐓ф帶鍒跺彴鎻愮ず鎿嶄綔鍗冲彲銆�
+
+娉細瀹炴椂璇煶璇嗗埆浣跨敤online鎴�2pass锛岃浆褰曟枃浠堕粯璁や娇鐢╫ffline锛屽湪win11涓嬪畬鎴愭祴璇曪紝缂栬瘧鐜VS2022銆�
\ No newline at end of file
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Online/WaveCollect.cs b/funasr/runtime/wss-client/FunASRWSClient_Online/WaveCollect.cs
new file mode 100644
index 0000000..f927b5e
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRWSClient_Online/WaveCollect.cs
@@ -0,0 +1,106 @@
+锘縰sing System.Collections.Concurrent;
+using NAudio.Wave;
+using NAudio.CoreAudioApi;
+
+namespace AliFsmnVadSharp
+{
+ class WaveCollect
+ {
+ private string fileName = string.Empty;
+ private WaveInEvent? waveSource = null;
+ private WaveFileWriter? waveFile = null;
+ public static int wave_buffer_milliseconds = 600;
+ public static int wave_buffer_collectbits = 16;
+ public static int wave_buffer_collectchannels = 1;
+ public static int wave_buffer_collectfrequency = 16000;
+ public static readonly ConcurrentQueue<byte[]> voicebuff = new ConcurrentQueue<byte[]>();
+
+ public void StartRec()
+ {
+ // 鑾峰彇楹﹀厠椋庤澶�
+ var captureDevices = new MMDeviceEnumerator().EnumerateAudioEndPoints(DataFlow.Capture, DeviceState.Active);
+ foreach (var device in captureDevices)
+ {
+ Console.WriteLine("Device Name: " + device.FriendlyName);
+ using (var capture = new WasapiLoopbackCapture(device))
+ {
+ // 鑾峰彇鏀寔鐨勯噰鏍风巼鍒楄〃
+ Console.WriteLine("Device Channels:" + capture.WaveFormat.Channels);
+ Console.WriteLine("Device SampleRate:" + capture.WaveFormat.SampleRate);
+ Console.WriteLine("Device BitsPerSample:" + capture.WaveFormat.BitsPerSample);
+ }
+ }
+ //娓呯┖缂撳瓨鏁版嵁
+ int buffnum = voicebuff.Count;
+ for (int i = 0; i < buffnum; i++)
+ voicebuff.TryDequeue(out byte[] buff);
+
+ waveSource = new WaveInEvent();
+ waveSource.BufferMilliseconds = wave_buffer_milliseconds;
+ waveSource.WaveFormat = new WaveFormat(wave_buffer_collectfrequency, wave_buffer_collectbits, wave_buffer_collectchannels); // 16bit,16KHz,Mono鐨勫綍闊虫牸寮�
+ waveSource.DataAvailable += new EventHandler<WaveInEventArgs>(waveSource_DataAvailable);
+ SetFileName(AppDomain.CurrentDomain.BaseDirectory + "tmp.wav");
+ waveFile = new WaveFileWriter(fileName, waveSource.WaveFormat);
+ waveSource.StartRecording();
+ }
+
+ public void StopRec()
+ {
+ if (waveSource != null)
+ {
+ waveSource.StopRecording();
+ if (waveSource != null)
+ {
+ waveSource.Dispose();
+ waveSource = null;
+ }
+ if (waveFile != null)
+ {
+ waveFile.Dispose();
+ waveFile = null;
+ }
+ }
+ }
+
+ public void SetFileName(string fileName)
+ {
+ this.fileName = fileName;
+ }
+
+ private void waveSource_DataAvailable(object sender, WaveInEventArgs e)
+ {
+ if (waveFile != null)
+ {
+ if (e.Buffer != null && e.BytesRecorded > 0)
+ {
+ voicebuff.Enqueue(e.Buffer);
+ //waveFile.Write(e.Buffer, 0, e.BytesRecorded);
+ waveFile.Flush();
+ }
+
+ }
+ }
+
+ public static byte[] Wavedata_Dequeue()
+ {
+ byte[] datas;
+ voicebuff.TryDequeue(out datas);
+ return datas;
+ }
+
+ private void waveSource_RecordingStopped(object sender, StoppedEventArgs e)
+ {
+ if (waveSource != null)
+ {
+ waveSource.Dispose();
+ waveSource = null;
+ }
+
+ if (waveFile != null)
+ {
+ waveFile.Dispose();
+ waveFile = null;
+ }
+ }
+ }
+}
diff --git a/funasr/runtime/wss-client/FunASRWSClient_Online/WebScoketClient.cs b/funasr/runtime/wss-client/FunASRWSClient_Online/WebScoketClient.cs
new file mode 100644
index 0000000..69cbe4d
--- /dev/null
+++ b/funasr/runtime/wss-client/FunASRWSClient_Online/WebScoketClient.cs
@@ -0,0 +1,219 @@
+锘縰sing System.Net.WebSockets;
+using Websocket.Client;
+using System.Text.Json;
+using NAudio.Wave;
+using AliFsmnVadSharp;
+using System.Reactive.Linq;
+using FunASRWSClient_Online;
+
+namespace WebSocketSpace
+{
+ internal class CWebSocketClient
+ {
+ private static int chunk_interval = 10;
+ private static int[] chunk_size = new int[] { 5, 10, 5 };
+ private static readonly Uri serverUri = new Uri($"ws://{WSClient_Online.host}:{WSClient_Online.port}"); // 浣犺杩炴帴鐨刉ebSocket鏈嶅姟鍣ㄥ湴鍧�
+ private static WebsocketClient client = new WebsocketClient(serverUri);
+ public async Task<string> ClientConnTest()
+ {
+ string commstatus = "WebSocket閫氫俊杩炴帴澶辫触";
+ try
+ {
+ client.Name = "funasr";
+ client.ReconnectTimeout = null;
+ client.ReconnectionHappened.Subscribe(info =>
+ Console.WriteLine($"Reconnection happened, type: {info.Type}, url: {client.Url}"));
+ client.DisconnectionHappened.Subscribe(info =>
+ Console.WriteLine($"Disconnection happened, type: {info.Type}"));
+
+ client
+ .MessageReceived
+ .Where(msg => msg.Text != null)
+ .Subscribe(msg =>
+ {
+ rec_message(msg.Text, client);
+ });
+
+ await client.Start();
+
+ if (client.IsRunning)
+ commstatus = "WebSocket閫氫俊杩炴帴鎴愬姛";
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine(ex.ToString());
+ client.Dispose();
+ }
+
+ return commstatus;
+ }
+
+ public bool ClientFirstConnOnline(string asrmode)
+ {
+ if (client.IsRunning)
+ {
+ string firstbuff = string.Format("{{\"mode\": \"{0}\", \"chunk_size\": [{1},{2},{3}], \"chunk_interval\": {4}, \"wav_name\": \"microphone\", \"is_speaking\": true}}"
+ , asrmode, chunk_size[0], chunk_size[1], chunk_size[2], chunk_interval);
+ Task.Run(() => client.Send(firstbuff));
+ }
+ else
+ {
+ client.Reconnect();
+ return false;
+ }
+
+ return true;
+ }
+ public bool ClientSendAudioFunc(byte[] buff) //瀹炴椂璇嗗埆
+ {
+ if (client.IsRunning)
+ {
+ ////鍙戦�侀煶棰戞暟鎹�
+ int CHUNK = WaveCollect.wave_buffer_collectfrequency / 1000 * 60 * chunk_size[1] / chunk_interval;
+ for (int i = 0; i < buff.Length; i += CHUNK)
+ {
+ byte[] send = buff.Skip(i).Take(CHUNK).ToArray();
+ Task.Run(() => client.Send(send));
+ Thread.Sleep(1);
+ }
+ }
+ else
+ {
+ client.Reconnect();
+ return false;
+ }
+
+ return true;
+ }
+ public void ClientLastConnOnline()
+ {
+ Task.Run(() => client.Send("{\"is_speaking\": false}"));
+ }
+
+ public int ClientSendFileFunc(string file_name)//鏂囦欢杞綍 0:鍙戦�佹垚鍔� ret -1:鏂囦欢绫诲瀷涓嶆敮鎸� -2:閫氫俊鏂紑
+ {
+ string fileExtension = Path.GetExtension(file_name);
+ fileExtension = fileExtension.Replace(".", "");
+ if (!(fileExtension == "mp3" || fileExtension == "mp4" || fileExtension == "wav" || fileExtension == "pcm"))
+ return -1;
+
+ if (client.IsRunning)
+ {
+ if (fileExtension == "wav" || fileExtension == "pcm")
+ {
+ string firstbuff = string.Format("{{\"mode\": \"office\", \"chunk_size\": [{0},{1},{2}], \"chunk_interval\": {3}, \"wav_name\": \"{4}\", \"is_speaking\": true, \"wav_format\":\"pcm\"}}"
+ , chunk_size[0], chunk_size[1], chunk_size[2], chunk_interval, Path.GetFileName(file_name));
+ Task.Run(() => client.Send(firstbuff));
+ if (fileExtension == "wav")
+ showWAVForm(file_name);
+ else if (fileExtension == "pcm")
+ showWAVForm_All(file_name);
+ }
+ else if (fileExtension == "mp3" || fileExtension == "mp4")
+ {
+ string firstbuff = string.Format("{{\"mode\": \"offline\", \"chunk_size\": \"{0},{1},{2}\", \"chunk_interval\": {3}, \"wav_name\": \"{4}\", \"is_speaking\": true, \"wav_format\":\"{5}\"}}"
+ , chunk_size[0], chunk_size[1], chunk_size[2], chunk_interval, Path.GetFileName(file_name), fileExtension);
+ Task.Run(() => client.Send(firstbuff));
+ showWAVForm_All(file_name);
+ }
+ }
+ else
+ {
+ client.Reconnect();
+ return -2;
+ }
+
+ return 0;
+ }
+ private string recbuff = string.Empty;//鎺ユ敹绱缂撳瓨鍐呭
+ private string onlinebuff = string.Empty;//鎺ユ敹绱鍦ㄧ嚎缂撳瓨鍐呭
+ public void rec_message(string message, WebsocketClient client)
+ {
+ if (message != null)
+ {
+ try
+ {
+ string name = string.Empty;
+ JsonDocument jsonDoc = JsonDocument.Parse(message);
+ JsonElement root = jsonDoc.RootElement;
+ string mode = root.GetProperty("mode").GetString();
+ string text = root.GetProperty("text").GetString();
+ bool isfinal = root.GetProperty("is_final").GetBoolean();
+ if (message.IndexOf("wav_name ") != -1)
+ name = root.GetProperty("wav_name").GetString();
+
+ //if (name == "microphone")
+ // Console.WriteLine($"瀹炴椂璇嗗埆鍐呭: {text}");
+ //else
+ // Console.WriteLine($"鏂囦欢鍚嶇О:{name} 鏂囦欢杞綍鍐呭: {text}");
+
+ if (mode == "2pass-online" && WSClient_Online.onlineasrmode != "offline")
+ {
+ onlinebuff += text;
+ Console.WriteLine(recbuff + onlinebuff);
+ }
+ else if (mode == "2pass-offline")
+ {
+ recbuff += text;
+ onlinebuff = string.Empty;
+ Console.WriteLine(recbuff);
+ }
+
+ if (isfinal && WSClient_Online.onlineasrmode != "offline")//鏈粨鏉熷綋鍓嶈瘑鍒�
+ {
+ recbuff = string.Empty;
+ }
+ }
+ catch (JsonException ex)
+ {
+ Console.WriteLine("JSON 瑙f瀽閿欒: " + ex.Message);
+ }
+ }
+ }
+
+ private void showWAVForm(string file_name)
+ {
+ byte[] getbyte = FileToByte(file_name).Skip(44).ToArray();
+
+ for (int i = 0; i < getbyte.Length; i += 102400)
+ {
+ byte[] send = getbyte.Skip(i).Take(102400).ToArray();
+ Task.Run(() => client.Send(send));
+ Thread.Sleep(5);
+ }
+ Thread.Sleep(100);
+ Task.Run(() => client.Send("{\"is_speaking\": false}"));
+ }
+
+ private void showWAVForm_All(string file_name)
+ {
+ byte[] getbyte = FileToByte(file_name).ToArray();
+
+ for (int i = 0; i < getbyte.Length; i += 1024000)
+ {
+ byte[] send = getbyte.Skip(i).Take(1024000).ToArray();
+ Task.Run(() => client.Send(send));
+ Thread.Sleep(5);
+ }
+ Thread.Sleep(10);
+ Task.Run(() => client.Send("{\"is_speaking\": false}"));
+ }
+
+ public byte[] FileToByte(string fileUrl)
+ {
+ try
+ {
+ using (FileStream fs = new FileStream(fileUrl, FileMode.Open, FileAccess.Read))
+ {
+ byte[] byteArray = new byte[fs.Length];
+ fs.Read(byteArray, 0, byteArray.Length);
+ return byteArray;
+ }
+ }
+ catch
+ {
+ return null;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/funasr/runtime/wss-client/confg/config.ini b/funasr/runtime/wss-client/confg/config.ini
new file mode 100644
index 0000000..8de6518
--- /dev/null
+++ b/funasr/runtime/wss-client/confg/config.ini
@@ -0,0 +1,2 @@
+host=127.0.0.1
+port=10095
\ No newline at end of file
diff --git a/funasr/runtime/wss-client/confg/tmp.wav b/funasr/runtime/wss-client/confg/tmp.wav
new file mode 100644
index 0000000..0f74f14
--- /dev/null
+++ b/funasr/runtime/wss-client/confg/tmp.wav
Binary files differ
--
Gitblit v1.9.1