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 "audio/remix_resample.h"
12
13 #include <cmath>
14
15 #include "common_audio/resampler/include/push_resampler.h"
16 #include "rtc_base/arraysize.h"
17 #include "rtc_base/checks.h"
18 #include "test/gtest.h"
19
20 namespace webrtc {
21 namespace voe {
22 namespace {
23
GetFrameSize(int sample_rate_hz)24 int GetFrameSize(int sample_rate_hz) {
25 return sample_rate_hz / 100;
26 }
27
28 class UtilityTest : public ::testing::Test {
29 protected:
UtilityTest()30 UtilityTest() {
31 src_frame_.sample_rate_hz_ = 16000;
32 src_frame_.samples_per_channel_ = src_frame_.sample_rate_hz_ / 100;
33 src_frame_.num_channels_ = 1;
34 dst_frame_.CopyFrom(src_frame_);
35 golden_frame_.CopyFrom(src_frame_);
36 }
37
38 void RunResampleTest(int src_channels,
39 int src_sample_rate_hz,
40 int dst_channels,
41 int dst_sample_rate_hz);
42
43 PushResampler<int16_t> resampler_;
44 AudioFrame src_frame_;
45 AudioFrame dst_frame_;
46 AudioFrame golden_frame_;
47 };
48
49 // Sets the signal value to increase by `data` with every sample. Floats are
50 // used so non-integer values result in rounding error, but not an accumulating
51 // error.
SetMonoFrame(float data,int sample_rate_hz,AudioFrame * frame)52 void SetMonoFrame(float data, int sample_rate_hz, AudioFrame* frame) {
53 frame->Mute();
54 frame->num_channels_ = 1;
55 frame->sample_rate_hz_ = sample_rate_hz;
56 frame->samples_per_channel_ = GetFrameSize(sample_rate_hz);
57 int16_t* frame_data = frame->mutable_data();
58 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
59 frame_data[i] = static_cast<int16_t>(data * i);
60 }
61 }
62
63 // Keep the existing sample rate.
SetMonoFrame(float data,AudioFrame * frame)64 void SetMonoFrame(float data, AudioFrame* frame) {
65 SetMonoFrame(data, frame->sample_rate_hz_, frame);
66 }
67
68 // Sets the signal value to increase by `left` and `right` with every sample in
69 // each channel respectively.
SetStereoFrame(float left,float right,int sample_rate_hz,AudioFrame * frame)70 void SetStereoFrame(float left,
71 float right,
72 int sample_rate_hz,
73 AudioFrame* frame) {
74 frame->Mute();
75 frame->num_channels_ = 2;
76 frame->sample_rate_hz_ = sample_rate_hz;
77 frame->samples_per_channel_ = GetFrameSize(sample_rate_hz);
78 int16_t* frame_data = frame->mutable_data();
79 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
80 frame_data[i * 2] = static_cast<int16_t>(left * i);
81 frame_data[i * 2 + 1] = static_cast<int16_t>(right * i);
82 }
83 }
84
85 // Keep the existing sample rate.
SetStereoFrame(float left,float right,AudioFrame * frame)86 void SetStereoFrame(float left, float right, AudioFrame* frame) {
87 SetStereoFrame(left, right, frame->sample_rate_hz_, frame);
88 }
89
90 // Sets the signal value to increase by `ch1`, `ch2`, `ch3`, `ch4` with every
91 // sample in each channel respectively.
SetQuadFrame(float ch1,float ch2,float ch3,float ch4,int sample_rate_hz,AudioFrame * frame)92 void SetQuadFrame(float ch1,
93 float ch2,
94 float ch3,
95 float ch4,
96 int sample_rate_hz,
97 AudioFrame* frame) {
98 frame->Mute();
99 frame->num_channels_ = 4;
100 frame->sample_rate_hz_ = sample_rate_hz;
101 frame->samples_per_channel_ = GetFrameSize(sample_rate_hz);
102 int16_t* frame_data = frame->mutable_data();
103 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
104 frame_data[i * 4] = static_cast<int16_t>(ch1 * i);
105 frame_data[i * 4 + 1] = static_cast<int16_t>(ch2 * i);
106 frame_data[i * 4 + 2] = static_cast<int16_t>(ch3 * i);
107 frame_data[i * 4 + 3] = static_cast<int16_t>(ch4 * i);
108 }
109 }
110
VerifyParams(const AudioFrame & ref_frame,const AudioFrame & test_frame)111 void VerifyParams(const AudioFrame& ref_frame, const AudioFrame& test_frame) {
112 EXPECT_EQ(ref_frame.num_channels_, test_frame.num_channels_);
113 EXPECT_EQ(ref_frame.samples_per_channel_, test_frame.samples_per_channel_);
114 EXPECT_EQ(ref_frame.sample_rate_hz_, test_frame.sample_rate_hz_);
115 }
116
117 // Computes the best SNR based on the error between `ref_frame` and
118 // `test_frame`. It allows for up to a `max_delay` in samples between the
119 // signals to compensate for the resampling delay.
ComputeSNR(const AudioFrame & ref_frame,const AudioFrame & test_frame,size_t max_delay)120 float ComputeSNR(const AudioFrame& ref_frame,
121 const AudioFrame& test_frame,
122 size_t max_delay) {
123 VerifyParams(ref_frame, test_frame);
124 float best_snr = 0;
125 size_t best_delay = 0;
126 for (size_t delay = 0; delay <= max_delay; delay++) {
127 float mse = 0;
128 float variance = 0;
129 const int16_t* ref_frame_data = ref_frame.data();
130 const int16_t* test_frame_data = test_frame.data();
131 for (size_t i = 0;
132 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_ - delay;
133 i++) {
134 int error = ref_frame_data[i] - test_frame_data[i + delay];
135 mse += error * error;
136 variance += ref_frame_data[i] * ref_frame_data[i];
137 }
138 float snr = 100; // We assign 100 dB to the zero-error case.
139 if (mse > 0)
140 snr = 10 * std::log10(variance / mse);
141 if (snr > best_snr) {
142 best_snr = snr;
143 best_delay = delay;
144 }
145 }
146 printf("SNR=%.1f dB at delay=%zu\n", best_snr, best_delay);
147 return best_snr;
148 }
149
VerifyFramesAreEqual(const AudioFrame & ref_frame,const AudioFrame & test_frame)150 void VerifyFramesAreEqual(const AudioFrame& ref_frame,
151 const AudioFrame& test_frame) {
152 VerifyParams(ref_frame, test_frame);
153 const int16_t* ref_frame_data = ref_frame.data();
154 const int16_t* test_frame_data = test_frame.data();
155 for (size_t i = 0;
156 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) {
157 EXPECT_EQ(ref_frame_data[i], test_frame_data[i]);
158 }
159 }
160
RunResampleTest(int src_channels,int src_sample_rate_hz,int dst_channels,int dst_sample_rate_hz)161 void UtilityTest::RunResampleTest(int src_channels,
162 int src_sample_rate_hz,
163 int dst_channels,
164 int dst_sample_rate_hz) {
165 PushResampler<int16_t> resampler; // Create a new one with every test.
166 const int16_t kSrcCh1 = 30; // Shouldn't overflow for any used sample rate.
167 const int16_t kSrcCh2 = 15;
168 const int16_t kSrcCh3 = 22;
169 const int16_t kSrcCh4 = 8;
170 const float resampling_factor =
171 (1.0 * src_sample_rate_hz) / dst_sample_rate_hz;
172 const float dst_ch1 = resampling_factor * kSrcCh1;
173 const float dst_ch2 = resampling_factor * kSrcCh2;
174 const float dst_ch3 = resampling_factor * kSrcCh3;
175 const float dst_ch4 = resampling_factor * kSrcCh4;
176 const float dst_stereo_to_mono = (dst_ch1 + dst_ch2) / 2;
177 const float dst_quad_to_mono = (dst_ch1 + dst_ch2 + dst_ch3 + dst_ch4) / 4;
178 const float dst_quad_to_stereo_ch1 = (dst_ch1 + dst_ch2) / 2;
179 const float dst_quad_to_stereo_ch2 = (dst_ch3 + dst_ch4) / 2;
180 if (src_channels == 1)
181 SetMonoFrame(kSrcCh1, src_sample_rate_hz, &src_frame_);
182 else if (src_channels == 2)
183 SetStereoFrame(kSrcCh1, kSrcCh2, src_sample_rate_hz, &src_frame_);
184 else
185 SetQuadFrame(kSrcCh1, kSrcCh2, kSrcCh3, kSrcCh4, src_sample_rate_hz,
186 &src_frame_);
187
188 if (dst_channels == 1) {
189 SetMonoFrame(0, dst_sample_rate_hz, &dst_frame_);
190 if (src_channels == 1)
191 SetMonoFrame(dst_ch1, dst_sample_rate_hz, &golden_frame_);
192 else if (src_channels == 2)
193 SetMonoFrame(dst_stereo_to_mono, dst_sample_rate_hz, &golden_frame_);
194 else
195 SetMonoFrame(dst_quad_to_mono, dst_sample_rate_hz, &golden_frame_);
196 } else {
197 SetStereoFrame(0, 0, dst_sample_rate_hz, &dst_frame_);
198 if (src_channels == 1)
199 SetStereoFrame(dst_ch1, dst_ch1, dst_sample_rate_hz, &golden_frame_);
200 else if (src_channels == 2)
201 SetStereoFrame(dst_ch1, dst_ch2, dst_sample_rate_hz, &golden_frame_);
202 else
203 SetStereoFrame(dst_quad_to_stereo_ch1, dst_quad_to_stereo_ch2,
204 dst_sample_rate_hz, &golden_frame_);
205 }
206
207 // The sinc resampler has a known delay, which we compute here. Multiplying by
208 // two gives us a crude maximum for any resampling, as the old resampler
209 // typically (but not always) has lower delay.
210 static const size_t kInputKernelDelaySamples = 16;
211 const size_t max_delay = static_cast<size_t>(
212 static_cast<double>(dst_sample_rate_hz) / src_sample_rate_hz *
213 kInputKernelDelaySamples * dst_channels * 2);
214 printf("(%d, %d Hz) -> (%d, %d Hz) ", // SNR reported on the same line later.
215 src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz);
216 RemixAndResample(src_frame_, &resampler, &dst_frame_);
217
218 if (src_sample_rate_hz == 96000 && dst_sample_rate_hz <= 11025) {
219 // The sinc resampler gives poor SNR at this extreme conversion, but we
220 // expect to see this rarely in practice.
221 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 14.0f);
222 } else {
223 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 46.0f);
224 }
225 }
226
TEST_F(UtilityTest,RemixAndResampleCopyFrameSucceeds)227 TEST_F(UtilityTest, RemixAndResampleCopyFrameSucceeds) {
228 // Stereo -> stereo.
229 SetStereoFrame(10, 10, &src_frame_);
230 SetStereoFrame(0, 0, &dst_frame_);
231 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
232 VerifyFramesAreEqual(src_frame_, dst_frame_);
233
234 // Mono -> mono.
235 SetMonoFrame(20, &src_frame_);
236 SetMonoFrame(0, &dst_frame_);
237 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
238 VerifyFramesAreEqual(src_frame_, dst_frame_);
239 }
240
TEST_F(UtilityTest,RemixAndResampleMixingOnlySucceeds)241 TEST_F(UtilityTest, RemixAndResampleMixingOnlySucceeds) {
242 // Stereo -> mono.
243 SetStereoFrame(0, 0, &dst_frame_);
244 SetMonoFrame(10, &src_frame_);
245 SetStereoFrame(10, 10, &golden_frame_);
246 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
247 VerifyFramesAreEqual(dst_frame_, golden_frame_);
248
249 // Mono -> stereo.
250 SetMonoFrame(0, &dst_frame_);
251 SetStereoFrame(10, 20, &src_frame_);
252 SetMonoFrame(15, &golden_frame_);
253 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
254 VerifyFramesAreEqual(golden_frame_, dst_frame_);
255 }
256
TEST_F(UtilityTest,RemixAndResampleSucceeds)257 TEST_F(UtilityTest, RemixAndResampleSucceeds) {
258 const int kSampleRates[] = {8000, 11025, 16000, 22050,
259 32000, 44100, 48000, 96000};
260 const int kSrcChannels[] = {1, 2, 4};
261 const int kDstChannels[] = {1, 2};
262
263 for (int src_rate : kSampleRates) {
264 for (int dst_rate : kSampleRates) {
265 for (size_t src_channels : kSrcChannels) {
266 for (size_t dst_channels : kDstChannels) {
267 RunResampleTest(src_channels, src_rate, dst_channels, dst_rate);
268 }
269 }
270 }
271 }
272 }
273
274 } // namespace
275 } // namespace voe
276 } // namespace webrtc
277