You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "modules/audio_processing/noise_suppression.h"
  11. #include "modules/audio_processing/audio_buffer.h"
  12. #include "rtc_base/checks.h"
  13. #if defined(WEBRTC_NS_FLOAT)
  14. #include "modules/audio_processing/ns/noise_suppression.h"
  15. #define NS_CREATE WebRtcNs_Create
  16. #define NS_FREE WebRtcNs_Free
  17. #define NS_INIT WebRtcNs_Init
  18. #define NS_SET_POLICY WebRtcNs_set_policy
  19. typedef NsHandle NsState;
  20. #elif defined(WEBRTC_NS_FIXED)
  21. #include "modules/audio_processing/ns/noise_suppression_x.h"
  22. #define NS_CREATE WebRtcNsx_Create
  23. #define NS_FREE WebRtcNsx_Free
  24. #define NS_INIT WebRtcNsx_Init
  25. #define NS_SET_POLICY WebRtcNsx_set_policy
  26. typedef NsxHandle NsState;
  27. #endif
  28. namespace webrtc {
  29. namespace {
  30. int NoiseSuppressionLevelToPolicy(NoiseSuppression::Level level) {
  31. switch (level) {
  32. case NoiseSuppression::Level::kLow:
  33. return 0;
  34. case NoiseSuppression::Level::kModerate:
  35. return 1;
  36. case NoiseSuppression::Level::kHigh:
  37. return 2;
  38. case NoiseSuppression::Level::kVeryHigh:
  39. return 3;
  40. default:
  41. RTC_NOTREACHED();
  42. }
  43. return 1;
  44. }
  45. } // namespace
  46. class NoiseSuppression::Suppressor {
  47. public:
  48. explicit Suppressor(int sample_rate_hz) {
  49. state_ = NS_CREATE();
  50. RTC_CHECK(state_);
  51. int error = NS_INIT(state_, sample_rate_hz);
  52. RTC_DCHECK_EQ(0, error);
  53. }
  54. ~Suppressor() { NS_FREE(state_); }
  55. Suppressor(Suppressor&) = delete;
  56. Suppressor& operator=(Suppressor&) = delete;
  57. NsState* state() { return state_; }
  58. private:
  59. NsState* state_ = nullptr;
  60. };
  61. NoiseSuppression::NoiseSuppression(size_t channels,
  62. int sample_rate_hz,
  63. Level level) {
  64. const int policy = NoiseSuppressionLevelToPolicy(level);
  65. for (size_t i = 0; i < channels; ++i) {
  66. suppressors_.push_back(std::make_unique<Suppressor>(sample_rate_hz));
  67. int error = NS_SET_POLICY(suppressors_[i]->state(), policy);
  68. RTC_DCHECK_EQ(0, error);
  69. }
  70. }
  71. NoiseSuppression::~NoiseSuppression() {}
  72. void NoiseSuppression::AnalyzeCaptureAudio(AudioBuffer* audio) {
  73. RTC_DCHECK(audio);
  74. #if defined(WEBRTC_NS_FLOAT)
  75. RTC_DCHECK_GE(160, audio->num_frames_per_band());
  76. RTC_DCHECK_EQ(suppressors_.size(), audio->num_channels());
  77. for (size_t i = 0; i < suppressors_.size(); i++) {
  78. WebRtcNs_Analyze(suppressors_[i]->state(),
  79. audio->split_bands_const(i)[kBand0To8kHz]);
  80. }
  81. #endif
  82. }
  83. void NoiseSuppression::ProcessCaptureAudio(AudioBuffer* audio) {
  84. RTC_DCHECK(audio);
  85. RTC_DCHECK_GE(160, audio->num_frames_per_band());
  86. RTC_DCHECK_EQ(suppressors_.size(), audio->num_channels());
  87. for (size_t i = 0; i < suppressors_.size(); i++) {
  88. #if defined(WEBRTC_NS_FLOAT)
  89. WebRtcNs_Process(suppressors_[i]->state(), audio->split_bands_const(i),
  90. audio->num_bands(), audio->split_bands(i));
  91. #elif defined(WEBRTC_NS_FIXED)
  92. int16_t split_band_data[AudioBuffer::kMaxNumBands]
  93. [AudioBuffer::kMaxSplitFrameLength];
  94. int16_t* split_bands[AudioBuffer::kMaxNumBands] = {
  95. split_band_data[0], split_band_data[1], split_band_data[2]};
  96. audio->ExportSplitChannelData(i, split_bands);
  97. WebRtcNsx_Process(suppressors_[i]->state(), split_bands, audio->num_bands(),
  98. split_bands);
  99. audio->ImportSplitChannelData(i, split_bands);
  100. #endif
  101. }
  102. }
  103. float NoiseSuppression::speech_probability() const {
  104. #if defined(WEBRTC_NS_FLOAT)
  105. float probability_average = 0.0f;
  106. for (auto& suppressor : suppressors_) {
  107. probability_average +=
  108. WebRtcNs_prior_speech_probability(suppressor->state());
  109. }
  110. if (!suppressors_.empty()) {
  111. probability_average /= suppressors_.size();
  112. }
  113. return probability_average;
  114. #elif defined(WEBRTC_NS_FIXED)
  115. // TODO(peah): Returning error code as a float! Remove this.
  116. // Currently not available for the fixed point implementation.
  117. return AudioProcessing::kUnsupportedFunctionError;
  118. #endif
  119. }
  120. std::vector<float> NoiseSuppression::NoiseEstimate() {
  121. std::vector<float> noise_estimate;
  122. #if defined(WEBRTC_NS_FLOAT)
  123. const float kNumChannelsFraction = 1.f / suppressors_.size();
  124. noise_estimate.assign(WebRtcNs_num_freq(), 0.f);
  125. for (auto& suppressor : suppressors_) {
  126. const float* noise = WebRtcNs_noise_estimate(suppressor->state());
  127. for (size_t i = 0; i < noise_estimate.size(); ++i) {
  128. noise_estimate[i] += kNumChannelsFraction * noise[i];
  129. }
  130. }
  131. #elif defined(WEBRTC_NS_FIXED)
  132. noise_estimate.assign(WebRtcNsx_num_freq(), 0.f);
  133. for (auto& suppressor : suppressors_) {
  134. int q_noise;
  135. const uint32_t* noise =
  136. WebRtcNsx_noise_estimate(suppressor->state(), &q_noise);
  137. const float kNormalizationFactor =
  138. 1.f / ((1 << q_noise) * suppressors_.size());
  139. for (size_t i = 0; i < noise_estimate.size(); ++i) {
  140. noise_estimate[i] += kNormalizationFactor * noise[i];
  141. }
  142. }
  143. #endif
  144. return noise_estimate;
  145. }
  146. size_t NoiseSuppression::num_noise_bins() {
  147. #if defined(WEBRTC_NS_FLOAT)
  148. return WebRtcNs_num_freq();
  149. #elif defined(WEBRTC_NS_FIXED)
  150. return WebRtcNsx_num_freq();
  151. #endif
  152. }
  153. } // namespace webrtc