gain_change_calculator.cc (1814B)
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 <cmath> 14 #include <cstdint> 15 #include <cstdlib> 16 #include <vector> 17 18 #include "api/array_view.h" 19 #include "rtc_base/checks.h" 20 21 namespace webrtc { 22 23 namespace { 24 constexpr int16_t kReliabilityThreshold = 100; 25 } // namespace 26 27 float GainChangeCalculator::CalculateGainChange(ArrayView<const int16_t> in, 28 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 36 float GainChangeCalculator::LatestGain() const { 37 return last_reliable_gain_; 38 } 39 40 void GainChangeCalculator::CalculateGain(ArrayView<const int16_t> in, 41 ArrayView<const int16_t> out, 42 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 54 float GainChangeCalculator::CalculateDifferences( 55 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