libaudions.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // libaudions.cpp : Defines the exported functions for the DLL application.
  2. //
  3. #include "stdafx.h"
  4. #include "SpBase.h"
  5. #include "libaudions.h"
  6. #include "signal_processing_library.h"
  7. //默认是10ms的帧长度
  8. #ifndef RVC_NS_AUDIO_FRAME_TIME
  9. #define RVC_NS_AUDIO_FRAME_TIME 10
  10. #endif
  11. AudioNsImpl::AudioNsImpl()
  12. {
  13. Dbg("AudioNsImpl construction.");
  14. m_NsHandle = NULL;
  15. if (0 == WebRtcNs_Create(&m_NsHandle)){
  16. Dbg("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. Dbg("AudioNsImpl Deconstruction.");
  25. if (NULL != m_NsHandle){
  26. int iRet = WebRtcNs_Free(m_NsHandle);
  27. if (0 == iRet){
  28. Dbg("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. Dbg("WebRtcNs init and set policy success.");
  42. }
  43. else{
  44. Dbg("WebRtcNs set policy failed.");
  45. }
  46. }
  47. else{
  48. Dbg("WebRtcNs Init failed.");
  49. }
  50. Dbg("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. Dbg("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. Dbg("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. return iRet;
  94. }
  95. //16k采样率,采样间隔20ms,每次传过来音频大小为640字节,10ms缓冲区大小为320字节,WebRtcNs_Process每次处理10ms数据
  96. int AudioNsImpl::NsProcess16kAudioSampleRate(char* pDst, uint32_t uDstLen, char* pSrc, uint32_t uSrcLen)
  97. {
  98. int iRet = -1;
  99. if (NULL == pDst || pSrc == NULL){
  100. return iRet;
  101. }
  102. if ((640 == uDstLen) && (640 == uSrcLen)){
  103. for (int i = 0; i < m_audio_capture_peroid/RVC_NS_AUDIO_FRAME_TIME; i++){
  104. short shBufferIn[160] = {0};
  105. short shBufferOut[160] = {0};
  106. memcpy(shBufferIn, (char*)pSrc+i*160*sizeof(short), sizeof(short)*160);
  107. if (iRet = WebRtcNs_Process(m_NsHandle ,shBufferIn ,NULL ,shBufferOut , NULL)){
  108. Dbg("Noise_Suppression WebRtcNs_Process 16k input err!");
  109. }
  110. else{
  111. memcpy((char*)pDst+i*160*sizeof(short),shBufferOut,160*sizeof(short));
  112. }
  113. }
  114. }
  115. return iRet;
  116. }
  117. //32k采样率,采样间隔20ms,每次传过来音频大小为1280字节,10ms缓冲区大小为640字节,WebRtcNs_Process每次处理10ms数据
  118. int AudioNsImpl::NsProcess32kAudioSampleRate(char* pDst, uint32_t uDstLen, char* pSrc, uint32_t uSrcLen)
  119. {
  120. int iRet = -1;
  121. if (NULL == pDst || pSrc == NULL){
  122. return iRet;
  123. }
  124. int filter_state1[6] = {0}, filter_state12[6] = {0};
  125. int Synthesis_state1[6] = {0}, Synthesis_state12[6] = {0};
  126. if ((1280 == uDstLen) && (1280 == uSrcLen)){
  127. for (int i = 0; i < m_audio_capture_peroid/RVC_NS_AUDIO_FRAME_TIME; i++){
  128. short shBufferIn[320] = {0};
  129. short shInL[160]={0},shInH[160]={0};
  130. short shOutL[160] ={0},shOutH[160] = {0};
  131. memcpy(shBufferIn, pSrc + i*320*sizeof(short), 320*sizeof(short));
  132. //首先需要使用滤波函数将音频数据分高低频,以高频和低频的方式传入降噪函数内部
  133. WebRtcSpl_AnalysisQMF(shBufferIn,320,shInL,shInH,filter_state1,filter_state12);
  134. //将需要降噪的数据以高频和低频传入对应接口,同时需要注意返回数据也是分高频和低频
  135. if (iRet == WebRtcNs_Process(m_NsHandle, shInL, shInH, shOutL, shOutH)){
  136. Dbg("Noise_Suppression WebRtcNs_Process 32k input err!");
  137. }
  138. else
  139. {
  140. short shBufferOut[320] = {0};
  141. //如果降噪成功,则根据降噪后高频和低频数据传入滤波接口,然后用将返回的数据写入文件
  142. WebRtcSpl_SynthesisQMF(shOutL,shOutH,160,shBufferOut,Synthesis_state1,Synthesis_state12);
  143. memcpy(pDst+i*320*sizeof(short),shBufferOut,320*sizeof(short));
  144. }
  145. }
  146. }
  147. return iRet;
  148. }
  149. void AudioNsImpl::ReleaseObj()
  150. {
  151. Dbg("AudioNsImpl ReleaseObj.");
  152. delete this;
  153. }