| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 /* |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 // found in the LICENSE file. | 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 // Modified from the Chromium original: |
| 12 // src/media/base/sinc_resampler_unittest.cc |
| 4 | 13 |
| 5 // MSVC++ requires this to be set before any other includes to get M_PI. | 14 // MSVC++ requires this to be set before any other includes to get M_PI. |
| 6 #define _USE_MATH_DEFINES | 15 #define _USE_MATH_DEFINES |
| 7 | 16 |
| 8 #include <cmath> | 17 #include <cmath> |
| 9 | 18 |
| 10 #include "base/bind.h" | |
| 11 #include "base/bind_helpers.h" | |
| 12 #include "base/command_line.h" | |
| 13 #include "base/cpu.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/strings/string_number_conversions.h" | |
| 16 #include "base/strings/stringize_macros.h" | |
| 17 #include "base/time/time.h" | |
| 18 #include "build/build_config.h" | |
| 19 #include "media/base/sinc_resampler.h" | |
| 20 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 21 #include "webrtc/common_audio/resampler/sinc_resampler.h" |
| 22 #include "webrtc/common_audio/resampler/sinusoidal_linear_chirp_source.h" |
| 23 #include "webrtc/system_wrappers/interface/cpu_features_wrapper.h" |
| 24 #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
| 25 #include "webrtc/system_wrappers/interface/stringize_macros.h" |
| 26 #include "webrtc/system_wrappers/interface/tick_util.h" |
| 27 #include "webrtc/test/test_suite.h" |
| 22 | 28 |
| 23 using testing::_; | 29 using testing::_; |
| 24 | 30 |
| 25 namespace media { | 31 namespace webrtc { |
| 26 | 32 |
| 27 static const double kSampleRateRatio = 192000.0 / 44100.0; | 33 static const double kSampleRateRatio = 192000.0 / 44100.0; |
| 28 static const double kKernelInterpolationFactor = 0.5; | 34 static const double kKernelInterpolationFactor = 0.5; |
| 29 | 35 |
| 30 // Command line switch for runtime adjustment of ConvolveBenchmark iterations. | |
| 31 static const char kConvolveIterations[] = "convolve-iterations"; | |
| 32 | |
| 33 // Helper class to ensure ChunkedResample() functions properly. | 36 // Helper class to ensure ChunkedResample() functions properly. |
| 34 class MockSource { | 37 class MockSource : public SincResamplerCallback { |
| 35 public: | 38 public: |
| 36 MOCK_METHOD2(ProvideInput, void(int frames, float* destination)); | 39 MOCK_METHOD2(Run, void(int frames, float* destination)); |
| 37 }; | 40 }; |
| 38 | 41 |
| 39 ACTION(ClearBuffer) { | 42 ACTION(ClearBuffer) { |
| 40 memset(arg1, 0, arg0 * sizeof(float)); | 43 memset(arg1, 0, arg0 * sizeof(float)); |
| 41 } | 44 } |
| 42 | 45 |
| 43 ACTION(FillBuffer) { | 46 ACTION(FillBuffer) { |
| 44 // Value chosen arbitrarily such that SincResampler resamples it to something | 47 // Value chosen arbitrarily such that SincResampler resamples it to something |
| 45 // easily representable on all platforms; e.g., using kSampleRateRatio this | 48 // easily representable on all platforms; e.g., using kSampleRateRatio this |
| 46 // becomes 1.81219. | 49 // becomes 1.81219. |
| 47 memset(arg1, 64, arg0 * sizeof(float)); | 50 memset(arg1, 64, arg0 * sizeof(float)); |
| 48 } | 51 } |
| 49 | 52 |
| 50 // Test requesting multiples of ChunkSize() frames results in the proper number | 53 // Test requesting multiples of ChunkSize() frames results in the proper number |
| 51 // of callbacks. | 54 // of callbacks. |
| 52 TEST(SincResamplerTest, ChunkedResample) { | 55 TEST(SincResamplerTest, ChunkedResample) { |
| 53 MockSource mock_source; | 56 MockSource mock_source; |
| 54 | 57 |
| 55 // Choose a high ratio of input to output samples which will result in quick | 58 // Choose a high ratio of input to output samples which will result in quick |
| 56 // exhaustion of SincResampler's internal buffers. | 59 // exhaustion of SincResampler's internal buffers. |
| 57 SincResampler resampler( | 60 SincResampler resampler(kSampleRateRatio, SincResampler::kDefaultRequestSize, |
| 58 kSampleRateRatio, SincResampler::kDefaultRequestSize, | 61 &mock_source); |
| 59 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); | |
| 60 | 62 |
| 61 static const int kChunks = 2; | 63 static const int kChunks = 2; |
| 62 int max_chunk_size = resampler.ChunkSize() * kChunks; | 64 int max_chunk_size = resampler.ChunkSize() * kChunks; |
| 63 scoped_ptr<float[]> resampled_destination(new float[max_chunk_size]); | 65 scoped_array<float> resampled_destination(new float[max_chunk_size]); |
| 64 | 66 |
| 65 // Verify requesting ChunkSize() frames causes a single callback. | 67 // Verify requesting ChunkSize() frames causes a single callback. |
| 66 EXPECT_CALL(mock_source, ProvideInput(_, _)) | 68 EXPECT_CALL(mock_source, Run(_, _)) |
| 67 .Times(1).WillOnce(ClearBuffer()); | 69 .Times(1).WillOnce(ClearBuffer()); |
| 68 resampler.Resample(resampler.ChunkSize(), resampled_destination.get()); | 70 resampler.Resample(resampler.ChunkSize(), resampled_destination.get()); |
| 69 | 71 |
| 70 // Verify requesting kChunks * ChunkSize() frames causes kChunks callbacks. | 72 // Verify requesting kChunks * ChunkSize() frames causes kChunks callbacks. |
| 71 testing::Mock::VerifyAndClear(&mock_source); | 73 testing::Mock::VerifyAndClear(&mock_source); |
| 72 EXPECT_CALL(mock_source, ProvideInput(_, _)) | 74 EXPECT_CALL(mock_source, Run(_, _)) |
| 73 .Times(kChunks).WillRepeatedly(ClearBuffer()); | 75 .Times(kChunks).WillRepeatedly(ClearBuffer()); |
| 74 resampler.Resample(max_chunk_size, resampled_destination.get()); | 76 resampler.Resample(max_chunk_size, resampled_destination.get()); |
| 75 } | 77 } |
| 76 | 78 |
| 77 // Test flush resets the internal state properly. | 79 // Test flush resets the internal state properly. |
| 78 TEST(SincResamplerTest, Flush) { | 80 TEST(SincResamplerTest, Flush) { |
| 79 MockSource mock_source; | 81 MockSource mock_source; |
| 80 SincResampler resampler( | 82 SincResampler resampler(kSampleRateRatio, SincResampler::kDefaultRequestSize, |
| 81 kSampleRateRatio, SincResampler::kDefaultRequestSize, | 83 &mock_source); |
| 82 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); | 84 scoped_array<float> resampled_destination(new float[resampler.ChunkSize()]); |
| 83 scoped_ptr<float[]> resampled_destination(new float[resampler.ChunkSize()]); | |
| 84 | 85 |
| 85 // Fill the resampler with junk data. | 86 // Fill the resampler with junk data. |
| 86 EXPECT_CALL(mock_source, ProvideInput(_, _)) | 87 EXPECT_CALL(mock_source, Run(_, _)) |
| 87 .Times(1).WillOnce(FillBuffer()); | 88 .Times(1).WillOnce(FillBuffer()); |
| 88 resampler.Resample(resampler.ChunkSize() / 2, resampled_destination.get()); | 89 resampler.Resample(resampler.ChunkSize() / 2, resampled_destination.get()); |
| 89 ASSERT_NE(resampled_destination[0], 0); | 90 ASSERT_NE(resampled_destination[0], 0); |
| 90 | 91 |
| 91 // Flush and request more data, which should all be zeros now. | 92 // Flush and request more data, which should all be zeros now. |
| 92 resampler.Flush(); | 93 resampler.Flush(); |
| 93 testing::Mock::VerifyAndClear(&mock_source); | 94 testing::Mock::VerifyAndClear(&mock_source); |
| 94 EXPECT_CALL(mock_source, ProvideInput(_, _)) | 95 EXPECT_CALL(mock_source, Run(_, _)) |
| 95 .Times(1).WillOnce(ClearBuffer()); | 96 .Times(1).WillOnce(ClearBuffer()); |
| 96 resampler.Resample(resampler.ChunkSize() / 2, resampled_destination.get()); | 97 resampler.Resample(resampler.ChunkSize() / 2, resampled_destination.get()); |
| 97 for (int i = 0; i < resampler.ChunkSize() / 2; ++i) | 98 for (int i = 0; i < resampler.ChunkSize() / 2; ++i) |
| 98 ASSERT_FLOAT_EQ(resampled_destination[i], 0); | 99 ASSERT_FLOAT_EQ(resampled_destination[i], 0); |
| 99 } | 100 } |
| 100 | 101 |
| 101 // Test flush resets the internal state properly. | 102 // Test flush resets the internal state properly. |
| 102 TEST(SincResamplerTest, DISABLED_SetRatioBench) { | 103 TEST(SincResamplerTest, DISABLED_SetRatioBench) { |
| 103 MockSource mock_source; | 104 MockSource mock_source; |
| 104 SincResampler resampler( | 105 SincResampler resampler(kSampleRateRatio, SincResampler::kDefaultRequestSize, |
| 105 kSampleRateRatio, SincResampler::kDefaultRequestSize, | 106 &mock_source); |
| 106 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); | |
| 107 | 107 |
| 108 base::TimeTicks start = base::TimeTicks::HighResNow(); | 108 TickTime start = TickTime::Now(); |
| 109 for (int i = 1; i < 10000; ++i) | 109 for (int i = 1; i < 10000; ++i) |
| 110 resampler.SetRatio(1.0 / i); | 110 resampler.SetRatio(1.0 / i); |
| 111 double total_time_c_ms = | 111 double total_time_c_us = (TickTime::Now() - start).Microseconds(); |
| 112 (base::TimeTicks::HighResNow() - start).InMillisecondsF(); | 112 printf("SetRatio() took %.2fms.\n", total_time_c_us / 1000); |
| 113 printf("SetRatio() took %.2fms.\n", total_time_c_ms); | |
| 114 } | 113 } |
| 115 | 114 |
| 116 | 115 |
| 117 // Define platform independent function name for Convolve* tests. | 116 // Define platform independent function name for Convolve* tests. |
| 118 #if defined(ARCH_CPU_X86_FAMILY) | 117 #if defined(WEBRTC_ARCH_X86_FAMILY) |
| 119 #define CONVOLVE_FUNC Convolve_SSE | 118 #define CONVOLVE_FUNC Convolve_SSE |
| 120 #elif defined(ARCH_CPU_ARM_FAMILY) && defined(USE_NEON) | 119 #elif defined(WEBRTC_ARCH_ARM_V7) |
| 121 #define CONVOLVE_FUNC Convolve_NEON | 120 #define CONVOLVE_FUNC Convolve_NEON |
| 122 #endif | 121 #endif |
| 123 | 122 |
| 124 // Ensure various optimized Convolve() methods return the same value. Only run | 123 // Ensure various optimized Convolve() methods return the same value. Only run |
| 125 // this test if other optimized methods exist, otherwise the default Convolve() | 124 // this test if other optimized methods exist, otherwise the default Convolve() |
| 126 // will be tested by the parameterized SincResampler tests below. | 125 // will be tested by the parameterized SincResampler tests below. |
| 127 #if defined(CONVOLVE_FUNC) | 126 #if defined(CONVOLVE_FUNC) |
| 128 TEST(SincResamplerTest, Convolve) { | 127 TEST(SincResamplerTest, Convolve) { |
| 129 #if defined(ARCH_CPU_X86_FAMILY) | 128 #if defined(WEBRTC_ARCH_X86_FAMILY) |
| 130 ASSERT_TRUE(base::CPU().has_sse()); | 129 ASSERT_TRUE(WebRtc_GetCPUInfo(kSSE2)); |
| 130 #elif defined(WEBRTC_ARCH_ARM_V7) |
| 131 ASSERT_TRUE(WebRtc_GetCPUFeaturesARM() & kCPUFeatureNEON); |
| 131 #endif | 132 #endif |
| 132 | 133 |
| 133 // Initialize a dummy resampler. | 134 // Initialize a dummy resampler. |
| 134 MockSource mock_source; | 135 MockSource mock_source; |
| 135 SincResampler resampler( | 136 SincResampler resampler(kSampleRateRatio, SincResampler::kDefaultRequestSize, |
| 136 kSampleRateRatio, SincResampler::kDefaultRequestSize, | 137 &mock_source); |
| 137 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); | |
| 138 | 138 |
| 139 // The optimized Convolve methods are slightly more precise than Convolve_C(), | 139 // The optimized Convolve methods are slightly more precise than Convolve_C(), |
| 140 // so comparison must be done using an epsilon. | 140 // so comparison must be done using an epsilon. |
| 141 static const double kEpsilon = 0.00000005; | 141 static const double kEpsilon = 0.00000005; |
| 142 | 142 |
| 143 // Use a kernel from SincResampler as input and kernel data, this has the | 143 // Use a kernel from SincResampler as input and kernel data, this has the |
| 144 // benefit of already being properly sized and aligned for Convolve_SSE(). | 144 // benefit of already being properly sized and aligned for Convolve_SSE(). |
| 145 double result = resampler.Convolve_C( | 145 double result = resampler.Convolve_C( |
| 146 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(), | 146 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(), |
| 147 resampler.kernel_storage_.get(), kKernelInterpolationFactor); | 147 resampler.kernel_storage_.get(), kKernelInterpolationFactor); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 160 EXPECT_NEAR(result2, result, kEpsilon); | 160 EXPECT_NEAR(result2, result, kEpsilon); |
| 161 } | 161 } |
| 162 #endif | 162 #endif |
| 163 | 163 |
| 164 // Benchmark for the various Convolve() methods. Make sure to build with | 164 // Benchmark for the various Convolve() methods. Make sure to build with |
| 165 // branding=Chrome so that DCHECKs are compiled out when benchmarking. Original | 165 // branding=Chrome so that DCHECKs are compiled out when benchmarking. Original |
| 166 // benchmarks were run with --convolve-iterations=50000000. | 166 // benchmarks were run with --convolve-iterations=50000000. |
| 167 TEST(SincResamplerTest, ConvolveBenchmark) { | 167 TEST(SincResamplerTest, ConvolveBenchmark) { |
| 168 // Initialize a dummy resampler. | 168 // Initialize a dummy resampler. |
| 169 MockSource mock_source; | 169 MockSource mock_source; |
| 170 SincResampler resampler( | 170 SincResampler resampler(kSampleRateRatio, SincResampler::kDefaultRequestSize, |
| 171 kSampleRateRatio, SincResampler::kDefaultRequestSize, | 171 &mock_source); |
| 172 base::Bind(&MockSource::ProvideInput, base::Unretained(&mock_source))); | |
| 173 | 172 |
| 174 // Retrieve benchmark iterations from command line. | 173 // Retrieve benchmark iterations from command line. |
| 175 int convolve_iterations = 10; | 174 // TODO(ajm): Reintroduce this as a command line option. |
| 176 std::string iterations(CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 175 const int kConvolveIterations = 1000000; |
| 177 kConvolveIterations)); | |
| 178 if (!iterations.empty()) | |
| 179 base::StringToInt(iterations, &convolve_iterations); | |
| 180 | 176 |
| 181 printf("Benchmarking %d iterations:\n", convolve_iterations); | 177 printf("Benchmarking %d iterations:\n", kConvolveIterations); |
| 182 | 178 |
| 183 // Benchmark Convolve_C(). | 179 // Benchmark Convolve_C(). |
| 184 base::TimeTicks start = base::TimeTicks::HighResNow(); | 180 TickTime start = TickTime::Now(); |
| 185 for (int i = 0; i < convolve_iterations; ++i) { | 181 for (int i = 0; i < kConvolveIterations; ++i) { |
| 186 resampler.Convolve_C( | 182 resampler.Convolve_C( |
| 187 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(), | 183 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(), |
| 188 resampler.kernel_storage_.get(), kKernelInterpolationFactor); | 184 resampler.kernel_storage_.get(), kKernelInterpolationFactor); |
| 189 } | 185 } |
| 190 double total_time_c_ms = | 186 double total_time_c_us = (TickTime::Now() - start).Microseconds(); |
| 191 (base::TimeTicks::HighResNow() - start).InMillisecondsF(); | 187 printf("Convolve_C took %.2fms.\n", total_time_c_us / 1000); |
| 192 printf("Convolve_C took %.2fms.\n", total_time_c_ms); | |
| 193 | 188 |
| 194 #if defined(CONVOLVE_FUNC) | 189 #if defined(CONVOLVE_FUNC) |
| 195 #if defined(ARCH_CPU_X86_FAMILY) | 190 #if defined(WEBRTC_ARCH_X86_FAMILY) |
| 196 ASSERT_TRUE(base::CPU().has_sse()); | 191 ASSERT_TRUE(WebRtc_GetCPUInfo(kSSE2)); |
| 192 #elif defined(WEBRTC_ARCH_ARM_V7) |
| 193 ASSERT_TRUE(WebRtc_GetCPUFeaturesARM() & kCPUFeatureNEON); |
| 197 #endif | 194 #endif |
| 198 | 195 |
| 199 // Benchmark with unaligned input pointer. | 196 // Benchmark with unaligned input pointer. |
| 200 start = base::TimeTicks::HighResNow(); | 197 start = TickTime::Now(); |
| 201 for (int j = 0; j < convolve_iterations; ++j) { | 198 for (int j = 0; j < kConvolveIterations; ++j) { |
| 202 resampler.CONVOLVE_FUNC( | 199 resampler.CONVOLVE_FUNC( |
| 203 resampler.kernel_storage_.get() + 1, resampler.kernel_storage_.get(), | 200 resampler.kernel_storage_.get() + 1, resampler.kernel_storage_.get(), |
| 204 resampler.kernel_storage_.get(), kKernelInterpolationFactor); | 201 resampler.kernel_storage_.get(), kKernelInterpolationFactor); |
| 205 } | 202 } |
| 206 double total_time_optimized_unaligned_ms = | 203 double total_time_optimized_unaligned_us = |
| 207 (base::TimeTicks::HighResNow() - start).InMillisecondsF(); | 204 (TickTime::Now() - start).Microseconds(); |
| 208 printf(STRINGIZE(CONVOLVE_FUNC) " (unaligned) took %.2fms; which is %.2fx " | 205 printf(STRINGIZE(CONVOLVE_FUNC) "(unaligned) took %.2fms; which is %.2fx " |
| 209 "faster than Convolve_C.\n", total_time_optimized_unaligned_ms, | 206 "faster than Convolve_C.\n", total_time_optimized_unaligned_us / 1000, |
| 210 total_time_c_ms / total_time_optimized_unaligned_ms); | 207 total_time_c_us / total_time_optimized_unaligned_us); |
| 211 | 208 |
| 212 // Benchmark with aligned input pointer. | 209 // Benchmark with aligned input pointer. |
| 213 start = base::TimeTicks::HighResNow(); | 210 start = TickTime::Now(); |
| 214 for (int j = 0; j < convolve_iterations; ++j) { | 211 for (int j = 0; j < kConvolveIterations; ++j) { |
| 215 resampler.CONVOLVE_FUNC( | 212 resampler.CONVOLVE_FUNC( |
| 216 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(), | 213 resampler.kernel_storage_.get(), resampler.kernel_storage_.get(), |
| 217 resampler.kernel_storage_.get(), kKernelInterpolationFactor); | 214 resampler.kernel_storage_.get(), kKernelInterpolationFactor); |
| 218 } | 215 } |
| 219 double total_time_optimized_aligned_ms = | 216 double total_time_optimized_aligned_us = |
| 220 (base::TimeTicks::HighResNow() - start).InMillisecondsF(); | 217 (TickTime::Now() - start).Microseconds(); |
| 221 printf(STRINGIZE(CONVOLVE_FUNC) " (aligned) took %.2fms; which is %.2fx " | 218 printf(STRINGIZE(CONVOLVE_FUNC) " (aligned) took %.2fms; which is %.2fx " |
| 222 "faster than Convolve_C and %.2fx faster than " | 219 "faster than Convolve_C and %.2fx faster than " |
| 223 STRINGIZE(CONVOLVE_FUNC) " (unaligned).\n", | 220 STRINGIZE(CONVOLVE_FUNC) " (unaligned).\n", |
| 224 total_time_optimized_aligned_ms, | 221 total_time_optimized_aligned_us / 1000, |
| 225 total_time_c_ms / total_time_optimized_aligned_ms, | 222 total_time_c_us / total_time_optimized_aligned_us, |
| 226 total_time_optimized_unaligned_ms / total_time_optimized_aligned_ms); | 223 total_time_optimized_unaligned_us / total_time_optimized_aligned_us); |
| 227 #endif | 224 #endif |
| 228 } | 225 } |
| 229 | 226 |
| 230 #undef CONVOLVE_FUNC | 227 #undef CONVOLVE_FUNC |
| 231 | 228 |
| 232 // Fake audio source for testing the resampler. Generates a sinusoidal linear | |
| 233 // chirp (http://en.wikipedia.org/wiki/Chirp) which can be tuned to stress the | |
| 234 // resampler for the specific sample rate conversion being used. | |
| 235 class SinusoidalLinearChirpSource { | |
| 236 public: | |
| 237 SinusoidalLinearChirpSource(int sample_rate, | |
| 238 int samples, | |
| 239 double max_frequency) | |
| 240 : sample_rate_(sample_rate), | |
| 241 total_samples_(samples), | |
| 242 max_frequency_(max_frequency), | |
| 243 current_index_(0) { | |
| 244 // Chirp rate. | |
| 245 double duration = static_cast<double>(total_samples_) / sample_rate_; | |
| 246 k_ = (max_frequency_ - kMinFrequency) / duration; | |
| 247 } | |
| 248 | |
| 249 virtual ~SinusoidalLinearChirpSource() {} | |
| 250 | |
| 251 void ProvideInput(int frames, float* destination) { | |
| 252 for (int i = 0; i < frames; ++i, ++current_index_) { | |
| 253 // Filter out frequencies higher than Nyquist. | |
| 254 if (Frequency(current_index_) > 0.5 * sample_rate_) { | |
| 255 destination[i] = 0; | |
| 256 } else { | |
| 257 // Calculate time in seconds. | |
| 258 double t = static_cast<double>(current_index_) / sample_rate_; | |
| 259 | |
| 260 // Sinusoidal linear chirp. | |
| 261 destination[i] = sin(2 * M_PI * (kMinFrequency * t + (k_ / 2) * t * t)); | |
| 262 } | |
| 263 } | |
| 264 } | |
| 265 | |
| 266 double Frequency(int position) { | |
| 267 return kMinFrequency + position * (max_frequency_ - kMinFrequency) | |
| 268 / total_samples_; | |
| 269 } | |
| 270 | |
| 271 private: | |
| 272 enum { | |
| 273 kMinFrequency = 5 | |
| 274 }; | |
| 275 | |
| 276 double sample_rate_; | |
| 277 int total_samples_; | |
| 278 double max_frequency_; | |
| 279 double k_; | |
| 280 int current_index_; | |
| 281 | |
| 282 DISALLOW_COPY_AND_ASSIGN(SinusoidalLinearChirpSource); | |
| 283 }; | |
| 284 | |
| 285 typedef std::tr1::tuple<int, int, double, double> SincResamplerTestData; | 229 typedef std::tr1::tuple<int, int, double, double> SincResamplerTestData; |
| 286 class SincResamplerTest | 230 class SincResamplerTest |
| 287 : public testing::TestWithParam<SincResamplerTestData> { | 231 : public testing::TestWithParam<SincResamplerTestData> { |
| 288 public: | 232 public: |
| 289 SincResamplerTest() | 233 SincResamplerTest() |
| 290 : input_rate_(std::tr1::get<0>(GetParam())), | 234 : input_rate_(std::tr1::get<0>(GetParam())), |
| 291 output_rate_(std::tr1::get<1>(GetParam())), | 235 output_rate_(std::tr1::get<1>(GetParam())), |
| 292 rms_error_(std::tr1::get<2>(GetParam())), | 236 rms_error_(std::tr1::get<2>(GetParam())), |
| 293 low_freq_error_(std::tr1::get<3>(GetParam())) { | 237 low_freq_error_(std::tr1::get<3>(GetParam())) { |
| 294 } | 238 } |
| 295 | 239 |
| 296 virtual ~SincResamplerTest() {} | 240 virtual ~SincResamplerTest() {} |
| 297 | 241 |
| 298 protected: | 242 protected: |
| 299 int input_rate_; | 243 int input_rate_; |
| 300 int output_rate_; | 244 int output_rate_; |
| 301 double rms_error_; | 245 double rms_error_; |
| 302 double low_freq_error_; | 246 double low_freq_error_; |
| 303 }; | 247 }; |
| 304 | 248 |
| 305 // Tests resampling using a given input and output sample rate. | 249 // Tests resampling using a given input and output sample rate. |
| 306 TEST_P(SincResamplerTest, Resample) { | 250 TEST_P(SincResamplerTest, Resample) { |
| 307 // Make comparisons using one second of data. | 251 // Make comparisons using one second of data. |
| 308 static const double kTestDurationSecs = 1; | 252 static const double kTestDurationSecs = 1; |
| 309 int input_samples = kTestDurationSecs * input_rate_; | 253 const int input_samples = kTestDurationSecs * input_rate_; |
| 310 int output_samples = kTestDurationSecs * output_rate_; | 254 const int output_samples = kTestDurationSecs * output_rate_; |
| 311 | 255 |
| 312 // Nyquist frequency for the input sampling rate. | 256 // Nyquist frequency for the input sampling rate. |
| 313 double input_nyquist_freq = 0.5 * input_rate_; | 257 const double input_nyquist_freq = 0.5 * input_rate_; |
| 314 | 258 |
| 315 // Source for data to be resampled. | 259 // Source for data to be resampled. |
| 316 SinusoidalLinearChirpSource resampler_source( | 260 SinusoidalLinearChirpSource resampler_source( |
| 317 input_rate_, input_samples, input_nyquist_freq); | 261 input_rate_, input_samples, input_nyquist_freq, 0); |
| 318 | 262 |
| 319 const double io_ratio = input_rate_ / static_cast<double>(output_rate_); | 263 const double io_ratio = input_rate_ / static_cast<double>(output_rate_); |
| 320 SincResampler resampler( | 264 SincResampler resampler(io_ratio, SincResampler::kDefaultRequestSize, |
| 321 io_ratio, SincResampler::kDefaultRequestSize, | 265 &resampler_source); |
| 322 base::Bind(&SinusoidalLinearChirpSource::ProvideInput, | |
| 323 base::Unretained(&resampler_source))); | |
| 324 | 266 |
| 325 // Force an update to the sample rate ratio to ensure dyanmic sample rate | 267 // Force an update to the sample rate ratio to ensure dyanmic sample rate |
| 326 // changes are working correctly. | 268 // changes are working correctly. |
| 327 scoped_ptr<float[]> kernel(new float[SincResampler::kKernelStorageSize]); | 269 scoped_array<float> kernel(new float[SincResampler::kKernelStorageSize]); |
| 328 memcpy(kernel.get(), resampler.get_kernel_for_testing(), | 270 memcpy(kernel.get(), resampler.get_kernel_for_testing(), |
| 329 SincResampler::kKernelStorageSize); | 271 SincResampler::kKernelStorageSize); |
| 330 resampler.SetRatio(M_PI); | 272 resampler.SetRatio(M_PI); |
| 331 ASSERT_NE(0, memcmp(kernel.get(), resampler.get_kernel_for_testing(), | 273 ASSERT_NE(0, memcmp(kernel.get(), resampler.get_kernel_for_testing(), |
| 332 SincResampler::kKernelStorageSize)); | 274 SincResampler::kKernelStorageSize)); |
| 333 resampler.SetRatio(io_ratio); | 275 resampler.SetRatio(io_ratio); |
| 334 ASSERT_EQ(0, memcmp(kernel.get(), resampler.get_kernel_for_testing(), | 276 ASSERT_EQ(0, memcmp(kernel.get(), resampler.get_kernel_for_testing(), |
| 335 SincResampler::kKernelStorageSize)); | 277 SincResampler::kKernelStorageSize)); |
| 336 | 278 |
| 337 // TODO(dalecurtis): If we switch to AVX/SSE optimization, we'll need to | 279 // TODO(dalecurtis): If we switch to AVX/SSE optimization, we'll need to |
| 338 // allocate these on 32-byte boundaries and ensure they're sized % 32 bytes. | 280 // allocate these on 32-byte boundaries and ensure they're sized % 32 bytes. |
| 339 scoped_ptr<float[]> resampled_destination(new float[output_samples]); | 281 scoped_array<float> resampled_destination(new float[output_samples]); |
| 340 scoped_ptr<float[]> pure_destination(new float[output_samples]); | 282 scoped_array<float> pure_destination(new float[output_samples]); |
| 341 | 283 |
| 342 // Generate resampled signal. | 284 // Generate resampled signal. |
| 343 resampler.Resample(output_samples, resampled_destination.get()); | 285 resampler.Resample(output_samples, resampled_destination.get()); |
| 344 | 286 |
| 345 // Generate pure signal. | 287 // Generate pure signal. |
| 346 SinusoidalLinearChirpSource pure_source( | 288 SinusoidalLinearChirpSource pure_source( |
| 347 output_rate_, output_samples, input_nyquist_freq); | 289 output_rate_, output_samples, input_nyquist_freq, 0); |
| 348 pure_source.ProvideInput(output_samples, pure_destination.get()); | 290 pure_source.Run(output_samples, pure_destination.get()); |
| 349 | 291 |
| 350 // Range of the Nyquist frequency (0.5 * min(input rate, output_rate)) which | 292 // Range of the Nyquist frequency (0.5 * min(input rate, output_rate)) which |
| 351 // we refer to as low and high. | 293 // we refer to as low and high. |
| 352 static const double kLowFrequencyNyquistRange = 0.7; | 294 static const double kLowFrequencyNyquistRange = 0.7; |
| 353 static const double kHighFrequencyNyquistRange = 0.9; | 295 static const double kHighFrequencyNyquistRange = 0.9; |
| 354 | 296 |
| 355 // Calculate Root-Mean-Square-Error and maximum error for the resampling. | 297 // Calculate Root-Mean-Square-Error and maximum error for the resampling. |
| 356 double sum_of_squares = 0; | 298 double sum_of_squares = 0; |
| 357 double low_freq_max_error = 0; | 299 double low_freq_max_error = 0; |
| 358 double high_freq_max_error = 0; | 300 double high_freq_max_error = 0; |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 434 std::tr1::make_tuple(8000, 192000, kResamplingRMSError, -63.10), | 376 std::tr1::make_tuple(8000, 192000, kResamplingRMSError, -63.10), |
| 435 std::tr1::make_tuple(11025, 192000, kResamplingRMSError, -62.61), | 377 std::tr1::make_tuple(11025, 192000, kResamplingRMSError, -62.61), |
| 436 std::tr1::make_tuple(16000, 192000, kResamplingRMSError, -63.14), | 378 std::tr1::make_tuple(16000, 192000, kResamplingRMSError, -63.14), |
| 437 std::tr1::make_tuple(22050, 192000, kResamplingRMSError, -62.42), | 379 std::tr1::make_tuple(22050, 192000, kResamplingRMSError, -62.42), |
| 438 std::tr1::make_tuple(32000, 192000, kResamplingRMSError, -63.38), | 380 std::tr1::make_tuple(32000, 192000, kResamplingRMSError, -63.38), |
| 439 std::tr1::make_tuple(44100, 192000, kResamplingRMSError, -62.63), | 381 std::tr1::make_tuple(44100, 192000, kResamplingRMSError, -62.63), |
| 440 std::tr1::make_tuple(48000, 192000, kResamplingRMSError, -73.44), | 382 std::tr1::make_tuple(48000, 192000, kResamplingRMSError, -73.44), |
| 441 std::tr1::make_tuple(96000, 192000, kResamplingRMSError, -73.52), | 383 std::tr1::make_tuple(96000, 192000, kResamplingRMSError, -73.52), |
| 442 std::tr1::make_tuple(192000, 192000, kResamplingRMSError, -73.52))); | 384 std::tr1::make_tuple(192000, 192000, kResamplingRMSError, -73.52))); |
| 443 | 385 |
| 444 } // namespace media | 386 } // namespace webrtc |
| OLD | NEW |