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" |
19 #include "webrtc/rtc_base/safe_minmax.h" | |
17 | 20 |
18 namespace webrtc { | 21 namespace webrtc { |
19 | 22 |
20 namespace { | |
21 | |
22 constexpr float kGain = 0.5f; | |
23 | |
24 } // namespace | |
25 | |
26 int GainController2::instance_count_ = 0; | 23 int GainController2::instance_count_ = 0; |
27 | 24 |
28 GainController2::GainController2(int sample_rate_hz) | 25 GainController2::GainController2() |
29 : sample_rate_hz_(sample_rate_hz), | 26 : data_dumper_(new ApmDataDumper(instance_count_)), |
peah-webrtc
2017/09/15 07:44:25
You need to increase instance_count_ for each new
AleBzk
2017/09/29 09:39:06
Done, but note that I took LC as reference, in whi
| |
30 data_dumper_(new ApmDataDumper( | 27 sample_rate_hz_(0), |
peah-webrtc
2017/09/15 07:44:25
Please initialize the sample rate to a valid rate.
AleBzk
2017/09/29 09:39:06
Done.
| |
31 rtc::AtomicOps::Increment(&instance_count_))), | 28 fixed_gain_(1.f) { |
32 digital_gain_applier_(), | 29 ++instance_count_; |
peah-webrtc
2017/09/15 07:44:25
Please remove this increase and instead do it as a
AleBzk
2017/09/29 09:39:06
Done.
| |
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 } | 30 } |
41 | 31 |
42 GainController2::~GainController2() = default; | 32 GainController2::~GainController2() = default; |
43 | 33 |
34 void GainController2::Initialize(int sample_rate_hz) { | |
35 RTC_DCHECK(sample_rate_hz == AudioProcessing::kSampleRate8kHz || | |
36 sample_rate_hz == AudioProcessing::kSampleRate16kHz || | |
37 sample_rate_hz == AudioProcessing::kSampleRate32kHz || | |
38 sample_rate_hz == AudioProcessing::kSampleRate48kHz); | |
39 data_dumper_->InitiateNewSetOfRecordings(); | |
40 data_dumper_->DumpRaw("fixed gain (linear)", fixed_gain_); | |
peah-webrtc
2017/09/15 07:44:25
Does filenames with spaces and parenthesis always
AleBzk
2017/09/29 09:39:06
Done.
| |
41 sample_rate_hz_ = sample_rate_hz; | |
42 } | |
43 | |
44 void GainController2::Process(AudioBuffer* audio) { | 44 void GainController2::Process(AudioBuffer* audio) { |
45 if (fixed_gain_ == 1.f) | |
46 return; | |
47 | |
48 bool saturated_frame = false; | |
45 for (size_t k = 0; k < audio->num_channels(); ++k) { | 49 for (size_t k = 0; k < audio->num_channels(); ++k) { |
46 auto channel_view = rtc::ArrayView<float>( | 50 for (size_t j = 0; j < audio->num_frames(); ++j) { |
47 audio->channels_f()[k], audio->num_frames()); | 51 audio->channels_f()[k][j] = rtc::SafeClamp( |
48 digital_gain_applier_.Process(gain_, channel_view); | 52 fixed_gain_ * audio->channels_f()[k][j], -32768.f, 32767.f); |
53 if (audio->channels_f()[k][j] == -32768.f || | |
peah-webrtc
2017/09/15 07:44:25
This is only used for the purpose of the DumpRaw c
AleBzk
2017/09/29 09:39:06
Done.
| |
54 audio->channels_f()[k][j] == 32767.f) { | |
55 saturated_frame = true; | |
56 } | |
57 } | |
49 } | 58 } |
59 | |
60 if (saturated_frame) { | |
61 data_dumper_->DumpRaw("saturated frame detected", true); | |
62 } | |
63 } | |
64 | |
65 void GainController2::ApplyConfig( | |
66 const AudioProcessing::Config::GainController2& config) { | |
67 RTC_DCHECK(Validate(config)); | |
68 fixed_gain_ = (config.fixed_gain_db == 0.f) | |
peah-webrtc
2017/09/15 07:44:25
The parentheses are not needed here.
AleBzk
2017/09/29 09:39:06
Done.
| |
69 ? 1.f | |
70 : std::pow(10.0, config.fixed_gain_db / 20.0); | |
50 } | 71 } |
51 | 72 |
52 bool GainController2::Validate( | 73 bool GainController2::Validate( |
53 const AudioProcessing::Config::GainController2& config) { | 74 const AudioProcessing::Config::GainController2& config) { |
54 return true; | 75 return config.fixed_gain_db >= 0.f; |
55 } | 76 } |
56 | 77 |
57 std::string GainController2::ToString( | 78 std::string GainController2::ToString( |
58 const AudioProcessing::Config::GainController2& config) { | 79 const AudioProcessing::Config::GainController2& config) { |
59 std::stringstream ss; | 80 std::stringstream ss; |
60 ss << "{" | 81 ss << "{" |
61 << "enabled: " << (config.enabled ? "true" : "false") << "}"; | 82 << "enabled: " << (config.enabled ? "true" : "false") << ", " |
83 << "fixed_gain_dB: " << config.fixed_gain_db << "}"; | |
62 return ss.str(); | 84 return ss.str(); |
63 } | 85 } |
64 | 86 |
65 } // namespace webrtc | 87 } // namespace webrtc |
OLD | NEW |