summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/modules/audio_processing/echo_control_mobile_bit_exact_unittest.cc
blob: f351811e08a73c716e5a4b114c5a2c5ac9d4f14c (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
/*
 *  Copyright (c) 2018 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 <vector>

#include "api/array_view.h"
#include "modules/audio_processing/audio_buffer.h"
#include "modules/audio_processing/echo_control_mobile_impl.h"
#include "modules/audio_processing/test/audio_buffer_tools.h"
#include "modules/audio_processing/test/bitexactness_tools.h"
#include "test/gtest.h"

namespace webrtc {
namespace {

// TODO(peah): Increase the number of frames to proces when the issue of
// non repeatable test results have been found.
const int kNumFramesToProcess = 200;

void SetupComponent(int sample_rate_hz,
                    EchoControlMobileImpl::RoutingMode routing_mode,
                    bool comfort_noise_enabled,
                    EchoControlMobileImpl* echo_control_mobile) {
  echo_control_mobile->Initialize(
      sample_rate_hz > 16000 ? 16000 : sample_rate_hz, 1, 1);
  echo_control_mobile->set_routing_mode(routing_mode);
  echo_control_mobile->enable_comfort_noise(comfort_noise_enabled);
}

void ProcessOneFrame(int sample_rate_hz,
                     int stream_delay_ms,
                     AudioBuffer* render_audio_buffer,
                     AudioBuffer* capture_audio_buffer,
                     EchoControlMobileImpl* echo_control_mobile) {
  if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) {
    render_audio_buffer->SplitIntoFrequencyBands();
    capture_audio_buffer->SplitIntoFrequencyBands();
  }

  std::vector<int16_t> render_audio;
  EchoControlMobileImpl::PackRenderAudioBuffer(
      render_audio_buffer, 1, render_audio_buffer->num_channels(),
      &render_audio);
  echo_control_mobile->ProcessRenderAudio(render_audio);

  echo_control_mobile->ProcessCaptureAudio(capture_audio_buffer,
                                           stream_delay_ms);

  if (sample_rate_hz > AudioProcessing::kSampleRate16kHz) {
    capture_audio_buffer->MergeFrequencyBands();
  }
}

void RunBitexactnessTest(int sample_rate_hz,
                         size_t num_channels,
                         int stream_delay_ms,
                         EchoControlMobileImpl::RoutingMode routing_mode,
                         bool comfort_noise_enabled,
                         const rtc::ArrayView<const float>& output_reference) {
  EchoControlMobileImpl echo_control_mobile;
  SetupComponent(sample_rate_hz, routing_mode, comfort_noise_enabled,
                 &echo_control_mobile);

  const int samples_per_channel = rtc::CheckedDivExact(sample_rate_hz, 100);
  const StreamConfig render_config(sample_rate_hz, num_channels);
  AudioBuffer render_buffer(
      render_config.sample_rate_hz(), render_config.num_channels(),
      render_config.sample_rate_hz(), 1, render_config.sample_rate_hz(), 1);
  test::InputAudioFile render_file(
      test::GetApmRenderTestVectorFileName(sample_rate_hz));
  std::vector<float> render_input(samples_per_channel * num_channels);

  const StreamConfig capture_config(sample_rate_hz, num_channels);
  AudioBuffer capture_buffer(
      capture_config.sample_rate_hz(), capture_config.num_channels(),
      capture_config.sample_rate_hz(), 1, capture_config.sample_rate_hz(), 1);
  test::InputAudioFile capture_file(
      test::GetApmCaptureTestVectorFileName(sample_rate_hz));
  std::vector<float> capture_input(samples_per_channel * num_channels);

  for (int frame_no = 0; frame_no < kNumFramesToProcess; ++frame_no) {
    ReadFloatSamplesFromStereoFile(samples_per_channel, num_channels,
                                   &render_file, render_input);
    ReadFloatSamplesFromStereoFile(samples_per_channel, num_channels,
                                   &capture_file, capture_input);

    test::CopyVectorToAudioBuffer(render_config, render_input, &render_buffer);
    test::CopyVectorToAudioBuffer(capture_config, capture_input,
                                  &capture_buffer);

    ProcessOneFrame(sample_rate_hz, stream_delay_ms, &render_buffer,
                    &capture_buffer, &echo_control_mobile);
  }

  // Extract and verify the test results.
  std::vector<float> capture_output;
  test::ExtractVectorFromAudioBuffer(capture_config, &capture_buffer,
                                     &capture_output);

  // Compare the output with the reference. Only the first values of the output
  // from last frame processed are compared in order not having to specify all
  // preceeding frames as testvectors. As the algorithm being tested has a
  // memory, testing only the last frame implicitly also tests the preceeding
  // frames.
  const float kElementErrorBound = 1.0f / 32768.0f;
  EXPECT_TRUE(test::VerifyDeinterleavedArray(
      capture_config.num_frames(), capture_config.num_channels(),
      output_reference, capture_output, kElementErrorBound));
}

}  // namespace

// TODO(peah): Renable once the integer overflow issue in aecm_core.c:932:69
// has been solved.
TEST(EchoControlMobileBitExactnessTest,
     DISABLED_Mono8kHz_LoudSpeakerPhone_CngOn_StreamDelay0) {
  const float kOutputReference[] = {0.005280f, 0.002380f, -0.000427f};

  RunBitexactnessTest(8000, 1, 0,
                      EchoControlMobileImpl::RoutingMode::kLoudSpeakerphone,
                      true, kOutputReference);
}

TEST(EchoControlMobileBitExactnessTest,
     DISABLED_Mono16kHz_LoudSpeakerPhone_CngOn_StreamDelay0) {
  const float kOutputReference[] = {0.003601f, 0.002991f, 0.001923f};
  RunBitexactnessTest(16000, 1, 0,
                      EchoControlMobileImpl::RoutingMode::kLoudSpeakerphone,
                      true, kOutputReference);
}

TEST(EchoControlMobileBitExactnessTest,
     DISABLED_Mono32kHz_LoudSpeakerPhone_CngOn_StreamDelay0) {
  const float kOutputReference[] = {0.002258f, 0.002899f, 0.003906f};

  RunBitexactnessTest(32000, 1, 0,
                      EchoControlMobileImpl::RoutingMode::kLoudSpeakerphone,
                      true, kOutputReference);
}

TEST(EchoControlMobileBitExactnessTest,
     DISABLED_Mono48kHz_LoudSpeakerPhone_CngOn_StreamDelay0) {
  const float kOutputReference[] = {-0.000046f, 0.000041f, 0.000249f};

  RunBitexactnessTest(48000, 1, 0,
                      EchoControlMobileImpl::RoutingMode::kLoudSpeakerphone,
                      true, kOutputReference);
}

TEST(EchoControlMobileBitExactnessTest,
     DISABLED_Mono16kHz_LoudSpeakerPhone_CngOff_StreamDelay0) {
  const float kOutputReference[] = {0.000000f, 0.000000f, 0.000000f};

  RunBitexactnessTest(16000, 1, 0,
                      EchoControlMobileImpl::RoutingMode::kLoudSpeakerphone,
                      false, kOutputReference);
}

// TODO(peah): Renable once the integer overflow issue in aecm_core.c:932:69
// has been solved.
TEST(EchoControlMobileBitExactnessTest,
     DISABLED_Mono16kHz_LoudSpeakerPhone_CngOn_StreamDelay5) {
  const float kOutputReference[] = {0.003693f, 0.002930f, 0.001801f};

  RunBitexactnessTest(16000, 1, 5,
                      EchoControlMobileImpl::RoutingMode::kLoudSpeakerphone,
                      true, kOutputReference);
}

TEST(EchoControlMobileBitExactnessTest,
     Mono16kHz_LoudSpeakerPhone_CngOn_StreamDelay10) {
  const float kOutputReference[] = {-0.002380f, -0.002533f, -0.002563f};

  RunBitexactnessTest(16000, 1, 10,
                      EchoControlMobileImpl::RoutingMode::kLoudSpeakerphone,
                      true, kOutputReference);
}

TEST(EchoControlMobileBitExactnessTest,
     DISABLED_Mono16kHz_QuietEarpieceOrHeadset_CngOn_StreamDelay0) {
  const float kOutputReference[] = {0.000397f, 0.000000f, -0.000305f};

  RunBitexactnessTest(
      16000, 1, 0, EchoControlMobileImpl::RoutingMode::kQuietEarpieceOrHeadset,
      true, kOutputReference);
}

TEST(EchoControlMobileBitExactnessTest,
     DISABLED_Mono16kHz_Earpiece_CngOn_StreamDelay0) {
  const float kOutputReference[] = {0.002167f, 0.001617f, 0.001038f};

  RunBitexactnessTest(16000, 1, 0,
                      EchoControlMobileImpl::RoutingMode::kEarpiece, true,
                      kOutputReference);
}

TEST(EchoControlMobileBitExactnessTest,
     DISABLED_Mono16kHz_LoudEarpiece_CngOn_StreamDelay0) {
  const float kOutputReference[] = {0.003540f, 0.002899f, 0.001862f};

  RunBitexactnessTest(16000, 1, 0,
                      EchoControlMobileImpl::RoutingMode::kLoudEarpiece, true,
                      kOutputReference);
}

TEST(EchoControlMobileBitExactnessTest,
     DISABLED_Mono16kHz_SpeakerPhone_CngOn_StreamDelay0) {
  const float kOutputReference[] = {0.003632f, 0.003052f, 0.001984f};

  RunBitexactnessTest(16000, 1, 0,
                      EchoControlMobileImpl::RoutingMode::kSpeakerphone, true,
                      kOutputReference);
}

}  // namespace webrtc