andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 11 | #include <algorithm> |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 12 | #include <cmath> |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame] | 13 | #include <memory> |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 14 | #include <vector> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "common_audio/audio_converter.h" |
| 17 | #include "common_audio/channel_buffer.h" |
| 18 | #include "common_audio/resampler/push_sinc_resampler.h" |
| 19 | #include "rtc_base/arraysize.h" |
| 20 | #include "rtc_base/format_macros.h" |
| 21 | #include "test/gtest.h" |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame] | 25 | typedef std::unique_ptr<ChannelBuffer<float>> ScopedBuffer; |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 26 | |
| 27 | // Sets the signal value to increase by |data| with every sample. |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 28 | ScopedBuffer CreateBuffer(const std::vector<float>& data, size_t frames) { |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 29 | const size_t num_channels = data.size(); |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 30 | ScopedBuffer sb(new ChannelBuffer<float>(frames, num_channels)); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 31 | for (size_t i = 0; i < num_channels; ++i) |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 32 | for (size_t j = 0; j < frames; ++j) |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 33 | sb->channels()[i][j] = data[i] * j; |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 34 | return sb; |
| 35 | } |
| 36 | |
| 37 | void VerifyParams(const ChannelBuffer<float>& ref, |
| 38 | const ChannelBuffer<float>& test) { |
| 39 | EXPECT_EQ(ref.num_channels(), test.num_channels()); |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 40 | EXPECT_EQ(ref.num_frames(), test.num_frames()); |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | // Computes the best SNR based on the error between |ref_frame| and |
| 44 | // |test_frame|. It searches around |expected_delay| in samples between the |
| 45 | // signals to compensate for the resampling delay. |
| 46 | float ComputeSNR(const ChannelBuffer<float>& ref, |
| 47 | const ChannelBuffer<float>& test, |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 48 | size_t expected_delay) { |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 49 | VerifyParams(ref, test); |
| 50 | float best_snr = 0; |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 51 | size_t best_delay = 0; |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 52 | |
| 53 | // Search within one sample of the expected delay. |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 54 | for (size_t delay = std::max(expected_delay, static_cast<size_t>(1)) - 1; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 55 | delay <= std::min(expected_delay + 1, ref.num_frames()); ++delay) { |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 56 | float mse = 0; |
| 57 | float variance = 0; |
| 58 | float mean = 0; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 59 | for (size_t i = 0; i < ref.num_channels(); ++i) { |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 60 | for (size_t j = 0; j < ref.num_frames() - delay; ++j) { |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 61 | float error = ref.channels()[i][j] - test.channels()[i][j + delay]; |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 62 | mse += error * error; |
aluebs@webrtc.org | d35a5c3 | 2015-02-10 22:52:15 +0000 | [diff] [blame] | 63 | variance += ref.channels()[i][j] * ref.channels()[i][j]; |
| 64 | mean += ref.channels()[i][j]; |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 +0000 | [diff] [blame] | 67 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 68 | const size_t length = ref.num_channels() * (ref.num_frames() - delay); |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 69 | mse /= length; |
| 70 | variance /= length; |
| 71 | mean /= length; |
| 72 | variance -= mean * mean; |
| 73 | float snr = 100; // We assign 100 dB to the zero-error case. |
| 74 | if (mse > 0) |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 +0000 | [diff] [blame] | 75 | snr = 10 * std::log10(variance / mse); |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 76 | if (snr > best_snr) { |
| 77 | best_snr = snr; |
| 78 | best_delay = delay; |
| 79 | } |
| 80 | } |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 81 | printf("SNR=%.1f dB at delay=%" PRIuS "\n", best_snr, best_delay); |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 82 | return best_snr; |
| 83 | } |
| 84 | |
| 85 | // Sets the source to a linearly increasing signal for which we can easily |
| 86 | // generate a reference. Runs the AudioConverter and ensures the output has |
| 87 | // sufficiently high SNR relative to the reference. |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 88 | void RunAudioConverterTest(size_t src_channels, |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 89 | int src_sample_rate_hz, |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 90 | size_t dst_channels, |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 91 | int dst_sample_rate_hz) { |
| 92 | const float kSrcLeft = 0.0002f; |
| 93 | const float kSrcRight = 0.0001f; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 94 | const float resampling_factor = |
| 95 | (1.f * src_sample_rate_hz) / dst_sample_rate_hz; |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 96 | const float dst_left = resampling_factor * kSrcLeft; |
| 97 | const float dst_right = resampling_factor * kSrcRight; |
| 98 | const float dst_mono = (dst_left + dst_right) / 2; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 99 | const size_t src_frames = static_cast<size_t>(src_sample_rate_hz / 100); |
| 100 | const size_t dst_frames = static_cast<size_t>(dst_sample_rate_hz / 100); |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 101 | |
| 102 | std::vector<float> src_data(1, kSrcLeft); |
| 103 | if (src_channels == 2) |
| 104 | src_data.push_back(kSrcRight); |
| 105 | ScopedBuffer src_buffer = CreateBuffer(src_data, src_frames); |
| 106 | |
| 107 | std::vector<float> dst_data(1, 0); |
| 108 | std::vector<float> ref_data; |
| 109 | if (dst_channels == 1) { |
| 110 | if (src_channels == 1) |
| 111 | ref_data.push_back(dst_left); |
| 112 | else |
| 113 | ref_data.push_back(dst_mono); |
| 114 | } else { |
| 115 | dst_data.push_back(0); |
| 116 | ref_data.push_back(dst_left); |
| 117 | if (src_channels == 1) |
| 118 | ref_data.push_back(dst_left); |
| 119 | else |
| 120 | ref_data.push_back(dst_right); |
| 121 | } |
| 122 | ScopedBuffer dst_buffer = CreateBuffer(dst_data, dst_frames); |
| 123 | ScopedBuffer ref_buffer = CreateBuffer(ref_data, dst_frames); |
| 124 | |
| 125 | // The sinc resampler has a known delay, which we compute here. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 126 | const size_t delay_frames = |
| 127 | src_sample_rate_hz == dst_sample_rate_hz |
| 128 | ? 0 |
| 129 | : static_cast<size_t>( |
| 130 | PushSincResampler::AlgorithmicDelaySeconds(src_sample_rate_hz) * |
| 131 | dst_sample_rate_hz); |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 132 | // SNR reported on the same line later. |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 133 | printf("(%" PRIuS ", %d Hz) -> (%" PRIuS ", %d Hz) ", src_channels, |
| 134 | src_sample_rate_hz, dst_channels, dst_sample_rate_hz); |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 135 | |
kwiberg | c2b785d | 2016-02-24 05:22:32 -0800 | [diff] [blame] | 136 | std::unique_ptr<AudioConverter> converter = AudioConverter::Create( |
kwiberg@webrtc.org | 00b8f6b | 2015-02-26 14:34:55 +0000 | [diff] [blame] | 137 | src_channels, src_frames, dst_channels, dst_frames); |
andrew@webrtc.org | 2c29c2e | 2015-02-11 01:09:50 +0000 | [diff] [blame] | 138 | converter->Convert(src_buffer->channels(), src_buffer->size(), |
| 139 | dst_buffer->channels(), dst_buffer->size()); |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 140 | |
| 141 | EXPECT_LT(43.f, |
| 142 | ComputeSNR(*ref_buffer.get(), *dst_buffer.get(), delay_frames)); |
| 143 | } |
| 144 | |
| 145 | TEST(AudioConverterTest, ConversionsPassSNRThreshold) { |
| 146 | const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000}; |
Peter Kasting | 6955870 | 2016-01-12 16:26:35 -0800 | [diff] [blame] | 147 | const size_t kChannels[] = {1, 2}; |
pkasting | 25702cb | 2016-01-08 13:50:27 -0800 | [diff] [blame] | 148 | for (size_t src_rate = 0; src_rate < arraysize(kSampleRates); ++src_rate) { |
| 149 | for (size_t dst_rate = 0; dst_rate < arraysize(kSampleRates); ++dst_rate) { |
| 150 | for (size_t src_channel = 0; src_channel < arraysize(kChannels); |
| 151 | ++src_channel) { |
| 152 | for (size_t dst_channel = 0; dst_channel < arraysize(kChannels); |
| 153 | ++dst_channel) { |
andrew@webrtc.org | aada86b | 2014-10-27 18:18:17 +0000 | [diff] [blame] | 154 | RunAudioConverterTest(kChannels[src_channel], kSampleRates[src_rate], |
| 155 | kChannels[dst_channel], kSampleRates[dst_rate]); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | } // namespace webrtc |