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