OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2017 The WebRTC project authors. All Rights Reserved. |
| 3 * |
| 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 |
| 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ |
| 10 |
| 11 #include <memory> |
| 12 #include <sstream> |
| 13 #include <string> |
| 14 #include <vector> |
| 15 |
| 16 #include "webrtc/base/array_view.h" |
| 17 #include "webrtc/base/ptr_util.h" |
| 18 #include "webrtc/modules/audio_processing/test/fake_rec_device_identity.h" |
| 19 #include "webrtc/modules/audio_processing/test/fake_rec_device_linear.h" |
| 20 #include "webrtc/test/gtest.h" |
| 21 |
| 22 namespace webrtc { |
| 23 namespace test { |
| 24 namespace { |
| 25 |
| 26 rtc::Optional<int> kRealDeviceLevelUnknown; |
| 27 |
| 28 constexpr int kInitialMicLevel = 100; |
| 29 |
| 30 // TODO(alessiob): Add new fake recording device kinds here as they are added to |
| 31 // FakeRecordingDevice::DeviceKind. |
| 32 const std::vector<FakeRecordingDevice::DeviceKind> kFakeRecDeviceKinds = { |
| 33 FakeRecordingDevice::DeviceKind::IDENTITY, |
| 34 FakeRecordingDevice::DeviceKind::LINEAR, |
| 35 }; |
| 36 |
| 37 const std::vector<std::vector<float>> kTestMultiChannelSamples{ |
| 38 std::vector<float>{-10.0, -1.0, -0.1, 0.0, 0.1, 1.0, 10.0}}; |
| 39 |
| 40 // Writes samples into ChannelBuffer<float>. |
| 41 void WritesDataIntoChannelBuffer(const std::vector<std::vector<float>>& data, |
| 42 ChannelBuffer<float>* buff) { |
| 43 EXPECT_EQ(data.size(), buff->num_channels()); |
| 44 EXPECT_EQ(data[0].size(), buff->num_frames()); |
| 45 for (size_t c = 0; c < buff->num_channels(); ++c) { |
| 46 for (size_t f = 0; f < buff->num_frames(); ++f) { |
| 47 buff->channels()[c][f] = data[c][f]; |
| 48 } |
| 49 } |
| 50 } |
| 51 |
| 52 std::unique_ptr<ChannelBuffer<float>> CreateChannelBufferWithData( |
| 53 const std::vector<std::vector<float>>& data) { |
| 54 auto buff = rtc::MakeUnique<ChannelBuffer<float>>( |
| 55 data[0].size(), data.size()); |
| 56 WritesDataIntoChannelBuffer(data, buff.get()); |
| 57 return buff; |
| 58 } |
| 59 |
| 60 // Checks that the samples modified using monotonic level values are also |
| 61 // monotonic. |
| 62 void CheckIfMonotoneSamplesModules( |
| 63 const ChannelBuffer<float>* prev, const ChannelBuffer<float>* curr) { |
| 64 RTC_DCHECK_EQ(prev->num_channels(), curr->num_channels()); |
| 65 RTC_DCHECK_EQ(prev->num_frames(), curr->num_frames()); |
| 66 bool valid = true; |
| 67 for (size_t i = 0; i < prev->num_channels(); ++i) { |
| 68 for (size_t j = 0; j < prev->num_frames(); ++j) { |
| 69 valid = std::fabs(prev->channels()[i][j]) <= std::fabs( |
| 70 curr->channels()[i][j]); |
| 71 if (!valid) { break; } |
| 72 } |
| 73 if (!valid) { break; } |
| 74 } |
| 75 EXPECT_TRUE(valid); |
| 76 } |
| 77 |
| 78 // Checks that the samples in each pair have the same sign unless the sample in |
| 79 // |dst| is zero (because of zero gain). |
| 80 void CheckSameSign( |
| 81 const ChannelBuffer<float>* src, const ChannelBuffer<float>* dst) { |
| 82 RTC_DCHECK_EQ(src->num_channels(), dst->num_channels()); |
| 83 RTC_DCHECK_EQ(src->num_frames(), dst->num_frames()); |
| 84 const auto fsgn = [](float x) { return ((x < 0) ? -1 : (x > 0) ? 1 : 0); }; |
| 85 bool valid = true; |
| 86 for (size_t i = 0; i < src->num_channels(); ++i) { |
| 87 for (size_t j = 0; j < src->num_frames(); ++j) { |
| 88 valid = dst->channels()[i][j] == 0.0f || |
| 89 fsgn(src->channels()[i][j]) == fsgn(dst->channels()[i][j]); |
| 90 if (!valid) { break; } |
| 91 } |
| 92 if (!valid) { break; } |
| 93 } |
| 94 EXPECT_TRUE(valid); |
| 95 } |
| 96 |
| 97 std::string FakeRecordingDeviceKindToString( |
| 98 FakeRecordingDevice::DeviceKind fake_rec_device_kind) { |
| 99 std::ostringstream ss; |
| 100 ss << "fake recording device: " << static_cast<size_t>(fake_rec_device_kind); |
| 101 return ss.str(); |
| 102 } |
| 103 |
| 104 std::string AnalogLevelToString(int level) { |
| 105 std::ostringstream ss; |
| 106 ss << "analog level: " << level; |
| 107 return ss.str(); |
| 108 } |
| 109 |
| 110 } // namespace |
| 111 |
| 112 TEST(FakeRecordingDevice, CheckHelperFunctions) { |
| 113 // Check read. |
| 114 auto buff = CreateChannelBufferWithData(kTestMultiChannelSamples); |
| 115 for (size_t c = 0; c < kTestMultiChannelSamples.size(); ++c) { |
| 116 for (size_t f = 0; f < kTestMultiChannelSamples[0].size(); ++f) { |
| 117 EXPECT_EQ(kTestMultiChannelSamples[c][f], buff->channels()[c][f]); |
| 118 } |
| 119 } |
| 120 |
| 121 // Check write. |
| 122 constexpr size_t channel_index = 0; |
| 123 constexpr size_t sample_index = 1; |
| 124 buff->channels()[channel_index][sample_index] = -5.0f; |
| 125 RTC_DCHECK_NE(buff->channels()[channel_index][sample_index], |
| 126 kTestMultiChannelSamples[channel_index][sample_index]); |
| 127 |
| 128 // Check reset. |
| 129 WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff.get()); |
| 130 EXPECT_EQ(buff->channels()[channel_index][sample_index], |
| 131 kTestMultiChannelSamples[channel_index][sample_index]); |
| 132 } |
| 133 |
| 134 TEST(FakeRecordingDevice, GainCurveShouldBeMonotone) { |
| 135 // Create input-output buffers. |
| 136 auto buff_prev = CreateChannelBufferWithData(kTestMultiChannelSamples); |
| 137 auto buff_curr = CreateChannelBufferWithData(kTestMultiChannelSamples); |
| 138 |
| 139 // Test different mappings. |
| 140 for (auto fake_rec_device_kind : kFakeRecDeviceKinds) { |
| 141 SCOPED_TRACE(FakeRecordingDeviceKindToString(fake_rec_device_kind)); |
| 142 auto fake_recording_device = FakeRecordingDevice::GetFakeRecDevice( |
| 143 fake_rec_device_kind, kInitialMicLevel); |
| 144 fake_recording_device->set_undo_mic_level(kRealDeviceLevelUnknown); |
| 145 // TODO(alessiob): The test below is designed for state-less recording |
| 146 // devices. If, for instance, a device has memory, the test might need |
| 147 // to be redesigned (e.g., re-initialize fake recording device). |
| 148 |
| 149 // Apply lowest analog level. |
| 150 WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff_prev.get()); |
| 151 fake_recording_device->set_mic_level(0); |
| 152 fake_recording_device->SimulateAnalogGain(buff_prev.get()); |
| 153 |
| 154 // Increment analog level to check monotonicity. |
| 155 for (int i = 1; i <= 255; ++i) { |
| 156 SCOPED_TRACE(AnalogLevelToString(i)); |
| 157 WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff_curr.get()); |
| 158 fake_recording_device->set_mic_level(i); |
| 159 fake_recording_device->SimulateAnalogGain(buff_curr.get()); |
| 160 CheckIfMonotoneSamplesModules(buff_prev.get(), buff_curr.get()); |
| 161 |
| 162 // Update prev. |
| 163 buff_prev.swap(buff_curr); |
| 164 } |
| 165 } |
| 166 } |
| 167 |
| 168 TEST(FakeRecordingDevice, GainCurveShouldNotChangeSign) { |
| 169 // Create view on orignal samples. |
| 170 std::unique_ptr<const ChannelBuffer<float>> buff_orig = |
| 171 CreateChannelBufferWithData(kTestMultiChannelSamples); |
| 172 |
| 173 // Create output buffer. |
| 174 auto buff = CreateChannelBufferWithData(kTestMultiChannelSamples); |
| 175 |
| 176 // Test different mappings. |
| 177 for (auto fake_rec_device_kind : kFakeRecDeviceKinds) { |
| 178 SCOPED_TRACE(FakeRecordingDeviceKindToString(fake_rec_device_kind)); |
| 179 auto fake_recording_device = FakeRecordingDevice::GetFakeRecDevice( |
| 180 fake_rec_device_kind, kInitialMicLevel); |
| 181 fake_recording_device->set_undo_mic_level(kRealDeviceLevelUnknown); |
| 182 // TODO(alessiob): The test below is designed for state-less recording |
| 183 // devices. If, for instance, a device has memory, the test might need |
| 184 // to be redesigned (e.g., re-initialize fake recording device). |
| 185 |
| 186 for (int i = 0; i <= 255; ++i) { |
| 187 SCOPED_TRACE(AnalogLevelToString(i)); |
| 188 WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff.get()); |
| 189 fake_recording_device->set_mic_level(i); |
| 190 fake_recording_device->SimulateAnalogGain(buff.get()); |
| 191 CheckSameSign(buff_orig.get(), buff.get()); |
| 192 } |
| 193 } |
| 194 } |
| 195 |
| 196 } // namespace test |
| 197 } // namespace webrtc |
OLD | NEW |