OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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 | |
18 #include "webrtc/base/array_view.h" | |
19 #include "webrtc/test/testsupport/fileutils.h" | |
20 | |
21 namespace webrtc { | |
22 namespace test { | |
23 | |
24 std::string GetApmRenderTestVectorFileName(int sample_rate_hz) { | |
25 switch (sample_rate_hz) { | |
26 case 8000: | |
27 return ResourcePath("far8_stereo", "pcm"); | |
28 break; | |
hlundin-webrtc
2016/03/17 14:14:40
You don't need break after return.
peah-webrtc
2016/03/17 22:26:02
Done.
| |
29 case 16000: | |
30 return ResourcePath("far16_stereo", "pcm"); | |
31 break; | |
32 case 32000: | |
33 return ResourcePath("far32_stereo", "pcm"); | |
34 break; | |
35 case 48000: | |
36 return ResourcePath("far48_stereo", "pcm"); | |
37 break; | |
38 default: | |
39 RTC_DCHECK(false); | |
40 } | |
41 return ""; | |
42 } | |
43 | |
44 std::string GetApmCaptureTestVectorFileName(int sample_rate_hz) { | |
45 switch (sample_rate_hz) { | |
46 case 8000: | |
47 return ResourcePath("near8_stereo", "pcm"); | |
48 break; | |
hlundin-webrtc
2016/03/17 14:14:40
Skip the breaks! :)
peah-webrtc
2016/03/17 22:26:02
Done.
| |
49 case 16000: | |
50 return ResourcePath("near16_stereo", "pcm"); | |
51 break; | |
52 case 32000: | |
53 return ResourcePath("near32_stereo", "pcm"); | |
54 break; | |
55 case 48000: | |
56 return ResourcePath("near48_stereo", "pcm"); | |
57 break; | |
58 default: | |
59 RTC_DCHECK(false); | |
60 } | |
61 return ""; | |
62 } | |
63 | |
64 void ReadFloatSamplesFromStereoFile(size_t samples_per_channel, | |
65 size_t num_channels, | |
66 InputAudioFile* stereo_pcm_file, | |
67 rtc::ArrayView<float> data) { | |
68 RTC_DCHECK_EQ(data.size(), samples_per_channel * num_channels); | |
69 std::vector<int16_t> read_samples(samples_per_channel * 2); | |
70 stereo_pcm_file->Read(samples_per_channel * 2, read_samples.data()); | |
71 | |
72 // Convert samples to float and discard any channels not needed. | |
73 for (size_t sample = 0; sample < samples_per_channel; ++sample) { | |
74 for (size_t channel = 0; channel < num_channels; ++channel) { | |
75 data[sample * num_channels + channel] = | |
76 read_samples[sample * 2 + channel] / 32768.0f; | |
77 } | |
78 } | |
79 } | |
80 | |
81 ::testing::AssertionResult BitExactFrame(size_t samples_per_channel, | |
82 size_t num_channels, | |
83 rtc::ArrayView<const float> reference, | |
84 rtc::ArrayView<const float> output, | |
85 float tolerance) { | |
86 // Form vectors to compare the reference to. Only the first values of the | |
87 // outputs are compared in order not having to specify all preceeding frames | |
88 // as testvectors. | |
89 const size_t reference_frame_length = | |
90 rtc::CheckedDivExact(reference.size(), num_channels); | |
91 | |
92 std::vector<float> output_to_verify; | |
93 for (size_t channel_no = 0; channel_no < num_channels; ++channel_no) { | |
94 output_to_verify.insert(output_to_verify.end(), | |
95 output.begin() + channel_no * samples_per_channel, | |
96 output.begin() + channel_no * samples_per_channel + | |
97 reference_frame_length); | |
98 } | |
99 | |
100 return BitExactVector(reference, output_to_verify, tolerance); | |
101 } | |
102 | |
103 ::testing::AssertionResult BitExactVector(rtc::ArrayView<const float> reference, | |
104 rtc::ArrayView<const float> output, | |
105 float tolerance) { | |
106 // The vectors are deemed to be bitexact only if | |
107 // a) output have a size at least as long as the reference. | |
108 // b) the samples in the reference are bitexact with the corresponding samples | |
109 // in the output. | |
110 | |
111 bool equal = true; | |
112 if (output.size() < reference.size()) { | |
113 equal = false; | |
114 } else { | |
115 // Compare the first samples in the vectors. | |
116 for (size_t k = 0; k < reference.size(); ++k) { | |
117 if (fabs(output[k] - reference[k]) > tolerance) { | |
118 equal = false; | |
119 break; | |
120 } | |
121 } | |
122 } | |
123 | |
124 if (equal) { | |
125 return ::testing::AssertionSuccess(); | |
126 } | |
127 | |
128 // Lambda function that produces a formatted string with the data in the | |
129 // vector. | |
130 auto print_vector_in_c_format = [](rtc::ArrayView<const float> v, | |
131 size_t num_values_to_print) { | |
132 std::string s = "{ "; | |
133 for (size_t k = 0; k < std::min(num_values_to_print, v.size()); ++k) { | |
134 s += std::to_string(v[k]) + "f"; | |
135 s += (k < (num_values_to_print - 1)) ? ", " : ""; | |
136 } | |
137 return s + " }"; | |
138 }; | |
139 | |
140 // If the vectors are deemed not to be similar, return a report of the | |
141 // difference. | |
142 return ::testing::AssertionFailure() | |
143 << std::endl | |
144 << " Actual values : " | |
145 << print_vector_in_c_format(output, | |
146 std::min(output.size(), reference.size())) | |
147 << std::endl | |
148 << " Expected values: " | |
149 << print_vector_in_c_format(reference, reference.size()) << std::endl; | |
150 } | |
151 | |
152 } // namespace test | |
153 } // namespace webrtc | |
OLD | NEW |