OLD | NEW |
---|---|
(Empty) | |
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 <cmath> | |
12 #include <numeric> | |
13 #include <vector> | |
14 | |
15 #include "webrtc/base/logging.h" | |
16 #include "webrtc/base/safe_conversions.h" | |
17 #include "webrtc/modules/audio_mixer/frame_combiner_test_tools.h" | |
18 | |
19 namespace webrtc { | |
20 | |
21 namespace { | |
22 constexpr float kPi = 3.14159265; | |
23 constexpr int16_t kReliabilityThreshold = 100; | |
24 } | |
25 | |
26 void SineWaveGenerator::GenerateNextFrame(AudioFrame* frame) { | |
ivoc
2017/03/28 16:11:06
Please add DCHECK(frame)
| |
27 for (size_t i = 0; i < frame->samples_per_channel_; ++i) { | |
28 for (size_t ch = 0; ch < frame->num_channels_; ++ch) { | |
29 frame->data_[frame->num_channels_ * i + ch] = | |
30 rtc::saturated_cast<int16_t>(amplitude_ * sin(phase_)); | |
ivoc
2017/03/28 16:11:06
sinf, sin is for doubles.
aleloi
2017/03/28 17:10:13
Done.
| |
31 } | |
32 phase_ += wave_frequency_hz_ * 2 * kPi / frame->sample_rate_hz_; | |
33 } | |
34 } | |
35 | |
36 float GainChangeCalculator::CalculateGainChange( | |
37 rtc::ArrayView<const int16_t> in, | |
38 rtc::ArrayView<const int16_t> out) { | |
39 RTC_DCHECK_EQ(in.size(), out.size()); | |
40 | |
41 std::vector<float> gain(in.size()); | |
42 auto gain_view = rtc::ArrayView<float>(gain.data(), gain.size()); | |
43 CalculateGain(in, out, gain_view); | |
44 return CalculateDifferences(gain_view); | |
45 } | |
46 | |
47 void GainChangeCalculator::CalculateGain(rtc::ArrayView<const int16_t> in, | |
48 rtc::ArrayView<const int16_t> out, | |
49 rtc::ArrayView<float> gain) { | |
50 RTC_DCHECK_EQ(in.size(), out.size()); | |
51 RTC_DCHECK_EQ(in.size(), gain.size()); | |
52 | |
53 for (size_t i = 0; i < in.size(); ++i) { | |
54 if (std::abs(in[i]) >= kReliabilityThreshold) { | |
ivoc
2017/03/28 16:11:06
Won't this cause some discontinuities in the calcu
aleloi
2017/03/28 17:10:13
If the threshold increases, the resulted sum of di
| |
55 last_reliable_gain_ = out[i] / static_cast<float>(in[i]); | |
56 } | |
57 gain[i] = last_reliable_gain_; | |
58 } | |
59 } | |
60 | |
61 float GainChangeCalculator::CalculateDifferences( | |
62 rtc::ArrayView<const float> values) { | |
63 float res = 0; | |
64 for (float f : values) { | |
65 res += std::abs(f - last_value_); | |
ivoc
2017/03/28 16:11:06
fabs, abs is for ints.
aleloi
2017/03/28 17:10:13
I checked that this returns floats. fabs seems mor
| |
66 last_value_ = f; | |
67 } | |
68 return res; | |
69 } | |
70 } // namespace webrtc | |
OLD | NEW |