agc2_testing_common.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2018 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_PROCESSING_AGC2_AGC2_TESTING_COMMON_H_
  11. #define MODULES_AUDIO_PROCESSING_AGC2_AGC2_TESTING_COMMON_H_
  12. #include <math.h>
  13. #include <limits>
  14. #include <vector>
  15. #include "rtc_base/checks.h"
  16. namespace webrtc {
  17. namespace test {
  18. // Level Estimator test parameters.
  19. constexpr float kDecayMs = 500.f;
  20. // Limiter parameters.
  21. constexpr float kLimiterMaxInputLevelDbFs = 1.f;
  22. constexpr float kLimiterKneeSmoothnessDb = 1.f;
  23. constexpr float kLimiterCompressionRatio = 5.f;
  24. constexpr float kPi = 3.1415926536f;
  25. std::vector<double> LinSpace(const double l, const double r, size_t num_points);
  26. class SineGenerator {
  27. public:
  28. SineGenerator(float frequency, int rate)
  29. : frequency_(frequency), rate_(rate) {}
  30. float operator()() {
  31. x_radians_ += frequency_ / rate_ * 2 * kPi;
  32. if (x_radians_ > 2 * kPi) {
  33. x_radians_ -= 2 * kPi;
  34. }
  35. return 1000.f * sinf(x_radians_);
  36. }
  37. private:
  38. float frequency_;
  39. int rate_;
  40. float x_radians_ = 0.f;
  41. };
  42. class PulseGenerator {
  43. public:
  44. PulseGenerator(float frequency, int rate)
  45. : samples_period_(
  46. static_cast<int>(static_cast<float>(rate) / frequency)) {
  47. RTC_DCHECK_GT(rate, frequency);
  48. }
  49. float operator()() {
  50. sample_counter_++;
  51. if (sample_counter_ >= samples_period_) {
  52. sample_counter_ -= samples_period_;
  53. }
  54. return static_cast<float>(
  55. sample_counter_ == 0 ? std::numeric_limits<int16_t>::max() : 10.f);
  56. }
  57. private:
  58. int samples_period_;
  59. int sample_counter_ = 0;
  60. };
  61. } // namespace test
  62. } // namespace webrtc
  63. #endif // MODULES_AUDIO_PROCESSING_AGC2_AGC2_TESTING_COMMON_H_