background_noise.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2012 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. #ifndef MODULES_AUDIO_CODING_NETEQ_BACKGROUND_NOISE_H_
  11. #define MODULES_AUDIO_CODING_NETEQ_BACKGROUND_NOISE_H_
  12. #include <string.h> // size_t
  13. #include <memory>
  14. #include "api/array_view.h"
  15. #include "rtc_base/constructor_magic.h"
  16. namespace webrtc {
  17. // Forward declarations.
  18. class AudioMultiVector;
  19. class PostDecodeVad;
  20. // This class handles estimation of background noise parameters.
  21. class BackgroundNoise {
  22. public:
  23. // TODO(hlundin): For 48 kHz support, increase kMaxLpcOrder to 10.
  24. // Will work anyway, but probably sound a little worse.
  25. static constexpr size_t kMaxLpcOrder = 8; // 32000 / 8000 + 4.
  26. explicit BackgroundNoise(size_t num_channels);
  27. virtual ~BackgroundNoise();
  28. void Reset();
  29. // Updates the parameter estimates based on the signal currently in the
  30. // |sync_buffer|, and on the latest decision in |vad| if it is running.
  31. // Returns true if the filter parameters are updated.
  32. bool Update(const AudioMultiVector& sync_buffer, const PostDecodeVad& vad);
  33. // Generates background noise given a random vector and writes the output to
  34. // |buffer|.
  35. void GenerateBackgroundNoise(rtc::ArrayView<const int16_t> random_vector,
  36. size_t channel,
  37. int mute_slope,
  38. bool too_many_expands,
  39. size_t num_noise_samples,
  40. int16_t* buffer);
  41. // Returns |energy_| for |channel|.
  42. int32_t Energy(size_t channel) const;
  43. // Sets the value of |mute_factor_| for |channel| to |value|.
  44. void SetMuteFactor(size_t channel, int16_t value);
  45. // Returns |mute_factor_| for |channel|.
  46. int16_t MuteFactor(size_t channel) const;
  47. // Returns a pointer to |filter_| for |channel|.
  48. const int16_t* Filter(size_t channel) const;
  49. // Returns a pointer to |filter_state_| for |channel|.
  50. const int16_t* FilterState(size_t channel) const;
  51. // Copies |input| to the filter state. Will not copy more than |kMaxLpcOrder|
  52. // elements.
  53. void SetFilterState(size_t channel, rtc::ArrayView<const int16_t> input);
  54. // Returns |scale_| for |channel|.
  55. int16_t Scale(size_t channel) const;
  56. // Returns |scale_shift_| for |channel|.
  57. int16_t ScaleShift(size_t channel) const;
  58. // Accessors.
  59. bool initialized() const { return initialized_; }
  60. private:
  61. static const int kThresholdIncrement = 229; // 0.0035 in Q16.
  62. static const size_t kVecLen = 256;
  63. static const int kLogVecLen = 8; // log2(kVecLen).
  64. static const size_t kResidualLength = 64;
  65. static const int16_t kLogResidualLength = 6; // log2(kResidualLength)
  66. struct ChannelParameters {
  67. // Constructor.
  68. ChannelParameters() { Reset(); }
  69. void Reset() {
  70. energy = 2500;
  71. max_energy = 0;
  72. energy_update_threshold = 500000;
  73. low_energy_update_threshold = 0;
  74. memset(filter_state, 0, sizeof(filter_state));
  75. memset(filter, 0, sizeof(filter));
  76. filter[0] = 4096;
  77. mute_factor = 0;
  78. scale = 20000;
  79. scale_shift = 24;
  80. }
  81. int32_t energy;
  82. int32_t max_energy;
  83. int32_t energy_update_threshold;
  84. int32_t low_energy_update_threshold;
  85. int16_t filter_state[kMaxLpcOrder];
  86. int16_t filter[kMaxLpcOrder + 1];
  87. int16_t mute_factor;
  88. int16_t scale;
  89. int16_t scale_shift;
  90. };
  91. int32_t CalculateAutoCorrelation(const int16_t* signal,
  92. size_t length,
  93. int32_t* auto_correlation) const;
  94. // Increments the energy threshold by a factor 1 + |kThresholdIncrement|.
  95. void IncrementEnergyThreshold(size_t channel, int32_t sample_energy);
  96. // Updates the filter parameters.
  97. void SaveParameters(size_t channel,
  98. const int16_t* lpc_coefficients,
  99. const int16_t* filter_state,
  100. int32_t sample_energy,
  101. int32_t residual_energy);
  102. size_t num_channels_;
  103. std::unique_ptr<ChannelParameters[]> channel_parameters_;
  104. bool initialized_;
  105. RTC_DISALLOW_COPY_AND_ASSIGN(BackgroundNoise);
  106. };
  107. } // namespace webrtc
  108. #endif // MODULES_AUDIO_CODING_NETEQ_BACKGROUND_NOISE_H_