Chromium Code Reviews| Index: remoting/host/audio_volume_applier.cc |
| diff --git a/remoting/host/audio_volume_applier.cc b/remoting/host/audio_volume_applier.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1fa81d5594571566007414688b4a0bfd75622673 |
| --- /dev/null |
| +++ b/remoting/host/audio_volume_applier.cc |
| @@ -0,0 +1,51 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "remoting/host/audio_volume_applier.h" |
| + |
| +namespace remoting { |
| + |
| +AudioVolumeApplier::AudioVolumeApplier(int silence_threshold) |
| + : silence_detector_before_(silence_threshold), |
| + silence_detector_after_(silence_threshold) {} |
|
Sergey Ulanov
2017/04/26 19:57:35
I don't think we need the post-processing silence
Hzj_jie
2017/04/28 03:53:34
Done.
|
| +AudioVolumeApplier::~AudioVolumeApplier() = default; |
| + |
| +bool AudioVolumeApplier::Apply(int16_t* data, size_t frames) { |
| + if (frames == 0) { |
| + return false; |
| + } |
| + |
| + bool result = ApplyVolumeLevel(data, frames); |
| + return !silence_detector_after_.IsSilence(data, frames) && result; |
| +} |
| + |
| +void AudioVolumeApplier::Initialize(int sampling_rate, int channels) { |
| + silence_detector_before_.Reset(sampling_rate, channels); |
| + silence_detector_after_.Reset(sampling_rate, channels); |
| +} |
| + |
| +bool AudioVolumeApplier::ApplyVolumeLevel(int16_t* data, size_t frames) { |
| + if (silence_detector_before_.IsSilence(data, frames)) { |
| + return false; |
| + } |
| + |
| + float level = GetAudioLevel(); |
| + if (level == 0) { |
| + return false; |
| + } |
| + |
| + if (level == 1) { |
| + return true; |
| + } |
| + |
| + const int sample_count = frames * silence_detector_before_.channels(); |
| + const int32_t level_int = static_cast<int32_t>(level * 65536); |
| + for (int i = 0; i < sample_count; i++) { |
| + data[i] = (static_cast<int32_t>(data[i]) * level_int) >> 16; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +} // namespace remoting |