chenmengzheAAA
2023-09-14 30c40c643c19f6e2ac8679fa76d09d0f9ceccc65
funasr/runtime/onnxruntime/src/funasrruntime.cpp
@@ -1,4 +1,5 @@
#include "precomp.h"
#include <vector>
#ifdef __cplusplus 
extern "C" {
@@ -216,19 +217,25 @@
   }
   // APIs for Offline-stream Infer
   _FUNASRAPI FUNASR_RESULT FunOfflineInferBuffer(FUNASR_HANDLE handle, const char* sz_buf, int n_len, FUNASR_MODE mode, QM_CALLBACK fn_callback, int sampling_rate, std::string wav_format)
   _FUNASRAPI FUNASR_RESULT FunOfflineInferBuffer(FUNASR_HANDLE handle, const char* sz_buf, int n_len, FUNASR_MODE mode, QM_CALLBACK fn_callback, const std::vector<std::vector<float>> &hw_emb, int sampling_rate, std::string wav_format)
   {
      funasr::OfflineStream* offline_stream = (funasr::OfflineStream*)handle;
      if (!offline_stream)
         return nullptr;
      funasr::Audio audio(1);
      if(wav_format == "pcm" || wav_format == "PCM"){
         if (!audio.LoadPcmwav(sz_buf, n_len, &sampling_rate))
            return nullptr;
      }else{
         if (!audio.FfmpegLoad(sz_buf, n_len))
            return nullptr;
      try{
         if(wav_format == "pcm" || wav_format == "PCM"){
            if (!audio.LoadPcmwav(sz_buf, n_len, &sampling_rate))
               return nullptr;
         }else{
            if (!audio.FfmpegLoad(sz_buf, n_len))
               return nullptr;
         }
      }catch (std::exception const &e)
      {
         LOG(ERROR)<<e.what();
         return nullptr;
      }
      funasr::FUNASR_RECOG_RESULT* p_result = new funasr::FUNASR_RECOG_RESULT;
@@ -247,24 +254,30 @@
      int n_step = 0;
      int n_total = audio.GetQueueSize();
      float start_time = 0.0;
      std::string cur_stamp = "[";
      while (audio.Fetch(buff, len, flag, start_time) > 0) {
         string msg = (offline_stream->asr_handle)->Forward(buff, len, true);
         string msg = (offline_stream->asr_handle)->Forward(buff, len, true, hw_emb);
         std::vector<std::string> msg_vec = funasr::split(msg, '|');
         if(msg_vec.size()==0){
            continue;
         }
         p_result->msg += msg_vec[0];
         //timestamp
         if(msg_vec.size() > 1){
            std::vector<std::string> msg_stamp = funasr::split(msg_vec[1], ',');
            std::string cur_stamp = "";
            for(int i=0; i<msg_stamp.size()-1; i+=2){
               float begin = std::stof(msg_stamp[i])+start_time;
               float end = std::stof(msg_stamp[i+1])+start_time;
               cur_stamp += "["+std::to_string(begin)+","+std::to_string(end)+"],";
               cur_stamp += "["+std::to_string((int)(1000*begin))+","+std::to_string((int)(1000*end))+"],";
            }
            p_result->stamp += cur_stamp;
         }
         n_step++;
         if (fn_callback)
            fn_callback(n_step, n_total);
      }
      if(cur_stamp != "["){
         cur_stamp.erase(cur_stamp.length() - 1);
         p_result->stamp += cur_stamp + "]";
      }
      if(offline_stream->UsePunc()){
         string punc_res = (offline_stream->punc_handle)->AddPunc((p_result->msg).c_str());
@@ -274,24 +287,31 @@
      return p_result;
   }
   _FUNASRAPI FUNASR_RESULT FunOfflineInfer(FUNASR_HANDLE handle, const char* sz_filename, FUNASR_MODE mode, QM_CALLBACK fn_callback, int sampling_rate)
   _FUNASRAPI FUNASR_RESULT FunOfflineInfer(FUNASR_HANDLE handle, const char* sz_filename, FUNASR_MODE mode, QM_CALLBACK fn_callback, const std::vector<std::vector<float>> &hw_emb, int sampling_rate)
   {
      funasr::OfflineStream* offline_stream = (funasr::OfflineStream*)handle;
      if (!offline_stream)
         return nullptr;
      
      funasr::Audio audio(1);
      if(funasr::is_target_file(sz_filename, "wav")){
         int32_t sampling_rate_ = -1;
         if(!audio.LoadWav(sz_filename, &sampling_rate_))
            return nullptr;
      }else if(funasr::is_target_file(sz_filename, "pcm")){
         if (!audio.LoadPcmwav(sz_filename, &sampling_rate))
            return nullptr;
      }else{
         if (!audio.FfmpegLoad(sz_filename))
            return nullptr;
      try{
         if(funasr::is_target_file(sz_filename, "wav")){
            int32_t sampling_rate_ = -1;
            if(!audio.LoadWav(sz_filename, &sampling_rate_))
               return nullptr;
         }else if(funasr::is_target_file(sz_filename, "pcm")){
            if (!audio.LoadPcmwav(sz_filename, &sampling_rate))
               return nullptr;
         }else{
            if (!audio.FfmpegLoad(sz_filename))
               return nullptr;
         }
      }catch (std::exception const &e)
      {
         LOG(ERROR)<<e.what();
         return nullptr;
      }
      funasr::FUNASR_RECOG_RESULT* p_result = new funasr::FUNASR_RECOG_RESULT;
      p_result->snippet_time = audio.GetTimeLen();
      if(p_result->snippet_time == 0){
@@ -307,24 +327,31 @@
      int n_step = 0;
      int n_total = audio.GetQueueSize();
      float start_time = 0.0;
      std::string cur_stamp = "[";
      while (audio.Fetch(buff, len, flag, start_time) > 0) {
         string msg = (offline_stream->asr_handle)->Forward(buff, len, true);
         string msg = (offline_stream->asr_handle)->Forward(buff, len, true, hw_emb);
         std::vector<std::string> msg_vec = funasr::split(msg, '|');
         if(msg_vec.size()==0){
            continue;
         }
         p_result->msg += msg_vec[0];
         //timestamp
         if(msg_vec.size() > 1){
            std::vector<std::string> msg_stamp = funasr::split(msg_vec[1], ',');
            std::string cur_stamp = "";
            for(int i=0; i<msg_stamp.size()-1; i+=2){
               float begin = std::stof(msg_stamp[i])+start_time;
               float end = std::stof(msg_stamp[i+1])+start_time;
               cur_stamp += "["+std::to_string(begin)+","+std::to_string(end)+"],";
               cur_stamp += "["+std::to_string((int)(1000*begin))+","+std::to_string((int)(1000*end))+"],";
            }
            p_result->stamp += cur_stamp;
         }
         n_step++;
         if (fn_callback)
            fn_callback(n_step, n_total);
      }
      if(cur_stamp != "["){
         cur_stamp.erase(cur_stamp.length() - 1);
         p_result->stamp += cur_stamp + "]";
      }
      if(offline_stream->UsePunc()){
         string punc_res = (offline_stream->punc_handle)->AddPunc((p_result->msg).c_str());
@@ -332,6 +359,14 @@
      }
   
      return p_result;
   }
   _FUNASRAPI const std::vector<std::vector<float>> CompileHotwordEmbedding(FUNASR_HANDLE handle, std::string &hotwords) {
      funasr::OfflineStream* offline_stream = (funasr::OfflineStream*)handle;
       std::vector<std::vector<float>> emb;
      if (!offline_stream)
         return emb;
      return (offline_stream->asr_handle)->CompileHotwordEmbedding(hotwords);
   }
   // APIs for 2pass-stream Infer
@@ -368,7 +403,7 @@
         // if (!audio->FfmpegLoad(sz_buf, n_len))
         //    return nullptr;
         LOG(ERROR) <<"Wrong wav_format: " << wav_format ;
         exit(-1);
         return nullptr;
      }
      funasr::FUNASR_RECOG_RESULT* p_result = new funasr::FUNASR_RECOG_RESULT;