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 <cmath> |
| 12 #include <memory> |
| 13 #include <sstream> |
| 14 #include <string> |
| 15 #include <vector> |
| 16 |
| 17 #include "webrtc/modules/audio_processing/test/fake_recording_device.h" |
| 18 #include "webrtc/rtc_base/array_view.h" |
| 19 #include "webrtc/rtc_base/optional.h" |
| 20 #include "webrtc/rtc_base/ptr_util.h" |
| 21 #include "webrtc/test/gtest.h" |
| 22 |
| 23 namespace webrtc { |
| 24 namespace test { |
| 25 namespace { |
| 26 |
| 27 rtc::Optional<int> kRealDeviceLevelUnknown; |
| 28 |
| 29 constexpr int kInitialMicLevel = 100; |
| 30 |
| 31 // TODO(alessiob): Add new fake recording device kind values here as they are |
| 32 // added in FakeRecordingDevice::FakeRecordingDevice. |
| 33 const std::vector<int> kFakeRecDeviceKinds = {0, 1}; |
| 34 |
| 35 const std::vector<std::vector<float>> kTestMultiChannelSamples{ |
| 36 std::vector<float>{-10.0, -1.0, -0.1, 0.0, 0.1, 1.0, 10.0}}; |
| 37 |
| 38 // Writes samples into ChannelBuffer<float>. |
| 39 void WritesDataIntoChannelBuffer(const std::vector<std::vector<float>>& data, |
| 40 ChannelBuffer<float>* buff) { |
| 41 EXPECT_EQ(data.size(), buff->num_channels()); |
| 42 EXPECT_EQ(data[0].size(), buff->num_frames()); |
| 43 for (size_t c = 0; c < buff->num_channels(); ++c) { |
| 44 for (size_t f = 0; f < buff->num_frames(); ++f) { |
| 45 buff->channels()[c][f] = data[c][f]; |
| 46 } |
| 47 } |
| 48 } |
| 49 |
| 50 std::unique_ptr<ChannelBuffer<float>> CreateChannelBufferWithData( |
| 51 const std::vector<std::vector<float>>& data) { |
| 52 auto buff = |
| 53 rtc::MakeUnique<ChannelBuffer<float>>(data[0].size(), data.size()); |
| 54 WritesDataIntoChannelBuffer(data, buff.get()); |
| 55 return buff; |
| 56 } |
| 57 |
| 58 // Checks that the samples modified using monotonic level values are also |
| 59 // monotonic. |
| 60 void CheckIfMonotoneSamplesModules(const ChannelBuffer<float>* prev, |
| 61 const ChannelBuffer<float>* curr) { |
| 62 RTC_DCHECK_EQ(prev->num_channels(), curr->num_channels()); |
| 63 RTC_DCHECK_EQ(prev->num_frames(), curr->num_frames()); |
| 64 bool valid = true; |
| 65 for (size_t i = 0; i < prev->num_channels(); ++i) { |
| 66 for (size_t j = 0; j < prev->num_frames(); ++j) { |
| 67 valid = std::fabs(prev->channels()[i][j]) <= |
| 68 std::fabs(curr->channels()[i][j]); |
| 69 if (!valid) { |
| 70 break; |
| 71 } |
| 72 } |
| 73 if (!valid) { |
| 74 break; |
| 75 } |
| 76 } |
| 77 EXPECT_TRUE(valid); |
| 78 } |
| 79 |
| 80 // Checks that the samples in each pair have the same sign unless the sample in |
| 81 // |dst| is zero (because of zero gain). |
| 82 void CheckSameSign(const ChannelBuffer<float>* src, |
| 83 const ChannelBuffer<float>* dst) { |
| 84 RTC_DCHECK_EQ(src->num_channels(), dst->num_channels()); |
| 85 RTC_DCHECK_EQ(src->num_frames(), dst->num_frames()); |
| 86 const auto fsgn = [](float x) { return ((x < 0) ? -1 : (x > 0) ? 1 : 0); }; |
| 87 bool valid = true; |
| 88 for (size_t i = 0; i < src->num_channels(); ++i) { |
| 89 for (size_t j = 0; j < src->num_frames(); ++j) { |
| 90 valid = dst->channels()[i][j] == 0.0f || |
| 91 fsgn(src->channels()[i][j]) == fsgn(dst->channels()[i][j]); |
| 92 if (!valid) { |
| 93 break; |
| 94 } |
| 95 } |
| 96 if (!valid) { |
| 97 break; |
| 98 } |
| 99 } |
| 100 EXPECT_TRUE(valid); |
| 101 } |
| 102 |
| 103 std::string FakeRecordingDeviceKindToString(int fake_rec_device_kind) { |
| 104 std::ostringstream ss; |
| 105 ss << "fake recording device: " << fake_rec_device_kind; |
| 106 return ss.str(); |
| 107 } |
| 108 |
| 109 std::string AnalogLevelToString(int level) { |
| 110 std::ostringstream ss; |
| 111 ss << "analog level: " << level; |
| 112 return ss.str(); |
| 113 } |
| 114 |
| 115 } // namespace |
| 116 |
| 117 TEST(FakeRecordingDevice, CheckHelperFunctions) { |
| 118 constexpr size_t kC = 0; // Channel index. |
| 119 constexpr size_t kS = 1; // Sample index. |
| 120 |
| 121 // Check read. |
| 122 auto buff = CreateChannelBufferWithData(kTestMultiChannelSamples); |
| 123 for (size_t c = 0; c < kTestMultiChannelSamples.size(); ++c) { |
| 124 for (size_t s = 0; s < kTestMultiChannelSamples[0].size(); ++s) { |
| 125 EXPECT_EQ(kTestMultiChannelSamples[c][s], buff->channels()[c][s]); |
| 126 } |
| 127 } |
| 128 |
| 129 // Check write. |
| 130 buff->channels()[kC][kS] = -5.0f; |
| 131 RTC_DCHECK_NE(buff->channels()[kC][kS], kTestMultiChannelSamples[kC][kS]); |
| 132 |
| 133 // Check reset. |
| 134 WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff.get()); |
| 135 EXPECT_EQ(buff->channels()[kC][kS], kTestMultiChannelSamples[kC][kS]); |
| 136 } |
| 137 |
| 138 // Implicitly checks that changes to the mic and undo levels are visible to the |
| 139 // FakeRecordingDeviceWorker implementation are injected in FakeRecordingDevice. |
| 140 TEST(FakeRecordingDevice, TestWorkerAbstractClass) { |
| 141 FakeRecordingDevice fake_recording_device(kInitialMicLevel, 1); |
| 142 |
| 143 auto buff1 = CreateChannelBufferWithData(kTestMultiChannelSamples); |
| 144 fake_recording_device.SetMicLevel(100); |
| 145 fake_recording_device.SetUndoMicLevel(rtc::Optional<int>()); |
| 146 fake_recording_device.SimulateAnalogGain(buff1.get()); |
| 147 |
| 148 auto buff2 = CreateChannelBufferWithData(kTestMultiChannelSamples); |
| 149 fake_recording_device.SetMicLevel(200); |
| 150 fake_recording_device.SetUndoMicLevel(rtc::Optional<int>()); |
| 151 fake_recording_device.SimulateAnalogGain(buff2.get()); |
| 152 |
| 153 for (size_t c = 0; c < kTestMultiChannelSamples.size(); ++c) { |
| 154 for (size_t s = 0; s < kTestMultiChannelSamples[0].size(); ++s) { |
| 155 EXPECT_LE(std::abs(buff1->channels()[c][s]), |
| 156 std::abs(buff2->channels()[c][s])); |
| 157 } |
| 158 } |
| 159 |
| 160 auto buff3 = CreateChannelBufferWithData(kTestMultiChannelSamples); |
| 161 fake_recording_device.SetMicLevel(200); |
| 162 fake_recording_device.SetUndoMicLevel(rtc::Optional<int>(100)); |
| 163 fake_recording_device.SimulateAnalogGain(buff3.get()); |
| 164 |
| 165 for (size_t c = 0; c < kTestMultiChannelSamples.size(); ++c) { |
| 166 for (size_t s = 0; s < kTestMultiChannelSamples[0].size(); ++s) { |
| 167 EXPECT_LE(std::abs(buff1->channels()[c][s]), |
| 168 std::abs(buff3->channels()[c][s])); |
| 169 EXPECT_LE(std::abs(buff2->channels()[c][s]), |
| 170 std::abs(buff3->channels()[c][s])); |
| 171 } |
| 172 } |
| 173 } |
| 174 |
| 175 TEST(FakeRecordingDevice, GainCurveShouldBeMonotone) { |
| 176 // Create input-output buffers. |
| 177 auto buff_prev = CreateChannelBufferWithData(kTestMultiChannelSamples); |
| 178 auto buff_curr = CreateChannelBufferWithData(kTestMultiChannelSamples); |
| 179 |
| 180 // Test different mappings. |
| 181 for (auto fake_rec_device_kind : kFakeRecDeviceKinds) { |
| 182 SCOPED_TRACE(FakeRecordingDeviceKindToString(fake_rec_device_kind)); |
| 183 FakeRecordingDevice fake_recording_device(kInitialMicLevel, |
| 184 fake_rec_device_kind); |
| 185 fake_recording_device.SetUndoMicLevel(kRealDeviceLevelUnknown); |
| 186 // TODO(alessiob): The test below is designed for state-less recording |
| 187 // devices. If, for instance, a device has memory, the test might need |
| 188 // to be redesigned (e.g., re-initialize fake recording device). |
| 189 |
| 190 // Apply lowest analog level. |
| 191 WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff_prev.get()); |
| 192 fake_recording_device.SetMicLevel(0); |
| 193 fake_recording_device.SimulateAnalogGain(buff_prev.get()); |
| 194 |
| 195 // Increment analog level to check monotonicity. |
| 196 for (int i = 1; i <= 255; ++i) { |
| 197 SCOPED_TRACE(AnalogLevelToString(i)); |
| 198 WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff_curr.get()); |
| 199 fake_recording_device.SetMicLevel(i); |
| 200 fake_recording_device.SimulateAnalogGain(buff_curr.get()); |
| 201 CheckIfMonotoneSamplesModules(buff_prev.get(), buff_curr.get()); |
| 202 |
| 203 // Update prev. |
| 204 buff_prev.swap(buff_curr); |
| 205 } |
| 206 } |
| 207 } |
| 208 |
| 209 TEST(FakeRecordingDevice, GainCurveShouldNotChangeSign) { |
| 210 // Create view on orignal samples. |
| 211 std::unique_ptr<const ChannelBuffer<float>> buff_orig = |
| 212 CreateChannelBufferWithData(kTestMultiChannelSamples); |
| 213 |
| 214 // Create output buffer. |
| 215 auto buff = CreateChannelBufferWithData(kTestMultiChannelSamples); |
| 216 |
| 217 // Test different mappings. |
| 218 for (auto fake_rec_device_kind : kFakeRecDeviceKinds) { |
| 219 SCOPED_TRACE(FakeRecordingDeviceKindToString(fake_rec_device_kind)); |
| 220 FakeRecordingDevice fake_recording_device(kInitialMicLevel, |
| 221 fake_rec_device_kind); |
| 222 fake_recording_device.SetUndoMicLevel(kRealDeviceLevelUnknown); |
| 223 // TODO(alessiob): The test below is designed for state-less recording |
| 224 // devices. If, for instance, a device has memory, the test might need |
| 225 // to be redesigned (e.g., re-initialize fake recording device). |
| 226 |
| 227 for (int i = 0; i <= 255; ++i) { |
| 228 SCOPED_TRACE(AnalogLevelToString(i)); |
| 229 WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff.get()); |
| 230 fake_recording_device.SetMicLevel(i); |
| 231 fake_recording_device.SimulateAnalogGain(buff.get()); |
| 232 CheckSameSign(buff_orig.get(), buff.get()); |
| 233 } |
| 234 } |
| 235 } |
| 236 |
| 237 } // namespace test |
| 238 } // namespace webrtc |
OLD | NEW |