blob: 753584b46d76164e74f46ce5890d3130793a5e0e [file] [log] [blame]
andrew@webrtc.org4ecea3e2012-06-27 03:25:311/*
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
Fredrik Solenberga8b7c7f2018-01-17 10:18:3113#include "audio/remix_resample.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3114#include "common_audio/resampler/include/push_resampler.h"
15#include "modules/include/module_common_types.h"
16#include "rtc_base/arraysize.h"
17#include "rtc_base/format_macros.h"
18#include "test/gtest.h"
andrew@webrtc.org4ecea3e2012-06-27 03:25:3119
20namespace webrtc {
21namespace voe {
22namespace {
23
andrew@webrtc.orga78a41f2014-04-08 23:09:2824class UtilityTest : public ::testing::Test {
andrew@webrtc.org4ecea3e2012-06-27 03:25:3125 protected:
andrew@webrtc.orga78a41f2014-04-08 23:09:2826 UtilityTest() {
andrew@webrtc.org4ecea3e2012-06-27 03:25:3127 src_frame_.sample_rate_hz_ = 16000;
28 src_frame_.samples_per_channel_ = src_frame_.sample_rate_hz_ / 100;
29 src_frame_.num_channels_ = 1;
andrew@webrtc.orgae1a58b2013-01-22 04:44:3030 dst_frame_.CopyFrom(src_frame_);
31 golden_frame_.CopyFrom(src_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:3132 }
33
Alejandro Luebscdfe20b2015-09-23 19:49:1234 void RunResampleTest(int src_channels,
35 int src_sample_rate_hz,
36 int dst_channels,
37 int dst_sample_rate_hz);
andrew@webrtc.org4ecea3e2012-06-27 03:25:3138
andrew@webrtc.orgf5a33f12014-04-19 00:32:0739 PushResampler<int16_t> resampler_;
andrew@webrtc.org4ecea3e2012-06-27 03:25:3140 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.
jens.nielsen228c2682017-03-01 13:11:2248void SetMonoFrame(float data, int sample_rate_hz, AudioFrame* frame) {
yujo36b1a5f2017-06-12 19:45:3249 frame->Mute();
andrew@webrtc.org4ecea3e2012-06-27 03:25:3150 frame->num_channels_ = 1;
51 frame->sample_rate_hz_ = sample_rate_hz;
jens.nielsen228c2682017-03-01 13:11:2252 frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100);
yujo36b1a5f2017-06-12 19:45:3253 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 21:52:2354 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
yujo36b1a5f2017-06-12 19:45:3255 frame_data[i] = static_cast<int16_t>(data * i);
andrew@webrtc.org4ecea3e2012-06-27 03:25:3156 }
57}
58
59// Keep the existing sample rate.
jens.nielsen228c2682017-03-01 13:11:2260void SetMonoFrame(float data, AudioFrame* frame) {
61 SetMonoFrame(data, frame->sample_rate_hz_, frame);
andrew@webrtc.org4ecea3e2012-06-27 03:25:3162}
63
64// Sets the signal value to increase by |left| and |right| with every sample in
65// each channel respectively.
jens.nielsen228c2682017-03-01 13:11:2266void SetStereoFrame(float left,
67 float right,
68 int sample_rate_hz,
69 AudioFrame* frame) {
yujo36b1a5f2017-06-12 19:45:3270 frame->Mute();
andrew@webrtc.org4ecea3e2012-06-27 03:25:3171 frame->num_channels_ = 2;
72 frame->sample_rate_hz_ = sample_rate_hz;
jens.nielsen228c2682017-03-01 13:11:2273 frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100);
yujo36b1a5f2017-06-12 19:45:3274 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 21:52:2375 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
yujo36b1a5f2017-06-12 19:45:3276 frame_data[i * 2] = static_cast<int16_t>(left * i);
77 frame_data[i * 2 + 1] = static_cast<int16_t>(right * i);
andrew@webrtc.org4ecea3e2012-06-27 03:25:3178 }
79}
80
81// Keep the existing sample rate.
jens.nielsen228c2682017-03-01 13:11:2282void SetStereoFrame(float left, float right, AudioFrame* frame) {
83 SetStereoFrame(left, right, frame->sample_rate_hz_, frame);
84}
85
86// Sets the signal value to increase by |ch1|, |ch2|, |ch3|, |ch4| with every
87// sample in each channel respectively.
88void SetQuadFrame(float ch1,
89 float ch2,
90 float ch3,
91 float ch4,
92 int sample_rate_hz,
93 AudioFrame* frame) {
yujo36b1a5f2017-06-12 19:45:3294 frame->Mute();
jens.nielsen228c2682017-03-01 13:11:2295 frame->num_channels_ = 4;
96 frame->sample_rate_hz_ = sample_rate_hz;
97 frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100);
yujo36b1a5f2017-06-12 19:45:3298 int16_t* frame_data = frame->mutable_data();
jens.nielsen228c2682017-03-01 13:11:2299 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
yujo36b1a5f2017-06-12 19:45:32100 frame_data[i * 4] = static_cast<int16_t>(ch1 * i);
101 frame_data[i * 4 + 1] = static_cast<int16_t>(ch2 * i);
102 frame_data[i * 4 + 2] = static_cast<int16_t>(ch3 * i);
103 frame_data[i * 4 + 3] = static_cast<int16_t>(ch4 * i);
jens.nielsen228c2682017-03-01 13:11:22104 }
andrew@webrtc.org4ecea3e2012-06-27 03:25:31105}
106
107void VerifyParams(const AudioFrame& ref_frame, const AudioFrame& test_frame) {
108 EXPECT_EQ(ref_frame.num_channels_, test_frame.num_channels_);
109 EXPECT_EQ(ref_frame.samples_per_channel_, test_frame.samples_per_channel_);
110 EXPECT_EQ(ref_frame.sample_rate_hz_, test_frame.sample_rate_hz_);
111}
112
113// Computes the best SNR based on the error between |ref_frame| and
andrew@webrtc.org50b2efe2013-04-29 17:27:29114// |test_frame|. It allows for up to a |max_delay| in samples between the
115// signals to compensate for the resampling delay.
116float ComputeSNR(const AudioFrame& ref_frame, const AudioFrame& test_frame,
Peter Kastingdce40cf2015-08-24 21:52:23117 size_t max_delay) {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31118 VerifyParams(ref_frame, test_frame);
119 float best_snr = 0;
Peter Kastingdce40cf2015-08-24 21:52:23120 size_t best_delay = 0;
121 for (size_t delay = 0; delay <= max_delay; delay++) {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31122 float mse = 0;
123 float variance = 0;
yujo36b1a5f2017-06-12 19:45:32124 const int16_t* ref_frame_data = ref_frame.data();
125 const int16_t* test_frame_data = test_frame.data();
Peter Kastingdce40cf2015-08-24 21:52:23126 for (size_t i = 0; i < ref_frame.samples_per_channel_ *
andrew@webrtc.org4ecea3e2012-06-27 03:25:31127 ref_frame.num_channels_ - delay; i++) {
yujo36b1a5f2017-06-12 19:45:32128 int error = ref_frame_data[i] - test_frame_data[i + delay];
andrew@webrtc.org4ecea3e2012-06-27 03:25:31129 mse += error * error;
yujo36b1a5f2017-06-12 19:45:32130 variance += ref_frame_data[i] * ref_frame_data[i];
andrew@webrtc.org4ecea3e2012-06-27 03:25:31131 }
132 float snr = 100; // We assign 100 dB to the zero-error case.
133 if (mse > 0)
134 snr = 10 * log10(variance / mse);
135 if (snr > best_snr) {
136 best_snr = snr;
137 best_delay = delay;
138 }
139 }
Peter Kastingdce40cf2015-08-24 21:52:23140 printf("SNR=%.1f dB at delay=%" PRIuS "\n", best_snr, best_delay);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31141 return best_snr;
142}
143
144void VerifyFramesAreEqual(const AudioFrame& ref_frame,
145 const AudioFrame& test_frame) {
146 VerifyParams(ref_frame, test_frame);
yujo36b1a5f2017-06-12 19:45:32147 const int16_t* ref_frame_data = ref_frame.data();
148 const int16_t* test_frame_data = test_frame.data();
Peter Kastingdce40cf2015-08-24 21:52:23149 for (size_t i = 0;
150 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) {
yujo36b1a5f2017-06-12 19:45:32151 EXPECT_EQ(ref_frame_data[i], test_frame_data[i]);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31152 }
153}
154
andrew@webrtc.orga78a41f2014-04-08 23:09:28155void UtilityTest::RunResampleTest(int src_channels,
andrew@webrtc.orgf5a33f12014-04-19 00:32:07156 int src_sample_rate_hz,
157 int dst_channels,
Alejandro Luebscdfe20b2015-09-23 19:49:12158 int dst_sample_rate_hz) {
andrew@webrtc.orgf5a33f12014-04-19 00:32:07159 PushResampler<int16_t> resampler; // Create a new one with every test.
jens.nielsen228c2682017-03-01 13:11:22160 const int16_t kSrcCh1 = 30; // Shouldn't overflow for any used sample rate.
161 const int16_t kSrcCh2 = 15;
162 const int16_t kSrcCh3 = 22;
163 const int16_t kSrcCh4 = 8;
andrew@webrtc.org50b2efe2013-04-29 17:27:29164 const float resampling_factor = (1.0 * src_sample_rate_hz) /
andrew@webrtc.org4ecea3e2012-06-27 03:25:31165 dst_sample_rate_hz;
jens.nielsen228c2682017-03-01 13:11:22166 const float dst_ch1 = resampling_factor * kSrcCh1;
167 const float dst_ch2 = resampling_factor * kSrcCh2;
168 const float dst_ch3 = resampling_factor * kSrcCh3;
169 const float dst_ch4 = resampling_factor * kSrcCh4;
170 const float dst_stereo_to_mono = (dst_ch1 + dst_ch2) / 2;
171 const float dst_quad_to_mono = (dst_ch1 + dst_ch2 + dst_ch3 + dst_ch4) / 4;
172 const float dst_quad_to_stereo_ch1 = (dst_ch1 + dst_ch2) / 2;
173 const float dst_quad_to_stereo_ch2 = (dst_ch3 + dst_ch4) / 2;
andrew@webrtc.org4ecea3e2012-06-27 03:25:31174 if (src_channels == 1)
jens.nielsen228c2682017-03-01 13:11:22175 SetMonoFrame(kSrcCh1, src_sample_rate_hz, &src_frame_);
176 else if (src_channels == 2)
177 SetStereoFrame(kSrcCh1, kSrcCh2, src_sample_rate_hz, &src_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31178 else
jens.nielsen228c2682017-03-01 13:11:22179 SetQuadFrame(kSrcCh1, kSrcCh2, kSrcCh3, kSrcCh4, src_sample_rate_hz,
180 &src_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31181
182 if (dst_channels == 1) {
jens.nielsen228c2682017-03-01 13:11:22183 SetMonoFrame(0, dst_sample_rate_hz, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31184 if (src_channels == 1)
jens.nielsen228c2682017-03-01 13:11:22185 SetMonoFrame(dst_ch1, dst_sample_rate_hz, &golden_frame_);
186 else if (src_channels == 2)
187 SetMonoFrame(dst_stereo_to_mono, dst_sample_rate_hz, &golden_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31188 else
jens.nielsen228c2682017-03-01 13:11:22189 SetMonoFrame(dst_quad_to_mono, dst_sample_rate_hz, &golden_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31190 } else {
jens.nielsen228c2682017-03-01 13:11:22191 SetStereoFrame(0, 0, dst_sample_rate_hz, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31192 if (src_channels == 1)
jens.nielsen228c2682017-03-01 13:11:22193 SetStereoFrame(dst_ch1, dst_ch1, dst_sample_rate_hz, &golden_frame_);
194 else if (src_channels == 2)
195 SetStereoFrame(dst_ch1, dst_ch2, dst_sample_rate_hz, &golden_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31196 else
jens.nielsen228c2682017-03-01 13:11:22197 SetStereoFrame(dst_quad_to_stereo_ch1, dst_quad_to_stereo_ch2,
198 dst_sample_rate_hz, &golden_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31199 }
200
andrew@webrtc.org50b2efe2013-04-29 17:27:29201 // The sinc resampler has a known delay, which we compute here. Multiplying by
202 // two gives us a crude maximum for any resampling, as the old resampler
203 // typically (but not always) has lower delay.
Peter Kastingdce40cf2015-08-24 21:52:23204 static const size_t kInputKernelDelaySamples = 16;
205 const size_t max_delay = static_cast<size_t>(
206 static_cast<double>(dst_sample_rate_hz) / src_sample_rate_hz *
207 kInputKernelDelaySamples * dst_channels * 2);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31208 printf("(%d, %d Hz) -> (%d, %d Hz) ", // SNR reported on the same line later.
209 src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz);
Alejandro Luebscdfe20b2015-09-23 19:49:12210 RemixAndResample(src_frame_, &resampler, &dst_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01211
andrew@webrtc.orgc1eb5602013-06-03 19:00:29212 if (src_sample_rate_hz == 96000 && dst_sample_rate_hz == 8000) {
213 // The sinc resampler gives poor SNR at this extreme conversion, but we
214 // expect to see this rarely in practice.
215 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 14.0f);
216 } else {
217 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 46.0f);
218 }
andrew@webrtc.org4ecea3e2012-06-27 03:25:31219}
220
andrew@webrtc.orga78a41f2014-04-08 23:09:28221TEST_F(UtilityTest, RemixAndResampleCopyFrameSucceeds) {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31222 // Stereo -> stereo.
jens.nielsen228c2682017-03-01 13:11:22223 SetStereoFrame(10, 10, &src_frame_);
224 SetStereoFrame(0, 0, &dst_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01225 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31226 VerifyFramesAreEqual(src_frame_, dst_frame_);
227
228 // Mono -> mono.
jens.nielsen228c2682017-03-01 13:11:22229 SetMonoFrame(20, &src_frame_);
230 SetMonoFrame(0, &dst_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01231 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31232 VerifyFramesAreEqual(src_frame_, dst_frame_);
233}
234
andrew@webrtc.orga78a41f2014-04-08 23:09:28235TEST_F(UtilityTest, RemixAndResampleMixingOnlySucceeds) {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31236 // Stereo -> mono.
jens.nielsen228c2682017-03-01 13:11:22237 SetStereoFrame(0, 0, &dst_frame_);
238 SetMonoFrame(10, &src_frame_);
239 SetStereoFrame(10, 10, &golden_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01240 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31241 VerifyFramesAreEqual(dst_frame_, golden_frame_);
242
243 // Mono -> stereo.
jens.nielsen228c2682017-03-01 13:11:22244 SetMonoFrame(0, &dst_frame_);
245 SetStereoFrame(10, 20, &src_frame_);
246 SetMonoFrame(15, &golden_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01247 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31248 VerifyFramesAreEqual(golden_frame_, dst_frame_);
249}
250
andrew@webrtc.orga78a41f2014-04-08 23:09:28251TEST_F(UtilityTest, RemixAndResampleSucceeds) {
andrew@webrtc.org50b2efe2013-04-29 17:27:29252 const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000, 96000};
jens.nielsen228c2682017-03-01 13:11:22253 const int kSampleRatesSize = arraysize(kSampleRates);
254 const int kSrcChannels[] = {1, 2, 4};
255 const int kSrcChannelsSize = arraysize(kSrcChannels);
256 const int kDstChannels[] = {1, 2};
257 const int kDstChannelsSize = arraysize(kDstChannels);
258
andrew@webrtc.org4ecea3e2012-06-27 03:25:31259 for (int src_rate = 0; src_rate < kSampleRatesSize; src_rate++) {
260 for (int dst_rate = 0; dst_rate < kSampleRatesSize; dst_rate++) {
jens.nielsen228c2682017-03-01 13:11:22261 for (int src_channel = 0; src_channel < kSrcChannelsSize;
262 src_channel++) {
263 for (int dst_channel = 0; dst_channel < kDstChannelsSize;
264 dst_channel++) {
265 RunResampleTest(kSrcChannels[src_channel], kSampleRates[src_rate],
266 kDstChannels[dst_channel], kSampleRates[dst_rate]);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31267 }
268 }
269 }
270 }
271}
272
273} // namespace
274} // namespace voe
275} // namespace webrtc