blob: c5e4d38dae49c230b028d18bd6a28d9ac784c48a [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
11#include "webrtc/modules/audio_processing/test/bitexactness_tools.h"
12
13#include <math.h>
14#include <algorithm>
15#include <string>
16#include <vector>
17
kwiberg529662a2017-09-04 05:43:17 -070018#include "webrtc/api/array_view.h"
peah55850012016-03-19 18:01:09 -070019#include "webrtc/test/testsupport/fileutils.h"
20
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) {
60 RTC_DCHECK_EQ(data.size(), samples_per_channel * num_channels);
61 std::vector<int16_t> read_samples(samples_per_channel * 2);
62 stereo_pcm_file->Read(samples_per_channel * 2, read_samples.data());
63
64 // Convert samples to float and discard any channels not needed.
65 for (size_t sample = 0; sample < samples_per_channel; ++sample) {
66 for (size_t channel = 0; channel < num_channels; ++channel) {
67 data[sample * num_channels + channel] =
68 read_samples[sample * 2 + channel] / 32768.0f;
69 }
70 }
71}
72
peah7ea928e2016-03-30 08:13:57 -070073::testing::AssertionResult VerifyDeinterleavedArray(
74 size_t samples_per_channel,
75 size_t num_channels,
76 rtc::ArrayView<const float> reference,
77 rtc::ArrayView<const float> output,
78 float element_error_bound) {
peah55850012016-03-19 18:01:09 -070079 // Form vectors to compare the reference to. Only the first values of the
80 // outputs are compared in order not having to specify all preceeding frames
81 // as testvectors.
82 const size_t reference_frame_length =
83 rtc::CheckedDivExact(reference.size(), num_channels);
84
85 std::vector<float> output_to_verify;
86 for (size_t channel_no = 0; channel_no < num_channels; ++channel_no) {
87 output_to_verify.insert(output_to_verify.end(),
88 output.begin() + channel_no * samples_per_channel,
89 output.begin() + channel_no * samples_per_channel +
90 reference_frame_length);
91 }
92
peah7ea928e2016-03-30 08:13:57 -070093 return VerifyArray(reference, output_to_verify, element_error_bound);
peah55850012016-03-19 18:01:09 -070094}
95
peah7ea928e2016-03-30 08:13:57 -070096::testing::AssertionResult VerifyArray(rtc::ArrayView<const float> reference,
97 rtc::ArrayView<const float> output,
98 float element_error_bound) {
peah55850012016-03-19 18:01:09 -070099 // The vectors are deemed to be bitexact only if
100 // a) output have a size at least as long as the reference.
101 // b) the samples in the reference are bitexact with the corresponding samples
102 // in the output.
103
104 bool equal = true;
105 if (output.size() < reference.size()) {
106 equal = false;
107 } else {
108 // Compare the first samples in the vectors.
109 for (size_t k = 0; k < reference.size(); ++k) {
peah7ea928e2016-03-30 08:13:57 -0700110 if (fabs(output[k] - reference[k]) > element_error_bound) {
peah55850012016-03-19 18:01:09 -0700111 equal = false;
112 break;
113 }
114 }
115 }
116
117 if (equal) {
118 return ::testing::AssertionSuccess();
119 }
120
121 // Lambda function that produces a formatted string with the data in the
122 // vector.
123 auto print_vector_in_c_format = [](rtc::ArrayView<const float> v,
124 size_t num_values_to_print) {
125 std::string s = "{ ";
126 for (size_t k = 0; k < std::min(num_values_to_print, v.size()); ++k) {
127 s += std::to_string(v[k]) + "f";
128 s += (k < (num_values_to_print - 1)) ? ", " : "";
129 }
130 return s + " }";
131 };
132
133 // If the vectors are deemed not to be similar, return a report of the
134 // difference.
135 return ::testing::AssertionFailure()
136 << std::endl
137 << " Actual values : "
138 << print_vector_in_c_format(output,
139 std::min(output.size(), reference.size()))
140 << std::endl
141 << " Expected values: "
142 << print_vector_in_c_format(reference, reference.size()) << std::endl;
143}
144
145} // namespace test
146} // namespace webrtc