From 2fe2464c55f96998e23f44ed2dce0edd3da830b1 Mon Sep 17 00:00:00 2001
From: root <root@localhost.localdomain>
Date: 星期五, 28 四月 2023 23:31:39 +0800
Subject: [PATCH] change dir to websocket

---
 funasr/runtime/onnxruntime/src/audio.cpp      |    4 
 funasr/runtime/websocket/CMakeLists.txt       |   69 +++++++++++++
 funasr/runtime/websocket/websocketsrv.cpp     |    2 
 funasr/runtime/onnxruntime/src/CMakeLists.txt |   11 --
 funasr/runtime/websocket/readme.md            |   96 +++++++++++++++++++
 funasr/runtime/websocket/websocketsrv.h       |    2 
 funasr/runtime/onnxruntime/CMakeLists.txt     |   38 -------
 funasr/runtime/websocket/websocketclient.cpp  |   19 ++-
 funasr/runtime/websocket/websocketmain.cpp    |    0 
 9 files changed, 183 insertions(+), 58 deletions(-)

diff --git a/funasr/runtime/onnxruntime/CMakeLists.txt b/funasr/runtime/onnxruntime/CMakeLists.txt
index be337ed..9f6013f 100644
--- a/funasr/runtime/onnxruntime/CMakeLists.txt
+++ b/funasr/runtime/onnxruntime/CMakeLists.txt
@@ -3,7 +3,7 @@
 project(FunASROnnx)
 
 option(ENABLE_GLOG "Whether to build glog" ON)
-option(ENABLE_WEBSOCKET "Whether to build websocket server" ON)
+
 # set(CMAKE_CXX_STANDARD 11)
 set(CMAKE_CXX_STANDARD 14 CACHE STRING "The C++ version to be used.")
 set(CMAKE_POSITION_INDEPENDENT_CODE ON)
@@ -27,38 +27,6 @@
     link_directories(${ONNXRUNTIME_DIR}/lib)
 endif()
 
-if(ENABLE_WEBSOCKET)
-  cmake_policy(SET CMP0135 NEW)
-
-  include(FetchContent)
-  FetchContent_Declare(websocketpp
-  GIT_REPOSITORY https://github.com/zaphoyd/websocketpp.git
-    GIT_TAG 0.8.2
-    SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/websocket
-    )
-  
-  FetchContent_MakeAvailable(websocketpp)
-  include_directories(${PROJECT_SOURCE_DIR}/third_party/websocket)
-  add_subdirectory(third_party/websocket)
-
-  FetchContent_Declare(asio
-     URL   https://github.com/chriskohlhoff/asio/archive/refs/tags/asio-1-24-0.tar.gz
-   SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/asio
-  )
-  
-  FetchContent_MakeAvailable(asio)
-  include_directories(${PROJECT_SOURCE_DIR}/third_party/asio/asio/include)
- 
-  FetchContent_Declare(json
-     URL   https://github.com/nlohmann/json/archive/refs/tags/v3.11.2.tar.gz
-   SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/json
-  )
-  
-  FetchContent_MakeAvailable(json)
-  include_directories(${PROJECT_SOURCE_DIR}/third_party/json/include)
- 
-endif()
-
 include_directories(${PROJECT_SOURCE_DIR}/third_party/kaldi-native-fbank)
 include_directories(${PROJECT_SOURCE_DIR}/third_party/yaml-cpp/include)
 
@@ -70,6 +38,4 @@
     include_directories(${PROJECT_SOURCE_DIR}/third_party/glog)
     set(BUILD_TESTING OFF)
     add_subdirectory(third_party/glog)
-endif()
-
-
+endif()
\ No newline at end of file
diff --git a/funasr/runtime/onnxruntime/src/CMakeLists.txt b/funasr/runtime/onnxruntime/src/CMakeLists.txt
index 9c8d51c..d33c540 100644
--- a/funasr/runtime/onnxruntime/src/CMakeLists.txt
+++ b/funasr/runtime/onnxruntime/src/CMakeLists.txt
@@ -25,16 +25,7 @@
 include_directories(${CMAKE_SOURCE_DIR}/include)
 target_link_libraries(funasr PUBLIC onnxruntime ${EXTRA_LIBS})
 
-if(ENABLE_WEBSOCKET)
-  add_executable(websocketmain "websocketmain.cpp")
-  add_executable(websocketclient "websocketclient.cpp")
-
-  target_link_libraries(websocketclient PUBLIC funasr)
-  target_link_libraries(websocketmain PUBLIC funasr)
-endif()
-
 add_executable(funasr-onnx-offline "funasr-onnx-offline.cpp")
 add_executable(funasr-onnx-offline-rtf "funasr-onnx-offline-rtf.cpp")
 target_link_libraries(funasr-onnx-offline PUBLIC funasr)
-target_link_libraries(funasr-onnx-offline-rtf PUBLIC funasr)
-
+target_link_libraries(funasr-onnx-offline-rtf PUBLIC funasr)
\ No newline at end of file
diff --git a/funasr/runtime/onnxruntime/src/audio.cpp b/funasr/runtime/onnxruntime/src/audio.cpp
index d104500..8f46a4f 100644
--- a/funasr/runtime/onnxruntime/src/audio.cpp
+++ b/funasr/runtime/onnxruntime/src/audio.cpp
@@ -380,8 +380,10 @@
     FILE* fp;
     fp = fopen(filename, "rb");
     if (fp == nullptr)
+	{
         LOG(ERROR) << "Failed to read " << filename;
         return false;
+	}
     fseek(fp, 0, SEEK_END);
     uint32_t n_file_len = ftell(fp);
     fseek(fp, 0, SEEK_SET);
@@ -517,4 +519,4 @@
         frame_queue.push(frame);
         frame = NULL;
     }
-}
+}
\ No newline at end of file
diff --git a/funasr/runtime/websocket/CMakeLists.txt b/funasr/runtime/websocket/CMakeLists.txt
new file mode 100644
index 0000000..dd8bf0e
--- /dev/null
+++ b/funasr/runtime/websocket/CMakeLists.txt
@@ -0,0 +1,69 @@
+cmake_minimum_required(VERSION 3.10)
+
+project(FunASRWebscoket) 
+
+set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
+
+ 
+
+ 
+option(ENABLE_WEBSOCKET "Whether to build websocket server" ON)
+ 
+if(ENABLE_WEBSOCKET)
+  cmake_policy(SET CMP0135 NEW)
+
+  include(FetchContent)
+  FetchContent_Declare(websocketpp
+  GIT_REPOSITORY https://github.com/zaphoyd/websocketpp.git
+    GIT_TAG 0.8.2
+    SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/websocket
+    )
+  
+  FetchContent_MakeAvailable(websocketpp)
+  include_directories(${PROJECT_SOURCE_DIR}/third_party/websocket)
+   
+
+  FetchContent_Declare(asio
+     URL   https://github.com/chriskohlhoff/asio/archive/refs/tags/asio-1-24-0.tar.gz
+   SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/asio
+  )
+  
+  FetchContent_MakeAvailable(asio)
+  include_directories(${PROJECT_SOURCE_DIR}/third_party/asio/asio/include)
+ 
+  FetchContent_Declare(json
+     URL   https://github.com/nlohmann/json/archive/refs/tags/v3.11.2.tar.gz
+   SOURCE_DIR ${PROJECT_SOURCE_DIR}/third_party/json
+  )
+  
+  FetchContent_MakeAvailable(json)
+  include_directories(${PROJECT_SOURCE_DIR}/third_party/json/include)
+ 
+ 
+
+endif()
+
+# Include generated *.pb.h files
+link_directories(${ONNXRUNTIME_DIR}/lib)
+
+include_directories(${PROJECT_SOURCE_DIR}/../onnxruntime/include/)
+include_directories(${PROJECT_SOURCE_DIR}/../onnxruntime/third_party/yaml-cpp/include/)
+include_directories(${PROJECT_SOURCE_DIR}/../onnxruntime/third_party/kaldi-native-fbank)
+
+add_subdirectory(${PROJECT_SOURCE_DIR}/../onnxruntime/third_party/yaml-cpp yaml-cpp)
+add_subdirectory(${PROJECT_SOURCE_DIR}/../onnxruntime/third_party/kaldi-native-fbank/kaldi-native-fbank/csrc csrc)
+add_subdirectory(${PROJECT_SOURCE_DIR}/../onnxruntime/src src)
+
+include_directories(${PROJECT_SOURCE_DIR}/../onnxruntime/third_party/glog)
+set(BUILD_TESTING OFF)
+add_subdirectory(${PROJECT_SOURCE_DIR}/../onnxruntime/third_party/glog glog)
+ 
+message("********************************${PROJECT_SOURCE_DIR}/../onnxruntime/include/" )
+
+ 
+
+add_executable(websocketmain "websocketmain.cpp" "websocketsrv.cpp")
+add_executable(websocketclient "websocketclient.cpp")
+
+target_link_libraries(websocketclient PUBLIC funasr)
+target_link_libraries(websocketmain PUBLIC funasr)
diff --git a/funasr/runtime/websocket/readme.md b/funasr/runtime/websocket/readme.md
new file mode 100644
index 0000000..4b2d55a
--- /dev/null
+++ b/funasr/runtime/websocket/readme.md
@@ -0,0 +1,96 @@
+# ONNXRuntime-cpp for Websocket Server
+
+## Export the model
+### Install [modelscope and funasr](https://github.com/alibaba-damo-academy/FunASR#installation)
+
+```shell
+# pip3 install torch torchaudio
+pip install -U modelscope funasr
+# For the users in China, you could install with the command:
+# pip install -U modelscope funasr -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html -i https://mirror.sjtu.edu.cn/pypi/web/simple
+```
+
+### Export [onnx model](https://github.com/alibaba-damo-academy/FunASR/tree/main/funasr/export)
+
+```shell
+python -m funasr.export.export_model --model-name damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch --export-dir ./export --type onnx --quantize True
+```
+
+## Building for Linux/Unix
+
+### Download onnxruntime
+```shell
+# download an appropriate onnxruntime from https://github.com/microsoft/onnxruntime/releases/tag/v1.14.0
+# here we get a copy of onnxruntime for linux 64
+wget https://github.com/microsoft/onnxruntime/releases/download/v1.14.0/onnxruntime-linux-x64-1.14.0.tgz
+tar -zxvf onnxruntime-linux-x64-1.14.0.tgz
+```
+
+### Install openblas
+```shell
+sudo apt-get install libopenblas-dev #ubuntu
+# sudo yum -y install openblas-devel #centos
+```
+
+### Build runtime
+```shell
+git clone https://github.com/alibaba-damo-academy/FunASR.git && cd funasr/runtime/websocket
+mkdir build && cd build
+cmake  -DCMAKE_BUILD_TYPE=release .. -DONNXRUNTIME_DIR=/path/to/onnxruntime-linux-x64-1.14.0
+make
+```
+## Run the websocket server
+
+```shell
+cd bin
+websocketmain  [--model_thread_num <int>] [--decoder_thread_num
+                        <int>] [--io_thread_num <int>] [--port <int>]
+                        [--listen_ip <string>] [--wav-scp <string>]
+                        [--wav-path <string>] [--punc-config <string>]
+                        [--punc-model <string>] --am-config <string>
+                        --am-cmvn <string> --am-model <string>
+                        [--vad-config <string>] [--vad-cmvn <string>]
+                        [--vad-model <string>] [--] [--version] [-h]
+Where:
+   --wav-scp <string>
+     wave scp path
+   --wav-path <string>
+     wave file path
+
+   --punc-config <string>
+     punc config path
+   --punc-model <string>
+     punc model path
+
+   --am-config <string>
+     (required)  am config path
+   --am-cmvn <string>
+     (required)  am cmvn path
+   --am-model <string>
+     (required)  am model path
+
+   --vad-config <string>
+     vad config path
+   --vad-cmvn <string>
+     vad cmvn path
+   --vad-model <string>
+     vad model path
+   --decoder_thread_num <int>
+     number of threads for decoder
+   --io_thread_num <int>
+     number of threads for network io
+  
+   Required: --am-config <string> --am-cmvn <string> --am-model <string> 
+   If use vad, please add: [--vad-config <string>] [--vad-cmvn <string>] [--vad-model <string>]
+   If use punc, please add: [--punc-config <string>] [--punc-model <string>] 
+example:
+./bin/websocketmain --am-config /FunASR/funasr/runtime/onnxruntime/export/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/config.yaml --am-model /FunASR/funasr/runtime/onnxruntime/export/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/model.onnx --am-cmvn /FunASR/funasr/runtime/onnxruntime/export/damo/speech_paraformer-large_asr_nat-zh-cn-16k-common-vocab8404-pytorch/am.mvn
+```
+
+## Run websocket client test
+Usage: websocketclient server_ip port wav_path threads_num
+example:
+websocketclient 127.0.0.1 8889 funasr/runtime/websocket/test.pcm.wav 64
+result json, example like:
+{"text":"涓�浜屼笁鍥涗簲鍏竷鍏節鍗佷竴浜屼笁鍥涗簲鍏竷鍏節鍗�"}
+
diff --git a/funasr/runtime/onnxruntime/src/websocketclient.cpp b/funasr/runtime/websocket/websocketclient.cpp
similarity index 91%
rename from funasr/runtime/onnxruntime/src/websocketclient.cpp
rename to funasr/runtime/websocket/websocketclient.cpp
index a0d943c..9ef1d5e 100644
--- a/funasr/runtime/onnxruntime/src/websocketclient.cpp
+++ b/funasr/runtime/websocket/websocketclient.cpp
@@ -12,7 +12,8 @@
 #include <websocketpp/common/thread.hpp>
 #include <websocketpp/config/asio_no_tls_client.hpp>
 
-#include "libfunasrapi.cpp"
+#include "audio.h"
+
 /**
  * Define a semi-cross platform helper method that waits/sleeps for a bit.
  */
@@ -88,28 +89,28 @@
     asio_thread.join();
   }
 
-  // The open handler will signal that we are ready to start sending telemetry
+  // The open handler will signal that we are ready to start sending data
   void on_open(websocketpp::connection_hdl) {
     m_client.get_alog().write(websocketpp::log::alevel::app,
-                              "Connection opened, starting telemetry!");
+                              "Connection opened, starting data!");
 
     scoped_lock guard(m_lock);
     m_open = true;
   }
 
-  // The close handler will signal that we should stop sending telemetry
+  // The close handler will signal that we should stop sending data
   void on_close(websocketpp::connection_hdl) {
     m_client.get_alog().write(websocketpp::log::alevel::app,
-                              "Connection closed, stopping telemetry!");
+                              "Connection closed, stopping data!");
 
     scoped_lock guard(m_lock);
     m_done = true;
   }
 
-  // The fail handler will signal that we should stop sending telemetry
+  // The fail handler will signal that we should stop sending data
   void on_fail(websocketpp::connection_hdl) {
     m_client.get_alog().write(websocketpp::log::alevel::app,
-                              "Connection failed, stopping telemetry!");
+                              "Connection failed, stopping data!");
 
     scoped_lock guard(m_lock);
     m_done = true;
@@ -134,7 +135,7 @@
     while (1) {
       {
         scoped_lock guard(m_lock);
-        // If the connection has been closed, stop generating telemetry
+        // If the connection has been closed, stop generating data
         if (m_done) {
           break;
         }
@@ -171,7 +172,7 @@
       // not in the right state. Usually this means we tried to send a
       // message to a connection that was closed or in the process of
       // closing. While many errors here can be easily recovered from,
-      // in this simple example, we'll stop the telemetry loop.
+      // in this simple example, we'll stop the data loop.
       if (ec) {
         m_client.get_alog().write(websocketpp::log::alevel::app,
                                   "Send Error: " + ec.message());
diff --git a/funasr/runtime/onnxruntime/src/websocketmain.cpp b/funasr/runtime/websocket/websocketmain.cpp
similarity index 100%
rename from funasr/runtime/onnxruntime/src/websocketmain.cpp
rename to funasr/runtime/websocket/websocketmain.cpp
diff --git a/funasr/runtime/onnxruntime/src/websocketsrv.cpp b/funasr/runtime/websocket/websocketsrv.cpp
similarity index 98%
rename from funasr/runtime/onnxruntime/src/websocketsrv.cpp
rename to funasr/runtime/websocket/websocketsrv.cpp
index 60c8a55..7e54210 100644
--- a/funasr/runtime/onnxruntime/src/websocketsrv.cpp
+++ b/funasr/runtime/websocket/websocketsrv.cpp
@@ -8,7 +8,7 @@
 // take some ideas from https://github.com/k2-fsa/sherpa-onnx
 // online-websocket-server-impl.cc, thanks. The websocket server has two threads
 // pools, one for handle network data and one for asr decoder.
-// now only support offline engine. 
+// now only support offline engine.
 
 #include "websocketsrv.h"
 
diff --git a/funasr/runtime/onnxruntime/src/websocketsrv.h b/funasr/runtime/websocket/websocketsrv.h
similarity index 98%
rename from funasr/runtime/onnxruntime/src/websocketsrv.h
rename to funasr/runtime/websocket/websocketsrv.h
index 42e2243..2d0c7bd 100644
--- a/funasr/runtime/onnxruntime/src/websocketsrv.h
+++ b/funasr/runtime/websocket/websocketsrv.h
@@ -8,7 +8,7 @@
 // take some ideas from https://github.com/k2-fsa/sherpa-onnx
 // online-websocket-server-impl.cc, thanks. The websocket server has two threads
 // pools, one for handle network data and one for asr decoder.
-// now only support offline engine. 
+// now only support offline engine.
 
 #ifndef WEBSOCKETSRV_SERVER_H_
 #define WEBSOCKETSRV_SERVER_H_

--
Gitblit v1.9.1