Chromium Code Reviews| Index: webrtc/modules/audio_processing/agc2/gain_controller2.cc | 
| diff --git a/webrtc/modules/audio_processing/agc2/gain_controller2.cc b/webrtc/modules/audio_processing/agc2/gain_controller2.cc | 
| index 20680f64a840eee3aa1c63ee8cb23d8dfdf0dae2..58beda301216a9cce45f6a3c07ceb04e4a0cd955 100644 | 
| --- a/webrtc/modules/audio_processing/agc2/gain_controller2.cc | 
| +++ b/webrtc/modules/audio_processing/agc2/gain_controller2.cc | 
| @@ -10,6 +10,8 @@ | 
| #include "webrtc/modules/audio_processing/agc2/gain_controller2.h" | 
| +#include <cmath> | 
| + | 
| #include "webrtc/modules/audio_processing/audio_buffer.h" | 
| #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" | 
| #include "webrtc/rtc_base/atomicops.h" | 
| @@ -17,48 +19,56 @@ | 
| namespace webrtc { | 
| -namespace { | 
| - | 
| -constexpr float kGain = 0.5f; | 
| - | 
| -} // namespace | 
| - | 
| int GainController2::instance_count_ = 0; | 
| -GainController2::GainController2(int sample_rate_hz) | 
| - : sample_rate_hz_(sample_rate_hz), | 
| - data_dumper_(new ApmDataDumper( | 
| - rtc::AtomicOps::Increment(&instance_count_))), | 
| - digital_gain_applier_(), | 
| - gain_(kGain) { | 
| - RTC_DCHECK(sample_rate_hz_ == AudioProcessing::kSampleRate8kHz || | 
| - sample_rate_hz_ == AudioProcessing::kSampleRate16kHz || | 
| - sample_rate_hz_ == AudioProcessing::kSampleRate32kHz || | 
| - sample_rate_hz_ == AudioProcessing::kSampleRate48kHz); | 
| - data_dumper_->InitiateNewSetOfRecordings(); | 
| - data_dumper_->DumpRaw("gain_", 1, &gain_); | 
| +GainController2::GainController2(const float fixed_gain_db) | 
| + : data_dumper_(new ApmDataDumper(instance_count_)), | 
| + fixed_gain_(std::pow(10.0, fixed_gain_db / 20.0)) { | 
| + Initialize(AudioProcessing::kSampleRate48kHz); | 
| 
 
peah-webrtc
2017/08/18 04:51:06
Is it really needed to default-initialize it to 48
 
 | 
| + ++instance_count_; | 
| } | 
| GainController2::~GainController2() = default; | 
| +void GainController2::Initialize(int sample_rate_hz) { | 
| + RTC_DCHECK(sample_rate_hz == AudioProcessing::kSampleRate8kHz || | 
| + sample_rate_hz == AudioProcessing::kSampleRate16kHz || | 
| + sample_rate_hz == AudioProcessing::kSampleRate32kHz || | 
| + sample_rate_hz == AudioProcessing::kSampleRate48kHz); | 
| + data_dumper_->InitiateNewSetOfRecordings(); | 
| + data_dumper_->DumpRaw("fixed gain (linear)", fixed_gain_); | 
| + sample_rate_hz_ = sample_rate_hz; | 
| +} | 
| + | 
| void GainController2::Process(AudioBuffer* audio) { | 
| + bool saturated_frame = false; | 
| for (size_t k = 0; k < audio->num_channels(); ++k) { | 
| - auto channel_view = rtc::ArrayView<float>( | 
| - audio->channels_f()[k], audio->num_frames()); | 
| - digital_gain_applier_.Process(gain_, channel_view); | 
| + for (size_t j = 0; j < audio->num_frames(); ++j) { | 
| + audio->channels_f()[k][j] = std::min( | 
| + 32767.f, std::max(-32768.f, fixed_gain_ * audio->channels_f()[k][j])); | 
| + if (audio->channels_f()[k][j] == -32768.f || | 
| + audio->channels_f()[k][j] == 32767.f) { | 
| + saturated_frame = true; | 
| + } | 
| + } | 
| + } | 
| + | 
| + if (saturated_frame) { | 
| + data_dumper_->DumpRaw("saturated frame detected", true); | 
| } | 
| } | 
| bool GainController2::Validate( | 
| const AudioProcessing::Config::GainController2& config) { | 
| - return true; | 
| + return config.fixed_gain_db >= 0.f; | 
| } | 
| std::string GainController2::ToString( | 
| const AudioProcessing::Config::GainController2& config) { | 
| std::stringstream ss; | 
| ss << "{" | 
| - << "enabled: " << (config.enabled ? "true" : "false") << "}"; | 
| + << "enabled: " << (config.enabled ? "true" : "false") << ", " | 
| + << "fixed_gain_dB: " << config.fixed_gain_db << "}"; | 
| return ss.str(); | 
| } |