blob: 0e9d36945257456bddcc0d3f6fa4d8d17582ba3a [file] [log] [blame]
peah55850012016-03-19 18:01:09 -07001/*
2 * Copyright (c) 2016 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/test/bitexactness_tools.h"
peah55850012016-03-19 18:01:09 -070012
13#include <math.h>
14#include <algorithm>
15#include <string>
16#include <vector>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "api/array_view.h"
19#include "test/testsupport/fileutils.h"
peah55850012016-03-19 18:01:09 -070020
21namespace webrtc {
22namespace test {
23
24std::string GetApmRenderTestVectorFileName(int sample_rate_hz) {
25 switch (sample_rate_hz) {
26 case 8000:
27 return ResourcePath("far8_stereo", "pcm");
28 case 16000:
29 return ResourcePath("far16_stereo", "pcm");
30 case 32000:
31 return ResourcePath("far32_stereo", "pcm");
32 case 48000:
33 return ResourcePath("far48_stereo", "pcm");
34 default:
nisseeb4ca4e2017-01-12 02:24:27 -080035 RTC_NOTREACHED();
peah55850012016-03-19 18:01:09 -070036 }
37 return "";
38}
39
40std::string GetApmCaptureTestVectorFileName(int sample_rate_hz) {
41 switch (sample_rate_hz) {
42 case 8000:
43 return ResourcePath("near8_stereo", "pcm");
44 case 16000:
45 return ResourcePath("near16_stereo", "pcm");
46 case 32000:
47 return ResourcePath("near32_stereo", "pcm");
48 case 48000:
49 return ResourcePath("near48_stereo", "pcm");
50 default:
nisseeb4ca4e2017-01-12 02:24:27 -080051 RTC_NOTREACHED();
peah55850012016-03-19 18:01:09 -070052 }
53 return "";
54}
55
56void ReadFloatSamplesFromStereoFile(size_t samples_per_channel,
57 size_t num_channels,
58 InputAudioFile* stereo_pcm_file,
59 rtc::ArrayView<float> data) {
Sam Zackrisson4030c652018-01-12 13:47:53 +010060 RTC_DCHECK_LE(num_channels, 2);
peah55850012016-03-19 18:01:09 -070061 RTC_DCHECK_EQ(data.size(), samples_per_channel * num_channels);
62 std::vector<int16_t> read_samples(samples_per_channel * 2);
63 stereo_pcm_file->Read(samples_per_channel * 2, read_samples.data());
64
65 // Convert samples to float and discard any channels not needed.
66 for (size_t sample = 0; sample < samples_per_channel; ++sample) {
67 for (size_t channel = 0; channel < num_channels; ++channel) {
68 data[sample * num_channels + channel] =
69 read_samples[sample * 2 + channel] / 32768.0f;
70 }
71 }
72}
73
peah7ea928e2016-03-30 08:13:57 -070074::testing::AssertionResult VerifyDeinterleavedArray(
75 size_t samples_per_channel,
76 size_t num_channels,
77 rtc::ArrayView<const float> reference,
78 rtc::ArrayView<const float> output,
79 float element_error_bound) {
peah55850012016-03-19 18:01:09 -070080 // Form vectors to compare the reference to. Only the first values of the
81 // outputs are compared in order not having to specify all preceeding frames
82 // as testvectors.
83 const size_t reference_frame_length =
84 rtc::CheckedDivExact(reference.size(), num_channels);
85
86 std::vector<float> output_to_verify;
87 for (size_t channel_no = 0; channel_no < num_channels; ++channel_no) {
88 output_to_verify.insert(output_to_verify.end(),
89 output.begin() + channel_no * samples_per_channel,
90 output.begin() + channel_no * samples_per_channel +
91 reference_frame_length);
92 }
93
peah7ea928e2016-03-30 08:13:57 -070094 return VerifyArray(reference, output_to_verify, element_error_bound);
peah55850012016-03-19 18:01:09 -070095}
96
peah7ea928e2016-03-30 08:13:57 -070097::testing::AssertionResult VerifyArray(rtc::ArrayView<const float> reference,
98 rtc::ArrayView<const float> output,
99 float element_error_bound) {
peah55850012016-03-19 18:01:09 -0700100 // The vectors are deemed to be bitexact only if
101 // a) output have a size at least as long as the reference.
102 // b) the samples in the reference are bitexact with the corresponding samples
103 // in the output.
104
105 bool equal = true;
106 if (output.size() < reference.size()) {
107 equal = false;
108 } else {
109 // Compare the first samples in the vectors.
110 for (size_t k = 0; k < reference.size(); ++k) {
peah7ea928e2016-03-30 08:13:57 -0700111 if (fabs(output[k] - reference[k]) > element_error_bound) {
peah55850012016-03-19 18:01:09 -0700112 equal = false;
113 break;
114 }
115 }
116 }
117
118 if (equal) {
119 return ::testing::AssertionSuccess();
120 }
121
122 // Lambda function that produces a formatted string with the data in the
123 // vector.
124 auto print_vector_in_c_format = [](rtc::ArrayView<const float> v,
125 size_t num_values_to_print) {
126 std::string s = "{ ";
127 for (size_t k = 0; k < std::min(num_values_to_print, v.size()); ++k) {
128 s += std::to_string(v[k]) + "f";
129 s += (k < (num_values_to_print - 1)) ? ", " : "";
130 }
131 return s + " }";
132 };
133
134 // If the vectors are deemed not to be similar, return a report of the
135 // difference.
136 return ::testing::AssertionFailure()
137 << std::endl
138 << " Actual values : "
139 << print_vector_in_c_format(output,
140 std::min(output.size(), reference.size()))
141 << std::endl
142 << " Expected values: "
143 << print_vector_in_c_format(reference, reference.size()) << std::endl;
144}
145
146} // namespace test
147} // namespace webrtc