1 /*
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 
13 #include "common_audio/resampler/include/push_resampler.h"
14 #include "modules/include/module_common_types.h"
15 #include "rtc_base/arraysize.h"
16 #include "rtc_base/format_macros.h"
17 #include "test/gtest.h"
18 #include "voice_engine/utility.h"
19 
20 namespace webrtc {
21 namespace voe {
22 namespace {
23 
24 class UtilityTest : public ::testing::Test {
25  protected:
UtilityTest()26   UtilityTest() {
27     src_frame_.sample_rate_hz_ = 16000;
28     src_frame_.samples_per_channel_ = src_frame_.sample_rate_hz_ / 100;
29     src_frame_.num_channels_ = 1;
30     dst_frame_.CopyFrom(src_frame_);
31     golden_frame_.CopyFrom(src_frame_);
32   }
33 
34   void RunResampleTest(int src_channels,
35                        int src_sample_rate_hz,
36                        int dst_channels,
37                        int dst_sample_rate_hz);
38 
39   PushResampler<int16_t> resampler_;
40   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.
SetMonoFrame(float data,int sample_rate_hz,AudioFrame * frame)48 void SetMonoFrame(float data, int sample_rate_hz, AudioFrame* frame) {
49   frame->Mute();
50   frame->num_channels_ = 1;
51   frame->sample_rate_hz_ = sample_rate_hz;
52   frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100);
53   int16_t* frame_data = frame->mutable_data();
54   for (size_t i = 0; i < frame->samples_per_channel_; i++) {
55     frame_data[i] = static_cast<int16_t>(data * i);
56   }
57 }
58 
59 // Keep the existing sample rate.
SetMonoFrame(float data,AudioFrame * frame)60 void SetMonoFrame(float data, AudioFrame* frame) {
61   SetMonoFrame(data, frame->sample_rate_hz_, frame);
62 }
63 
64 // Sets the signal value to increase by |left| and |right| with every sample in
65 // each channel respectively.
SetStereoFrame(float left,float right,int sample_rate_hz,AudioFrame * frame)66 void SetStereoFrame(float left,
67                     float right,
68                     int sample_rate_hz,
69                     AudioFrame* frame) {
70   frame->Mute();
71   frame->num_channels_ = 2;
72   frame->sample_rate_hz_ = sample_rate_hz;
73   frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100);
74   int16_t* frame_data = frame->mutable_data();
75   for (size_t i = 0; i < frame->samples_per_channel_; i++) {
76     frame_data[i * 2] = static_cast<int16_t>(left * i);
77     frame_data[i * 2 + 1] = static_cast<int16_t>(right * i);
78   }
79 }
80 
81 // Keep the existing sample rate.
SetStereoFrame(float left,float right,AudioFrame * frame)82 void 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.
SetQuadFrame(float ch1,float ch2,float ch3,float ch4,int sample_rate_hz,AudioFrame * frame)88 void SetQuadFrame(float ch1,
89                   float ch2,
90                   float ch3,
91                   float ch4,
92                   int sample_rate_hz,
93                   AudioFrame* frame) {
94   frame->Mute();
95   frame->num_channels_ = 4;
96   frame->sample_rate_hz_ = sample_rate_hz;
97   frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100);
98   int16_t* frame_data = frame->mutable_data();
99   for (size_t i = 0; i < frame->samples_per_channel_; i++) {
100     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);
104   }
105 }
106 
VerifyParams(const AudioFrame & ref_frame,const AudioFrame & test_frame)107 void 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
114 // |test_frame|. It allows for up to a |max_delay| in samples between the
115 // signals to compensate for the resampling delay.
ComputeSNR(const AudioFrame & ref_frame,const AudioFrame & test_frame,size_t max_delay)116 float ComputeSNR(const AudioFrame& ref_frame, const AudioFrame& test_frame,
117                  size_t max_delay) {
118   VerifyParams(ref_frame, test_frame);
119   float best_snr = 0;
120   size_t best_delay = 0;
121   for (size_t delay = 0; delay <= max_delay; delay++) {
122     float mse = 0;
123     float variance = 0;
124     const int16_t* ref_frame_data = ref_frame.data();
125     const int16_t* test_frame_data = test_frame.data();
126     for (size_t i = 0; i < ref_frame.samples_per_channel_ *
127         ref_frame.num_channels_ - delay; i++) {
128       int error = ref_frame_data[i] - test_frame_data[i + delay];
129       mse += error * error;
130       variance += ref_frame_data[i] * ref_frame_data[i];
131     }
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   }
140   printf("SNR=%.1f dB at delay=%" PRIuS "\n", best_snr, best_delay);
141   return best_snr;
142 }
143 
VerifyFramesAreEqual(const AudioFrame & ref_frame,const AudioFrame & test_frame)144 void VerifyFramesAreEqual(const AudioFrame& ref_frame,
145                           const AudioFrame& test_frame) {
146   VerifyParams(ref_frame, test_frame);
147   const int16_t* ref_frame_data = ref_frame.data();
148   const int16_t* test_frame_data  = test_frame.data();
149   for (size_t i = 0;
150        i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) {
151     EXPECT_EQ(ref_frame_data[i], test_frame_data[i]);
152   }
153 }
154 
RunResampleTest(int src_channels,int src_sample_rate_hz,int dst_channels,int dst_sample_rate_hz)155 void UtilityTest::RunResampleTest(int src_channels,
156                                   int src_sample_rate_hz,
157                                   int dst_channels,
158                                   int dst_sample_rate_hz) {
159   PushResampler<int16_t> resampler;  // Create a new one with every test.
160   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;
164   const float resampling_factor = (1.0 * src_sample_rate_hz) /
165       dst_sample_rate_hz;
166   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;
174   if (src_channels == 1)
175     SetMonoFrame(kSrcCh1, src_sample_rate_hz, &src_frame_);
176   else if (src_channels == 2)
177     SetStereoFrame(kSrcCh1, kSrcCh2, src_sample_rate_hz, &src_frame_);
178   else
179     SetQuadFrame(kSrcCh1, kSrcCh2, kSrcCh3, kSrcCh4, src_sample_rate_hz,
180                  &src_frame_);
181 
182   if (dst_channels == 1) {
183     SetMonoFrame(0, dst_sample_rate_hz, &dst_frame_);
184     if (src_channels == 1)
185       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_);
188     else
189       SetMonoFrame(dst_quad_to_mono, dst_sample_rate_hz, &golden_frame_);
190   } else {
191     SetStereoFrame(0, 0, dst_sample_rate_hz, &dst_frame_);
192     if (src_channels == 1)
193       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_);
196     else
197       SetStereoFrame(dst_quad_to_stereo_ch1, dst_quad_to_stereo_ch2,
198                      dst_sample_rate_hz, &golden_frame_);
199   }
200 
201   // 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.
204   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);
208   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);
210   RemixAndResample(src_frame_, &resampler, &dst_frame_);
211 
212   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   }
219 }
220 
TEST_F(UtilityTest,RemixAndResampleCopyFrameSucceeds)221 TEST_F(UtilityTest, RemixAndResampleCopyFrameSucceeds) {
222   // Stereo -> stereo.
223   SetStereoFrame(10, 10, &src_frame_);
224   SetStereoFrame(0, 0, &dst_frame_);
225   RemixAndResample(src_frame_, &resampler_, &dst_frame_);
226   VerifyFramesAreEqual(src_frame_, dst_frame_);
227 
228   // Mono -> mono.
229   SetMonoFrame(20, &src_frame_);
230   SetMonoFrame(0, &dst_frame_);
231   RemixAndResample(src_frame_, &resampler_, &dst_frame_);
232   VerifyFramesAreEqual(src_frame_, dst_frame_);
233 }
234 
TEST_F(UtilityTest,RemixAndResampleMixingOnlySucceeds)235 TEST_F(UtilityTest, RemixAndResampleMixingOnlySucceeds) {
236   // Stereo -> mono.
237   SetStereoFrame(0, 0, &dst_frame_);
238   SetMonoFrame(10, &src_frame_);
239   SetStereoFrame(10, 10, &golden_frame_);
240   RemixAndResample(src_frame_, &resampler_, &dst_frame_);
241   VerifyFramesAreEqual(dst_frame_, golden_frame_);
242 
243   // Mono -> stereo.
244   SetMonoFrame(0, &dst_frame_);
245   SetStereoFrame(10, 20, &src_frame_);
246   SetMonoFrame(15, &golden_frame_);
247   RemixAndResample(src_frame_, &resampler_, &dst_frame_);
248   VerifyFramesAreEqual(golden_frame_, dst_frame_);
249 }
250 
TEST_F(UtilityTest,RemixAndResampleSucceeds)251 TEST_F(UtilityTest, RemixAndResampleSucceeds) {
252   const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000, 96000};
253   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 
259   for (int src_rate = 0; src_rate < kSampleRatesSize; src_rate++) {
260     for (int dst_rate = 0; dst_rate < kSampleRatesSize; dst_rate++) {
261       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]);
267         }
268       }
269     }
270   }
271 }
272 
273 }  // namespace
274 }  // namespace voe
275 }  // namespace webrtc
276