peah | 5585001 | 2016-03-19 18:01:09 -0700 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/test/bitexactness_tools.h" |
peah | 5585001 | 2016-03-19 18:01:09 -0700 | [diff] [blame] | 12 | |
| 13 | #include <math.h> |
| 14 | #include <algorithm> |
| 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "api/array_view.h" |
| 19 | #include "test/testsupport/fileutils.h" |
peah | 5585001 | 2016-03-19 18:01:09 -0700 | [diff] [blame] | 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: |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 35 | RTC_NOTREACHED(); |
peah | 5585001 | 2016-03-19 18:01:09 -0700 | [diff] [blame] | 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: |
nisse | eb4ca4e | 2017-01-12 02:24:27 -0800 | [diff] [blame] | 51 | RTC_NOTREACHED(); |
peah | 5585001 | 2016-03-19 18:01:09 -0700 | [diff] [blame] | 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) { |
Sam Zackrisson | 4030c65 | 2018-01-12 13:47:53 +0100 | [diff] [blame] | 60 | RTC_DCHECK_LE(num_channels, 2); |
peah | 5585001 | 2016-03-19 18:01:09 -0700 | [diff] [blame] | 61 | 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 | |
peah | 7ea928e | 2016-03-30 08:13:57 -0700 | [diff] [blame] | 74 | ::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) { |
peah | 5585001 | 2016-03-19 18:01:09 -0700 | [diff] [blame] | 80 | // 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 | |
peah | 7ea928e | 2016-03-30 08:13:57 -0700 | [diff] [blame] | 94 | return VerifyArray(reference, output_to_verify, element_error_bound); |
peah | 5585001 | 2016-03-19 18:01:09 -0700 | [diff] [blame] | 95 | } |
| 96 | |
peah | 7ea928e | 2016-03-30 08:13:57 -0700 | [diff] [blame] | 97 | ::testing::AssertionResult VerifyArray(rtc::ArrayView<const float> reference, |
| 98 | rtc::ArrayView<const float> output, |
| 99 | float element_error_bound) { |
peah | 5585001 | 2016-03-19 18:01:09 -0700 | [diff] [blame] | 100 | // 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) { |
peah | 7ea928e | 2016-03-30 08:13:57 -0700 | [diff] [blame] | 111 | if (fabs(output[k] - reference[k]) > element_error_bound) { |
peah | 5585001 | 2016-03-19 18:01:09 -0700 | [diff] [blame] | 112 | 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 |