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