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.

128 lines
3.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright (c) 2014 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/rms_level.h"
  11. #include <algorithm>
  12. #include <cmath>
  13. #include <numeric>
  14. #include "rtc_base/checks.h"
  15. namespace webrtc {
  16. namespace {
  17. static constexpr float kMaxSquaredLevel = 32768 * 32768;
  18. // kMinLevel is the level corresponding to kMinLevelDb, that is 10^(-127/10).
  19. static constexpr float kMinLevel = 1.995262314968883e-13f;
  20. // Calculates the normalized RMS value from a mean square value. The input
  21. // should be the sum of squared samples divided by the number of samples. The
  22. // value will be normalized to full range before computing the RMS, wich is
  23. // returned as a negated dBfs. That is, 0 is full amplitude while 127 is very
  24. // faint.
  25. int ComputeRms(float mean_square) {
  26. if (mean_square <= kMinLevel * kMaxSquaredLevel) {
  27. // Very faint; simply return the minimum value.
  28. return RmsLevel::kMinLevelDb;
  29. }
  30. // Normalize by the max level.
  31. const float mean_square_norm = mean_square / kMaxSquaredLevel;
  32. RTC_DCHECK_GT(mean_square_norm, kMinLevel);
  33. // 20log_10(x^0.5) = 10log_10(x)
  34. const float rms = 10.f * std::log10(mean_square_norm);
  35. RTC_DCHECK_LE(rms, 0.f);
  36. RTC_DCHECK_GT(rms, -RmsLevel::kMinLevelDb);
  37. // Return the negated value.
  38. return static_cast<int>(-rms + 0.5f);
  39. }
  40. } // namespace
  41. RmsLevel::RmsLevel() {
  42. Reset();
  43. }
  44. RmsLevel::~RmsLevel() = default;
  45. void RmsLevel::Reset() {
  46. sum_square_ = 0.f;
  47. sample_count_ = 0;
  48. max_sum_square_ = 0.f;
  49. block_size_ = absl::nullopt;
  50. }
  51. void RmsLevel::Analyze(rtc::ArrayView<const int16_t> data) {
  52. if (data.empty()) {
  53. return;
  54. }
  55. CheckBlockSize(data.size());
  56. const float sum_square =
  57. std::accumulate(data.begin(), data.end(), 0.f,
  58. [](float a, int16_t b) { return a + b * b; });
  59. RTC_DCHECK_GE(sum_square, 0.f);
  60. sum_square_ += sum_square;
  61. sample_count_ += data.size();
  62. max_sum_square_ = std::max(max_sum_square_, sum_square);
  63. }
  64. void RmsLevel::Analyze(rtc::ArrayView<const float> data) {
  65. if (data.empty()) {
  66. return;
  67. }
  68. CheckBlockSize(data.size());
  69. float sum_square = 0.f;
  70. for (float data_k : data) {
  71. int16_t tmp =
  72. static_cast<int16_t>(std::min(std::max(data_k, -32768.f), 32767.f));
  73. sum_square += tmp * tmp;
  74. }
  75. RTC_DCHECK_GE(sum_square, 0.f);
  76. sum_square_ += sum_square;
  77. sample_count_ += data.size();
  78. max_sum_square_ = std::max(max_sum_square_, sum_square);
  79. }
  80. void RmsLevel::AnalyzeMuted(size_t length) {
  81. CheckBlockSize(length);
  82. sample_count_ += length;
  83. }
  84. int RmsLevel::Average() {
  85. int rms = (sample_count_ == 0) ? RmsLevel::kMinLevelDb
  86. : ComputeRms(sum_square_ / sample_count_);
  87. Reset();
  88. return rms;
  89. }
  90. RmsLevel::Levels RmsLevel::AverageAndPeak() {
  91. // Note that block_size_ should by design always be non-empty when
  92. // sample_count_ != 0. Also, the * operator of absl::optional enforces this
  93. // with a DCHECK.
  94. Levels levels = (sample_count_ == 0)
  95. ? Levels{RmsLevel::kMinLevelDb, RmsLevel::kMinLevelDb}
  96. : Levels{ComputeRms(sum_square_ / sample_count_),
  97. ComputeRms(max_sum_square_ / *block_size_)};
  98. Reset();
  99. return levels;
  100. }
  101. void RmsLevel::CheckBlockSize(size_t block_size) {
  102. if (block_size_ != block_size) {
  103. Reset();
  104. block_size_ = block_size;
  105. }
  106. }
  107. } // namespace webrtc