Open-source speech recognition toolkit for training, inference, streaming ASR, VAD, punctuation, speaker diarization pipelines, and OpenAI-compatible/MCP serving.
Python asraudiochineseemotion-recognitionfunasrmcp-servermultilingual-asropenai-compatible-apiparaformerpunctuationpytorchreal-time-asrspeaker-diarizationspeech-recognitionspeech-to-textstreaming-asrtranscriptionvllmvoice-activity-detectionwhisper-alternative
change dir to websocket
root2fe2464c55f9
diff --git a/funasr/runtime/onnxruntime/CMakeLists.txt b/funasr/runtime/onnxruntime/CMakeLists.txtindex be337ed17..9f6013f76 100644--- a/funasr/runtime/onnxruntime/CMakeLists.txt+++ b/funasr/runtime/onnxruntime/CMakeLists.txt@@ -3,7 +3,7 @@ cmake_minimum_required(VERSION 3.16)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 @@ ELSE()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 @@ if(ENABLE_GLOG)include_directories(${PROJECT_SOURCE_DIR}/third_party/glog)set(BUILD_TESTING OFF)add_subdirectory(third_party/glog)-endif()--+endif()\ No newline at end of filediff --git a/funasr/runtime/onnxruntime/src/CMakeLists.txt b/funasr/runtime/onnxruntime/src/CMakeLists.txtindex 9c8d51cd1..d33c5402f 100644--- a/funasr/runtime/onnxruntime/src/CMakeLists.txt+++ b/funasr/runtime/onnxruntime/src/CMakeLists.txt@@ -25,16 +25,7 @@ endif()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 filediff --git a/funasr/runtime/onnxruntime/src/audio.cpp b/funasr/runtime/onnxruntime/src/audio.cppindex d104500d1..8f46a4f63 100644--- a/funasr/runtime/onnxruntime/src/audio.cpp+++ b/funasr/runtime/onnxruntime/src/audio.cpp@@ -380,8 +380,10 @@ bool Audio::LoadPcmwav(const char* filename, int32_t* sampling_rate)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 @@ void Audio::Split(Model* recog_obj)frame_queue.push(frame);frame = NULL;}-}+}\ No newline at end of filediff --git a/funasr/runtime/websocket/CMakeLists.txt b/funasr/runtime/websocket/CMakeLists.txtnew file mode 100644index 000000000..dd8bf0ebf--- /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.mdnew file mode 100644index 000000000..4b2d55a4d--- /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.cppsimilarity index 92%rename from funasr/runtime/onnxruntime/src/websocketclient.cpprename to funasr/runtime/websocket/websocketclient.cppindex a0d943cbd..9ef1d5e5e 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 @@ class websocket_client {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 datavoid 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 datavoid 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 datavoid 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 @@ class websocket_client {while (1) {{scoped_lock guard(m_lock);- // If the connection has been closed, stop generating telemetry+ // If the connection has been closed, stop generating dataif (m_done) {break;}@@ -171,7 +172,7 @@ class websocket_client {// 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.cppsimilarity index 100%rename from funasr/runtime/onnxruntime/src/websocketmain.cpprename to funasr/runtime/websocket/websocketmain.cppdiff --git a/funasr/runtime/onnxruntime/src/websocketsrv.cpp b/funasr/runtime/websocket/websocketsrv.cppsimilarity index 96%rename from funasr/runtime/onnxruntime/src/websocketsrv.cpprename to funasr/runtime/websocket/websocketsrv.cppindex 60c8a553d..7e54210cb 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.hsimilarity index 98%rename from funasr/runtime/onnxruntime/src/websocketsrv.hrename to funasr/runtime/websocket/websocketsrv.hindex 42e2243fe..2d0c7bda3 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_