summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/modules/audio_processing/test/fake_recording_device_unittest.cc
blob: 2ac8b1dc4860df358820f236e3d7088703b6e4d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 *  Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "modules/audio_processing/test/fake_recording_device.h"

#include <cmath>
#include <memory>
#include <string>
#include <vector>

#include "api/array_view.h"
#include "rtc_base/strings/string_builder.h"
#include "test/gtest.h"

namespace webrtc {
namespace test {
namespace {

constexpr int kInitialMicLevel = 100;

// TODO(alessiob): Add new fake recording device kind values here as they are
// added in FakeRecordingDevice::FakeRecordingDevice.
const std::vector<int> kFakeRecDeviceKinds = {0, 1, 2};

const std::vector<std::vector<float>> kTestMultiChannelSamples{
    std::vector<float>{-10.f, -1.f, -0.1f, 0.f, 0.1f, 1.f, 10.f}};

// Writes samples into ChannelBuffer<float>.
void WritesDataIntoChannelBuffer(const std::vector<std::vector<float>>& data,
                                 ChannelBuffer<float>* buff) {
  EXPECT_EQ(data.size(), buff->num_channels());
  EXPECT_EQ(data[0].size(), buff->num_frames());
  for (size_t c = 0; c < buff->num_channels(); ++c) {
    for (size_t f = 0; f < buff->num_frames(); ++f) {
      buff->channels()[c][f] = data[c][f];
    }
  }
}

std::unique_ptr<ChannelBuffer<float>> CreateChannelBufferWithData(
    const std::vector<std::vector<float>>& data) {
  auto buff =
      std::make_unique<ChannelBuffer<float>>(data[0].size(), data.size());
  WritesDataIntoChannelBuffer(data, buff.get());
  return buff;
}

// Checks that the samples modified using monotonic level values are also
// monotonic.
void CheckIfMonotoneSamplesModules(const ChannelBuffer<float>* prev,
                                   const ChannelBuffer<float>* curr) {
  RTC_DCHECK_EQ(prev->num_channels(), curr->num_channels());
  RTC_DCHECK_EQ(prev->num_frames(), curr->num_frames());
  bool valid = true;
  for (size_t i = 0; i < prev->num_channels(); ++i) {
    for (size_t j = 0; j < prev->num_frames(); ++j) {
      valid = std::fabs(prev->channels()[i][j]) <=
              std::fabs(curr->channels()[i][j]);
      if (!valid) {
        break;
      }
    }
    if (!valid) {
      break;
    }
  }
  EXPECT_TRUE(valid);
}

// Checks that the samples in each pair have the same sign unless the sample in
// `dst` is zero (because of zero gain).
void CheckSameSign(const ChannelBuffer<float>* src,
                   const ChannelBuffer<float>* dst) {
  RTC_DCHECK_EQ(src->num_channels(), dst->num_channels());
  RTC_DCHECK_EQ(src->num_frames(), dst->num_frames());
  const auto fsgn = [](float x) { return ((x < 0) ? -1 : (x > 0) ? 1 : 0); };
  bool valid = true;
  for (size_t i = 0; i < src->num_channels(); ++i) {
    for (size_t j = 0; j < src->num_frames(); ++j) {
      valid = dst->channels()[i][j] == 0.0f ||
              fsgn(src->channels()[i][j]) == fsgn(dst->channels()[i][j]);
      if (!valid) {
        break;
      }
    }
    if (!valid) {
      break;
    }
  }
  EXPECT_TRUE(valid);
}

std::string FakeRecordingDeviceKindToString(int fake_rec_device_kind) {
  rtc::StringBuilder ss;
  ss << "fake recording device: " << fake_rec_device_kind;
  return ss.Release();
}

std::string AnalogLevelToString(int level) {
  rtc::StringBuilder ss;
  ss << "analog level: " << level;
  return ss.Release();
}

}  // namespace

TEST(FakeRecordingDevice, CheckHelperFunctions) {
  constexpr size_t kC = 0;  // Channel index.
  constexpr size_t kS = 1;  // Sample index.

  // Check read.
  auto buff = CreateChannelBufferWithData(kTestMultiChannelSamples);
  for (size_t c = 0; c < kTestMultiChannelSamples.size(); ++c) {
    for (size_t s = 0; s < kTestMultiChannelSamples[0].size(); ++s) {
      EXPECT_EQ(kTestMultiChannelSamples[c][s], buff->channels()[c][s]);
    }
  }

  // Check write.
  buff->channels()[kC][kS] = -5.0f;
  RTC_DCHECK_NE(buff->channels()[kC][kS], kTestMultiChannelSamples[kC][kS]);

  // Check reset.
  WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff.get());
  EXPECT_EQ(buff->channels()[kC][kS], kTestMultiChannelSamples[kC][kS]);
}

// Implicitly checks that changes to the mic and undo levels are visible to the
// FakeRecordingDeviceWorker implementation are injected in FakeRecordingDevice.
TEST(FakeRecordingDevice, TestWorkerAbstractClass) {
  FakeRecordingDevice fake_recording_device(kInitialMicLevel, 1);

  auto buff1 = CreateChannelBufferWithData(kTestMultiChannelSamples);
  fake_recording_device.SetMicLevel(100);
  fake_recording_device.SimulateAnalogGain(buff1.get());

  auto buff2 = CreateChannelBufferWithData(kTestMultiChannelSamples);
  fake_recording_device.SetMicLevel(200);
  fake_recording_device.SimulateAnalogGain(buff2.get());

  for (size_t c = 0; c < kTestMultiChannelSamples.size(); ++c) {
    for (size_t s = 0; s < kTestMultiChannelSamples[0].size(); ++s) {
      EXPECT_LE(std::abs(buff1->channels()[c][s]),
                std::abs(buff2->channels()[c][s]));
    }
  }

  auto buff3 = CreateChannelBufferWithData(kTestMultiChannelSamples);
  fake_recording_device.SetMicLevel(200);
  fake_recording_device.SetUndoMicLevel(100);
  fake_recording_device.SimulateAnalogGain(buff3.get());

  for (size_t c = 0; c < kTestMultiChannelSamples.size(); ++c) {
    for (size_t s = 0; s < kTestMultiChannelSamples[0].size(); ++s) {
      EXPECT_LE(std::abs(buff1->channels()[c][s]),
                std::abs(buff3->channels()[c][s]));
      EXPECT_LE(std::abs(buff2->channels()[c][s]),
                std::abs(buff3->channels()[c][s]));
    }
  }
}

TEST(FakeRecordingDevice, GainCurveShouldBeMonotone) {
  // Create input-output buffers.
  auto buff_prev = CreateChannelBufferWithData(kTestMultiChannelSamples);
  auto buff_curr = CreateChannelBufferWithData(kTestMultiChannelSamples);

  // Test different mappings.
  for (auto fake_rec_device_kind : kFakeRecDeviceKinds) {
    SCOPED_TRACE(FakeRecordingDeviceKindToString(fake_rec_device_kind));
    FakeRecordingDevice fake_recording_device(kInitialMicLevel,
                                              fake_rec_device_kind);
    // TODO(alessiob): The test below is designed for state-less recording
    // devices. If, for instance, a device has memory, the test might need
    // to be redesigned (e.g., re-initialize fake recording device).

    // Apply lowest analog level.
    WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff_prev.get());
    fake_recording_device.SetMicLevel(0);
    fake_recording_device.SimulateAnalogGain(buff_prev.get());

    // Increment analog level to check monotonicity.
    for (int i = 1; i <= 255; ++i) {
      SCOPED_TRACE(AnalogLevelToString(i));
      WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff_curr.get());
      fake_recording_device.SetMicLevel(i);
      fake_recording_device.SimulateAnalogGain(buff_curr.get());
      CheckIfMonotoneSamplesModules(buff_prev.get(), buff_curr.get());

      // Update prev.
      buff_prev.swap(buff_curr);
    }
  }
}

TEST(FakeRecordingDevice, GainCurveShouldNotChangeSign) {
  // Create view on original samples.
  std::unique_ptr<const ChannelBuffer<float>> buff_orig =
      CreateChannelBufferWithData(kTestMultiChannelSamples);

  // Create output buffer.
  auto buff = CreateChannelBufferWithData(kTestMultiChannelSamples);

  // Test different mappings.
  for (auto fake_rec_device_kind : kFakeRecDeviceKinds) {
    SCOPED_TRACE(FakeRecordingDeviceKindToString(fake_rec_device_kind));
    FakeRecordingDevice fake_recording_device(kInitialMicLevel,
                                              fake_rec_device_kind);

    // TODO(alessiob): The test below is designed for state-less recording
    // devices. If, for instance, a device has memory, the test might need
    // to be redesigned (e.g., re-initialize fake recording device).
    for (int i = 0; i <= 255; ++i) {
      SCOPED_TRACE(AnalogLevelToString(i));
      WritesDataIntoChannelBuffer(kTestMultiChannelSamples, buff.get());
      fake_recording_device.SetMicLevel(i);
      fake_recording_device.SimulateAnalogGain(buff.get());
      CheckSameSign(buff_orig.get(), buff.get());
    }
  }
}

}  // namespace test
}  // namespace webrtc