Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(338)

Unified Diff: webrtc/modules/audio_processing/test/fake_recording_device.cc

Issue 2834643002: audioproc_f with simulated mic analog gain (Closed)
Patch Set: AGC simulated gain Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: webrtc/modules/audio_processing/test/fake_recording_device.cc
diff --git a/webrtc/modules/audio_processing/test/fake_recording_device.cc b/webrtc/modules/audio_processing/test/fake_recording_device.cc
new file mode 100644
index 0000000000000000000000000000000000000000..de20b299a2cfcd9c3014a2393e6608d5a32c1702
--- /dev/null
+++ b/webrtc/modules/audio_processing/test/fake_recording_device.cc
@@ -0,0 +1,99 @@
+/*
+ * Copyright (c) 2017 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/test/fake_recording_device.h"
+
+#include "webrtc/base/logging.h"
+
+namespace webrtc {
+
+FakeRecordingDevice::FakeRecordingDevice(LevelToScalingMappingKind mapping_kind)
+ : mapping_kind_(mapping_kind) {}
+
+FakeRecordingDevice::~FakeRecordingDevice() = default;
+
+// |level| must be within [0, 255].
+void FakeRecordingDevice::set_analog_level(int level) {
+ RTC_DCHECK_LE(0, level);
+ RTC_DCHECK_LE(level, 255);
+ level_ = level;
+}
+
+int FakeRecordingDevice::analog_level() const {
+ return level_;
+}
+
+FakeRecordingDevice::LevelToScalingMappingKind
+ FakeRecordingDevice::mapping_kind() const {
+ return mapping_kind_;
+}
+
+void FakeRecordingDevice::ProcessStream(
+ std::vector<rtc::ArrayView<const float>> src,
+ std::vector<rtc::ArrayView<float>> dest) {
+ RTC_DCHECK_EQ(src.size(), dest.size());
+
+ const float scaling_factor = ComputeCompoundScalingFactor();
+
+ for (size_t i = 0; i < src.size(); ++i) {
+ RTC_DCHECK_EQ(src[i].size(), dest[i].size());
+ std::transform(src[i].begin(), src[i].end(), dest[i].begin(),
+ [scaling_factor](float x) { return scaling_factor * x; });
+ }
+ real_device_level_.reset();
+}
+
+void FakeRecordingDevice::ProcessStream(const AudioFrame* src,
+ AudioFrame* dest) {
+ RTC_DCHECK_EQ(src->num_channels_, dest->num_channels_);
+ RTC_DCHECK_EQ(src->samples_per_channel_, dest->samples_per_channel_);
+ const size_t number_of_samples =
+ src->samples_per_channel_ * src->num_channels_;
+ RTC_DCHECK_LE(number_of_samples, AudioFrame::kMaxDataSizeSamples);
+ const float scaling_factor = ComputeCompoundScalingFactor();
+
+ std::transform(src->data_, src->data_ + number_of_samples, dest->data_,
+ [scaling_factor](int16_t x) {
+ return rtc::saturated_cast<int16_t>(scaling_factor * x);
+ });
+
+ real_device_level_.reset();
+}
+
+void FakeRecordingDevice::NotifyAudioDeviceLevel(int level) {
+ real_device_level_.emplace(level);
+}
+
+float FakeRecordingDevice::ComputeCompoundScalingFactor() {
+ const float level_scaling_factor = GetScalingFactor(level_);
+ const float undo_factor =
+ real_device_level_ ? GetScalingFactor(*real_device_level_) : 1.0f;
+
+ if (undo_factor == 0) {
+ LOG(LS_INFO) << "[fake rec device] Cannot undo analog level of 0.";
aleloi 2017/05/04 12:47:13 Maybe this one should be verbose? It's probably no
AleBzk 2017/05/05 12:20:18 Done.
+ return level_scaling_factor;
+ } else {
+ return level_scaling_factor / undo_factor;
+ }
+}
+
+float FakeRecordingDevice::GetScalingFactor(int level) const {
+ switch (mapping_kind_) {
+ case LevelToScalingMappingKind::kLinear: {
+ return static_cast<float>(level) / 255.0f;
+ }
+ case LevelToScalingMappingKind::kIdentity: {
+ return 1.0f;
+ }
+ default: { RTC_NOTREACHED(); }
+ }
+}
+
+} // namespace webrtc

Powered by Google App Engine
This is Rietveld 408576698