blob: 31dcfac1fe75c00cd64ad5cf2da9b755dd16d0c1 [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
Jonas Olssona4d87372019-07-05 17:08:3311#include "audio/remix_resample.h"
12
Mirko Bonadeif0b8dee2019-03-15 09:47:1113#include <cmath>
andrew@webrtc.org4ecea3e2012-06-27 03:25:3114
Mirko Bonadei92ea95e2017-09-15 04:47:3115#include "common_audio/resampler/include/push_resampler.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3116#include "rtc_base/arraysize.h"
Fredrik Solenbergbbf21a32018-04-12 20:44:0917#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3118#include "test/gtest.h"
andrew@webrtc.org4ecea3e2012-06-27 03:25:3119
20namespace webrtc {
21namespace voe {
22namespace {
23
Sam Zackrisson3bd444f2022-08-03 12:37:0024int GetFrameSize(int sample_rate_hz) {
25 return sample_rate_hz / 100;
26}
27
andrew@webrtc.orga78a41f2014-04-08 23:09:2828class UtilityTest : public ::testing::Test {
andrew@webrtc.org4ecea3e2012-06-27 03:25:3129 protected:
andrew@webrtc.orga78a41f2014-04-08 23:09:2830 UtilityTest() {
andrew@webrtc.org4ecea3e2012-06-27 03:25:3131 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:3034 dst_frame_.CopyFrom(src_frame_);
35 golden_frame_.CopyFrom(src_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:3136 }
37
Alejandro Luebscdfe20b2015-09-23 19:49:1238 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:3142
andrew@webrtc.orgf5a33f12014-04-19 00:32:0743 PushResampler<int16_t> resampler_;
andrew@webrtc.org4ecea3e2012-06-27 03:25:3144 AudioFrame src_frame_;
45 AudioFrame dst_frame_;
46 AudioFrame golden_frame_;
47};
48
Artem Titovb0ea6372021-07-26 09:47:0749// Sets the signal value to increase by `data` with every sample. Floats are
andrew@webrtc.org4ecea3e2012-06-27 03:25:3150// used so non-integer values result in rounding error, but not an accumulating
51// error.
jens.nielsen228c2682017-03-01 13:11:2252void SetMonoFrame(float data, int sample_rate_hz, AudioFrame* frame) {
yujo36b1a5f2017-06-12 19:45:3253 frame->Mute();
andrew@webrtc.org4ecea3e2012-06-27 03:25:3154 frame->num_channels_ = 1;
55 frame->sample_rate_hz_ = sample_rate_hz;
Sam Zackrisson3bd444f2022-08-03 12:37:0056 frame->samples_per_channel_ = GetFrameSize(sample_rate_hz);
yujo36b1a5f2017-06-12 19:45:3257 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 21:52:2358 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
yujo36b1a5f2017-06-12 19:45:3259 frame_data[i] = static_cast<int16_t>(data * i);
andrew@webrtc.org4ecea3e2012-06-27 03:25:3160 }
61}
62
63// Keep the existing sample rate.
jens.nielsen228c2682017-03-01 13:11:2264void SetMonoFrame(float data, AudioFrame* frame) {
65 SetMonoFrame(data, frame->sample_rate_hz_, frame);
andrew@webrtc.org4ecea3e2012-06-27 03:25:3166}
67
Artem Titovb0ea6372021-07-26 09:47:0768// Sets the signal value to increase by `left` and `right` with every sample in
andrew@webrtc.org4ecea3e2012-06-27 03:25:3169// each channel respectively.
jens.nielsen228c2682017-03-01 13:11:2270void SetStereoFrame(float left,
71 float right,
72 int sample_rate_hz,
73 AudioFrame* frame) {
yujo36b1a5f2017-06-12 19:45:3274 frame->Mute();
andrew@webrtc.org4ecea3e2012-06-27 03:25:3175 frame->num_channels_ = 2;
76 frame->sample_rate_hz_ = sample_rate_hz;
Sam Zackrisson3bd444f2022-08-03 12:37:0077 frame->samples_per_channel_ = GetFrameSize(sample_rate_hz);
yujo36b1a5f2017-06-12 19:45:3278 int16_t* frame_data = frame->mutable_data();
Peter Kastingdce40cf2015-08-24 21:52:2379 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
yujo36b1a5f2017-06-12 19:45:3280 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:3182 }
83}
84
85// Keep the existing sample rate.
jens.nielsen228c2682017-03-01 13:11:2286void SetStereoFrame(float left, float right, AudioFrame* frame) {
87 SetStereoFrame(left, right, frame->sample_rate_hz_, frame);
88}
89
Artem Titovb0ea6372021-07-26 09:47:0790// Sets the signal value to increase by `ch1`, `ch2`, `ch3`, `ch4` with every
jens.nielsen228c2682017-03-01 13:11:2291// 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 19:45:3298 frame->Mute();
jens.nielsen228c2682017-03-01 13:11:2299 frame->num_channels_ = 4;
100 frame->sample_rate_hz_ = sample_rate_hz;
Sam Zackrisson3bd444f2022-08-03 12:37:00101 frame->samples_per_channel_ = GetFrameSize(sample_rate_hz);
yujo36b1a5f2017-06-12 19:45:32102 int16_t* frame_data = frame->mutable_data();
jens.nielsen228c2682017-03-01 13:11:22103 for (size_t i = 0; i < frame->samples_per_channel_; i++) {
yujo36b1a5f2017-06-12 19:45:32104 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 13:11:22108 }
andrew@webrtc.org4ecea3e2012-06-27 03:25:31109}
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 09:47:07117// 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:29119// signals to compensate for the resampling delay.
Yves Gerey665174f2018-06-19 13:03:05120float ComputeSNR(const AudioFrame& ref_frame,
121 const AudioFrame& test_frame,
Peter Kastingdce40cf2015-08-24 21:52:23122 size_t max_delay) {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31123 VerifyParams(ref_frame, test_frame);
124 float best_snr = 0;
Peter Kastingdce40cf2015-08-24 21:52:23125 size_t best_delay = 0;
126 for (size_t delay = 0; delay <= max_delay; delay++) {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31127 float mse = 0;
128 float variance = 0;
yujo36b1a5f2017-06-12 19:45:32129 const int16_t* ref_frame_data = ref_frame.data();
130 const int16_t* test_frame_data = test_frame.data();
Yves Gerey665174f2018-06-19 13:03:05131 for (size_t i = 0;
132 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_ - delay;
133 i++) {
yujo36b1a5f2017-06-12 19:45:32134 int error = ref_frame_data[i] - test_frame_data[i + delay];
andrew@webrtc.org4ecea3e2012-06-27 03:25:31135 mse += error * error;
yujo36b1a5f2017-06-12 19:45:32136 variance += ref_frame_data[i] * ref_frame_data[i];
andrew@webrtc.org4ecea3e2012-06-27 03:25:31137 }
138 float snr = 100; // We assign 100 dB to the zero-error case.
139 if (mse > 0)
Mirko Bonadeif0b8dee2019-03-15 09:47:11140 snr = 10 * std::log10(variance / mse);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31141 if (snr > best_snr) {
142 best_snr = snr;
143 best_delay = delay;
144 }
145 }
Niels Möllerea1e6f42022-05-09 07:21:14146 printf("SNR=%.1f dB at delay=%zu\n", best_snr, best_delay);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31147 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 19:45:32153 const int16_t* ref_frame_data = ref_frame.data();
Yves Gerey665174f2018-06-19 13:03:05154 const int16_t* test_frame_data = test_frame.data();
Peter Kastingdce40cf2015-08-24 21:52:23155 for (size_t i = 0;
156 i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) {
yujo36b1a5f2017-06-12 19:45:32157 EXPECT_EQ(ref_frame_data[i], test_frame_data[i]);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31158 }
159}
160
andrew@webrtc.orga78a41f2014-04-08 23:09:28161void UtilityTest::RunResampleTest(int src_channels,
andrew@webrtc.orgf5a33f12014-04-19 00:32:07162 int src_sample_rate_hz,
163 int dst_channels,
Alejandro Luebscdfe20b2015-09-23 19:49:12164 int dst_sample_rate_hz) {
andrew@webrtc.orgf5a33f12014-04-19 00:32:07165 PushResampler<int16_t> resampler; // Create a new one with every test.
jens.nielsen228c2682017-03-01 13:11:22166 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 13:03:05170 const float resampling_factor =
171 (1.0 * src_sample_rate_hz) / dst_sample_rate_hz;
jens.nielsen228c2682017-03-01 13:11:22172 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:31180 if (src_channels == 1)
jens.nielsen228c2682017-03-01 13:11:22181 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:31184 else
jens.nielsen228c2682017-03-01 13:11:22185 SetQuadFrame(kSrcCh1, kSrcCh2, kSrcCh3, kSrcCh4, src_sample_rate_hz,
186 &src_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31187
188 if (dst_channels == 1) {
jens.nielsen228c2682017-03-01 13:11:22189 SetMonoFrame(0, dst_sample_rate_hz, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31190 if (src_channels == 1)
jens.nielsen228c2682017-03-01 13:11:22191 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:31194 else
jens.nielsen228c2682017-03-01 13:11:22195 SetMonoFrame(dst_quad_to_mono, dst_sample_rate_hz, &golden_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31196 } else {
jens.nielsen228c2682017-03-01 13:11:22197 SetStereoFrame(0, 0, dst_sample_rate_hz, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31198 if (src_channels == 1)
jens.nielsen228c2682017-03-01 13:11:22199 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:31202 else
jens.nielsen228c2682017-03-01 13:11:22203 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:31205 }
206
andrew@webrtc.org50b2efe2013-04-29 17:27:29207 // 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 21:52:23210 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:31214 printf("(%d, %d Hz) -> (%d, %d Hz) ", // SNR reported on the same line later.
Yves Gerey665174f2018-06-19 13:03:05215 src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz);
Alejandro Luebscdfe20b2015-09-23 19:49:12216 RemixAndResample(src_frame_, &resampler, &dst_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01217
Sam Zackrisson3bd444f2022-08-03 12:37:00218 if (src_sample_rate_hz == 96000 && dst_sample_rate_hz <= 11025) {
andrew@webrtc.orgc1eb5602013-06-03 19:00:29219 // 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:31225}
226
andrew@webrtc.orga78a41f2014-04-08 23:09:28227TEST_F(UtilityTest, RemixAndResampleCopyFrameSucceeds) {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31228 // Stereo -> stereo.
jens.nielsen228c2682017-03-01 13:11:22229 SetStereoFrame(10, 10, &src_frame_);
230 SetStereoFrame(0, 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 // Mono -> mono.
jens.nielsen228c2682017-03-01 13:11:22235 SetMonoFrame(20, &src_frame_);
236 SetMonoFrame(0, &dst_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01237 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31238 VerifyFramesAreEqual(src_frame_, dst_frame_);
239}
240
andrew@webrtc.orga78a41f2014-04-08 23:09:28241TEST_F(UtilityTest, RemixAndResampleMixingOnlySucceeds) {
andrew@webrtc.org4ecea3e2012-06-27 03:25:31242 // Stereo -> mono.
jens.nielsen228c2682017-03-01 13:11:22243 SetStereoFrame(0, 0, &dst_frame_);
244 SetMonoFrame(10, &src_frame_);
245 SetStereoFrame(10, 10, &golden_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01246 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31247 VerifyFramesAreEqual(dst_frame_, golden_frame_);
248
249 // Mono -> stereo.
jens.nielsen228c2682017-03-01 13:11:22250 SetMonoFrame(0, &dst_frame_);
251 SetStereoFrame(10, 20, &src_frame_);
252 SetMonoFrame(15, &golden_frame_);
andrew@webrtc.org40ee3d02014-04-03 21:56:01253 RemixAndResample(src_frame_, &resampler_, &dst_frame_);
andrew@webrtc.org4ecea3e2012-06-27 03:25:31254 VerifyFramesAreEqual(golden_frame_, dst_frame_);
255}
256
andrew@webrtc.orga78a41f2014-04-08 23:09:28257TEST_F(UtilityTest, RemixAndResampleSucceeds) {
Sam Zackrisson3bd444f2022-08-03 12:37:00258 const int kSampleRates[] = {8000, 11025, 16000, 22050,
259 32000, 44100, 48000, 96000};
jens.nielsen228c2682017-03-01 13:11:22260 const int kSrcChannels[] = {1, 2, 4};
jens.nielsen228c2682017-03-01 13:11:22261 const int kDstChannels[] = {1, 2};
jens.nielsen228c2682017-03-01 13:11:22262
Sam Zackrisson3bd444f2022-08-03 12:37:00263 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:31268 }
269 }
270 }
271 }
272}
273
274} // namespace
275} // namespace voe
276} // namespace webrtc