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.

98 lines
3.1 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 "audio/audio_level.h"
  11. #include "api/audio/audio_frame.h"
  12. #include "common_audio/signal_processing/include/signal_processing_library.h"
  13. namespace webrtc {
  14. namespace voe {
  15. AudioLevel::AudioLevel()
  16. : abs_max_(0), count_(0), current_level_full_range_(0) {}
  17. AudioLevel::~AudioLevel() {}
  18. void AudioLevel::Reset() {
  19. rtc::CritScope cs(&crit_sect_);
  20. abs_max_ = 0;
  21. count_ = 0;
  22. current_level_full_range_ = 0;
  23. total_energy_ = 0.0;
  24. total_duration_ = 0.0;
  25. }
  26. int16_t AudioLevel::LevelFullRange() const {
  27. rtc::CritScope cs(&crit_sect_);
  28. return current_level_full_range_;
  29. }
  30. void AudioLevel::ResetLevelFullRange() {
  31. rtc::CritScope cs(&crit_sect_);
  32. abs_max_ = 0;
  33. count_ = 0;
  34. current_level_full_range_ = 0;
  35. }
  36. double AudioLevel::TotalEnergy() const {
  37. rtc::CritScope cs(&crit_sect_);
  38. return total_energy_;
  39. }
  40. double AudioLevel::TotalDuration() const {
  41. rtc::CritScope cs(&crit_sect_);
  42. return total_duration_;
  43. }
  44. void AudioLevel::ComputeLevel(const AudioFrame& audioFrame, double duration) {
  45. // Check speech level (works for 2 channels as well)
  46. int16_t abs_value =
  47. audioFrame.muted()
  48. ? 0
  49. : WebRtcSpl_MaxAbsValueW16(
  50. audioFrame.data(),
  51. audioFrame.samples_per_channel_ * audioFrame.num_channels_);
  52. // Protect member access using a lock since this method is called on a
  53. // dedicated audio thread in the RecordedDataIsAvailable() callback.
  54. rtc::CritScope cs(&crit_sect_);
  55. if (abs_value > abs_max_)
  56. abs_max_ = abs_value;
  57. // Update level approximately 9 times per second, assuming audio frame
  58. // duration is approximately 10 ms. (The update frequency is every
  59. // 11th (= |kUpdateFrequency+1|) call: 1000/(11*10)=9.09..., we should
  60. // probably change this behavior, see https://crbug.com/webrtc/10784).
  61. if (count_++ == kUpdateFrequency) {
  62. current_level_full_range_ = abs_max_;
  63. count_ = 0;
  64. // Decay the absolute maximum (divide by 4)
  65. abs_max_ >>= 2;
  66. }
  67. // See the description for "totalAudioEnergy" in the WebRTC stats spec
  68. // (https://w3c.github.io/webrtc-stats/#dom-rtcmediastreamtrackstats-totalaudioenergy)
  69. // for an explanation of these formulas. In short, we need a value that can
  70. // be used to compute RMS audio levels over different time intervals, by
  71. // taking the difference between the results from two getStats calls. To do
  72. // this, the value needs to be of units "squared sample value * time".
  73. double additional_energy =
  74. static_cast<double>(current_level_full_range_) / INT16_MAX;
  75. additional_energy *= additional_energy;
  76. total_energy_ += additional_energy * duration;
  77. total_duration_ += duration;
  78. }
  79. } // namespace voe
  80. } // namespace webrtc