blob: f1fb5f7dd05bbf606f900633a5ff2f436740181c [file] [log] [blame]
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +00001/*
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 11:18:31 +010013#include "audio/remix_resample.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "common_audio/resampler/include/push_resampler.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "rtc_base/arraysize.h"
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020016#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "rtc_base/format_macros.h"
18#include "test/gtest.h"
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000019
20namespace webrtc {
21namespace voe {
22namespace {
23
andrew@webrtc.orga78a41f2014-04-08 23:09:28 +000024class UtilityTest : public ::testing::Test {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000025 protected:
andrew@webrtc.orga78a41f2014-04-08 23:09:28 +000026 UtilityTest() {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000027 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:30 +000030 dst_frame_.CopyFrom(src_frame_);
31 golden_frame_.CopyFrom(src_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000032 }
33
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070034 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:31 +000038
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000039 PushResampler<int16_t> resampler_;
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000040 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 05:11:22 -080048void SetMonoFrame(float data, int sample_rate_hz, AudioFrame* frame) {
yujo36b1a5f2017-06-12 12:45:32 -070049 frame->Mute();
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000050 frame->num_channels_ = 1;
51 frame->sample_rate_hz_ = sample_rate_hz;
jens.nielsen228c2682017-03-01 05:11:22 -080052 frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100);
yujo36b1a5f2017-06-12 12:45:32 -070053 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -070054 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
yujo36b1a5f2017-06-12 12:45:32 -070055 frame_data[i] = static_cast<int16_t>(data * i);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000056 }
57}
58
59// Keep the existing sample rate.
jens.nielsen228c2682017-03-01 05:11:22 -080060void SetMonoFrame(float data, AudioFrame* frame) {
61 SetMonoFrame(data, frame->sample_rate_hz_, frame);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000062}
63
64// Sets the signal value to increase by |left| and |right| with every sample in
65// each channel respectively.
jens.nielsen228c2682017-03-01 05:11:22 -080066void SetStereoFrame(float left,
67 float right,
68 int sample_rate_hz,
69 AudioFrame* frame) {
yujo36b1a5f2017-06-12 12:45:32 -070070 frame->Mute();
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000071 frame->num_channels_ = 2;
72 frame->sample_rate_hz_ = sample_rate_hz;
jens.nielsen228c2682017-03-01 05:11:22 -080073 frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100);
yujo36b1a5f2017-06-12 12:45:32 -070074 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -070075 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
yujo36b1a5f2017-06-12 12:45:32 -070076 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:31 +000078 }
79}
80
81// Keep the existing sample rate.
jens.nielsen228c2682017-03-01 05:11:22 -080082void 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 12:45:32 -070094 frame->Mute();
jens.nielsen228c2682017-03-01 05:11:22 -080095 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 12:45:32 -070098 int16_t* frame_data = frame->mutable_data();
jens.nielsen228c2682017-03-01 05:11:22 -080099 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700100 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 05:11:22 -0800104 }
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000105}
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:29 +0000114// |test_frame|. It allows for up to a |max_delay| in samples between the
115// signals to compensate for the resampling delay.
Yves Gerey665174f2018-06-19 15:03:05 +0200116float ComputeSNR(const AudioFrame& ref_frame,
117 const AudioFrame& test_frame,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700118 size_t max_delay) {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000119 VerifyParams(ref_frame, test_frame);
120 float best_snr = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700121 size_t best_delay = 0;
122 for (size_t delay = 0; delay <= max_delay; delay++) {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000123 float mse = 0;
124 float variance = 0;
yujo36b1a5f2017-06-12 12:45:32 -0700125 const int16_t* ref_frame_data = ref_frame.data();
126 const int16_t* test_frame_data = test_frame.data();
Yves Gerey665174f2018-06-19 15:03:05 +0200127 for (size_t i = 0;
128 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_ - delay;
129 i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700130 int error = ref_frame_data[i] - test_frame_data[i + delay];
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000131 mse += error * error;
yujo36b1a5f2017-06-12 12:45:32 -0700132 variance += ref_frame_data[i] * ref_frame_data[i];
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000133 }
134 float snr = 100; // We assign 100 dB to the zero-error case.
135 if (mse > 0)
136 snr = 10 * log10(variance / mse);
137 if (snr > best_snr) {
138 best_snr = snr;
139 best_delay = delay;
140 }
141 }
Peter Kastingdce40cf2015-08-24 14:52:23 -0700142 printf("SNR=%.1f dB at delay=%" PRIuS "\n", best_snr, best_delay);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000143 return best_snr;
144}
145
146void VerifyFramesAreEqual(const AudioFrame& ref_frame,
147 const AudioFrame& test_frame) {
148 VerifyParams(ref_frame, test_frame);
yujo36b1a5f2017-06-12 12:45:32 -0700149 const int16_t* ref_frame_data = ref_frame.data();
Yves Gerey665174f2018-06-19 15:03:05 +0200150 const int16_t* test_frame_data = test_frame.data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700151 for (size_t i = 0;
152 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700153 EXPECT_EQ(ref_frame_data[i], test_frame_data[i]);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000154 }
155}
156
andrew@webrtc.orga78a41f2014-04-08 23:09:28 +0000157void UtilityTest::RunResampleTest(int src_channels,
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +0000158 int src_sample_rate_hz,
159 int dst_channels,
Alejandro Luebscdfe20b2015-09-23 12:49:12 -0700160 int dst_sample_rate_hz) {
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +0000161 PushResampler<int16_t> resampler; // Create a new one with every test.
jens.nielsen228c2682017-03-01 05:11:22 -0800162 const int16_t kSrcCh1 = 30; // Shouldn't overflow for any used sample rate.
163 const int16_t kSrcCh2 = 15;
164 const int16_t kSrcCh3 = 22;
165 const int16_t kSrcCh4 = 8;
Yves Gerey665174f2018-06-19 15:03:05 +0200166 const float resampling_factor =
167 (1.0 * src_sample_rate_hz) / dst_sample_rate_hz;
jens.nielsen228c2682017-03-01 05:11:22 -0800168 const float dst_ch1 = resampling_factor * kSrcCh1;
169 const float dst_ch2 = resampling_factor * kSrcCh2;
170 const float dst_ch3 = resampling_factor * kSrcCh3;
171 const float dst_ch4 = resampling_factor * kSrcCh4;
172 const float dst_stereo_to_mono = (dst_ch1 + dst_ch2) / 2;
173 const float dst_quad_to_mono = (dst_ch1 + dst_ch2 + dst_ch3 + dst_ch4) / 4;
174 const float dst_quad_to_stereo_ch1 = (dst_ch1 + dst_ch2) / 2;
175 const float dst_quad_to_stereo_ch2 = (dst_ch3 + dst_ch4) / 2;
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000176 if (src_channels == 1)
jens.nielsen228c2682017-03-01 05:11:22 -0800177 SetMonoFrame(kSrcCh1, src_sample_rate_hz, &src_frame_);
178 else if (src_channels == 2)
179 SetStereoFrame(kSrcCh1, kSrcCh2, src_sample_rate_hz, &src_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000180 else
jens.nielsen228c2682017-03-01 05:11:22 -0800181 SetQuadFrame(kSrcCh1, kSrcCh2, kSrcCh3, kSrcCh4, src_sample_rate_hz,
182 &src_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000183
184 if (dst_channels == 1) {
jens.nielsen228c2682017-03-01 05:11:22 -0800185 SetMonoFrame(0, dst_sample_rate_hz, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000186 if (src_channels == 1)
jens.nielsen228c2682017-03-01 05:11:22 -0800187 SetMonoFrame(dst_ch1, dst_sample_rate_hz, &golden_frame_);
188 else if (src_channels == 2)
189 SetMonoFrame(dst_stereo_to_mono, dst_sample_rate_hz, &golden_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000190 else
jens.nielsen228c2682017-03-01 05:11:22 -0800191 SetMonoFrame(dst_quad_to_mono, dst_sample_rate_hz, &golden_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000192 } else {
jens.nielsen228c2682017-03-01 05:11:22 -0800193 SetStereoFrame(0, 0, dst_sample_rate_hz, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000194 if (src_channels == 1)
jens.nielsen228c2682017-03-01 05:11:22 -0800195 SetStereoFrame(dst_ch1, dst_ch1, dst_sample_rate_hz, &golden_frame_);
196 else if (src_channels == 2)
197 SetStereoFrame(dst_ch1, dst_ch2, dst_sample_rate_hz, &golden_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000198 else
jens.nielsen228c2682017-03-01 05:11:22 -0800199 SetStereoFrame(dst_quad_to_stereo_ch1, dst_quad_to_stereo_ch2,
200 dst_sample_rate_hz, &golden_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000201 }
202
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000203 // The sinc resampler has a known delay, which we compute here. Multiplying by
204 // two gives us a crude maximum for any resampling, as the old resampler
205 // typically (but not always) has lower delay.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700206 static const size_t kInputKernelDelaySamples = 16;
207 const size_t max_delay = static_cast<size_t>(
208 static_cast<double>(dst_sample_rate_hz) / src_sample_rate_hz *
209 kInputKernelDelaySamples * dst_channels * 2);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000210 printf("(%d, %d Hz) -> (%d, %d Hz) ", // SNR reported on the same line later.
Yves Gerey665174f2018-06-19 15:03:05 +0200211 src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz);
Alejandro Luebscdfe20b2015-09-23 12:49:12 -0700212 RemixAndResample(src_frame_, &resampler, &dst_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000213
andrew@webrtc.orgc1eb5602013-06-03 19:00:29 +0000214 if (src_sample_rate_hz == 96000 && dst_sample_rate_hz == 8000) {
215 // The sinc resampler gives poor SNR at this extreme conversion, but we
216 // expect to see this rarely in practice.
217 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 14.0f);
218 } else {
219 EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 46.0f);
220 }
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000221}
222
andrew@webrtc.orga78a41f2014-04-08 23:09:28 +0000223TEST_F(UtilityTest, RemixAndResampleCopyFrameSucceeds) {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000224 // Stereo -> stereo.
jens.nielsen228c2682017-03-01 05:11:22 -0800225 SetStereoFrame(10, 10, &src_frame_);
226 SetStereoFrame(0, 0, &dst_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000227 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000228 VerifyFramesAreEqual(src_frame_, dst_frame_);
229
230 // Mono -> mono.
jens.nielsen228c2682017-03-01 05:11:22 -0800231 SetMonoFrame(20, &src_frame_);
232 SetMonoFrame(0, &dst_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000233 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000234 VerifyFramesAreEqual(src_frame_, dst_frame_);
235}
236
andrew@webrtc.orga78a41f2014-04-08 23:09:28 +0000237TEST_F(UtilityTest, RemixAndResampleMixingOnlySucceeds) {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000238 // Stereo -> mono.
jens.nielsen228c2682017-03-01 05:11:22 -0800239 SetStereoFrame(0, 0, &dst_frame_);
240 SetMonoFrame(10, &src_frame_);
241 SetStereoFrame(10, 10, &golden_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000242 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000243 VerifyFramesAreEqual(dst_frame_, golden_frame_);
244
245 // Mono -> stereo.
jens.nielsen228c2682017-03-01 05:11:22 -0800246 SetMonoFrame(0, &dst_frame_);
247 SetStereoFrame(10, 20, &src_frame_);
248 SetMonoFrame(15, &golden_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000249 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000250 VerifyFramesAreEqual(golden_frame_, dst_frame_);
251}
252
andrew@webrtc.orga78a41f2014-04-08 23:09:28 +0000253TEST_F(UtilityTest, RemixAndResampleSucceeds) {
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000254 const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000, 96000};
jens.nielsen228c2682017-03-01 05:11:22 -0800255 const int kSampleRatesSize = arraysize(kSampleRates);
256 const int kSrcChannels[] = {1, 2, 4};
257 const int kSrcChannelsSize = arraysize(kSrcChannels);
258 const int kDstChannels[] = {1, 2};
259 const int kDstChannelsSize = arraysize(kDstChannels);
260
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000261 for (int src_rate = 0; src_rate < kSampleRatesSize; src_rate++) {
262 for (int dst_rate = 0; dst_rate < kSampleRatesSize; dst_rate++) {
Yves Gerey665174f2018-06-19 15:03:05 +0200263 for (int src_channel = 0; src_channel < kSrcChannelsSize; src_channel++) {
jens.nielsen228c2682017-03-01 05:11:22 -0800264 for (int dst_channel = 0; dst_channel < kDstChannelsSize;
265 dst_channel++) {
266 RunResampleTest(kSrcChannels[src_channel], kSampleRates[src_rate],
267 kDstChannels[dst_channel], kSampleRates[dst_rate]);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000268 }
269 }
270 }
271 }
272}
273
274} // namespace
275} // namespace voe
276} // namespace webrtc