aleloi | 2c9306e | 2017-03-29 04:25:16 -0700 | [diff] [blame] | 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 "webrtc/modules/audio_mixer/gain_change_calculator.h" |
| 12 | |
| 13 | #include <math.h> |
| 14 | #include <vector> |
| 15 | |
| 16 | namespace webrtc { |
| 17 | |
| 18 | namespace { |
| 19 | constexpr int16_t kReliabilityThreshold = 100; |
| 20 | } // namespace |
| 21 | |
| 22 | float GainChangeCalculator::CalculateGainChange( |
| 23 | rtc::ArrayView<const int16_t> in, |
| 24 | rtc::ArrayView<const int16_t> out) { |
| 25 | RTC_DCHECK_EQ(in.size(), out.size()); |
| 26 | |
| 27 | std::vector<float> gain(in.size()); |
| 28 | CalculateGain(in, out, gain); |
| 29 | return CalculateDifferences(gain); |
| 30 | } |
| 31 | |
| 32 | void GainChangeCalculator::CalculateGain(rtc::ArrayView<const int16_t> in, |
| 33 | rtc::ArrayView<const int16_t> out, |
| 34 | rtc::ArrayView<float> gain) { |
| 35 | RTC_DCHECK_EQ(in.size(), out.size()); |
| 36 | RTC_DCHECK_EQ(in.size(), gain.size()); |
| 37 | |
| 38 | for (size_t i = 0; i < in.size(); ++i) { |
| 39 | if (std::abs(in[i]) >= kReliabilityThreshold) { |
| 40 | last_reliable_gain_ = out[i] / static_cast<float>(in[i]); |
| 41 | } |
| 42 | gain[i] = last_reliable_gain_; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | float GainChangeCalculator::CalculateDifferences( |
| 47 | rtc::ArrayView<const float> values) { |
| 48 | float res = 0; |
| 49 | for (float f : values) { |
| 50 | res += fabs(f - last_value_); |
| 51 | last_value_ = f; |
| 52 | } |
| 53 | return res; |
| 54 | } |
| 55 | } // namespace webrtc |