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.

104 lines
2.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #include "modules/audio_processing/agc/agc.h"
  11. #include <cmath>
  12. #include <cstdlib>
  13. #include <vector>
  14. #include "modules/audio_processing/agc/loudness_histogram.h"
  15. #include "modules/audio_processing/agc/utility.h"
  16. #include "rtc_base/checks.h"
  17. namespace webrtc {
  18. namespace {
  19. const int kDefaultLevelDbfs = -18;
  20. const int kNumAnalysisFrames = 100;
  21. const double kActivityThreshold = 0.3;
  22. } // namespace
  23. Agc::Agc()
  24. : target_level_loudness_(Dbfs2Loudness(kDefaultLevelDbfs)),
  25. target_level_dbfs_(kDefaultLevelDbfs),
  26. histogram_(LoudnessHistogram::Create(kNumAnalysisFrames)),
  27. inactive_histogram_(LoudnessHistogram::Create()) {}
  28. Agc::~Agc() {}
  29. float Agc::AnalyzePreproc(const int16_t* audio, size_t length) {
  30. RTC_DCHECK_GT(length, 0);
  31. size_t num_clipped = 0;
  32. for (size_t i = 0; i < length; ++i) {
  33. if (audio[i] == 32767 || audio[i] == -32768)
  34. ++num_clipped;
  35. }
  36. return 1.0f * num_clipped / length;
  37. }
  38. void Agc::Process(const int16_t* audio, size_t length, int sample_rate_hz) {
  39. vad_.ProcessChunk(audio, length, sample_rate_hz);
  40. const std::vector<double>& rms = vad_.chunkwise_rms();
  41. const std::vector<double>& probabilities =
  42. vad_.chunkwise_voice_probabilities();
  43. RTC_DCHECK_EQ(rms.size(), probabilities.size());
  44. for (size_t i = 0; i < rms.size(); ++i) {
  45. histogram_->Update(rms[i], probabilities[i]);
  46. }
  47. }
  48. bool Agc::GetRmsErrorDb(int* error) {
  49. if (!error) {
  50. RTC_NOTREACHED();
  51. return false;
  52. }
  53. if (histogram_->num_updates() < kNumAnalysisFrames) {
  54. // We haven't yet received enough frames.
  55. return false;
  56. }
  57. if (histogram_->AudioContent() < kNumAnalysisFrames * kActivityThreshold) {
  58. // We are likely in an inactive segment.
  59. return false;
  60. }
  61. double loudness = Linear2Loudness(histogram_->CurrentRms());
  62. *error = std::floor(Loudness2Db(target_level_loudness_ - loudness) + 0.5);
  63. histogram_->Reset();
  64. return true;
  65. }
  66. void Agc::Reset() {
  67. histogram_->Reset();
  68. }
  69. int Agc::set_target_level_dbfs(int level) {
  70. // TODO(turajs): just some arbitrary sanity check. We can come up with better
  71. // limits. The upper limit should be chosen such that the risk of clipping is
  72. // low. The lower limit should not result in a too quiet signal.
  73. if (level >= 0 || level <= -100)
  74. return -1;
  75. target_level_dbfs_ = level;
  76. target_level_loudness_ = Dbfs2Loudness(level);
  77. return 0;
  78. }
  79. int Agc::target_level_dbfs() const {
  80. return target_level_dbfs_;
  81. }
  82. float Agc::voice_probability() const {
  83. return vad_.last_voice_probability();
  84. }
  85. } // namespace webrtc