audio_power_monitor.cc 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright 2013 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "media/base/audio_power_monitor.h"
  5. #include <algorithm>
  6. #include <cmath>
  7. #include "base/check_op.h"
  8. #include "base/cxx17_backports.h"
  9. #include "base/time/time.h"
  10. #include "media/base/audio_bus.h"
  11. #include "media/base/vector_math.h"
  12. namespace media {
  13. AudioPowerMonitor::AudioPowerMonitor(int sample_rate,
  14. base::TimeDelta time_constant)
  15. : sample_weight_(1.0f -
  16. expf(-1.0f / (sample_rate * time_constant.InSecondsF()))) {
  17. Reset();
  18. }
  19. AudioPowerMonitor::~AudioPowerMonitor() = default;
  20. void AudioPowerMonitor::Reset() {
  21. // These are only read/written by Scan(), but Scan() should not be running
  22. // when Reset() is called.
  23. average_power_ = 0.0f;
  24. has_clipped_ = false;
  25. // These are the copies read by ReadCurrentPowerAndClip(). The lock here is
  26. // not necessary, as racey writes/reads are acceptable, but this prevents
  27. // quality-enhancement tools like TSAN from complaining.
  28. base::AutoLock for_reset(reading_lock_);
  29. power_reading_ = 0.0f;
  30. clipped_reading_ = false;
  31. }
  32. void AudioPowerMonitor::Scan(const AudioBus& buffer, int num_frames) {
  33. DCHECK_LE(num_frames, buffer.frames());
  34. const int num_channels = buffer.channels();
  35. if (num_frames <= 0 || num_channels <= 0)
  36. return;
  37. // Calculate a new average power by applying a first-order low-pass filter
  38. // (a.k.a. an exponentially-weighted moving average) over the audio samples in
  39. // each channel in |buffer|.
  40. float sum_power = 0.0f;
  41. for (int i = 0; i < num_channels; ++i) {
  42. const std::pair<float, float> ewma_and_max = vector_math::EWMAAndMaxPower(
  43. average_power_, buffer.channel(i), num_frames, sample_weight_);
  44. // If data in audio buffer is garbage, ignore its effect on the result.
  45. if (!std::isfinite(ewma_and_max.first)) {
  46. sum_power += average_power_;
  47. } else {
  48. sum_power += ewma_and_max.first;
  49. has_clipped_ |= (ewma_and_max.second > 1.0f);
  50. }
  51. }
  52. // Update accumulated results, with clamping for sanity.
  53. average_power_ = base::clamp(sum_power / num_channels, 0.0f, 1.0f);
  54. // Push results for reading by other threads, non-blocking.
  55. if (reading_lock_.Try()) {
  56. power_reading_ = average_power_;
  57. if (has_clipped_) {
  58. clipped_reading_ = true;
  59. has_clipped_ = false;
  60. }
  61. reading_lock_.Release();
  62. }
  63. }
  64. std::pair<float, bool> AudioPowerMonitor::ReadCurrentPowerAndClip() {
  65. base::AutoLock for_reading(reading_lock_);
  66. // Convert power level to dBFS units, and pin it down to zero if it is
  67. // insignificantly small.
  68. const float kInsignificantPower = 1.0e-10f; // -100 dBFS
  69. const float power_dbfs = power_reading_ < kInsignificantPower
  70. ? zero_power()
  71. : 10.0f * log10f(power_reading_);
  72. const bool clipped = clipped_reading_;
  73. clipped_reading_ = false;
  74. return std::make_pair(power_dbfs, clipped);
  75. }
  76. } // namespace media