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 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: |
| 35 RTC_DCHECK(false); |
| 36 } |
| 37 return ""; |
| 38 } |
| 39 |
| 40 std::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: |
| 51 RTC_DCHECK(false); |
| 52 } |
| 53 return ""; |
| 54 } |
| 55 |
| 56 void 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 |
| 73 ::testing::AssertionResult BitExactFrame(size_t samples_per_channel, |
| 74 size_t num_channels, |
| 75 rtc::ArrayView<const float> reference, |
| 76 rtc::ArrayView<const float> output, |
| 77 float tolerance) { |
| 78 // Form vectors to compare the reference to. Only the first values of the |
| 79 // outputs are compared in order not having to specify all preceeding frames |
| 80 // as testvectors. |
| 81 const size_t reference_frame_length = |
| 82 rtc::CheckedDivExact(reference.size(), num_channels); |
| 83 |
| 84 std::vector<float> output_to_verify; |
| 85 for (size_t channel_no = 0; channel_no < num_channels; ++channel_no) { |
| 86 output_to_verify.insert(output_to_verify.end(), |
| 87 output.begin() + channel_no * samples_per_channel, |
| 88 output.begin() + channel_no * samples_per_channel + |
| 89 reference_frame_length); |
| 90 } |
| 91 |
| 92 return BitExactVector(reference, output_to_verify, tolerance); |
| 93 } |
| 94 |
| 95 ::testing::AssertionResult BitExactVector(rtc::ArrayView<const float> reference, |
| 96 rtc::ArrayView<const float> output, |
| 97 float tolerance) { |
| 98 // The vectors are deemed to be bitexact only if |
| 99 // a) output have a size at least as long as the reference. |
| 100 // b) the samples in the reference are bitexact with the corresponding samples |
| 101 // in the output. |
| 102 |
| 103 bool equal = true; |
| 104 if (output.size() < reference.size()) { |
| 105 equal = false; |
| 106 } else { |
| 107 // Compare the first samples in the vectors. |
| 108 for (size_t k = 0; k < reference.size(); ++k) { |
| 109 if (fabs(output[k] - reference[k]) > tolerance) { |
| 110 equal = false; |
| 111 break; |
| 112 } |
| 113 } |
| 114 } |
| 115 |
| 116 if (equal) { |
| 117 return ::testing::AssertionSuccess(); |
| 118 } |
| 119 |
| 120 // Lambda function that produces a formatted string with the data in the |
| 121 // vector. |
| 122 auto print_vector_in_c_format = [](rtc::ArrayView<const float> v, |
| 123 size_t num_values_to_print) { |
| 124 std::string s = "{ "; |
| 125 for (size_t k = 0; k < std::min(num_values_to_print, v.size()); ++k) { |
| 126 s += std::to_string(v[k]) + "f"; |
| 127 s += (k < (num_values_to_print - 1)) ? ", " : ""; |
| 128 } |
| 129 return s + " }"; |
| 130 }; |
| 131 |
| 132 // If the vectors are deemed not to be similar, return a report of the |
| 133 // difference. |
| 134 return ::testing::AssertionFailure() |
| 135 << std::endl |
| 136 << " Actual values : " |
| 137 << print_vector_in_c_format(output, |
| 138 std::min(output.size(), reference.size())) |
| 139 << std::endl |
| 140 << " Expected values: " |
| 141 << print_vector_in_c_format(reference, reference.size()) << std::endl; |
| 142 } |
| 143 |
| 144 } // namespace test |
| 145 } // namespace webrtc |
OLD | NEW |