| 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..080bb586fc9e83f04b6977107c63e354d0a1ff1c
|
| --- /dev/null
|
| +++ b/webrtc/modules/audio_processing/test/fake_recording_device_unittest.cc
|
| @@ -0,0 +1,197 @@
|
| +/*
|
| + * 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 <memory>
|
| +#include <sstream>
|
| +#include <string>
|
| +#include <vector>
|
| +
|
| +#include "webrtc/base/array_view.h"
|
| +#include "webrtc/base/ptr_util.h"
|
| +#include "webrtc/modules/audio_processing/test/fake_rec_device_identity.h"
|
| +#include "webrtc/modules/audio_processing/test/fake_rec_device_linear.h"
|
| +#include "webrtc/test/gtest.h"
|
| +
|
| +namespace webrtc {
|
| +namespace test {
|
| +namespace {
|
| +
|
| +rtc::Optional<int> kRealDeviceLevelUnknown;
|
| +
|
| +constexpr int kInitialMicLevel = 100;
|
| +
|
| +// TODO(alessiob): Add new fake recording device kinds here as they are added to
|
| +// FakeRecordingDevice::DeviceKind.
|
| +const std::vector<FakeRecordingDevice::DeviceKind> kFakeRecDeviceKinds = {
|
| + FakeRecordingDevice::DeviceKind::IDENTITY,
|
| + FakeRecordingDevice::DeviceKind::LINEAR,
|
| +};
|
| +
|
| +const std::vector<std::vector<float>> kTestMultiChannelSamples{
|
| + std::vector<float>{-10.0, -1.0, -0.1, 0.0, 0.1, 1.0, 10.0}};
|
| +
|
| +// Writes samples into ChannelBuffer<float>.
|
| +void WritesDataIntoChannelBuffer(const std::vector<std::vector<float>>& data,
|
| + ChannelBuffer<float>* buff) {
|
| + EXPECT_EQ(data.size(), buff->num_channels());
|
| + EXPECT_EQ(data[0].size(), buff->num_frames());
|
| + for (size_t c = 0; c < buff->num_channels(); ++c) {
|
| + for (size_t f = 0; f < buff->num_frames(); ++f) {
|
| + buff->channels()[c][f] = data[c][f];
|
| + }
|
| + }
|
| +}
|
| +
|
| +std::unique_ptr<ChannelBuffer<float>> CreateChannelBufferWithData(
|
| + const std::vector<std::vector<float>>& data) {
|
| + auto buff = rtc::MakeUnique<ChannelBuffer<float>>(
|
| + data[0].size(), data.size());
|
| + WritesDataIntoChannelBuffer(data, buff.get());
|
| + return buff;
|
| +}
|
| +
|
| +// Checks that the samples modified using monotonic level values are also
|
| +// monotonic.
|
| +void CheckIfMonotoneSamplesModules(
|
| + const ChannelBuffer<float>* prev, const ChannelBuffer<float>* curr) {
|
| + RTC_DCHECK_EQ(prev->num_channels(), curr->num_channels());
|
| + RTC_DCHECK_EQ(prev->num_frames(), curr->num_frames());
|
| + bool valid = true;
|
| + for (size_t i = 0; i < prev->num_channels(); ++i) {
|
| + for (size_t j = 0; j < prev->num_frames(); ++j) {
|
| + valid = std::fabs(prev->channels()[i][j]) <= std::fabs(
|
| + curr->channels()[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 ChannelBuffer<float>* src, const ChannelBuffer<float>* dst) {
|
| + RTC_DCHECK_EQ(src->num_channels(), dst->num_channels());
|
| + RTC_DCHECK_EQ(src->num_frames(), dst->num_frames());
|
| + const auto fsgn = [](float x) { return ((x < 0) ? -1 : (x > 0) ? 1 : 0); };
|
| + bool valid = true;
|
| + for (size_t i = 0; i < src->num_channels(); ++i) {
|
| + for (size_t j = 0; j < src->num_frames(); ++j) {
|
| + valid = dst->channels()[i][j] == 0.0f ||
|
| + fsgn(src->channels()[i][j]) == fsgn(dst->channels()[i][j]);
|
| + if (!valid) { break; }
|
| + }
|
| + if (!valid) { break; }
|
| + }
|
| + EXPECT_TRUE(valid);
|
| +}
|
| +
|
| +std::string FakeRecordingDeviceKindToString(
|
| + FakeRecordingDevice::DeviceKind fake_rec_device_kind) {
|
| + std::ostringstream ss;
|
| + ss << "fake recording device: " << static_cast<size_t>(fake_rec_device_kind);
|
| + return ss.str();
|
| +}
|
| +
|
| +std::string AnalogLevelToString(int level) {
|
| + std::ostringstream ss;
|
| + ss << "analog level: " << level;
|
| + return ss.str();
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +TEST(FakeRecordingDevice, CheckHelperFunctions) {
|
| + // Check read.
|
| + auto buff = CreateChannelBufferWithData(kTestMultiChannelSamples);
|
| + for (size_t c = 0; c < kTestMultiChannelSamples.size(); ++c) {
|
| + for (size_t f = 0; f < kTestMultiChannelSamples[0].size(); ++f) {
|
| + EXPECT_EQ(kTestMultiChannelSamples[c][f], buff->channels()[c][f]);
|
| + }
|
| + }
|
| +
|
| + // Check write.
|
| + constexpr size_t channel_index = 0;
|
| + constexpr size_t sample_index = 1;
|
| + buff->channels()[channel_index][sample_index] = -5.0f;
|
| + RTC_DCHECK_NE(buff->channels()[channel_index][sample_index],
|
| + kTestMultiChannelSamples[channel_index][sample_index]);
|
| +
|
| + // Check reset.
|
| + WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff.get());
|
| + EXPECT_EQ(buff->channels()[channel_index][sample_index],
|
| + kTestMultiChannelSamples[channel_index][sample_index]);
|
| +}
|
| +
|
| +TEST(FakeRecordingDevice, GainCurveShouldBeMonotone) {
|
| + // Create input-output buffers.
|
| + auto buff_prev = CreateChannelBufferWithData(kTestMultiChannelSamples);
|
| + auto buff_curr = CreateChannelBufferWithData(kTestMultiChannelSamples);
|
| +
|
| + // Test different mappings.
|
| + for (auto fake_rec_device_kind : kFakeRecDeviceKinds) {
|
| + SCOPED_TRACE(FakeRecordingDeviceKindToString(fake_rec_device_kind));
|
| + auto fake_recording_device = FakeRecordingDevice::GetFakeRecDevice(
|
| + fake_rec_device_kind, kInitialMicLevel);
|
| + fake_recording_device->set_undo_mic_level(kRealDeviceLevelUnknown);
|
| + // TODO(alessiob): The test below is designed for state-less recording
|
| + // devices. If, for instance, a device has memory, the test might need
|
| + // to be redesigned (e.g., re-initialize fake recording device).
|
| +
|
| + // Apply lowest analog level.
|
| + WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff_prev.get());
|
| + fake_recording_device->set_mic_level(0);
|
| + fake_recording_device->SimulateAnalogGain(buff_prev.get());
|
| +
|
| + // Increment analog level to check monotonicity.
|
| + for (int i = 1; i <= 255; ++i) {
|
| + SCOPED_TRACE(AnalogLevelToString(i));
|
| + WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff_curr.get());
|
| + fake_recording_device->set_mic_level(i);
|
| + fake_recording_device->SimulateAnalogGain(buff_curr.get());
|
| + CheckIfMonotoneSamplesModules(buff_prev.get(), buff_curr.get());
|
| +
|
| + // Update prev.
|
| + buff_prev.swap(buff_curr);
|
| + }
|
| + }
|
| +}
|
| +
|
| +TEST(FakeRecordingDevice, GainCurveShouldNotChangeSign) {
|
| + // Create view on orignal samples.
|
| + std::unique_ptr<const ChannelBuffer<float>> buff_orig =
|
| + CreateChannelBufferWithData(kTestMultiChannelSamples);
|
| +
|
| + // Create output buffer.
|
| + auto buff = CreateChannelBufferWithData(kTestMultiChannelSamples);
|
| +
|
| + // Test different mappings.
|
| + for (auto fake_rec_device_kind : kFakeRecDeviceKinds) {
|
| + SCOPED_TRACE(FakeRecordingDeviceKindToString(fake_rec_device_kind));
|
| + auto fake_recording_device = FakeRecordingDevice::GetFakeRecDevice(
|
| + fake_rec_device_kind, kInitialMicLevel);
|
| + fake_recording_device->set_undo_mic_level(kRealDeviceLevelUnknown);
|
| + // TODO(alessiob): The test below is designed for state-less recording
|
| + // devices. If, for instance, a device has memory, the test might need
|
| + // to be redesigned (e.g., re-initialize fake recording device).
|
| +
|
| + for (int i = 0; i <= 255; ++i) {
|
| + SCOPED_TRACE(AnalogLevelToString(i));
|
| + WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff.get());
|
| + fake_recording_device->set_mic_level(i);
|
| + fake_recording_device->SimulateAnalogGain(buff.get());
|
| + CheckSameSign(buff_orig.get(), buff.get());
|
| + }
|
| + }
|
| +}
|
| +
|
| +} // namespace test
|
| +} // namespace webrtc
|
|
|