1 /*
2 * Copyright (c) 2012 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
11 #include <math.h>
12
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "webrtc/base/format_macros.h"
15 #include "webrtc/common_audio/resampler/include/push_resampler.h"
16 #include "webrtc/modules/include/module_common_types.h"
17 #include "webrtc/voice_engine/utility.h"
18 #include "webrtc/voice_engine/voice_engine_defines.h"
19
20 namespace webrtc {
21 namespace voe {
22 namespace {
23
24 class UtilityTest : public ::testing::Test {
25 protected:
UtilityTest()26 UtilityTest() {
27 src_frame_.sample_rate_hz_ = 16000;
28 src_frame_.samples_per_channel_ = src_frame_.sample_rate_hz_ / 100;
29 src_frame_.num_channels_ = 1;
30 dst_frame_.CopyFrom(src_frame_);
31 golden_frame_.CopyFrom(src_frame_);
32 }
33
34 void RunResampleTest(int src_channels,
35 int src_sample_rate_hz,
36 int dst_channels,
37 int dst_sample_rate_hz);
38
39 PushResampler<int16_t> resampler_;
40 AudioFrame src_frame_;
41 AudioFrame dst_frame_;
42 AudioFrame golden_frame_;
43 };
44
45 // Sets the signal value to increase by |data| with every sample. Floats are
46 // used so non-integer values result in rounding error, but not an accumulating
47 // error.
SetMonoFrame(AudioFrame * frame,float data,int sample_rate_hz)48 void SetMonoFrame(AudioFrame* frame, float data, int sample_rate_hz) {
49 memset(frame->data_, 0, sizeof(frame->data_));
50 frame->num_channels_ = 1;
51 frame->sample_rate_hz_ = sample_rate_hz;
52 frame->samples_per_channel_ = sample_rate_hz / 100;
53 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
54 frame->data_[i] = static_cast<int16_t>(data * i);
55 }
56 }
57
58 // Keep the existing sample rate.
SetMonoFrame(AudioFrame * frame,float data)59 void SetMonoFrame(AudioFrame* frame, float data) {
60 SetMonoFrame(frame, data, frame->sample_rate_hz_);
61 }
62
63 // Sets the signal value to increase by |left| and |right| with every sample in
64 // each channel respectively.
SetStereoFrame(AudioFrame * frame,float left,float right,int sample_rate_hz)65 void SetStereoFrame(AudioFrame* frame, float left, float right,
66 int sample_rate_hz) {
67 memset(frame->data_, 0, sizeof(frame->data_));
68 frame->num_channels_ = 2;
69 frame->sample_rate_hz_ = sample_rate_hz;
70 frame->samples_per_channel_ = sample_rate_hz / 100;
71 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
72 frame->data_[i * 2] = static_cast<int16_t>(left * i);
73 frame->data_[i * 2 + 1] = static_cast<int16_t>(right * i);
74 }
75 }
76
77 // Keep the existing sample rate.
SetStereoFrame(AudioFrame * frame,float left,float right)78 void SetStereoFrame(AudioFrame* frame, float left, float right) {
79 SetStereoFrame(frame, left, right, frame->sample_rate_hz_);
80 }
81
VerifyParams(const AudioFrame & ref_frame,const AudioFrame & test_frame)82 void VerifyParams(const AudioFrame& ref_frame, const AudioFrame& test_frame) {
83 EXPECT_EQ(ref_frame.num_channels_, test_frame.num_channels_);
84 EXPECT_EQ(ref_frame.samples_per_channel_, test_frame.samples_per_channel_);
85 EXPECT_EQ(ref_frame.sample_rate_hz_, test_frame.sample_rate_hz_);
86 }
87
88 // Computes the best SNR based on the error between |ref_frame| and
89 // |test_frame|. It allows for up to a |max_delay| in samples between the
90 // signals to compensate for the resampling delay.
ComputeSNR(const AudioFrame & ref_frame,const AudioFrame & test_frame,size_t max_delay)91 float ComputeSNR(const AudioFrame& ref_frame, const AudioFrame& test_frame,
92 size_t max_delay) {
93 VerifyParams(ref_frame, test_frame);
94 float best_snr = 0;
95 size_t best_delay = 0;
96 for (size_t delay = 0; delay <= max_delay; delay++) {
97 float mse = 0;
98 float variance = 0;
99 for (size_t i = 0; i < ref_frame.samples_per_channel_ *
100 ref_frame.num_channels_ - delay; i++) {
101 int error = ref_frame.data_[i] - test_frame.data_[i + delay];
102 mse += error * error;
103 variance += ref_frame.data_[i] * ref_frame.data_[i];
104 }
105 float snr = 100; // We assign 100 dB to the zero-error case.
106 if (mse > 0)
107 snr = 10 * log10(variance / mse);
108 if (snr > best_snr) {
109 best_snr = snr;
110 best_delay = delay;
111 }
112 }
113 printf("SNR=%.1f dB at delay=%" PRIuS "\n", best_snr, best_delay);
114 return best_snr;
115 }
116
VerifyFramesAreEqual(const AudioFrame & ref_frame,const AudioFrame & test_frame)117 void VerifyFramesAreEqual(const AudioFrame& ref_frame,
118 const AudioFrame& test_frame) {
119 VerifyParams(ref_frame, test_frame);
120 for (size_t i = 0;
121 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) {
122 EXPECT_EQ(ref_frame.data_[i], test_frame.data_[i]);
123 }
124 }
125
RunResampleTest(int src_channels,int src_sample_rate_hz,int dst_channels,int dst_sample_rate_hz)126 void UtilityTest::RunResampleTest(int src_channels,
127 int src_sample_rate_hz,
128 int dst_channels,
129 int dst_sample_rate_hz) {
130 PushResampler<int16_t> resampler; // Create a new one with every test.
131 const int16_t kSrcLeft = 30; // Shouldn't overflow for any used sample rate.
132 const int16_t kSrcRight = 15;
133 const float resampling_factor = (1.0 * src_sample_rate_hz) /
134 dst_sample_rate_hz;
135 const float dst_left = resampling_factor * kSrcLeft;
136 const float dst_right = resampling_factor * kSrcRight;
137 const float dst_mono = (dst_left + dst_right) / 2;
138 if (src_channels == 1)
139 SetMonoFrame(&src_frame_, kSrcLeft, src_sample_rate_hz);
140 else
141 SetStereoFrame(&src_frame_, kSrcLeft, kSrcRight, src_sample_rate_hz);
142
143 if (dst_channels == 1) {
144 SetMonoFrame(&dst_frame_, 0, dst_sample_rate_hz);
145 if (src_channels == 1)
146 SetMonoFrame(&golden_frame_, dst_left, dst_sample_rate_hz);
147 else
148 SetMonoFrame(&golden_frame_, dst_mono, dst_sample_rate_hz);
149 } else {
150 SetStereoFrame(&dst_frame_, 0, 0, dst_sample_rate_hz);
151 if (src_channels == 1)
152 SetStereoFrame(&golden_frame_, dst_left, dst_left, dst_sample_rate_hz);
153 else
154 SetStereoFrame(&golden_frame_, dst_left, dst_right, dst_sample_rate_hz);
155 }
156
157 // The sinc resampler has a known delay, which we compute here. Multiplying by
158 // two gives us a crude maximum for any resampling, as the old resampler
159 // typically (but not always) has lower delay.
160 static const size_t kInputKernelDelaySamples = 16;
161 const size_t max_delay = static_cast<size_t>(
162 static_cast<double>(dst_sample_rate_hz) / src_sample_rate_hz *
163 kInputKernelDelaySamples * dst_channels * 2);
164 printf("(%d, %d Hz) -> (%d, %d Hz) ", // SNR reported on the same line later.
165 src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz);
166 RemixAndResample(src_frame_, &resampler, &dst_frame_);
167
168 if (src_sample_rate_hz == 96000 && dst_sample_rate_hz == 8000) {
169 // The sinc resampler gives poor SNR at this extreme conversion, but we
170 // expect to see this rarely in practice.
171 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 14.0f);
172 } else {
173 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 46.0f);
174 }
175 }
176
TEST_F(UtilityTest,RemixAndResampleCopyFrameSucceeds)177 TEST_F(UtilityTest, RemixAndResampleCopyFrameSucceeds) {
178 // Stereo -> stereo.
179 SetStereoFrame(&src_frame_, 10, 10);
180 SetStereoFrame(&dst_frame_, 0, 0);
181 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
182 VerifyFramesAreEqual(src_frame_, dst_frame_);
183
184 // Mono -> mono.
185 SetMonoFrame(&src_frame_, 20);
186 SetMonoFrame(&dst_frame_, 0);
187 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
188 VerifyFramesAreEqual(src_frame_, dst_frame_);
189 }
190
TEST_F(UtilityTest,RemixAndResampleMixingOnlySucceeds)191 TEST_F(UtilityTest, RemixAndResampleMixingOnlySucceeds) {
192 // Stereo -> mono.
193 SetStereoFrame(&dst_frame_, 0, 0);
194 SetMonoFrame(&src_frame_, 10);
195 SetStereoFrame(&golden_frame_, 10, 10);
196 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
197 VerifyFramesAreEqual(dst_frame_, golden_frame_);
198
199 // Mono -> stereo.
200 SetMonoFrame(&dst_frame_, 0);
201 SetStereoFrame(&src_frame_, 10, 20);
202 SetMonoFrame(&golden_frame_, 15);
203 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
204 VerifyFramesAreEqual(golden_frame_, dst_frame_);
205 }
206
TEST_F(UtilityTest,RemixAndResampleSucceeds)207 TEST_F(UtilityTest, RemixAndResampleSucceeds) {
208 const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000, 96000};
209 const int kSampleRatesSize = sizeof(kSampleRates) / sizeof(*kSampleRates);
210 const int kChannels[] = {1, 2};
211 const int kChannelsSize = sizeof(kChannels) / sizeof(*kChannels);
212 for (int src_rate = 0; src_rate < kSampleRatesSize; src_rate++) {
213 for (int dst_rate = 0; dst_rate < kSampleRatesSize; dst_rate++) {
214 for (int src_channel = 0; src_channel < kChannelsSize; src_channel++) {
215 for (int dst_channel = 0; dst_channel < kChannelsSize; dst_channel++) {
216 RunResampleTest(kChannels[src_channel], kSampleRates[src_rate],
217 kChannels[dst_channel], kSampleRates[dst_rate]);
218 }
219 }
220 }
221 }
222 }
223
224 } // namespace
225 } // namespace voe
226 } // namespace webrtc
227