From 3bb14b5029becec85034f88dd66ca2d027134072 Mon Sep 17 00:00:00 2001
From: cdevelop <cdevelop@qq.com>
Date: 星期三, 11 十月 2023 15:15:14 +0800
Subject: [PATCH] 解决vc编译错误 (#987)

---
 funasr/runtime/onnxruntime/src/audio.cpp                 |    3 +
 funasr/runtime/onnxruntime/src/ct-transformer.cpp        |   14 +++---
 funasr/runtime/onnxruntime/src/ct-transformer-online.cpp |   16 ++++----
 funasr/runtime/onnxruntime/src/funasrruntime.cpp         |    7 ---
 funasr/runtime/onnxruntime/src/tpass-stream.cpp          |    1 
 funasr/runtime/onnxruntime/src/paraformer.cpp            |   10 ++--
 funasr/runtime/onnxruntime/include/funasrruntime.h       |    7 ---
 funasr/runtime/onnxruntime/src/offline-stream.cpp        |    1 
 funasr/runtime/onnxruntime/src/fsmn-vad.cpp              |    2 
 funasr/runtime/onnxruntime/src/precomp.h                 |   21 ++++++++++
 funasr/runtime/onnxruntime/src/commonfunc.h              |   26 +++++++++---
 funasr/runtime/onnxruntime/src/tpass-online-stream.cpp   |    1 
 12 files changed, 64 insertions(+), 45 deletions(-)

diff --git a/funasr/runtime/onnxruntime/include/funasrruntime.h b/funasr/runtime/onnxruntime/include/funasrruntime.h
index 14f6122..3b5ae8f 100644
--- a/funasr/runtime/onnxruntime/include/funasrruntime.h
+++ b/funasr/runtime/onnxruntime/include/funasrruntime.h
@@ -18,10 +18,6 @@
 #define FUNASR_CALLBCK_PREFIX __stdcall
 #endif
 
-#ifdef __cplusplus 
-
-extern "C" {
-#endif
 
 typedef void* FUNASR_HANDLE;
 typedef void* FUNASR_RESULT;
@@ -122,7 +118,4 @@
 _FUNASRAPI void				FunTpassUninit(FUNASR_HANDLE handle);
 _FUNASRAPI void				FunTpassOnlineUninit(FUNASR_HANDLE handle);
 
-#ifdef __cplusplus 
 
-}
-#endif
diff --git a/funasr/runtime/onnxruntime/src/audio.cpp b/funasr/runtime/onnxruntime/src/audio.cpp
index d62151e..1eafa3f 100644
--- a/funasr/runtime/onnxruntime/src/audio.cpp
+++ b/funasr/runtime/onnxruntime/src/audio.cpp
@@ -9,6 +9,9 @@
 #include "audio.h"
 #include "precomp.h"
 
+#ifdef _MSC_VER
+#pragma warning(disable:4996)
+#endif
 
 #if defined(__APPLE__)
 #include <string.h>
diff --git a/funasr/runtime/onnxruntime/src/commonfunc.h b/funasr/runtime/onnxruntime/src/commonfunc.h
index d7e5f13..9bd2a00 100644
--- a/funasr/runtime/onnxruntime/src/commonfunc.h
+++ b/funasr/runtime/onnxruntime/src/commonfunc.h
@@ -1,29 +1,34 @@
 #pragma once 
 #include <algorithm>
+#ifdef _WIN32
+#include <codecvt>
+#endif
 
 namespace funasr {
 typedef struct
 {
-    std::string msg="";
-    std::string stamp="";
-    std::string tpass_msg="";
-    float snippet_time=0;
+    std::string msg;
+    std::string stamp;
+    std::string tpass_msg;
+    float snippet_time;
 }FUNASR_RECOG_RESULT;
 
 typedef struct
 {
     std::vector<std::vector<int>>* segments;
-    float  snippet_time=0;
+    float  snippet_time;
 }FUNASR_VAD_RESULT;
 
 typedef struct
 {
-    string msg="";
+    string msg;
     vector<string> arr_cache;
 }FUNASR_PUNC_RESULT;
 
 #ifdef _WIN32
-#include <codecvt>
+
+#define ORTSTRING(str) StrToWstr(str)
+#define ORTCHAR(str) StrToWstr(str).c_str()
 
 inline std::wstring String2wstring(const std::string& str, const std::string& locale)
 {
@@ -39,8 +44,15 @@
 
 }
 
+#else
+
+#define ORTSTRING(str) str
+#define ORTCHAR(str) str
+
 #endif
 
+
+
 inline void GetInputName(Ort::Session* session, string& inputName,int nIndex=0) {
     size_t numInputNodes = session->GetInputCount();
     if (numInputNodes > 0) {
diff --git a/funasr/runtime/onnxruntime/src/ct-transformer-online.cpp b/funasr/runtime/onnxruntime/src/ct-transformer-online.cpp
index 51f2a6a..2198667 100644
--- a/funasr/runtime/onnxruntime/src/ct-transformer-online.cpp
+++ b/funasr/runtime/onnxruntime/src/ct-transformer-online.cpp
@@ -17,7 +17,7 @@
     session_options.DisableCpuMemArena();
 
     try{
-        m_session = std::make_unique<Ort::Session>(env_, punc_model.c_str(), session_options);
+        m_session = std::make_unique<Ort::Session>(env_, ORTSTRING(punc_model).c_str(), session_options);
         LOG(INFO) << "Successfully load model from " << punc_model;
     }
     catch (std::exception const &e) {
@@ -74,8 +74,8 @@
     for (size_t i = 0; i < InputData.size(); i += TOKEN_LEN)
     {
         nDiff = (i + TOKEN_LEN) < InputData.size() ? (0) : (i + TOKEN_LEN - InputData.size());
-        vector<int32_t> InputIDs(InputData.begin() + i, InputData.begin() + i + TOKEN_LEN - nDiff);
-        vector<string> InputStr(strOut.begin() + i, strOut.begin() + i + TOKEN_LEN - nDiff);
+        vector<int32_t> InputIDs(InputData.begin() + i, InputData.begin() + i + (TOKEN_LEN - nDiff));
+        vector<string> InputStr(strOut.begin() + i, strOut.begin() + i + (TOKEN_LEN - nDiff));
         InputIDs.insert(InputIDs.begin(), RemainIDs.begin(), RemainIDs.end()); // RemainIDs+InputIDs;
         InputStr.insert(InputStr.begin(), RemainStr.begin(), RemainStr.end()); // RemainStr+InputStr;
 
@@ -102,10 +102,10 @@
                 nSentEnd = nLastCommaIndex;
                 Punction[nSentEnd] = PERIOD_INDEX;
             }
-            RemainStr.assign(InputStr.begin() + nSentEnd + 1, InputStr.end());
-            RemainIDs.assign(InputIDs.begin() + nSentEnd + 1, InputIDs.end());
-            InputStr.assign(InputStr.begin(), InputStr.begin() + nSentEnd + 1);  // minit_sentence
-            Punction.assign(Punction.begin(), Punction.begin() + nSentEnd + 1);
+            RemainStr.assign(InputStr.begin() + (nSentEnd + 1), InputStr.end());
+            RemainIDs.assign(InputIDs.begin() + (nSentEnd + 1), InputIDs.end());
+            InputStr.assign(InputStr.begin(), InputStr.begin() + (nSentEnd + 1));  // minit_sentence
+            Punction.assign(Punction.begin(), Punction.begin() + (nSentEnd + 1));
         }
         
         for (auto& item : Punction)  
@@ -149,7 +149,7 @@
             break;
         }
     }
-    arr_cache.assign(sentence_words_list.begin() + nSentEnd + 1, sentence_words_list.end());
+    arr_cache.assign(sentence_words_list.begin() + (nSentEnd + 1), sentence_words_list.end());
 
     if (sentenceOut.size() > 0 && m_tokenizer.IsPunc(sentenceOut[sentenceOut.size() - 1]))
     {
diff --git a/funasr/runtime/onnxruntime/src/ct-transformer.cpp b/funasr/runtime/onnxruntime/src/ct-transformer.cpp
index 64a70da..71a8847 100644
--- a/funasr/runtime/onnxruntime/src/ct-transformer.cpp
+++ b/funasr/runtime/onnxruntime/src/ct-transformer.cpp
@@ -17,7 +17,7 @@
     session_options.DisableCpuMemArena();
 
     try{
-        m_session = std::make_unique<Ort::Session>(env_, punc_model.c_str(), session_options);
+        m_session = std::make_unique<Ort::Session>(env_, ORTSTRING(punc_model).c_str(), session_options);
         LOG(INFO) << "Successfully load model from " << punc_model;
     }
     catch (std::exception const &e) {
@@ -66,8 +66,8 @@
     for (size_t i = 0; i < InputData.size(); i += TOKEN_LEN)
     {
         nDiff = (i + TOKEN_LEN) < InputData.size() ? (0) : (i + TOKEN_LEN - InputData.size());
-        vector<int32_t> InputIDs(InputData.begin() + i, InputData.begin() + i + TOKEN_LEN - nDiff);
-        vector<string> InputStr(strOut.begin() + i, strOut.begin() + i + TOKEN_LEN - nDiff);
+        vector<int32_t> InputIDs(InputData.begin() + i, InputData.begin() + i + (TOKEN_LEN - nDiff));
+        vector<string> InputStr(strOut.begin() + i, strOut.begin() + i + (TOKEN_LEN - nDiff));
         InputIDs.insert(InputIDs.begin(), RemainIDs.begin(), RemainIDs.end()); // RemainIDs+InputIDs;
         InputStr.insert(InputStr.begin(), RemainStr.begin(), RemainStr.end()); // RemainStr+InputStr;
 
@@ -94,10 +94,10 @@
                 nSentEnd = nLastCommaIndex;
                 Punction[nSentEnd] = PERIOD_INDEX;
             }
-            RemainStr.assign(InputStr.begin() + nSentEnd + 1, InputStr.end());
-            RemainIDs.assign(InputIDs.begin() + nSentEnd + 1, InputIDs.end());
-            InputStr.assign(InputStr.begin(), InputStr.begin() + nSentEnd + 1);  // minit_sentence
-            Punction.assign(Punction.begin(), Punction.begin() + nSentEnd + 1);
+            RemainStr.assign(InputStr.begin() + (nSentEnd + 1), InputStr.end());
+            RemainIDs.assign(InputIDs.begin() + (nSentEnd + 1), InputIDs.end());
+            InputStr.assign(InputStr.begin(), InputStr.begin() + (nSentEnd + 1));  // minit_sentence
+            Punction.assign(Punction.begin(), Punction.begin() + (nSentEnd + 1));
         }
         
         NewPunctuation.insert(NewPunctuation.end(), Punction.begin(), Punction.end());
diff --git a/funasr/runtime/onnxruntime/src/fsmn-vad.cpp b/funasr/runtime/onnxruntime/src/fsmn-vad.cpp
index fb79ad7..db633b0 100644
--- a/funasr/runtime/onnxruntime/src/fsmn-vad.cpp
+++ b/funasr/runtime/onnxruntime/src/fsmn-vad.cpp
@@ -54,7 +54,7 @@
 void FsmnVad::ReadModel(const char* vad_model) {
     try {
         vad_session_ = std::make_shared<Ort::Session>(
-                env_, vad_model, session_options_);
+                env_, ORTCHAR(vad_model), session_options_);
         LOG(INFO) << "Successfully load model from " << vad_model;
     } catch (std::exception const &e) {
         LOG(ERROR) << "Error when load vad onnx model: " << e.what();
diff --git a/funasr/runtime/onnxruntime/src/funasrruntime.cpp b/funasr/runtime/onnxruntime/src/funasrruntime.cpp
index 150be70..91ef188 100644
--- a/funasr/runtime/onnxruntime/src/funasrruntime.cpp
+++ b/funasr/runtime/onnxruntime/src/funasrruntime.cpp
@@ -1,9 +1,6 @@
 #include "precomp.h"
 #include <vector>
-#ifdef __cplusplus 
 
-extern "C" {
-#endif
 
 	// APIs for Init
 	_FUNASRAPI FUNASR_HANDLE  FunASRInit(std::map<std::string, std::string>& model_path, int thread_num, ASR_TYPE type)
@@ -694,8 +691,4 @@
 		delete tpass_online_stream;
 	}
 
-#ifdef __cplusplus 
-
-}
-#endif
 
diff --git a/funasr/runtime/onnxruntime/src/offline-stream.cpp b/funasr/runtime/onnxruntime/src/offline-stream.cpp
index 50d0188..caea72e 100644
--- a/funasr/runtime/onnxruntime/src/offline-stream.cpp
+++ b/funasr/runtime/onnxruntime/src/offline-stream.cpp
@@ -1,5 +1,4 @@
 #include "precomp.h"
-#include <unistd.h>
 
 namespace funasr {
 OfflineStream::OfflineStream(std::map<std::string, std::string>& model_path, int thread_num)
diff --git a/funasr/runtime/onnxruntime/src/paraformer.cpp b/funasr/runtime/onnxruntime/src/paraformer.cpp
index a2dd038..5bbaeef 100644
--- a/funasr/runtime/onnxruntime/src/paraformer.cpp
+++ b/funasr/runtime/onnxruntime/src/paraformer.cpp
@@ -37,7 +37,7 @@
     session_options_.DisableCpuMemArena();
 
     try {
-        m_session_ = std::make_unique<Ort::Session>(env_, am_model.c_str(), session_options_);
+        m_session_ = std::make_unique<Ort::Session>(env_, ORTSTRING(am_model).c_str(), session_options_);
         LOG(INFO) << "Successfully load model from " << am_model;
     } catch (std::exception const &e) {
         LOG(ERROR) << "Error when load am onnx model: " << e.what();
@@ -90,7 +90,7 @@
     session_options_.DisableCpuMemArena();
 
     try {
-        encoder_session_ = std::make_unique<Ort::Session>(env_, en_model.c_str(), session_options_);
+        encoder_session_ = std::make_unique<Ort::Session>(env_, ORTSTRING(en_model).c_str(), session_options_);
         LOG(INFO) << "Successfully load model from " << en_model;
     } catch (std::exception const &e) {
         LOG(ERROR) << "Error when load am encoder model: " << e.what();
@@ -98,7 +98,7 @@
     }
 
     try {
-        decoder_session_ = std::make_unique<Ort::Session>(env_, de_model.c_str(), session_options_);
+        decoder_session_ = std::make_unique<Ort::Session>(env_, ORTSTRING(de_model).c_str(), session_options_);
         LOG(INFO) << "Successfully load model from " << de_model;
     } catch (std::exception const &e) {
         LOG(ERROR) << "Error when load am decoder model: " << e.what();
@@ -153,7 +153,7 @@
 
     // offline
     try {
-        m_session_ = std::make_unique<Ort::Session>(env_, am_model.c_str(), session_options_);
+        m_session_ = std::make_unique<Ort::Session>(env_, ORTSTRING(am_model).c_str(), session_options_);
         LOG(INFO) << "Successfully load model from " << am_model;
     } catch (std::exception const &e) {
         LOG(ERROR) << "Error when load am onnx model: " << e.what();
@@ -250,7 +250,7 @@
     hw_session_options.DisableCpuMemArena();
 
     try {
-        hw_m_session = std::make_unique<Ort::Session>(hw_env_, hw_model.c_str(), hw_session_options);
+        hw_m_session = std::make_unique<Ort::Session>(hw_env_, ORTSTRING(hw_model).c_str(), hw_session_options);
         LOG(INFO) << "Successfully load model from " << hw_model;
     } catch (std::exception const &e) {
         LOG(ERROR) << "Error when load hw compiler onnx model: " << e.what();
diff --git a/funasr/runtime/onnxruntime/src/precomp.h b/funasr/runtime/onnxruntime/src/precomp.h
index 59e594e..5c49fd6 100644
--- a/funasr/runtime/onnxruntime/src/precomp.h
+++ b/funasr/runtime/onnxruntime/src/precomp.h
@@ -17,6 +17,25 @@
 #include <numeric>
 #include <cstring>
 
+#ifdef _WIN32
+#include<io.h>
+#ifndef R_OK
+#define R_OK 4
+#endif
+#ifndef W_OK
+#define W_OK 2
+#endif
+#ifndef X_OK
+#define X_OK 0 
+#endif
+#ifndef F_OK
+#define F_OK 0
+#endif
+#define access _access
+#else
+#include <unistd.h>
+#endif
+
 using namespace std;
 // third part
 #if defined(__APPLE__)
@@ -33,6 +52,8 @@
 
 // mine
 #include <glog/logging.h>
+
+
 #include "common-struct.h"
 #include "com-define.h"
 #include "commonfunc.h"
diff --git a/funasr/runtime/onnxruntime/src/tpass-online-stream.cpp b/funasr/runtime/onnxruntime/src/tpass-online-stream.cpp
index 832cfd1..7788e0b 100644
--- a/funasr/runtime/onnxruntime/src/tpass-online-stream.cpp
+++ b/funasr/runtime/onnxruntime/src/tpass-online-stream.cpp
@@ -1,5 +1,4 @@
 #include "precomp.h"
-#include <unistd.h>
 
 namespace funasr {
 TpassOnlineStream::TpassOnlineStream(TpassStream* tpass_stream, std::vector<int> chunk_size){
diff --git a/funasr/runtime/onnxruntime/src/tpass-stream.cpp b/funasr/runtime/onnxruntime/src/tpass-stream.cpp
index 47734af..a3e1b0e 100644
--- a/funasr/runtime/onnxruntime/src/tpass-stream.cpp
+++ b/funasr/runtime/onnxruntime/src/tpass-stream.cpp
@@ -1,5 +1,4 @@
 #include "precomp.h"
-#include <unistd.h>
 
 namespace funasr {
 TpassStream::TpassStream(std::map<std::string, std::string>& model_path, int thread_num)

--
Gitblit v1.9.1