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

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

Powered by Google App Engine
This is Rietveld 408576698