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

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

Issue 2834643002: audioproc_f with simulated mic analog gain (Closed)
Patch Set: comments addressed 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_unittest.cc
diff --git a/webrtc/modules/audio_processing/test/fake_recording_device_unittest.cc b/webrtc/modules/audio_processing/test/fake_recording_device_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..51075d4aa5951895fb210cbae3a3e970732b521f
--- /dev/null
+++ b/webrtc/modules/audio_processing/test/fake_recording_device_unittest.cc
@@ -0,0 +1,202 @@
+/*
+ * 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 <sstream>
+#include <string>
+#include <vector>
+
+#include "webrtc/base/array_view.h"
+#include "webrtc/modules/audio_processing/test/fake_recording_device.h"
+#include "webrtc/test/gtest.h"
+
+namespace webrtc {
+
+namespace {
+
+using MappingKind = FakeRecordingDevice::MicrophoneKind;
+
+rtc::Optional<int> kRealDeviceLevelUnknown;
+
+// TODO(...): Add new mapping kinds here as they are added to MicrophoneKind.
+const std::vector<MappingKind> kMappingKindsToTest =
+ {MappingKind::kIdentity, MappingKind::kLinear};
+
+const std::vector<std::vector<float>> kTestMultiChannelSamples{
+ std::vector<float>{-10.0, -1.0, -0.1, 0.0, 0.1, 1.0, 10.0}};
+
+// Returns a vector of ArrayView items of non-const floats.
+std::vector<rtc::ArrayView<float>> VecVecToVecArrayView(
+ std::vector<std::vector<float>>* vectorOfVectors) {
+ std::vector<rtc::ArrayView<float>> vectorOfArrayViews;
+ for (auto& v : *vectorOfVectors) { vectorOfArrayViews.emplace_back(v); }
+ return vectorOfArrayViews;
+}
+
+// Returns a vector of ArrayView items of const floats.
+std::vector<rtc::ArrayView<const float>> VecVecToVecArrayViewOfConst(
+ const std::vector<std::vector<float>>& vectorOfVectors) {
+ std::vector<rtc::ArrayView<const float>> vectorOfArrayViews;
+ for (const auto& v : vectorOfVectors) { vectorOfArrayViews.emplace_back(v); }
+ return vectorOfArrayViews;
+}
+
+// Resets samples to kTestMultiChannelSamples using the ArrayView interface.
+void ResetVecArrayView(std::vector<rtc::ArrayView<float>>* vectorOfArrayViews) {
+ RTC_DCHECK_EQ(kTestMultiChannelSamples.size(), vectorOfArrayViews->size());
+ RTC_DCHECK_EQ(kTestMultiChannelSamples[0].size(),
+ (*vectorOfArrayViews)[0].size());
+ for (size_t i = 0; i < vectorOfArrayViews->size(); ++i) {
+ for (size_t j = 0; j < (*vectorOfArrayViews)[i].size(); ++j) {
+ (*vectorOfArrayViews)[i][j] = kTestMultiChannelSamples[i][j];
+ }
+ }
+}
+
+// Checks that the samples modified using monotonic level values are also
+// monotonic.
+void CheckIfMonotoneSamplesModules(
+ const std::vector<rtc::ArrayView<float>>& prev,
+ const std::vector<rtc::ArrayView<float>>& curr) {
+ bool valid = true;
+ for (size_t i = 0; i < prev.size(); ++i) {
+ for (size_t j = 0; j < prev[i].size(); ++j) {
+ valid = std::fabs(prev[i][j]) <= std::fabs(curr[i][j]);
+ if (!valid) { break; }
+ }
+ if (!valid) { break; }
+ }
+ EXPECT_TRUE(valid);
+}
+
+// Checks that the samples in each pair have the same sign unless the sample in
+// |dst| is zero (because of zero gain).
+void CheckSameSign(
+ const std::vector<rtc::ArrayView<const float>>& src,
+ const std::vector<rtc::ArrayView<float>>& dst) {
+ const auto fsgn = [](float x) { return ((x < 0) ? -1 : (x > 0) ? 1 : 0); };
+ bool valid = true;
+ for (size_t i = 0; i < src.size(); ++i) {
+ for (size_t j = 0; j < src[i].size(); ++j) {
+ valid = dst[i][j] == 0.0f || fsgn(src[i][j]) == fsgn(dst[i][j]);
+ if (!valid) { break; }
+ }
+ if (!valid) { break; }
+ }
+ EXPECT_TRUE(valid);
+}
+
+std::string MappingKindToString(MappingKind kind) {
+ std::ostringstream ss;
+ ss << "simulated microphone kind code: " << static_cast<size_t>(kind);
+ return ss.str();
+}
+
+std::string AnalogLevelToString(int level) {
+ std::ostringstream ss;
+ ss << "analog level: " << level;
+ return ss.str();
+}
+
+} // namespace
+
+TEST(AnalogVolumeMapper, CheckHelperFunctions) {
+ auto multichannel_samples = kTestMultiChannelSamples;
+
+ // Check read.
+ auto multichannel_samples_view = VecVecToVecArrayView(&multichannel_samples);
+ EXPECT_EQ(kTestMultiChannelSamples[0][0], multichannel_samples_view[0][0]);
+
+ // Check sizes.
+ EXPECT_EQ(kTestMultiChannelSamples.size(), // Same number of channels.
+ multichannel_samples_view.size());
+ EXPECT_EQ(kTestMultiChannelSamples[0].size(), // Same number of samples.
+ multichannel_samples_view[0].size());
+
+ // Check write.
+ constexpr size_t channel_index = 0;
+ constexpr size_t sample_index = 1;
+ multichannel_samples[channel_index][sample_index] = -5.0f;
+ RTC_DCHECK_NE(multichannel_samples[channel_index][sample_index],
+ kTestMultiChannelSamples[channel_index][sample_index]);
+ EXPECT_EQ(multichannel_samples[channel_index][sample_index],
+ multichannel_samples_view[channel_index][sample_index]);
+
+ // Check reset.
+ ResetVecArrayView(&multichannel_samples_view);
+ EXPECT_EQ(multichannel_samples[channel_index][sample_index],
+ kTestMultiChannelSamples[channel_index][sample_index]);
+}
+
+TEST(AnalogVolumeMapper, GainCurveShouldBeMonotone) {
+ // Create input-output buffers.
+ auto multichannel_samples_prev = kTestMultiChannelSamples;
+ auto multichannel_samples_prev_view = VecVecToVecArrayView(
+ &multichannel_samples_prev);
+ auto multichannel_samples = kTestMultiChannelSamples;
+ auto multichannel_samples_view = VecVecToVecArrayView(&multichannel_samples);
+
+ // Test different mappings.
+ for (const auto& kind : kMappingKindsToTest) {
+ SCOPED_TRACE(MappingKindToString(kind));
+ webrtc::FakeRecordingDevice level_mapper(kind);
+
+ // Apply lowest analog level.
+ ResetVecArrayView(&multichannel_samples_prev_view);
+ level_mapper.SimulateAnalogGain(
+ 0, kRealDeviceLevelUnknown, multichannel_samples_prev_view);
+
+ // Increment analog level to check monotonicity.
+ for (int i = 1; i <= 255; ++i) {
+ SCOPED_TRACE(AnalogLevelToString(i));
+ ResetVecArrayView(&multichannel_samples_view);
+ level_mapper.SimulateAnalogGain(
+ i, kRealDeviceLevelUnknown, multichannel_samples_view);
+ // TODO(alessiob): The test below is designed for state-less recording
+ // devices. If, for instance, soft-clipping is used, the test might need
+ // to be redesigned.
+ CheckIfMonotoneSamplesModules(
+ multichannel_samples_prev_view, multichannel_samples_view);
+
+ // Update prev.
+ multichannel_samples_prev = multichannel_samples;
+ multichannel_samples_prev_view = VecVecToVecArrayView(
+ &multichannel_samples_prev);
+ }
+ }
+}
+
+TEST(AnalogVolumeMapper, GainCurveShouldNotChangeSign) {
+ // Create view on orignal samples.
+ const auto multichannel_samples_orig_view = VecVecToVecArrayViewOfConst(
+ kTestMultiChannelSamples);
+
+ // Create output buffer.
+ auto multichannel_samples = kTestMultiChannelSamples;
+ auto multichannel_samples_view = VecVecToVecArrayView(&multichannel_samples);
+
+ // Test different mappings.
+ for (const auto& kind : kMappingKindsToTest) {
+ SCOPED_TRACE(MappingKindToString(kind));
+ webrtc::FakeRecordingDevice level_mapper(kind);
+
+ for (int i = 0; i <= 255; ++i) {
+ SCOPED_TRACE(AnalogLevelToString(i));
+ ResetVecArrayView(&multichannel_samples_view);
+ level_mapper.SimulateAnalogGain(
+ i, kRealDeviceLevelUnknown, multichannel_samples_view);
+ // TODO(alessiob): The test below is designed for state-less recording
+ // devices. If, for instance, soft-clipping is used, the test might need
+ // to be redesigned.
+ CheckSameSign(multichannel_samples_orig_view, multichannel_samples_view);
+ }
+ }
+}
+
+} // namespace webrtc

Powered by Google App Engine
This is Rietveld 408576698