OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 52 matching lines...) Loading... | |
63 gain = std::min(new_gain, gain + step_size); | 63 gain = std::min(new_gain, gain + step_size); |
64 v *= gain; | 64 v *= gain; |
65 } | 65 } |
66 return gain; | 66 return gain; |
67 } | 67 } |
68 | 68 |
69 float ApplyDecreasingGain(float new_gain, | 69 float ApplyDecreasingGain(float new_gain, |
70 float old_gain, | 70 float old_gain, |
71 float step_size, | 71 float step_size, |
72 rtc::ArrayView<float> x) { | 72 rtc::ArrayView<float> x) { |
73 RTC_DCHECK_LT(0.f, step_size); | 73 RTC_DCHECK_GT(0.f, step_size); |
74 float gain = old_gain; | 74 float gain = old_gain; |
75 for (auto& v : x) { | 75 for (auto& v : x) { |
76 gain = std::max(new_gain, gain - step_size); | 76 gain = std::max(new_gain, gain + step_size); |
hlundin-webrtc
2016/06/30 07:36:23
Now that you are passing the step size with a sign
peah-webrtc
2016/06/30 14:38:18
I'll keep it as it is for now to avoid the branchi
| |
77 v *= gain; | 77 v *= gain; |
78 } | 78 } |
79 return gain; | 79 return gain; |
80 } | 80 } |
81 | 81 |
82 float ApplyConstantGain(float gain, rtc::ArrayView<float> x) { | 82 float ApplyConstantGain(float gain, rtc::ArrayView<float> x) { |
83 for (auto& v : x) { | 83 for (auto& v : x) { |
84 v *= gain; | 84 v *= gain; |
85 } | 85 } |
86 | 86 |
87 return gain; | 87 return gain; |
88 } | 88 } |
89 | 89 |
90 float ApplyGain(float new_gain, | 90 float ApplyGain(float new_gain, |
91 float old_gain, | 91 float old_gain, |
92 float step_size, | 92 float increase_step_size, |
93 float decrease_step_size, | |
93 rtc::ArrayView<float> x) { | 94 rtc::ArrayView<float> x) { |
95 RTC_DCHECK_LT(0.f, increase_step_size); | |
96 RTC_DCHECK_GT(0.f, decrease_step_size); | |
94 if (new_gain == old_gain) { | 97 if (new_gain == old_gain) { |
95 return ApplyConstantGain(new_gain, x); | 98 return ApplyConstantGain(new_gain, x); |
96 } else if (new_gain > old_gain) { | 99 } else if (new_gain > old_gain) { |
97 return ApplyIncreasingGain(new_gain, old_gain, step_size, x); | 100 return ApplyIncreasingGain(new_gain, old_gain, increase_step_size, x); |
98 } else { | 101 } else { |
99 return ApplyDecreasingGain(new_gain, old_gain, step_size, x); | 102 return ApplyDecreasingGain(new_gain, old_gain, decrease_step_size, x); |
100 } | 103 } |
101 } | 104 } |
102 | 105 |
103 } // namespace | 106 } // namespace |
104 | 107 |
105 GainApplier::GainApplier(ApmDataDumper* data_dumper) | 108 GainApplier::GainApplier(ApmDataDumper* data_dumper) |
106 : data_dumper_(data_dumper) {} | 109 : data_dumper_(data_dumper) {} |
107 | 110 |
108 void GainApplier::Initialize(int sample_rate_hz) { | 111 void GainApplier::Initialize(int sample_rate_hz) { |
109 RTC_DCHECK(sample_rate_hz == AudioProcessing::kSampleRate8kHz || | 112 RTC_DCHECK(sample_rate_hz == AudioProcessing::kSampleRate8kHz || |
110 sample_rate_hz == AudioProcessing::kSampleRate16kHz || | 113 sample_rate_hz == AudioProcessing::kSampleRate16kHz || |
111 sample_rate_hz == AudioProcessing::kSampleRate32kHz || | 114 sample_rate_hz == AudioProcessing::kSampleRate32kHz || |
112 sample_rate_hz == AudioProcessing::kSampleRate48kHz); | 115 sample_rate_hz == AudioProcessing::kSampleRate48kHz); |
113 const float kStepSize48kHz = 0.001f; | 116 const float kGainIncreaseStepSize48kHz = 0.001f; |
117 const float kGainDecreaseStepSize48kHz = -0.01f; | |
118 const float kGainSaturatedDecreaseStepSize48kHz = -0.05f; | |
119 | |
120 last_frame_was_saturated_ = false; | |
114 old_gain_ = 1.f; | 121 old_gain_ = 1.f; |
115 gain_change_step_size_ = | 122 gain_increase_step_size_ = |
116 kStepSize48kHz * | 123 kGainIncreaseStepSize48kHz * |
124 (static_cast<float>(AudioProcessing::kSampleRate48kHz) / sample_rate_hz); | |
125 gain_normal_decrease_step_size_ = | |
126 kGainDecreaseStepSize48kHz * | |
127 (static_cast<float>(AudioProcessing::kSampleRate48kHz) / sample_rate_hz); | |
128 gain_saturated_decrease_step_size_ = | |
129 kGainSaturatedDecreaseStepSize48kHz * | |
117 (static_cast<float>(AudioProcessing::kSampleRate48kHz) / sample_rate_hz); | 130 (static_cast<float>(AudioProcessing::kSampleRate48kHz) / sample_rate_hz); |
118 } | 131 } |
119 | 132 |
120 int GainApplier::Process(float new_gain, AudioBuffer* audio) { | 133 int GainApplier::Process(float new_gain, AudioBuffer* audio) { |
121 RTC_CHECK_NE(0.f, gain_change_step_size_); | 134 RTC_CHECK_NE(0.f, gain_increase_step_size_); |
135 RTC_CHECK_NE(0.f, gain_normal_decrease_step_size_); | |
136 RTC_CHECK_NE(0.f, gain_saturated_decrease_step_size_); | |
122 int num_saturations = 0; | 137 int num_saturations = 0; |
123 if (new_gain != 1.f) { | 138 if (new_gain != 1.f) { |
124 float last_applied_gain = 1.f; | 139 float last_applied_gain = 1.f; |
140 float gain_decrease_step_size = last_frame_was_saturated_ | |
141 ? gain_saturated_decrease_step_size_ | |
142 : gain_normal_decrease_step_size_; | |
125 for (size_t k = 0; k < audio->num_channels(); ++k) { | 143 for (size_t k = 0; k < audio->num_channels(); ++k) { |
126 // TODO(peah): Consider using a faster update rate downwards than upwards. | |
127 last_applied_gain = ApplyGain( | 144 last_applied_gain = ApplyGain( |
128 new_gain, old_gain_, gain_change_step_size_, | 145 new_gain, old_gain_, gain_increase_step_size_, |
146 gain_decrease_step_size, | |
129 rtc::ArrayView<float>(audio->channels_f()[k], audio->num_frames())); | 147 rtc::ArrayView<float>(audio->channels_f()[k], audio->num_frames())); |
130 } | 148 } |
131 // TODO(peah): Consider the need for faster gain reduction in case of | 149 |
132 // excessive saturation. | |
133 num_saturations = CountSaturations(*audio); | 150 num_saturations = CountSaturations(*audio); |
134 LimitToAllowedRange(audio); | 151 LimitToAllowedRange(audio); |
135 old_gain_ = last_applied_gain; | 152 old_gain_ = last_applied_gain; |
136 } | 153 } |
137 | 154 |
138 data_dumper_->DumpRaw("lc_last_applied_gain", 1, &old_gain_); | 155 data_dumper_->DumpRaw("lc_last_applied_gain", 1, &old_gain_); |
139 | 156 |
140 return num_saturations; | 157 return num_saturations; |
141 } | 158 } |
142 | 159 |
143 } // namespace webrtc | 160 } // namespace webrtc |
OLD | NEW |