• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2017 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 
11 #include "modules/audio_mixer/gain_change_calculator.h"
12 
13 #include <math.h>
14 
15 #include <cstdlib>
16 #include <vector>
17 
18 #include "rtc_base/checks.h"
19 
20 namespace webrtc {
21 
22 namespace {
23 constexpr int16_t kReliabilityThreshold = 100;
24 }  // namespace
25 
CalculateGainChange(rtc::ArrayView<const int16_t> in,rtc::ArrayView<const int16_t> out)26 float GainChangeCalculator::CalculateGainChange(
27     rtc::ArrayView<const int16_t> in,
28     rtc::ArrayView<const int16_t> out) {
29   RTC_DCHECK_EQ(in.size(), out.size());
30 
31   std::vector<float> gain(in.size());
32   CalculateGain(in, out, gain);
33   return CalculateDifferences(gain);
34 }
35 
LatestGain() const36 float GainChangeCalculator::LatestGain() const {
37   return last_reliable_gain_;
38 }
39 
CalculateGain(rtc::ArrayView<const int16_t> in,rtc::ArrayView<const int16_t> out,rtc::ArrayView<float> gain)40 void GainChangeCalculator::CalculateGain(rtc::ArrayView<const int16_t> in,
41                                          rtc::ArrayView<const int16_t> out,
42                                          rtc::ArrayView<float> gain) {
43   RTC_DCHECK_EQ(in.size(), out.size());
44   RTC_DCHECK_EQ(in.size(), gain.size());
45 
46   for (size_t i = 0; i < in.size(); ++i) {
47     if (std::abs(in[i]) >= kReliabilityThreshold) {
48       last_reliable_gain_ = out[i] / static_cast<float>(in[i]);
49     }
50     gain[i] = last_reliable_gain_;
51   }
52 }
53 
CalculateDifferences(rtc::ArrayView<const float> values)54 float GainChangeCalculator::CalculateDifferences(
55     rtc::ArrayView<const float> values) {
56   float res = 0;
57   for (float f : values) {
58     res += fabs(f - last_value_);
59     last_value_ = f;
60   }
61   return res;
62 }
63 }  // namespace webrtc
64