Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2017 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 |
| 11 #include "webrtc/modules/audio_processing/agc2/gain_controller2.h" | 11 #include "webrtc/modules/audio_processing/agc2/gain_controller2.h" |
| 12 | 12 |
| 13 #include <cmath> | |
| 14 | |
| 13 #include "webrtc/modules/audio_processing/audio_buffer.h" | 15 #include "webrtc/modules/audio_processing/audio_buffer.h" |
| 14 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" | 16 #include "webrtc/modules/audio_processing/logging/apm_data_dumper.h" |
| 15 #include "webrtc/rtc_base/atomicops.h" | 17 #include "webrtc/rtc_base/atomicops.h" |
| 16 #include "webrtc/rtc_base/checks.h" | 18 #include "webrtc/rtc_base/checks.h" |
| 17 | 19 |
| 18 namespace webrtc { | 20 namespace webrtc { |
| 19 | 21 |
| 20 namespace { | |
| 21 | |
| 22 constexpr float kGain = 0.5f; | |
| 23 | |
| 24 } // namespace | |
| 25 | |
| 26 int GainController2::instance_count_ = 0; | 22 int GainController2::instance_count_ = 0; |
| 27 | 23 |
| 28 GainController2::GainController2(int sample_rate_hz) | 24 GainController2::GainController2(const float fixed_gain_db) |
| 29 : sample_rate_hz_(sample_rate_hz), | 25 : data_dumper_(new ApmDataDumper(instance_count_)), |
| 30 data_dumper_(new ApmDataDumper( | 26 fixed_gain_(std::pow(10.0, fixed_gain_db / 20.0)) { |
| 31 rtc::AtomicOps::Increment(&instance_count_))), | 27 Initialize(AudioProcessing::kSampleRate48kHz); |
|
peah-webrtc
2017/08/18 04:51:06
Is it really needed to default-initialize it to 48
| |
| 32 digital_gain_applier_(), | 28 ++instance_count_; |
| 33 gain_(kGain) { | |
| 34 RTC_DCHECK(sample_rate_hz_ == AudioProcessing::kSampleRate8kHz || | |
| 35 sample_rate_hz_ == AudioProcessing::kSampleRate16kHz || | |
| 36 sample_rate_hz_ == AudioProcessing::kSampleRate32kHz || | |
| 37 sample_rate_hz_ == AudioProcessing::kSampleRate48kHz); | |
| 38 data_dumper_->InitiateNewSetOfRecordings(); | |
| 39 data_dumper_->DumpRaw("gain_", 1, &gain_); | |
| 40 } | 29 } |
| 41 | 30 |
| 42 GainController2::~GainController2() = default; | 31 GainController2::~GainController2() = default; |
| 43 | 32 |
| 33 void GainController2::Initialize(int sample_rate_hz) { | |
| 34 RTC_DCHECK(sample_rate_hz == AudioProcessing::kSampleRate8kHz || | |
| 35 sample_rate_hz == AudioProcessing::kSampleRate16kHz || | |
| 36 sample_rate_hz == AudioProcessing::kSampleRate32kHz || | |
| 37 sample_rate_hz == AudioProcessing::kSampleRate48kHz); | |
| 38 data_dumper_->InitiateNewSetOfRecordings(); | |
| 39 data_dumper_->DumpRaw("fixed gain (linear)", fixed_gain_); | |
| 40 sample_rate_hz_ = sample_rate_hz; | |
| 41 } | |
| 42 | |
| 44 void GainController2::Process(AudioBuffer* audio) { | 43 void GainController2::Process(AudioBuffer* audio) { |
| 44 bool saturated_frame = false; | |
| 45 for (size_t k = 0; k < audio->num_channels(); ++k) { | 45 for (size_t k = 0; k < audio->num_channels(); ++k) { |
| 46 auto channel_view = rtc::ArrayView<float>( | 46 for (size_t j = 0; j < audio->num_frames(); ++j) { |
| 47 audio->channels_f()[k], audio->num_frames()); | 47 audio->channels_f()[k][j] = std::min( |
| 48 digital_gain_applier_.Process(gain_, channel_view); | 48 32767.f, std::max(-32768.f, fixed_gain_ * audio->channels_f()[k][j])); |
| 49 if (audio->channels_f()[k][j] == -32768.f || | |
| 50 audio->channels_f()[k][j] == 32767.f) { | |
| 51 saturated_frame = true; | |
| 52 } | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 if (saturated_frame) { | |
| 57 data_dumper_->DumpRaw("saturated frame detected", true); | |
| 49 } | 58 } |
| 50 } | 59 } |
| 51 | 60 |
| 52 bool GainController2::Validate( | 61 bool GainController2::Validate( |
| 53 const AudioProcessing::Config::GainController2& config) { | 62 const AudioProcessing::Config::GainController2& config) { |
| 54 return true; | 63 return config.fixed_gain_db >= 0.f; |
| 55 } | 64 } |
| 56 | 65 |
| 57 std::string GainController2::ToString( | 66 std::string GainController2::ToString( |
| 58 const AudioProcessing::Config::GainController2& config) { | 67 const AudioProcessing::Config::GainController2& config) { |
| 59 std::stringstream ss; | 68 std::stringstream ss; |
| 60 ss << "{" | 69 ss << "{" |
| 61 << "enabled: " << (config.enabled ? "true" : "false") << "}"; | 70 << "enabled: " << (config.enabled ? "true" : "false") << ", " |
| 71 << "fixed_gain_dB: " << config.fixed_gain_db << "}"; | |
| 62 return ss.str(); | 72 return ss.str(); |
| 63 } | 73 } |
| 64 | 74 |
| 65 } // namespace webrtc | 75 } // namespace webrtc |
| OLD | NEW |