funasrruntime.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #pragma once
  2. #include <map>
  3. #include <vector>
  4. #include <unordered_map>
  5. #ifdef WIN32
  6. #ifdef _FUNASR_API_EXPORT
  7. #define _FUNASRAPI __declspec(dllexport)
  8. #else
  9. #define _FUNASRAPI __declspec(dllimport)
  10. #endif
  11. #else
  12. #define _FUNASRAPI
  13. #endif
  14. #ifndef _WIN32
  15. #define FUNASR_CALLBCK_PREFIX __attribute__((__stdcall__))
  16. #else
  17. #define FUNASR_CALLBCK_PREFIX __stdcall
  18. #endif
  19. typedef void* FUNASR_HANDLE;
  20. typedef void* FUNASR_RESULT;
  21. typedef void* FUNASR_DEC_HANDLE;
  22. typedef unsigned char FUNASR_BOOL;
  23. #define FUNASR_TRUE 1
  24. #define FUNASR_FALSE 0
  25. #define QM_DEFAULT_THREAD_NUM 4
  26. typedef enum
  27. {
  28. RASR_NONE=-1,
  29. RASRM_CTC_GREEDY_SEARCH=0,
  30. RASRM_CTC_RPEFIX_BEAM_SEARCH = 1,
  31. RASRM_ATTENSION_RESCORING = 2,
  32. }FUNASR_MODE;
  33. typedef enum {
  34. FUNASR_MODEL_PADDLE = 0,
  35. FUNASR_MODEL_PADDLE_2 = 1,
  36. FUNASR_MODEL_K2 = 2,
  37. FUNASR_MODEL_PARAFORMER = 3,
  38. }FUNASR_MODEL_TYPE;
  39. typedef enum {
  40. ASR_OFFLINE=0,
  41. ASR_ONLINE=1,
  42. ASR_TWO_PASS=2,
  43. }ASR_TYPE;
  44. typedef enum {
  45. PUNC_OFFLINE=0,
  46. PUNC_ONLINE=1,
  47. }PUNC_TYPE;
  48. typedef void (* QM_CALLBACK)(int cur_step, int n_total); // n_total: total steps; cur_step: Current Step.
  49. // ASR
  50. _FUNASRAPI FUNASR_HANDLE FunASRInit(std::map<std::string, std::string>& model_path, int thread_num, ASR_TYPE type=ASR_OFFLINE);
  51. _FUNASRAPI FUNASR_HANDLE FunASROnlineInit(FUNASR_HANDLE asr_handle, std::vector<int> chunk_size={5,10,5});
  52. _FUNASRAPI void FunASRReset(FUNASR_HANDLE handle, FUNASR_DEC_HANDLE dec_handle=nullptr);
  53. // buffer
  54. _FUNASRAPI FUNASR_RESULT FunASRInferBuffer(FUNASR_HANDLE handle, const char* sz_buf, int n_len, FUNASR_MODE mode, QM_CALLBACK fn_callback, bool input_finished=true, int sampling_rate=16000, std::string wav_format="pcm");
  55. // file, support wav & pcm
  56. _FUNASRAPI FUNASR_RESULT FunASRInfer(FUNASR_HANDLE handle, const char* sz_filename, FUNASR_MODE mode, QM_CALLBACK fn_callback, int sampling_rate=16000);
  57. _FUNASRAPI const char* FunASRGetResult(FUNASR_RESULT result,int n_index);
  58. _FUNASRAPI const char* FunASRGetStamp(FUNASR_RESULT result);
  59. _FUNASRAPI const char* FunASRGetTpassResult(FUNASR_RESULT result,int n_index);
  60. _FUNASRAPI const int FunASRGetRetNumber(FUNASR_RESULT result);
  61. _FUNASRAPI void FunASRFreeResult(FUNASR_RESULT result);
  62. _FUNASRAPI void FunASRUninit(FUNASR_HANDLE handle);
  63. _FUNASRAPI const float FunASRGetRetSnippetTime(FUNASR_RESULT result);
  64. // VAD
  65. _FUNASRAPI FUNASR_HANDLE FsmnVadInit(std::map<std::string, std::string>& model_path, int thread_num);
  66. _FUNASRAPI FUNASR_HANDLE FsmnVadOnlineInit(FUNASR_HANDLE fsmnvad_handle);
  67. // buffer
  68. _FUNASRAPI FUNASR_RESULT FsmnVadInferBuffer(FUNASR_HANDLE handle, const char* sz_buf, int n_len, QM_CALLBACK fn_callback, bool input_finished=true, int sampling_rate=16000, std::string wav_format="pcm");
  69. // file, support wav & pcm
  70. _FUNASRAPI FUNASR_RESULT FsmnVadInfer(FUNASR_HANDLE handle, const char* sz_filename, QM_CALLBACK fn_callback, int sampling_rate=16000);
  71. _FUNASRAPI std::vector<std::vector<int>>* FsmnVadGetResult(FUNASR_RESULT result,int n_index);
  72. _FUNASRAPI void FsmnVadFreeResult(FUNASR_RESULT result);
  73. _FUNASRAPI void FsmnVadUninit(FUNASR_HANDLE handle);
  74. _FUNASRAPI const float FsmnVadGetRetSnippetTime(FUNASR_RESULT result);
  75. // PUNC
  76. _FUNASRAPI FUNASR_HANDLE CTTransformerInit(std::map<std::string, std::string>& model_path, int thread_num, PUNC_TYPE type=PUNC_OFFLINE);
  77. _FUNASRAPI FUNASR_RESULT CTTransformerInfer(FUNASR_HANDLE handle, const char* sz_sentence, FUNASR_MODE mode, QM_CALLBACK fn_callback, PUNC_TYPE type=PUNC_OFFLINE, FUNASR_RESULT pre_result=nullptr);
  78. _FUNASRAPI const char* CTTransformerGetResult(FUNASR_RESULT result,int n_index);
  79. _FUNASRAPI void CTTransformerFreeResult(FUNASR_RESULT result);
  80. _FUNASRAPI void CTTransformerUninit(FUNASR_HANDLE handle);
  81. //OfflineStream
  82. _FUNASRAPI FUNASR_HANDLE FunOfflineInit(std::map<std::string, std::string>& model_path, int thread_num);
  83. _FUNASRAPI void FunOfflineReset(FUNASR_HANDLE handle, FUNASR_DEC_HANDLE dec_handle=nullptr);
  84. // buffer
  85. _FUNASRAPI FUNASR_RESULT FunOfflineInferBuffer(FUNASR_HANDLE handle, const char* sz_buf, int n_len,
  86. FUNASR_MODE mode, QM_CALLBACK fn_callback, const std::vector<std::vector<float>> &hw_emb,
  87. int sampling_rate=16000, std::string wav_format="pcm", bool itn=true, FUNASR_DEC_HANDLE dec_handle=nullptr);
  88. // file, support wav & pcm
  89. _FUNASRAPI FUNASR_RESULT FunOfflineInfer(FUNASR_HANDLE handle, const char* sz_filename, FUNASR_MODE mode,
  90. QM_CALLBACK fn_callback, const std::vector<std::vector<float>> &hw_emb,
  91. int sampling_rate=16000, bool itn=true, FUNASR_DEC_HANDLE dec_handle=nullptr);
  92. #if !defined(__APPLE__)
  93. _FUNASRAPI const std::vector<std::vector<float>> CompileHotwordEmbedding(FUNASR_HANDLE handle, std::string &hotwords, ASR_TYPE mode=ASR_OFFLINE);
  94. #endif
  95. _FUNASRAPI void FunOfflineUninit(FUNASR_HANDLE handle);
  96. //2passStream
  97. _FUNASRAPI FUNASR_HANDLE FunTpassInit(std::map<std::string, std::string>& model_path, int thread_num);
  98. _FUNASRAPI FUNASR_HANDLE FunTpassOnlineInit(FUNASR_HANDLE tpass_handle, std::vector<int> chunk_size={5,10,5});
  99. // buffer
  100. _FUNASRAPI FUNASR_RESULT FunTpassInferBuffer(FUNASR_HANDLE handle, FUNASR_HANDLE online_handle, const char* sz_buf,
  101. int n_len, std::vector<std::vector<std::string>> &punc_cache, bool input_finished=true,
  102. int sampling_rate=16000, std::string wav_format="pcm", ASR_TYPE mode=ASR_TWO_PASS,
  103. const std::vector<std::vector<float>> &hw_emb={{0.0}}, bool itn=true);
  104. _FUNASRAPI void FunTpassUninit(FUNASR_HANDLE handle);
  105. _FUNASRAPI void FunTpassOnlineUninit(FUNASR_HANDLE handle);
  106. // wfst decoder
  107. _FUNASRAPI FUNASR_DEC_HANDLE FunASRWfstDecoderInit(FUNASR_HANDLE handle, int asr_type, float glob_beam, float lat_beam, float am_scale);
  108. _FUNASRAPI void FunASRWfstDecoderUninit(FUNASR_DEC_HANDLE handle);
  109. _FUNASRAPI void FunWfstDecoderLoadHwsRes(FUNASR_DEC_HANDLE handle, int inc_bias, std::unordered_map<std::string, int> &hws_map);
  110. _FUNASRAPI void FunWfstDecoderUnloadHwsRes(FUNASR_DEC_HANDLE handle);