blob: 84d8f5568e00499712f3df588d378ba6e83b69ed [file] [log] [blame]
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +00001/*
2 * Copyright (c) 2014 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 "common_audio/audio_converter.h"
12
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000013#include <algorithm>
Yves Gerey665174f2018-06-19 15:03:05 +020014#include <cmath>
kwibergc2b785d2016-02-24 05:22:32 -080015#include <memory>
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000016#include <vector>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "common_audio/channel_buffer.h"
19#include "common_audio/resampler/push_sinc_resampler.h"
20#include "rtc_base/arraysize.h"
21#include "rtc_base/format_macros.h"
22#include "test/gtest.h"
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000023
24namespace webrtc {
25
kwibergc2b785d2016-02-24 05:22:32 -080026typedef std::unique_ptr<ChannelBuffer<float>> ScopedBuffer;
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000027
28// Sets the signal value to increase by |data| with every sample.
pkasting25702cb2016-01-08 13:50:27 -080029ScopedBuffer CreateBuffer(const std::vector<float>& data, size_t frames) {
Peter Kasting69558702016-01-12 16:26:35 -080030 const size_t num_channels = data.size();
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000031 ScopedBuffer sb(new ChannelBuffer<float>(frames, num_channels));
Peter Kasting69558702016-01-12 16:26:35 -080032 for (size_t i = 0; i < num_channels; ++i)
pkasting25702cb2016-01-08 13:50:27 -080033 for (size_t j = 0; j < frames; ++j)
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000034 sb->channels()[i][j] = data[i] * j;
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000035 return sb;
36}
37
38void VerifyParams(const ChannelBuffer<float>& ref,
39 const ChannelBuffer<float>& test) {
40 EXPECT_EQ(ref.num_channels(), test.num_channels());
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000041 EXPECT_EQ(ref.num_frames(), test.num_frames());
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000042}
43
44// Computes the best SNR based on the error between |ref_frame| and
45// |test_frame|. It searches around |expected_delay| in samples between the
46// signals to compensate for the resampling delay.
47float ComputeSNR(const ChannelBuffer<float>& ref,
48 const ChannelBuffer<float>& test,
Peter Kastingdce40cf2015-08-24 14:52:23 -070049 size_t expected_delay) {
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000050 VerifyParams(ref, test);
51 float best_snr = 0;
Peter Kastingdce40cf2015-08-24 14:52:23 -070052 size_t best_delay = 0;
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000053
54 // Search within one sample of the expected delay.
Peter Kastingdce40cf2015-08-24 14:52:23 -070055 for (size_t delay = std::max(expected_delay, static_cast<size_t>(1)) - 1;
Yves Gerey665174f2018-06-19 15:03:05 +020056 delay <= std::min(expected_delay + 1, ref.num_frames()); ++delay) {
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000057 float mse = 0;
58 float variance = 0;
59 float mean = 0;
Peter Kasting69558702016-01-12 16:26:35 -080060 for (size_t i = 0; i < ref.num_channels(); ++i) {
Peter Kastingdce40cf2015-08-24 14:52:23 -070061 for (size_t j = 0; j < ref.num_frames() - delay; ++j) {
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000062 float error = ref.channels()[i][j] - test.channels()[i][j + delay];
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000063 mse += error * error;
aluebs@webrtc.orgd35a5c32015-02-10 22:52:15 +000064 variance += ref.channels()[i][j] * ref.channels()[i][j];
65 mean += ref.channels()[i][j];
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000066 }
67 }
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000068
Peter Kastingdce40cf2015-08-24 14:52:23 -070069 const size_t length = ref.num_channels() * (ref.num_frames() - delay);
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000070 mse /= length;
71 variance /= length;
72 mean /= length;
73 variance -= mean * mean;
74 float snr = 100; // We assign 100 dB to the zero-error case.
75 if (mse > 0)
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +000076 snr = 10 * std::log10(variance / mse);
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000077 if (snr > best_snr) {
78 best_snr = snr;
79 best_delay = delay;
80 }
81 }
Oleh Prypinb1686782019-08-02 09:36:47 +020082 printf("SNR=%.1f dB at delay=%" RTC_PRIuS "\n", best_snr, best_delay);
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000083 return best_snr;
84}
85
86// Sets the source to a linearly increasing signal for which we can easily
87// generate a reference. Runs the AudioConverter and ensures the output has
88// sufficiently high SNR relative to the reference.
Peter Kasting69558702016-01-12 16:26:35 -080089void RunAudioConverterTest(size_t src_channels,
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000090 int src_sample_rate_hz,
Peter Kasting69558702016-01-12 16:26:35 -080091 size_t dst_channels,
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000092 int dst_sample_rate_hz) {
93 const float kSrcLeft = 0.0002f;
94 const float kSrcRight = 0.0001f;
Yves Gerey665174f2018-06-19 15:03:05 +020095 const float resampling_factor =
96 (1.f * src_sample_rate_hz) / dst_sample_rate_hz;
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +000097 const float dst_left = resampling_factor * kSrcLeft;
98 const float dst_right = resampling_factor * kSrcRight;
99 const float dst_mono = (dst_left + dst_right) / 2;
pkasting25702cb2016-01-08 13:50:27 -0800100 const size_t src_frames = static_cast<size_t>(src_sample_rate_hz / 100);
101 const size_t dst_frames = static_cast<size_t>(dst_sample_rate_hz / 100);
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +0000102
103 std::vector<float> src_data(1, kSrcLeft);
104 if (src_channels == 2)
105 src_data.push_back(kSrcRight);
106 ScopedBuffer src_buffer = CreateBuffer(src_data, src_frames);
107
108 std::vector<float> dst_data(1, 0);
109 std::vector<float> ref_data;
110 if (dst_channels == 1) {
111 if (src_channels == 1)
112 ref_data.push_back(dst_left);
113 else
114 ref_data.push_back(dst_mono);
115 } else {
116 dst_data.push_back(0);
117 ref_data.push_back(dst_left);
118 if (src_channels == 1)
119 ref_data.push_back(dst_left);
120 else
121 ref_data.push_back(dst_right);
122 }
123 ScopedBuffer dst_buffer = CreateBuffer(dst_data, dst_frames);
124 ScopedBuffer ref_buffer = CreateBuffer(ref_data, dst_frames);
125
126 // The sinc resampler has a known delay, which we compute here.
Yves Gerey665174f2018-06-19 15:03:05 +0200127 const size_t delay_frames =
128 src_sample_rate_hz == dst_sample_rate_hz
129 ? 0
130 : static_cast<size_t>(
131 PushSincResampler::AlgorithmicDelaySeconds(src_sample_rate_hz) *
132 dst_sample_rate_hz);
Peter Kasting69558702016-01-12 16:26:35 -0800133 // SNR reported on the same line later.
Oleh Prypinb1686782019-08-02 09:36:47 +0200134 printf("(%" RTC_PRIuS ", %d Hz) -> (%" RTC_PRIuS ", %d Hz) ", src_channels,
Yves Gerey665174f2018-06-19 15:03:05 +0200135 src_sample_rate_hz, dst_channels, dst_sample_rate_hz);
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +0000136
kwibergc2b785d2016-02-24 05:22:32 -0800137 std::unique_ptr<AudioConverter> converter = AudioConverter::Create(
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +0000138 src_channels, src_frames, dst_channels, dst_frames);
andrew@webrtc.org2c29c2e2015-02-11 01:09:50 +0000139 converter->Convert(src_buffer->channels(), src_buffer->size(),
140 dst_buffer->channels(), dst_buffer->size());
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +0000141
142 EXPECT_LT(43.f,
143 ComputeSNR(*ref_buffer.get(), *dst_buffer.get(), delay_frames));
144}
145
146TEST(AudioConverterTest, ConversionsPassSNRThreshold) {
147 const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000};
Peter Kasting69558702016-01-12 16:26:35 -0800148 const size_t kChannels[] = {1, 2};
pkasting25702cb2016-01-08 13:50:27 -0800149 for (size_t src_rate = 0; src_rate < arraysize(kSampleRates); ++src_rate) {
150 for (size_t dst_rate = 0; dst_rate < arraysize(kSampleRates); ++dst_rate) {
151 for (size_t src_channel = 0; src_channel < arraysize(kChannels);
152 ++src_channel) {
153 for (size_t dst_channel = 0; dst_channel < arraysize(kChannels);
154 ++dst_channel) {
andrew@webrtc.orgaada86b2014-10-27 18:18:17 +0000155 RunAudioConverterTest(kChannels[src_channel], kSampleRates[src_rate],
156 kChannels[dst_channel], kSampleRates[dst_rate]);
157 }
158 }
159 }
160 }
161}
162
163} // namespace webrtc