| Index: webrtc/modules/audio_processing/level_controller/level_controller.cc
 | 
| diff --git a/webrtc/modules/audio_processing/level_controller/level_controller.cc b/webrtc/modules/audio_processing/level_controller/level_controller.cc
 | 
| new file mode 100644
 | 
| index 0000000000000000000000000000000000000000..fa26c059cbbb333c7fbe481b59ffb49495990bc3
 | 
| --- /dev/null
 | 
| +++ b/webrtc/modules/audio_processing/level_controller/level_controller.cc
 | 
| @@ -0,0 +1,147 @@
 | 
| +/*
 | 
| + *  Copyright (c) 2016 The WebRTC project authors. All Rights Reserved.
 | 
| + *
 | 
| + *  Use of this source code is governed by a BSD-style license
 | 
| + *  that can be found in the LICENSE file in the root of the source
 | 
| + *  tree. An additional intellectual property rights grant can be found
 | 
| + *  in the file PATENTS.  All contributing project authors may
 | 
| + *  be found in the AUTHORS file in the root of the source tree.
 | 
| + */
 | 
| +
 | 
| +#include "webrtc/modules/audio_processing/level_controller/level_controller.h"
 | 
| +
 | 
| +#include <algorithm>
 | 
| +#include <numeric>
 | 
| +
 | 
| +#include "webrtc/base/array_view.h"
 | 
| +#include "webrtc/base/checks.h"
 | 
| +#include "webrtc/modules/audio_processing/audio_buffer.h"
 | 
| +#include "webrtc/modules/audio_processing/level_controller/gain_applier.h"
 | 
| +#include "webrtc/modules/audio_processing/level_controller/gain_selector.h"
 | 
| +#include "webrtc/modules/audio_processing/level_controller/noise_level_estimator.h"
 | 
| +#include "webrtc/modules/audio_processing/level_controller/peak_level_estimator.h"
 | 
| +#include "webrtc/modules/audio_processing/level_controller/saturating_gain_estimator.h"
 | 
| +#include "webrtc/modules/audio_processing/level_controller/signal_classifier.h"
 | 
| +#include "webrtc/modules/audio_processing/logging/apm_data_dumper.h"
 | 
| +
 | 
| +namespace webrtc {
 | 
| +namespace {
 | 
| +
 | 
| +void UpdateAndRemoveDcLevel(float forgetting_factor,
 | 
| +                            float* dc_level,
 | 
| +                            rtc::ArrayView<float> x) {
 | 
| +  RTC_DCHECK(!x.empty());
 | 
| +  float mean =
 | 
| +      std::accumulate(x.begin(), x.end(), 0) / static_cast<float>(x.size());
 | 
| +  *dc_level += forgetting_factor * (mean - *dc_level);
 | 
| +
 | 
| +  for (float& v : x) {
 | 
| +    v -= *dc_level;
 | 
| +  }
 | 
| +}
 | 
| +
 | 
| +float FrameEnergy(const AudioBuffer& audio) {
 | 
| +  float energy = 0.f;
 | 
| +  for (size_t k = 0; k < audio.num_channels(); ++k) {
 | 
| +    float channel_energy =
 | 
| +        std::accumulate(audio.channels_const_f()[k],
 | 
| +                        audio.channels_const_f()[k] + audio.num_frames(), 0,
 | 
| +                        [](float a, float b) -> float { return a + b * b; });
 | 
| +    energy = std::max(channel_energy, energy);
 | 
| +  }
 | 
| +  return energy;
 | 
| +}
 | 
| +
 | 
| +float PeakLevel(const AudioBuffer& audio) {
 | 
| +  float peak_level = 0.f;
 | 
| +  for (size_t k = 0; k < audio.num_channels(); ++k) {
 | 
| +    auto channel_peak_level = std::max_element(
 | 
| +        audio.channels_const_f()[k],
 | 
| +        audio.channels_const_f()[k] + audio.num_frames(),
 | 
| +        [](float a, float b) { return std::abs(a) < std::abs(b); });
 | 
| +    peak_level = std::max(*channel_peak_level, peak_level);
 | 
| +  }
 | 
| +  return peak_level;
 | 
| +}
 | 
| +
 | 
| +}  // namespace
 | 
| +
 | 
| +int LevelController::instance_count_ = 0;
 | 
| +
 | 
| +LevelController::LevelController()
 | 
| +    : data_dumper_(new ApmDataDumper(instance_count_)),
 | 
| +      gain_applier_(data_dumper_.get()),
 | 
| +      signal_classifier_(data_dumper_.get()) {
 | 
| +  Initialize(AudioProcessing::kSampleRate48kHz);
 | 
| +  ++instance_count_;
 | 
| +}
 | 
| +
 | 
| +LevelController::~LevelController() {}
 | 
| +
 | 
| +void LevelController::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();
 | 
| +  gain_selector_.Initialize(sample_rate_hz);
 | 
| +  gain_applier_.Initialize(sample_rate_hz);
 | 
| +  signal_classifier_.Initialize(sample_rate_hz);
 | 
| +  noise_level_estimator_.Initialize(sample_rate_hz);
 | 
| +  peak_level_estimator_.Initialize();
 | 
| +  saturating_gain_estimator_.Initialize();
 | 
| +
 | 
| +  sample_rate_hz_ = rtc::Optional<int>(sample_rate_hz);
 | 
| +  dc_forgetting_factor_ = 0.01f * sample_rate_hz / 48000.f;
 | 
| +}
 | 
| +
 | 
| +void LevelController::Process(AudioBuffer* audio) {
 | 
| +  RTC_DCHECK_LT(0u, audio->num_channels());
 | 
| +  RTC_DCHECK_GE(2u, audio->num_channels());
 | 
| +  RTC_DCHECK_NE(0.f, dc_forgetting_factor_);
 | 
| +  RTC_DCHECK(sample_rate_hz_);
 | 
| +  data_dumper_->DumpWav("lc_input", audio->num_frames(),
 | 
| +                        audio->channels_const_f()[0], *sample_rate_hz_, 1);
 | 
| +
 | 
| +  // Remove DC level.
 | 
| +  for (size_t k = 0; k < audio->num_channels(); ++k) {
 | 
| +    UpdateAndRemoveDcLevel(
 | 
| +        dc_forgetting_factor_, &dc_level_[k],
 | 
| +        rtc::ArrayView<float>(audio->channels_f()[k], audio->num_frames()));
 | 
| +  }
 | 
| +
 | 
| +  SignalClassifier::SignalType signal_type;
 | 
| +  signal_classifier_.Analyze(*audio, &signal_type);
 | 
| +  int tmp = static_cast<int>(signal_type);
 | 
| +  data_dumper_->DumpRaw("lc_signal_type", 1, &tmp);
 | 
| +
 | 
| +  // Estimate the noise energy.
 | 
| +  float noise_energy =
 | 
| +      noise_level_estimator_.Analyze(signal_type, FrameEnergy(*audio));
 | 
| +
 | 
| +  // Estimate the overall signal peak level.
 | 
| +  float peak_level =
 | 
| +      peak_level_estimator_.Analyze(signal_type, PeakLevel(*audio));
 | 
| +
 | 
| +  float saturating_gain = saturating_gain_estimator_.GetGain();
 | 
| +
 | 
| +  // Compute the new gain to apply.
 | 
| +  float new_gain = gain_selector_.GetNewGain(peak_level, noise_energy,
 | 
| +                                             saturating_gain, signal_type);
 | 
| +
 | 
| +  // Apply the gain to the signal.
 | 
| +  int num_saturations = gain_applier_.Process(new_gain, audio);
 | 
| +
 | 
| +  // Estimate the gain that saturates the overall signal.
 | 
| +  saturating_gain_estimator_.Update(new_gain, num_saturations);
 | 
| +
 | 
| +  data_dumper_->DumpRaw("lc_selected_gain", 1, &new_gain);
 | 
| +  data_dumper_->DumpRaw("lc_noise_energy", 1, &noise_energy);
 | 
| +  data_dumper_->DumpRaw("lc_peak_level", 1, &peak_level);
 | 
| +  data_dumper_->DumpRaw("lc_saturating_gain", 1, &saturating_gain);
 | 
| +
 | 
| +  data_dumper_->DumpWav("lc_output", audio->num_frames(),
 | 
| +                        audio->channels_f()[0], *sample_rate_hz_, 1);
 | 
| +}
 | 
| +
 | 
| +}  // namespace webrtc
 | 
| 
 |