| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2014 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 |
| 11 #include <cmath> | 11 #include <cmath> |
| 12 #include <algorithm> | 12 #include <algorithm> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "webrtc/base/format_macros.h" |
| 16 #include "webrtc/base/scoped_ptr.h" | 17 #include "webrtc/base/scoped_ptr.h" |
| 17 #include "webrtc/common_audio/audio_converter.h" | 18 #include "webrtc/common_audio/audio_converter.h" |
| 18 #include "webrtc/common_audio/channel_buffer.h" | 19 #include "webrtc/common_audio/channel_buffer.h" |
| 19 #include "webrtc/common_audio/resampler/push_sinc_resampler.h" | 20 #include "webrtc/common_audio/resampler/push_sinc_resampler.h" |
| 20 | 21 |
| 21 namespace webrtc { | 22 namespace webrtc { |
| 22 | 23 |
| 23 typedef rtc::scoped_ptr<ChannelBuffer<float>> ScopedBuffer; | 24 typedef rtc::scoped_ptr<ChannelBuffer<float>> ScopedBuffer; |
| 24 | 25 |
| 25 // Sets the signal value to increase by |data| with every sample. | 26 // Sets the signal value to increase by |data| with every sample. |
| (...skipping 10 matching lines...) Expand all Loading... |
| 36 const ChannelBuffer<float>& test) { | 37 const ChannelBuffer<float>& test) { |
| 37 EXPECT_EQ(ref.num_channels(), test.num_channels()); | 38 EXPECT_EQ(ref.num_channels(), test.num_channels()); |
| 38 EXPECT_EQ(ref.num_frames(), test.num_frames()); | 39 EXPECT_EQ(ref.num_frames(), test.num_frames()); |
| 39 } | 40 } |
| 40 | 41 |
| 41 // Computes the best SNR based on the error between |ref_frame| and | 42 // Computes the best SNR based on the error between |ref_frame| and |
| 42 // |test_frame|. It searches around |expected_delay| in samples between the | 43 // |test_frame|. It searches around |expected_delay| in samples between the |
| 43 // signals to compensate for the resampling delay. | 44 // signals to compensate for the resampling delay. |
| 44 float ComputeSNR(const ChannelBuffer<float>& ref, | 45 float ComputeSNR(const ChannelBuffer<float>& ref, |
| 45 const ChannelBuffer<float>& test, | 46 const ChannelBuffer<float>& test, |
| 46 int expected_delay) { | 47 size_t expected_delay) { |
| 47 VerifyParams(ref, test); | 48 VerifyParams(ref, test); |
| 48 float best_snr = 0; | 49 float best_snr = 0; |
| 49 int best_delay = 0; | 50 size_t best_delay = 0; |
| 50 | 51 |
| 51 // Search within one sample of the expected delay. | 52 // Search within one sample of the expected delay. |
| 52 for (int delay = std::max(expected_delay, 1) - 1; | 53 for (size_t delay = std::max(expected_delay, static_cast<size_t>(1)) - 1; |
| 53 delay <= std::min(expected_delay + 1, ref.num_frames()); | 54 delay <= std::min(expected_delay + 1, ref.num_frames()); |
| 54 ++delay) { | 55 ++delay) { |
| 55 float mse = 0; | 56 float mse = 0; |
| 56 float variance = 0; | 57 float variance = 0; |
| 57 float mean = 0; | 58 float mean = 0; |
| 58 for (int i = 0; i < ref.num_channels(); ++i) { | 59 for (int i = 0; i < ref.num_channels(); ++i) { |
| 59 for (int j = 0; j < ref.num_frames() - delay; ++j) { | 60 for (size_t j = 0; j < ref.num_frames() - delay; ++j) { |
| 60 float error = ref.channels()[i][j] - test.channels()[i][j + delay]; | 61 float error = ref.channels()[i][j] - test.channels()[i][j + delay]; |
| 61 mse += error * error; | 62 mse += error * error; |
| 62 variance += ref.channels()[i][j] * ref.channels()[i][j]; | 63 variance += ref.channels()[i][j] * ref.channels()[i][j]; |
| 63 mean += ref.channels()[i][j]; | 64 mean += ref.channels()[i][j]; |
| 64 } | 65 } |
| 65 } | 66 } |
| 66 | 67 |
| 67 const int length = ref.num_channels() * (ref.num_frames() - delay); | 68 const size_t length = ref.num_channels() * (ref.num_frames() - delay); |
| 68 mse /= length; | 69 mse /= length; |
| 69 variance /= length; | 70 variance /= length; |
| 70 mean /= length; | 71 mean /= length; |
| 71 variance -= mean * mean; | 72 variance -= mean * mean; |
| 72 float snr = 100; // We assign 100 dB to the zero-error case. | 73 float snr = 100; // We assign 100 dB to the zero-error case. |
| 73 if (mse > 0) | 74 if (mse > 0) |
| 74 snr = 10 * std::log10(variance / mse); | 75 snr = 10 * std::log10(variance / mse); |
| 75 if (snr > best_snr) { | 76 if (snr > best_snr) { |
| 76 best_snr = snr; | 77 best_snr = snr; |
| 77 best_delay = delay; | 78 best_delay = delay; |
| 78 } | 79 } |
| 79 } | 80 } |
| 80 printf("SNR=%.1f dB at delay=%d\n", best_snr, best_delay); | 81 printf("SNR=%.1f dB at delay=%" PRIuS "\n", best_snr, best_delay); |
| 81 return best_snr; | 82 return best_snr; |
| 82 } | 83 } |
| 83 | 84 |
| 84 // Sets the source to a linearly increasing signal for which we can easily | 85 // Sets the source to a linearly increasing signal for which we can easily |
| 85 // generate a reference. Runs the AudioConverter and ensures the output has | 86 // generate a reference. Runs the AudioConverter and ensures the output has |
| 86 // sufficiently high SNR relative to the reference. | 87 // sufficiently high SNR relative to the reference. |
| 87 void RunAudioConverterTest(int src_channels, | 88 void RunAudioConverterTest(int src_channels, |
| 88 int src_sample_rate_hz, | 89 int src_sample_rate_hz, |
| 89 int dst_channels, | 90 int dst_channels, |
| 90 int dst_sample_rate_hz) { | 91 int dst_sample_rate_hz) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 115 ref_data.push_back(dst_left); | 116 ref_data.push_back(dst_left); |
| 116 if (src_channels == 1) | 117 if (src_channels == 1) |
| 117 ref_data.push_back(dst_left); | 118 ref_data.push_back(dst_left); |
| 118 else | 119 else |
| 119 ref_data.push_back(dst_right); | 120 ref_data.push_back(dst_right); |
| 120 } | 121 } |
| 121 ScopedBuffer dst_buffer = CreateBuffer(dst_data, dst_frames); | 122 ScopedBuffer dst_buffer = CreateBuffer(dst_data, dst_frames); |
| 122 ScopedBuffer ref_buffer = CreateBuffer(ref_data, dst_frames); | 123 ScopedBuffer ref_buffer = CreateBuffer(ref_data, dst_frames); |
| 123 | 124 |
| 124 // The sinc resampler has a known delay, which we compute here. | 125 // The sinc resampler has a known delay, which we compute here. |
| 125 const int delay_frames = src_sample_rate_hz == dst_sample_rate_hz ? 0 : | 126 const size_t delay_frames = src_sample_rate_hz == dst_sample_rate_hz ? 0 : |
| 126 PushSincResampler::AlgorithmicDelaySeconds(src_sample_rate_hz) * | 127 static_cast<size_t>( |
| 127 dst_sample_rate_hz; | 128 PushSincResampler::AlgorithmicDelaySeconds(src_sample_rate_hz) * |
| 129 dst_sample_rate_hz); |
| 128 printf("(%d, %d Hz) -> (%d, %d Hz) ", // SNR reported on the same line later. | 130 printf("(%d, %d Hz) -> (%d, %d Hz) ", // SNR reported on the same line later. |
| 129 src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz); | 131 src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz); |
| 130 | 132 |
| 131 rtc::scoped_ptr<AudioConverter> converter = AudioConverter::Create( | 133 rtc::scoped_ptr<AudioConverter> converter = AudioConverter::Create( |
| 132 src_channels, src_frames, dst_channels, dst_frames); | 134 src_channels, src_frames, dst_channels, dst_frames); |
| 133 converter->Convert(src_buffer->channels(), src_buffer->size(), | 135 converter->Convert(src_buffer->channels(), src_buffer->size(), |
| 134 dst_buffer->channels(), dst_buffer->size()); | 136 dst_buffer->channels(), dst_buffer->size()); |
| 135 | 137 |
| 136 EXPECT_LT(43.f, | 138 EXPECT_LT(43.f, |
| 137 ComputeSNR(*ref_buffer.get(), *dst_buffer.get(), delay_frames)); | 139 ComputeSNR(*ref_buffer.get(), *dst_buffer.get(), delay_frames)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 148 for (int dst_channel = 0; dst_channel < kChannelsSize; ++dst_channel) { | 150 for (int dst_channel = 0; dst_channel < kChannelsSize; ++dst_channel) { |
| 149 RunAudioConverterTest(kChannels[src_channel], kSampleRates[src_rate], | 151 RunAudioConverterTest(kChannels[src_channel], kSampleRates[src_rate], |
| 150 kChannels[dst_channel], kSampleRates[dst_rate]); | 152 kChannels[dst_channel], kSampleRates[dst_rate]); |
| 151 } | 153 } |
| 152 } | 154 } |
| 153 } | 155 } |
| 154 } | 156 } |
| 155 } | 157 } |
| 156 | 158 |
| 157 } // namespace webrtc | 159 } // namespace webrtc |
| OLD | NEW |