| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 using testing::_; | 29 using testing::_; |
| 30 | 30 |
| 31 namespace webrtc { | 31 namespace webrtc { |
| 32 | 32 |
| 33 static const double kSampleRateRatio = 192000.0 / 44100.0; | 33 static const double kSampleRateRatio = 192000.0 / 44100.0; |
| 34 static const double kKernelInterpolationFactor = 0.5; | 34 static const double kKernelInterpolationFactor = 0.5; |
| 35 | 35 |
| 36 // Helper class to ensure ChunkedResample() functions properly. | 36 // Helper class to ensure ChunkedResample() functions properly. |
| 37 class MockSource : public SincResamplerCallback { | 37 class MockSource : public SincResamplerCallback { |
| 38 public: | 38 public: |
| 39 MOCK_METHOD2(Run, void(int frames, float* destination)); | 39 MOCK_METHOD2(Run, void(size_t frames, float* destination)); |
| 40 }; | 40 }; |
| 41 | 41 |
| 42 ACTION(ClearBuffer) { | 42 ACTION(ClearBuffer) { |
| 43 memset(arg1, 0, arg0 * sizeof(float)); | 43 memset(arg1, 0, arg0 * sizeof(float)); |
| 44 } | 44 } |
| 45 | 45 |
| 46 ACTION(FillBuffer) { | 46 ACTION(FillBuffer) { |
| 47 // Value chosen arbitrarily such that SincResampler resamples it to something | 47 // Value chosen arbitrarily such that SincResampler resamples it to something |
| 48 // easily representable on all platforms; e.g., using kSampleRateRatio this | 48 // easily representable on all platforms; e.g., using kSampleRateRatio this |
| 49 // becomes 1.81219. | 49 // becomes 1.81219. |
| 50 memset(arg1, 64, arg0 * sizeof(float)); | 50 memset(arg1, 64, arg0 * sizeof(float)); |
| 51 } | 51 } |
| 52 | 52 |
| 53 // Test requesting multiples of ChunkSize() frames results in the proper number | 53 // Test requesting multiples of ChunkSize() frames results in the proper number |
| 54 // of callbacks. | 54 // of callbacks. |
| 55 TEST(SincResamplerTest, ChunkedResample) { | 55 TEST(SincResamplerTest, ChunkedResample) { |
| 56 MockSource mock_source; | 56 MockSource mock_source; |
| 57 | 57 |
| 58 // 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 |
| 59 // exhaustion of SincResampler's internal buffers. | 59 // exhaustion of SincResampler's internal buffers. |
| 60 SincResampler resampler(kSampleRateRatio, SincResampler::kDefaultRequestSize, | 60 SincResampler resampler(kSampleRateRatio, SincResampler::kDefaultRequestSize, |
| 61 &mock_source); | 61 &mock_source); |
| 62 | 62 |
| 63 static const int kChunks = 2; | 63 static const int kChunks = 2; |
| 64 int max_chunk_size = resampler.ChunkSize() * kChunks; | 64 size_t max_chunk_size = resampler.ChunkSize() * kChunks; |
| 65 rtc::scoped_ptr<float[]> resampled_destination(new float[max_chunk_size]); | 65 rtc::scoped_ptr<float[]> resampled_destination(new float[max_chunk_size]); |
| 66 | 66 |
| 67 // Verify requesting ChunkSize() frames causes a single callback. | 67 // Verify requesting ChunkSize() frames causes a single callback. |
| 68 EXPECT_CALL(mock_source, Run(_, _)) | 68 EXPECT_CALL(mock_source, Run(_, _)) |
| 69 .Times(1).WillOnce(ClearBuffer()); | 69 .Times(1).WillOnce(ClearBuffer()); |
| 70 resampler.Resample(resampler.ChunkSize(), resampled_destination.get()); | 70 resampler.Resample(resampler.ChunkSize(), resampled_destination.get()); |
| 71 | 71 |
| 72 // Verify requesting kChunks * ChunkSize() frames causes kChunks callbacks. | 72 // Verify requesting kChunks * ChunkSize() frames causes kChunks callbacks. |
| 73 testing::Mock::VerifyAndClear(&mock_source); | 73 testing::Mock::VerifyAndClear(&mock_source); |
| 74 EXPECT_CALL(mock_source, Run(_, _)) | 74 EXPECT_CALL(mock_source, Run(_, _)) |
| (...skipping 14 matching lines...) Expand all Loading... |
| 89 .Times(1).WillOnce(FillBuffer()); | 89 .Times(1).WillOnce(FillBuffer()); |
| 90 resampler.Resample(resampler.ChunkSize() / 2, resampled_destination.get()); | 90 resampler.Resample(resampler.ChunkSize() / 2, resampled_destination.get()); |
| 91 ASSERT_NE(resampled_destination[0], 0); | 91 ASSERT_NE(resampled_destination[0], 0); |
| 92 | 92 |
| 93 // Flush and request more data, which should all be zeros now. | 93 // Flush and request more data, which should all be zeros now. |
| 94 resampler.Flush(); | 94 resampler.Flush(); |
| 95 testing::Mock::VerifyAndClear(&mock_source); | 95 testing::Mock::VerifyAndClear(&mock_source); |
| 96 EXPECT_CALL(mock_source, Run(_, _)) | 96 EXPECT_CALL(mock_source, Run(_, _)) |
| 97 .Times(1).WillOnce(ClearBuffer()); | 97 .Times(1).WillOnce(ClearBuffer()); |
| 98 resampler.Resample(resampler.ChunkSize() / 2, resampled_destination.get()); | 98 resampler.Resample(resampler.ChunkSize() / 2, resampled_destination.get()); |
| 99 for (int i = 0; i < resampler.ChunkSize() / 2; ++i) | 99 for (size_t i = 0; i < resampler.ChunkSize() / 2; ++i) |
| 100 ASSERT_FLOAT_EQ(resampled_destination[i], 0); | 100 ASSERT_FLOAT_EQ(resampled_destination[i], 0); |
| 101 } | 101 } |
| 102 | 102 |
| 103 // Test flush resets the internal state properly. | 103 // Test flush resets the internal state properly. |
| 104 TEST(SincResamplerTest, DISABLED_SetRatioBench) { | 104 TEST(SincResamplerTest, DISABLED_SetRatioBench) { |
| 105 MockSource mock_source; | 105 MockSource mock_source; |
| 106 SincResampler resampler(kSampleRateRatio, SincResampler::kDefaultRequestSize, | 106 SincResampler resampler(kSampleRateRatio, SincResampler::kDefaultRequestSize, |
| 107 &mock_source); | 107 &mock_source); |
| 108 | 108 |
| 109 TickTime start = TickTime::Now(); | 109 TickTime start = TickTime::Now(); |
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 int input_rate_; | 244 int input_rate_; |
| 245 int output_rate_; | 245 int output_rate_; |
| 246 double rms_error_; | 246 double rms_error_; |
| 247 double low_freq_error_; | 247 double low_freq_error_; |
| 248 }; | 248 }; |
| 249 | 249 |
| 250 // Tests resampling using a given input and output sample rate. | 250 // Tests resampling using a given input and output sample rate. |
| 251 TEST_P(SincResamplerTest, Resample) { | 251 TEST_P(SincResamplerTest, Resample) { |
| 252 // Make comparisons using one second of data. | 252 // Make comparisons using one second of data. |
| 253 static const double kTestDurationSecs = 1; | 253 static const double kTestDurationSecs = 1; |
| 254 const int input_samples = kTestDurationSecs * input_rate_; | 254 const size_t input_samples = |
| 255 const int output_samples = kTestDurationSecs * output_rate_; | 255 static_cast<size_t>(kTestDurationSecs * input_rate_); |
| 256 const size_t output_samples = |
| 257 static_cast<size_t>(kTestDurationSecs * output_rate_); |
| 256 | 258 |
| 257 // Nyquist frequency for the input sampling rate. | 259 // Nyquist frequency for the input sampling rate. |
| 258 const double input_nyquist_freq = 0.5 * input_rate_; | 260 const double input_nyquist_freq = 0.5 * input_rate_; |
| 259 | 261 |
| 260 // Source for data to be resampled. | 262 // Source for data to be resampled. |
| 261 SinusoidalLinearChirpSource resampler_source( | 263 SinusoidalLinearChirpSource resampler_source( |
| 262 input_rate_, input_samples, input_nyquist_freq, 0); | 264 input_rate_, input_samples, input_nyquist_freq, 0); |
| 263 | 265 |
| 264 const double io_ratio = input_rate_ / static_cast<double>(output_rate_); | 266 const double io_ratio = input_rate_ / static_cast<double>(output_rate_); |
| 265 SincResampler resampler(io_ratio, SincResampler::kDefaultRequestSize, | 267 SincResampler resampler(io_ratio, SincResampler::kDefaultRequestSize, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 295 static const double kLowFrequencyNyquistRange = 0.7; | 297 static const double kLowFrequencyNyquistRange = 0.7; |
| 296 static const double kHighFrequencyNyquistRange = 0.9; | 298 static const double kHighFrequencyNyquistRange = 0.9; |
| 297 | 299 |
| 298 // Calculate Root-Mean-Square-Error and maximum error for the resampling. | 300 // Calculate Root-Mean-Square-Error and maximum error for the resampling. |
| 299 double sum_of_squares = 0; | 301 double sum_of_squares = 0; |
| 300 double low_freq_max_error = 0; | 302 double low_freq_max_error = 0; |
| 301 double high_freq_max_error = 0; | 303 double high_freq_max_error = 0; |
| 302 int minimum_rate = std::min(input_rate_, output_rate_); | 304 int minimum_rate = std::min(input_rate_, output_rate_); |
| 303 double low_frequency_range = kLowFrequencyNyquistRange * 0.5 * minimum_rate; | 305 double low_frequency_range = kLowFrequencyNyquistRange * 0.5 * minimum_rate; |
| 304 double high_frequency_range = kHighFrequencyNyquistRange * 0.5 * minimum_rate; | 306 double high_frequency_range = kHighFrequencyNyquistRange * 0.5 * minimum_rate; |
| 305 for (int i = 0; i < output_samples; ++i) { | 307 for (size_t i = 0; i < output_samples; ++i) { |
| 306 double error = fabs(resampled_destination[i] - pure_destination[i]); | 308 double error = fabs(resampled_destination[i] - pure_destination[i]); |
| 307 | 309 |
| 308 if (pure_source.Frequency(i) < low_frequency_range) { | 310 if (pure_source.Frequency(i) < low_frequency_range) { |
| 309 if (error > low_freq_max_error) | 311 if (error > low_freq_max_error) |
| 310 low_freq_max_error = error; | 312 low_freq_max_error = error; |
| 311 } else if (pure_source.Frequency(i) < high_frequency_range) { | 313 } else if (pure_source.Frequency(i) < high_frequency_range) { |
| 312 if (error > high_freq_max_error) | 314 if (error > high_freq_max_error) |
| 313 high_freq_max_error = error; | 315 high_freq_max_error = error; |
| 314 } | 316 } |
| 315 // TODO(dalecurtis): Sanity check frequencies > kHighFrequencyNyquistRange. | 317 // TODO(dalecurtis): Sanity check frequencies > kHighFrequencyNyquistRange. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 378 std::tr1::make_tuple(11025, 192000, kResamplingRMSError, -62.61), | 380 std::tr1::make_tuple(11025, 192000, kResamplingRMSError, -62.61), |
| 379 std::tr1::make_tuple(16000, 192000, kResamplingRMSError, -63.14), | 381 std::tr1::make_tuple(16000, 192000, kResamplingRMSError, -63.14), |
| 380 std::tr1::make_tuple(22050, 192000, kResamplingRMSError, -62.42), | 382 std::tr1::make_tuple(22050, 192000, kResamplingRMSError, -62.42), |
| 381 std::tr1::make_tuple(32000, 192000, kResamplingRMSError, -63.38), | 383 std::tr1::make_tuple(32000, 192000, kResamplingRMSError, -63.38), |
| 382 std::tr1::make_tuple(44100, 192000, kResamplingRMSError, -62.63), | 384 std::tr1::make_tuple(44100, 192000, kResamplingRMSError, -62.63), |
| 383 std::tr1::make_tuple(48000, 192000, kResamplingRMSError, -73.44), | 385 std::tr1::make_tuple(48000, 192000, kResamplingRMSError, -73.44), |
| 384 std::tr1::make_tuple(96000, 192000, kResamplingRMSError, -73.52), | 386 std::tr1::make_tuple(96000, 192000, kResamplingRMSError, -73.52), |
| 385 std::tr1::make_tuple(192000, 192000, kResamplingRMSError, -73.52))); | 387 std::tr1::make_tuple(192000, 192000, kResamplingRMSError, -73.52))); |
| 386 | 388 |
| 387 } // namespace webrtc | 389 } // namespace webrtc |
| OLD | NEW |