blob: 31dcfac1fe75c00cd64ad5cf2da9b755dd16d0c1 [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
Jonas Olssona4d87372019-07-05 19:08:33 +020011#include "audio/remix_resample.h"
12
Mirko Bonadeif0b8dee2019-03-15 10:47:11 +010013#include <cmath>
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000014
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "common_audio/resampler/include/push_resampler.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/arraysize.h"
Fredrik Solenbergbbf21a32018-04-12 22:44:09 +020017#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "test/gtest.h"
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000019
20namespace webrtc {
21namespace voe {
22namespace {
23
Sam Zackrisson3bd444f2022-08-03 14:37:00 +020024int GetFrameSize(int sample_rate_hz) {
25 return sample_rate_hz / 100;
26}
27
andrew@webrtc.orga78a41f2014-04-08 23:09:28 +000028class UtilityTest : public ::testing::Test {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000029 protected:
andrew@webrtc.orga78a41f2014-04-08 23:09:28 +000030 UtilityTest() {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000031 src_frame_.sample_rate_hz_ = 16000;
32 src_frame_.samples_per_channel_ = src_frame_.sample_rate_hz_ / 100;
33 src_frame_.num_channels_ = 1;
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +000034 dst_frame_.CopyFrom(src_frame_);
35 golden_frame_.CopyFrom(src_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000036 }
37
Alejandro Luebscdfe20b2015-09-23 12:49:12 -070038 void RunResampleTest(int src_channels,
39 int src_sample_rate_hz,
40 int dst_channels,
41 int dst_sample_rate_hz);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000042
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +000043 PushResampler<int16_t> resampler_;
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000044 AudioFrame src_frame_;
45 AudioFrame dst_frame_;
46 AudioFrame golden_frame_;
47};
48
Artem Titovb0ea6372021-07-26 11:47:07 +020049// Sets the signal value to increase by `data` with every sample. Floats are
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000050// used so non-integer values result in rounding error, but not an accumulating
51// error.
jens.nielsen228c2682017-03-01 05:11:22 -080052void SetMonoFrame(float data, int sample_rate_hz, AudioFrame* frame) {
yujo36b1a5f2017-06-12 12:45:32 -070053 frame->Mute();
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000054 frame->num_channels_ = 1;
55 frame->sample_rate_hz_ = sample_rate_hz;
Sam Zackrisson3bd444f2022-08-03 14:37:00 +020056 frame->samples_per_channel_ = GetFrameSize(sample_rate_hz);
yujo36b1a5f2017-06-12 12:45:32 -070057 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -070058 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
yujo36b1a5f2017-06-12 12:45:32 -070059 frame_data[i] = static_cast<int16_t>(data * i);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000060 }
61}
62
63// Keep the existing sample rate.
jens.nielsen228c2682017-03-01 05:11:22 -080064void SetMonoFrame(float data, AudioFrame* frame) {
65 SetMonoFrame(data, frame->sample_rate_hz_, frame);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000066}
67
Artem Titovb0ea6372021-07-26 11:47:07 +020068// Sets the signal value to increase by `left` and `right` with every sample in
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000069// each channel respectively.
jens.nielsen228c2682017-03-01 05:11:22 -080070void SetStereoFrame(float left,
71 float right,
72 int sample_rate_hz,
73 AudioFrame* frame) {
yujo36b1a5f2017-06-12 12:45:32 -070074 frame->Mute();
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000075 frame->num_channels_ = 2;
76 frame->sample_rate_hz_ = sample_rate_hz;
Sam Zackrisson3bd444f2022-08-03 14:37:00 +020077 frame->samples_per_channel_ = GetFrameSize(sample_rate_hz);
yujo36b1a5f2017-06-12 12:45:32 -070078 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 14:52:23 -070079 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
yujo36b1a5f2017-06-12 12:45:32 -070080 frame_data[i * 2] = static_cast<int16_t>(left * i);
81 frame_data[i * 2 + 1] = static_cast<int16_t>(right * i);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +000082 }
83}
84
85// Keep the existing sample rate.
jens.nielsen228c2682017-03-01 05:11:22 -080086void SetStereoFrame(float left, float right, AudioFrame* frame) {
87 SetStereoFrame(left, right, frame->sample_rate_hz_, frame);
88}
89
Artem Titovb0ea6372021-07-26 11:47:07 +020090// Sets the signal value to increase by `ch1`, `ch2`, `ch3`, `ch4` with every
jens.nielsen228c2682017-03-01 05:11:22 -080091// sample in each channel respectively.
92void SetQuadFrame(float ch1,
93 float ch2,
94 float ch3,
95 float ch4,
96 int sample_rate_hz,
97 AudioFrame* frame) {
yujo36b1a5f2017-06-12 12:45:32 -070098 frame->Mute();
jens.nielsen228c2682017-03-01 05:11:22 -080099 frame->num_channels_ = 4;
100 frame->sample_rate_hz_ = sample_rate_hz;
Sam Zackrisson3bd444f2022-08-03 14:37:00 +0200101 frame->samples_per_channel_ = GetFrameSize(sample_rate_hz);
yujo36b1a5f2017-06-12 12:45:32 -0700102 int16_t* frame_data = frame->mutable_data();
jens.nielsen228c2682017-03-01 05:11:22 -0800103 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700104 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);
jens.nielsen228c2682017-03-01 05:11:22 -0800108 }
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000109}
110
111void 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
Artem Titovb0ea6372021-07-26 11:47:07 +0200117// 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
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000119// signals to compensate for the resampling delay.
Yves Gerey665174f2018-06-19 15:03:05 +0200120float ComputeSNR(const AudioFrame& ref_frame,
121 const AudioFrame& test_frame,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700122 size_t max_delay) {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000123 VerifyParams(ref_frame, test_frame);
124 float best_snr = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -0700125 size_t best_delay = 0;
126 for (size_t delay = 0; delay <= max_delay; delay++) {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000127 float mse = 0;
128 float variance = 0;
yujo36b1a5f2017-06-12 12:45:32 -0700129 const int16_t* ref_frame_data = ref_frame.data();
130 const int16_t* test_frame_data = test_frame.data();
Yves Gerey665174f2018-06-19 15:03:05 +0200131 for (size_t i = 0;
132 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_ - delay;
133 i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700134 int error = ref_frame_data[i] - test_frame_data[i + delay];
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000135 mse += error * error;
yujo36b1a5f2017-06-12 12:45:32 -0700136 variance += ref_frame_data[i] * ref_frame_data[i];
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000137 }
138 float snr = 100; // We assign 100 dB to the zero-error case.
139 if (mse > 0)
Mirko Bonadeif0b8dee2019-03-15 10:47:11 +0100140 snr = 10 * std::log10(variance / mse);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000141 if (snr > best_snr) {
142 best_snr = snr;
143 best_delay = delay;
144 }
145 }
Niels Möllerea1e6f42022-05-09 09:21:14 +0200146 printf("SNR=%.1f dB at delay=%zu\n", best_snr, best_delay);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000147 return best_snr;
148}
149
150void VerifyFramesAreEqual(const AudioFrame& ref_frame,
151 const AudioFrame& test_frame) {
152 VerifyParams(ref_frame, test_frame);
yujo36b1a5f2017-06-12 12:45:32 -0700153 const int16_t* ref_frame_data = ref_frame.data();
Yves Gerey665174f2018-06-19 15:03:05 +0200154 const int16_t* test_frame_data = test_frame.data();
Peter Kastingdce40cf2015-08-24 14:52:23 -0700155 for (size_t i = 0;
156 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) {
yujo36b1a5f2017-06-12 12:45:32 -0700157 EXPECT_EQ(ref_frame_data[i], test_frame_data[i]);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000158 }
159}
160
andrew@webrtc.orga78a41f2014-04-08 23:09:28 +0000161void UtilityTest::RunResampleTest(int src_channels,
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +0000162 int src_sample_rate_hz,
163 int dst_channels,
Alejandro Luebscdfe20b2015-09-23 12:49:12 -0700164 int dst_sample_rate_hz) {
andrew@webrtc.orgf5a33f12014-04-19 00:32:07 +0000165 PushResampler<int16_t> resampler; // Create a new one with every test.
jens.nielsen228c2682017-03-01 05:11:22 -0800166 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;
Yves Gerey665174f2018-06-19 15:03:05 +0200170 const float resampling_factor =
171 (1.0 * src_sample_rate_hz) / dst_sample_rate_hz;
jens.nielsen228c2682017-03-01 05:11:22 -0800172 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;
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000180 if (src_channels == 1)
jens.nielsen228c2682017-03-01 05:11:22 -0800181 SetMonoFrame(kSrcCh1, src_sample_rate_hz, &src_frame_);
182 else if (src_channels == 2)
183 SetStereoFrame(kSrcCh1, kSrcCh2, src_sample_rate_hz, &src_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000184 else
jens.nielsen228c2682017-03-01 05:11:22 -0800185 SetQuadFrame(kSrcCh1, kSrcCh2, kSrcCh3, kSrcCh4, src_sample_rate_hz,
186 &src_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000187
188 if (dst_channels == 1) {
jens.nielsen228c2682017-03-01 05:11:22 -0800189 SetMonoFrame(0, dst_sample_rate_hz, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000190 if (src_channels == 1)
jens.nielsen228c2682017-03-01 05:11:22 -0800191 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_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000194 else
jens.nielsen228c2682017-03-01 05:11:22 -0800195 SetMonoFrame(dst_quad_to_mono, dst_sample_rate_hz, &golden_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000196 } else {
jens.nielsen228c2682017-03-01 05:11:22 -0800197 SetStereoFrame(0, 0, dst_sample_rate_hz, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000198 if (src_channels == 1)
jens.nielsen228c2682017-03-01 05:11:22 -0800199 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_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000202 else
jens.nielsen228c2682017-03-01 05:11:22 -0800203 SetStereoFrame(dst_quad_to_stereo_ch1, dst_quad_to_stereo_ch2,
204 dst_sample_rate_hz, &golden_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000205 }
206
andrew@webrtc.org50b2efe2013-04-29 17:27:29 +0000207 // 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.
Peter Kastingdce40cf2015-08-24 14:52:23 -0700210 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);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000214 printf("(%d, %d Hz) -> (%d, %d Hz) ", // SNR reported on the same line later.
Yves Gerey665174f2018-06-19 15:03:05 +0200215 src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz);
Alejandro Luebscdfe20b2015-09-23 12:49:12 -0700216 RemixAndResample(src_frame_, &resampler, &dst_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000217
Sam Zackrisson3bd444f2022-08-03 14:37:00 +0200218 if (src_sample_rate_hz == 96000 && dst_sample_rate_hz <= 11025) {
andrew@webrtc.orgc1eb5602013-06-03 19:00:29 +0000219 // 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 }
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000225}
226
andrew@webrtc.orga78a41f2014-04-08 23:09:28 +0000227TEST_F(UtilityTest, RemixAndResampleCopyFrameSucceeds) {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000228 // Stereo -> stereo.
jens.nielsen228c2682017-03-01 05:11:22 -0800229 SetStereoFrame(10, 10, &src_frame_);
230 SetStereoFrame(0, 0, &dst_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000231 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000232 VerifyFramesAreEqual(src_frame_, dst_frame_);
233
234 // Mono -> mono.
jens.nielsen228c2682017-03-01 05:11:22 -0800235 SetMonoFrame(20, &src_frame_);
236 SetMonoFrame(0, &dst_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000237 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000238 VerifyFramesAreEqual(src_frame_, dst_frame_);
239}
240
andrew@webrtc.orga78a41f2014-04-08 23:09:28 +0000241TEST_F(UtilityTest, RemixAndResampleMixingOnlySucceeds) {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000242 // Stereo -> mono.
jens.nielsen228c2682017-03-01 05:11:22 -0800243 SetStereoFrame(0, 0, &dst_frame_);
244 SetMonoFrame(10, &src_frame_);
245 SetStereoFrame(10, 10, &golden_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000246 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000247 VerifyFramesAreEqual(dst_frame_, golden_frame_);
248
249 // Mono -> stereo.
jens.nielsen228c2682017-03-01 05:11:22 -0800250 SetMonoFrame(0, &dst_frame_);
251 SetStereoFrame(10, 20, &src_frame_);
252 SetMonoFrame(15, &golden_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000253 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000254 VerifyFramesAreEqual(golden_frame_, dst_frame_);
255}
256
andrew@webrtc.orga78a41f2014-04-08 23:09:28 +0000257TEST_F(UtilityTest, RemixAndResampleSucceeds) {
Sam Zackrisson3bd444f2022-08-03 14:37:00 +0200258 const int kSampleRates[] = {8000, 11025, 16000, 22050,
259 32000, 44100, 48000, 96000};
jens.nielsen228c2682017-03-01 05:11:22 -0800260 const int kSrcChannels[] = {1, 2, 4};
jens.nielsen228c2682017-03-01 05:11:22 -0800261 const int kDstChannels[] = {1, 2};
jens.nielsen228c2682017-03-01 05:11:22 -0800262
Sam Zackrisson3bd444f2022-08-03 14:37:00 +0200263 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);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000268 }
269 }
270 }
271 }
272}
273
274} // namespace
275} // namespace voe
276} // namespace webrtc