librapidasrapi.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "precomp.h"
  2. #ifdef __cplusplus
  3. extern "C" {
  4. #endif
  5. // APIs for qmasr
  6. _RAPIDASRAPI RPASR_HANDLE RapidAsrInit(const char* szModelDir, int nThreadNum)
  7. {
  8. Model* mm = create_model(szModelDir, nThreadNum);
  9. return mm;
  10. }
  11. _RAPIDASRAPI RPASR_RESULT RapidAsrRecogBuffer(RPASR_HANDLE handle, const char* szBuf, int nLen, RPASR_MODE Mode, QM_CALLBACK fnCallback)
  12. {
  13. Model* pRecogObj = (Model*)handle;
  14. if (!pRecogObj)
  15. return nullptr;
  16. Audio audio(1);
  17. audio.loadwav(szBuf,nLen);
  18. audio.split();
  19. float* buff;
  20. int len;
  21. int flag=0;
  22. RPASR_RECOG_RESULT* pResult = new RPASR_RECOG_RESULT;
  23. pResult->snippet_time = audio.get_time_len();
  24. int nStep = 0;
  25. int nTotal = audio.get_queue_size();
  26. while (audio.fetch(buff, len, flag) > 0) {
  27. pRecogObj->reset();
  28. string msg = pRecogObj->forward(buff, len, flag);
  29. pResult->msg += msg;
  30. nStep++;
  31. if (fnCallback)
  32. fnCallback(nStep, nTotal);
  33. }
  34. return pResult;
  35. }
  36. _RAPIDASRAPI RPASR_RESULT RapidAsrRecogPCMBuffer(RPASR_HANDLE handle, const char* szBuf, int nLen, RPASR_MODE Mode, QM_CALLBACK fnCallback)
  37. {
  38. Model* pRecogObj = (Model*)handle;
  39. if (!pRecogObj)
  40. return nullptr;
  41. Audio audio(1);
  42. audio.loadpcmwav(szBuf, nLen);
  43. audio.split();
  44. float* buff;
  45. int len;
  46. int flag = 0;
  47. RPASR_RECOG_RESULT* pResult = new RPASR_RECOG_RESULT;
  48. pResult->snippet_time = audio.get_time_len();
  49. int nStep = 0;
  50. int nTotal = audio.get_queue_size();
  51. while (audio.fetch(buff, len, flag) > 0) {
  52. pRecogObj->reset();
  53. string msg = pRecogObj->forward(buff, len, flag);
  54. pResult->msg += msg;
  55. nStep++;
  56. if (fnCallback)
  57. fnCallback(nStep, nTotal);
  58. }
  59. return pResult;
  60. }
  61. _RAPIDASRAPI RPASR_RESULT RapidAsrRecogPCMFile(RPASR_HANDLE handle, const char* szFileName, RPASR_MODE Mode, QM_CALLBACK fnCallback)
  62. {
  63. Model* pRecogObj = (Model*)handle;
  64. if (!pRecogObj)
  65. return nullptr;
  66. Audio audio(1);
  67. audio.loadpcmwav(szFileName);
  68. audio.split();
  69. float* buff;
  70. int len;
  71. int flag = 0;
  72. RPASR_RECOG_RESULT* pResult = new RPASR_RECOG_RESULT;
  73. pResult->snippet_time = audio.get_time_len();
  74. int nStep = 0;
  75. int nTotal = audio.get_queue_size();
  76. while (audio.fetch(buff, len, flag) > 0) {
  77. pRecogObj->reset();
  78. string msg = pRecogObj->forward(buff, len, flag);
  79. pResult->msg += msg;
  80. nStep++;
  81. if (fnCallback)
  82. fnCallback(nStep, nTotal);
  83. }
  84. return pResult;
  85. }
  86. _RAPIDASRAPI RPASR_RESULT RapidAsrRecogFile(RPASR_HANDLE handle, const char* szWavfile, RPASR_MODE Mode, QM_CALLBACK fnCallback)
  87. {
  88. Model* pRecogObj = (Model*)handle;
  89. if (!pRecogObj)
  90. return nullptr;
  91. Audio audio(1);
  92. if(!audio.loadwav(szWavfile))
  93. return nullptr;
  94. audio.split();
  95. float* buff;
  96. int len;
  97. int flag = 0;
  98. int nStep = 0;
  99. int nTotal = audio.get_queue_size();
  100. RPASR_RECOG_RESULT* pResult = new RPASR_RECOG_RESULT;
  101. pResult->snippet_time = audio.get_time_len();
  102. while (audio.fetch(buff, len, flag) > 0) {
  103. pRecogObj->reset();
  104. string msg = pRecogObj->forward(buff, len, flag);
  105. pResult->msg+= msg;
  106. nStep++;
  107. if (fnCallback)
  108. fnCallback(nStep, nTotal);
  109. }
  110. return pResult;
  111. }
  112. _RAPIDASRAPI const int RapidAsrGetRetNumber(RPASR_RESULT Result)
  113. {
  114. if (!Result)
  115. return 0;
  116. return 1;
  117. }
  118. _RAPIDASRAPI const float RapidAsrGetRetSnippetTime(RPASR_RESULT Result)
  119. {
  120. if (!Result)
  121. return 0.0f;
  122. return ((RPASR_RECOG_RESULT*)Result)->snippet_time;
  123. }
  124. _RAPIDASRAPI const char* RapidAsrGetResult(RPASR_RESULT Result,int nIndex)
  125. {
  126. RPASR_RECOG_RESULT * pResult = (RPASR_RECOG_RESULT*)Result;
  127. if(!pResult)
  128. return nullptr;
  129. return pResult->msg.c_str();
  130. }
  131. _RAPIDASRAPI void RapidAsrFreeResult(RPASR_RESULT Result)
  132. {
  133. if (Result)
  134. {
  135. delete (RPASR_RECOG_RESULT*)Result;
  136. }
  137. }
  138. _RAPIDASRAPI void RapidAsrUninit(RPASR_HANDLE handle)
  139. {
  140. Model* pRecogObj = (Model*)handle;
  141. if (!pRecogObj)
  142. return;
  143. delete pRecogObj;
  144. }
  145. #ifdef __cplusplus
  146. }
  147. #endif