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