libaudions.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // libaudions.cpp : Defines the exported functions for the DLL application.
  2. //
  3. //#include "stdafx.h"
  4. #include "libaudions.h"
  5. #include "signal_processing_library.h"
  6. //默认是10ms的帧长度
  7. #ifndef RVC_NS_AUDIO_FRAME_TIME
  8. #define RVC_NS_AUDIO_FRAME_TIME 10
  9. #endif
  10. AudioNsImpl::AudioNsImpl(audions_callback_t* pCallback)
  11. {
  12. memcpy(&m_nscallback, pCallback, sizeof(audions_callback_t));
  13. NsLog("AudioNsImpl construction.");
  14. m_NsHandle = NULL;
  15. if (0 == WebRtcNs_Create(&m_NsHandle)){
  16. NsLog("AudioNsImpl success.");
  17. }
  18. m_audio_sample_rate = 16000;
  19. m_audio_capture_peroid = 20;
  20. m_ns_policy = 2;
  21. }
  22. AudioNsImpl::~AudioNsImpl()
  23. {
  24. NsLog("AudioNsImpl Deconstruction.");
  25. if (NULL != m_NsHandle){
  26. int iRet = WebRtcNs_Free(m_NsHandle);
  27. if (0 == iRet){
  28. NsLog("WebRtcNs Free success.");
  29. }
  30. }
  31. }
  32. int AudioNsImpl::SetNsParams(uint32_t uSampleRate, uint32_t uAudioCapturePeroid, int iMode)
  33. {
  34. int iRet = -1;
  35. m_audio_sample_rate = uSampleRate;
  36. m_audio_capture_peroid = uAudioCapturePeroid;
  37. m_ns_policy = iMode;
  38. if (0 == WebRtcNs_Init(m_NsHandle, uSampleRate)){
  39. iRet = WebRtcNs_set_policy(m_NsHandle, iMode);
  40. if (0 == iRet){
  41. NsLog("WebRtcNs init and set policy success.");
  42. }
  43. else{
  44. NsLog("WebRtcNs set policy failed.");
  45. }
  46. }
  47. else{
  48. NsLog("WebRtcNs Init failed.");
  49. }
  50. NsLog("audio sample rate is %d, audio frame time is %dms, noise suppression policy is %d.",m_audio_sample_rate, m_audio_capture_peroid, m_ns_policy);
  51. return iRet;
  52. }
  53. int AudioNsImpl::NsProcess(char* pDst, uint32_t uDstLen, char* pSrc, uint32_t uSrcLen)
  54. {
  55. int iRet = -1;
  56. switch(m_audio_sample_rate)
  57. {
  58. case 8000:
  59. iRet = NsProcess8kAudioSampleRate(pDst, uDstLen, pSrc, uSrcLen);
  60. break;
  61. case 16000:
  62. iRet = NsProcess16kAudioSampleRate(pDst, uDstLen, pSrc, uSrcLen);
  63. break;
  64. case 32000:
  65. iRet = NsProcess32kAudioSampleRate(pDst, uDstLen, pSrc, uSrcLen);
  66. break;
  67. default:
  68. NsLog("Rvc_NsProcess not support audio sample rate.");
  69. break;
  70. }
  71. return iRet;
  72. }
  73. //8k采样率,采样间隔20ms,每次传过来音频大小为320字节,10ms缓冲区大小为160字节,WebRtcNs_Process每次处理10ms数据
  74. int AudioNsImpl::NsProcess8kAudioSampleRate(char* pDst, uint32_t uDstLen, char* pSrc, uint32_t uSrcLen)
  75. {
  76. int iRet = -1;
  77. if (NULL == pDst || pSrc == NULL){
  78. return iRet;
  79. }
  80. if ((320 == uDstLen) && (320 == uSrcLen)){
  81. for (int i = 0; i < m_audio_capture_peroid/RVC_NS_AUDIO_FRAME_TIME; i++){
  82. short shBufferIn[80] = {0};
  83. short shBufferOut[80] = {0};
  84. memcpy(shBufferIn, (char*)pSrc+i*80*sizeof(short), sizeof(short)*80);
  85. if (iRet = WebRtcNs_Process(m_NsHandle ,shBufferIn ,NULL ,shBufferOut , NULL)){
  86. NsLog("Noise_Suppression WebRtcNs_Process 8k input err!");
  87. }
  88. else{
  89. memcpy((char*)pDst+i*80*sizeof(short),shBufferOut,80*sizeof(short));
  90. }
  91. }
  92. }
  93. else if ((160 == uDstLen) && (160 == uSrcLen)){
  94. iRet = WebRtcNs_Process(m_NsHandle, (short*)pSrc, NULL, (short*)pDst, NULL);
  95. }
  96. return iRet;
  97. }
  98. //16k采样率,采样间隔20ms,每次传过来音频大小为640字节,10ms缓冲区大小为320字节,WebRtcNs_Process每次处理10ms数据
  99. int AudioNsImpl::NsProcess16kAudioSampleRate(char* pDst, uint32_t uDstLen, char* pSrc, uint32_t uSrcLen)
  100. {
  101. int iRet = -1;
  102. if (NULL == pDst || pSrc == NULL){
  103. return iRet;
  104. }
  105. if ((640 == uDstLen) && (640 == uSrcLen)){
  106. for (int i = 0; i < m_audio_capture_peroid/RVC_NS_AUDIO_FRAME_TIME; i++){
  107. short shBufferIn[160] = {0};
  108. short shBufferOut[160] = {0};
  109. memcpy(shBufferIn, (char*)pSrc+i*160*sizeof(short), sizeof(short)*160);
  110. if (iRet = WebRtcNs_Process(m_NsHandle ,shBufferIn ,NULL ,shBufferOut , NULL)){
  111. NsLog("Noise_Suppression WebRtcNs_Process 16k input err!");
  112. }
  113. else{
  114. memcpy((char*)pDst+i*160*sizeof(short),shBufferOut,160*sizeof(short));
  115. }
  116. }
  117. }
  118. return iRet;
  119. }
  120. //32k采样率,采样间隔20ms,每次传过来音频大小为1280字节,10ms缓冲区大小为640字节,WebRtcNs_Process每次处理10ms数据
  121. int AudioNsImpl::NsProcess32kAudioSampleRate(char* pDst, uint32_t uDstLen, char* pSrc, uint32_t uSrcLen)
  122. {
  123. int iRet = -1;
  124. if (NULL == pDst || pSrc == NULL){
  125. return iRet;
  126. }
  127. int filter_state1[6] = {0}, filter_state12[6] = {0};
  128. int Synthesis_state1[6] = {0}, Synthesis_state12[6] = {0};
  129. if ((1280 == uDstLen) && (1280 == uSrcLen)){
  130. for (int i = 0; i < m_audio_capture_peroid/RVC_NS_AUDIO_FRAME_TIME; i++){
  131. short shBufferIn[320] = {0};
  132. short shInL[160]={0},shInH[160]={0};
  133. short shOutL[160] ={0},shOutH[160] = {0};
  134. memcpy(shBufferIn, pSrc + i*320*sizeof(short), 320*sizeof(short));
  135. //首先需要使用滤波函数将音频数据分高低频,以高频和低频的方式传入降噪函数内部
  136. WebRtcSpl_AnalysisQMF(shBufferIn,320,shInL,shInH,filter_state1,filter_state12);
  137. //将需要降噪的数据以高频和低频传入对应接口,同时需要注意返回数据也是分高频和低频
  138. if (iRet == WebRtcNs_Process(m_NsHandle, shInL, shInH, shOutL, shOutH)){
  139. NsLog("Noise_Suppression WebRtcNs_Process 32k input err!");
  140. }
  141. else
  142. {
  143. short shBufferOut[320] = {0};
  144. //如果降噪成功,则根据降噪后高频和低频数据传入滤波接口,然后用将返回的数据写入文件
  145. WebRtcSpl_SynthesisQMF(shOutL,shOutH,160,shBufferOut,Synthesis_state1,Synthesis_state12);
  146. memcpy(pDst+i*320*sizeof(short),shBufferOut,320*sizeof(short));
  147. }
  148. }
  149. }
  150. return iRet;
  151. }
  152. void AudioNsImpl::NsLog(const char* fmt, ...)
  153. {
  154. if (m_nscallback.debug){
  155. va_list arg;
  156. va_start(arg, fmt);
  157. (*m_nscallback.debug)(m_nscallback.user_data, fmt, arg);
  158. va_end(arg);
  159. }
  160. }
  161. void AudioNsImpl::ReleaseObj()
  162. {
  163. NsLog("AudioNsImpl ReleaseObj.");
  164. delete this;
  165. }