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

Side by Side Diff: webrtc/modules/audio_processing/test/fake_recording_device_unittest.cc

Issue 2834643002: audioproc_f with simulated mic analog gain (Closed)
Patch Set: fake rec device boilerplate reduced, aec dump simulated analog gain logic moved Created 3 years, 6 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 unified diff | Download patch
OLDNEW
(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/optional.h"
18 #include "webrtc/base/ptr_util.h"
19 #include "webrtc/modules/audio_processing/test/fake_recording_device.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 constexpr size_t kC = 0; // Channel index.
114 constexpr size_t kS = 1; // Sample index.
115
116 // Check read.
117 auto buff = CreateChannelBufferWithData(kTestMultiChannelSamples);
118 for (size_t c = 0; c < kTestMultiChannelSamples.size(); ++c) {
119 for (size_t s = 0; s < kTestMultiChannelSamples[0].size(); ++s) {
120 EXPECT_EQ(kTestMultiChannelSamples[c][s], buff->channels()[c][s]);
121 }
122 }
123
124 // Check write.
125 buff->channels()[kC][kS] = -5.0f;
126 RTC_DCHECK_NE(buff->channels()[kC][kS],
127 kTestMultiChannelSamples[kC][kS]);
128
129 // Check reset.
130 WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff.get());
131 EXPECT_EQ(buff->channels()[kC][kS], kTestMultiChannelSamples[kC][kS]);
132 }
133
134 // Implicitly checks that changes to the mic and undo levels are visible to the
135 // FakeRecordingDeviceWorker implementation are injected in FakeRecordingDevice.
136 TEST(FakeRecordingDevice, TestWorkerAbstractClass) {
137 FakeRecordingDevice fake_recording_device(
138 kInitialMicLevel, FakeRecordingDevice::DeviceKind::LINEAR);
139
140 auto buff1 = CreateChannelBufferWithData(kTestMultiChannelSamples);
141 fake_recording_device.set_mic_level(100);
142 fake_recording_device.set_undo_mic_level(rtc::Optional<int>());
143 fake_recording_device.SimulateAnalogGain(buff1.get());
144
145 auto buff2 = CreateChannelBufferWithData(kTestMultiChannelSamples);
146 fake_recording_device.set_mic_level(200);
147 fake_recording_device.set_undo_mic_level(rtc::Optional<int>());
148 fake_recording_device.SimulateAnalogGain(buff2.get());
149
150 for (size_t c = 0; c < kTestMultiChannelSamples.size(); ++c) {
151 for (size_t s = 0; s < kTestMultiChannelSamples[0].size(); ++s) {
152 EXPECT_LE(std::abs(buff1->channels()[c][s]),
153 std::abs(buff2->channels()[c][s]));
154 }
155 }
156
157 auto buff3 = CreateChannelBufferWithData(kTestMultiChannelSamples);
158 fake_recording_device.set_mic_level(200);
159 fake_recording_device.set_undo_mic_level(rtc::Optional<int>(100));
160 fake_recording_device.SimulateAnalogGain(buff3.get());
161
162 for (size_t c = 0; c < kTestMultiChannelSamples.size(); ++c) {
163 for (size_t s = 0; s < kTestMultiChannelSamples[0].size(); ++s) {
164 EXPECT_LE(std::abs(buff1->channels()[c][s]),
165 std::abs(buff3->channels()[c][s]));
166 EXPECT_LE(std::abs(buff2->channels()[c][s]),
167 std::abs(buff3->channels()[c][s]));
168 }
169 }
170 }
171
172 TEST(FakeRecordingDevice, GainCurveShouldBeMonotone) {
173 // Create input-output buffers.
174 auto buff_prev = CreateChannelBufferWithData(kTestMultiChannelSamples);
175 auto buff_curr = CreateChannelBufferWithData(kTestMultiChannelSamples);
176
177 // Test different mappings.
178 for (auto fake_rec_device_kind : kFakeRecDeviceKinds) {
179 SCOPED_TRACE(FakeRecordingDeviceKindToString(fake_rec_device_kind));
180 FakeRecordingDevice fake_recording_device(
181 kInitialMicLevel, fake_rec_device_kind);
182 fake_recording_device.set_undo_mic_level(kRealDeviceLevelUnknown);
183 // TODO(alessiob): The test below is designed for state-less recording
184 // devices. If, for instance, a device has memory, the test might need
185 // to be redesigned (e.g., re-initialize fake recording device).
186
187 // Apply lowest analog level.
188 WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff_prev.get());
189 fake_recording_device.set_mic_level(0);
190 fake_recording_device.SimulateAnalogGain(buff_prev.get());
191
192 // Increment analog level to check monotonicity.
193 for (int i = 1; i <= 255; ++i) {
194 SCOPED_TRACE(AnalogLevelToString(i));
195 WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff_curr.get());
196 fake_recording_device.set_mic_level(i);
197 fake_recording_device.SimulateAnalogGain(buff_curr.get());
198 CheckIfMonotoneSamplesModules(buff_prev.get(), buff_curr.get());
199
200 // Update prev.
201 buff_prev.swap(buff_curr);
202 }
203 }
204 }
205
206 TEST(FakeRecordingDevice, GainCurveShouldNotChangeSign) {
207 // Create view on orignal samples.
208 std::unique_ptr<const ChannelBuffer<float>> buff_orig =
209 CreateChannelBufferWithData(kTestMultiChannelSamples);
210
211 // Create output buffer.
212 auto buff = CreateChannelBufferWithData(kTestMultiChannelSamples);
213
214 // Test different mappings.
215 for (auto fake_rec_device_kind : kFakeRecDeviceKinds) {
216 SCOPED_TRACE(FakeRecordingDeviceKindToString(fake_rec_device_kind));
217 FakeRecordingDevice fake_recording_device(
218 kInitialMicLevel, fake_rec_device_kind);
219 fake_recording_device.set_undo_mic_level(kRealDeviceLevelUnknown);
220 // TODO(alessiob): The test below is designed for state-less recording
221 // devices. If, for instance, a device has memory, the test might need
222 // to be redesigned (e.g., re-initialize fake recording device).
223
224 for (int i = 0; i <= 255; ++i) {
225 SCOPED_TRACE(AnalogLevelToString(i));
226 WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff.get());
227 fake_recording_device.set_mic_level(i);
228 fake_recording_device.SimulateAnalogGain(buff.get());
229 CheckSameSign(buff_orig.get(), buff.get());
230 }
231 }
232 }
233
234 } // namespace test
235 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698