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 "webrtc/base/format_macros.h"
14 #include "webrtc/common_audio/resampler/include/push_resampler.h"
15 #include "webrtc/modules/include/module_common_types.h"
16 #include "webrtc/test/gtest.h"
17 #include "webrtc/voice_engine/utility.h"
18 #include "webrtc/voice_engine/voice_engine_defines.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(AudioFrame * frame,float data,int sample_rate_hz)48 void SetMonoFrame(AudioFrame* frame, float data, int sample_rate_hz) {
49   memset(frame->data_, 0, sizeof(frame->data_));
50   frame->num_channels_ = 1;
51   frame->sample_rate_hz_ = sample_rate_hz;
52   frame->samples_per_channel_ = sample_rate_hz / 100;
53   for (size_t i = 0; i < frame->samples_per_channel_; i++) {
54     frame->data_[i] = static_cast<int16_t>(data * i);
55   }
56 }
57 
58 // Keep the existing sample rate.
SetMonoFrame(AudioFrame * frame,float data)59 void SetMonoFrame(AudioFrame* frame, float data) {
60   SetMonoFrame(frame, data, frame->sample_rate_hz_);
61 }
62 
63 // Sets the signal value to increase by |left| and |right| with every sample in
64 // each channel respectively.
SetStereoFrame(AudioFrame * frame,float left,float right,int sample_rate_hz)65 void SetStereoFrame(AudioFrame* frame, float left, float right,
66                     int sample_rate_hz) {
67   memset(frame->data_, 0, sizeof(frame->data_));
68   frame->num_channels_ = 2;
69   frame->sample_rate_hz_ = sample_rate_hz;
70   frame->samples_per_channel_ = sample_rate_hz / 100;
71   for (size_t i = 0; i < frame->samples_per_channel_; i++) {
72     frame->data_[i * 2] = static_cast<int16_t>(left * i);
73     frame->data_[i * 2 + 1] = static_cast<int16_t>(right * i);
74   }
75 }
76 
77 // Keep the existing sample rate.
SetStereoFrame(AudioFrame * frame,float left,float right)78 void SetStereoFrame(AudioFrame* frame, float left, float right) {
79   SetStereoFrame(frame, left, right, frame->sample_rate_hz_);
80 }
81 
VerifyParams(const AudioFrame & ref_frame,const AudioFrame & test_frame)82 void VerifyParams(const AudioFrame& ref_frame, const AudioFrame& test_frame) {
83   EXPECT_EQ(ref_frame.num_channels_, test_frame.num_channels_);
84   EXPECT_EQ(ref_frame.samples_per_channel_, test_frame.samples_per_channel_);
85   EXPECT_EQ(ref_frame.sample_rate_hz_, test_frame.sample_rate_hz_);
86 }
87 
88 // Computes the best SNR based on the error between |ref_frame| and
89 // |test_frame|. It allows for up to a |max_delay| in samples between the
90 // signals to compensate for the resampling delay.
ComputeSNR(const AudioFrame & ref_frame,const AudioFrame & test_frame,size_t max_delay)91 float ComputeSNR(const AudioFrame& ref_frame, const AudioFrame& test_frame,
92                  size_t max_delay) {
93   VerifyParams(ref_frame, test_frame);
94   float best_snr = 0;
95   size_t best_delay = 0;
96   for (size_t delay = 0; delay <= max_delay; delay++) {
97     float mse = 0;
98     float variance = 0;
99     for (size_t i = 0; i < ref_frame.samples_per_channel_ *
100         ref_frame.num_channels_ - delay; i++) {
101       int error = ref_frame.data_[i] - test_frame.data_[i + delay];
102       mse += error * error;
103       variance += ref_frame.data_[i] * ref_frame.data_[i];
104     }
105     float snr = 100;  // We assign 100 dB to the zero-error case.
106     if (mse > 0)
107       snr = 10 * log10(variance / mse);
108     if (snr > best_snr) {
109       best_snr = snr;
110       best_delay = delay;
111     }
112   }
113   printf("SNR=%.1f dB at delay=%" PRIuS "\n", best_snr, best_delay);
114   return best_snr;
115 }
116 
VerifyFramesAreEqual(const AudioFrame & ref_frame,const AudioFrame & test_frame)117 void VerifyFramesAreEqual(const AudioFrame& ref_frame,
118                           const AudioFrame& test_frame) {
119   VerifyParams(ref_frame, test_frame);
120   for (size_t i = 0;
121        i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) {
122     EXPECT_EQ(ref_frame.data_[i], test_frame.data_[i]);
123   }
124 }
125 
RunResampleTest(int src_channels,int src_sample_rate_hz,int dst_channels,int dst_sample_rate_hz)126 void UtilityTest::RunResampleTest(int src_channels,
127                                   int src_sample_rate_hz,
128                                   int dst_channels,
129                                   int dst_sample_rate_hz) {
130   PushResampler<int16_t> resampler;  // Create a new one with every test.
131   const int16_t kSrcLeft = 30;  // Shouldn't overflow for any used sample rate.
132   const int16_t kSrcRight = 15;
133   const float resampling_factor = (1.0 * src_sample_rate_hz) /
134       dst_sample_rate_hz;
135   const float dst_left = resampling_factor * kSrcLeft;
136   const float dst_right = resampling_factor * kSrcRight;
137   const float dst_mono = (dst_left + dst_right) / 2;
138   if (src_channels == 1)
139     SetMonoFrame(&src_frame_, kSrcLeft, src_sample_rate_hz);
140   else
141     SetStereoFrame(&src_frame_, kSrcLeft, kSrcRight, src_sample_rate_hz);
142 
143   if (dst_channels == 1) {
144     SetMonoFrame(&dst_frame_, 0, dst_sample_rate_hz);
145     if (src_channels == 1)
146       SetMonoFrame(&golden_frame_, dst_left, dst_sample_rate_hz);
147     else
148       SetMonoFrame(&golden_frame_, dst_mono, dst_sample_rate_hz);
149   } else {
150     SetStereoFrame(&dst_frame_, 0, 0, dst_sample_rate_hz);
151     if (src_channels == 1)
152       SetStereoFrame(&golden_frame_, dst_left, dst_left, dst_sample_rate_hz);
153     else
154       SetStereoFrame(&golden_frame_, dst_left, dst_right, dst_sample_rate_hz);
155   }
156 
157   // The sinc resampler has a known delay, which we compute here. Multiplying by
158   // two gives us a crude maximum for any resampling, as the old resampler
159   // typically (but not always) has lower delay.
160   static const size_t kInputKernelDelaySamples = 16;
161   const size_t max_delay = static_cast<size_t>(
162       static_cast<double>(dst_sample_rate_hz) / src_sample_rate_hz *
163       kInputKernelDelaySamples * dst_channels * 2);
164   printf("(%d, %d Hz) -> (%d, %d Hz) ",  // SNR reported on the same line later.
165       src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz);
166   RemixAndResample(src_frame_, &resampler, &dst_frame_);
167 
168   if (src_sample_rate_hz == 96000 && dst_sample_rate_hz == 8000) {
169     // The sinc resampler gives poor SNR at this extreme conversion, but we
170     // expect to see this rarely in practice.
171     EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 14.0f);
172   } else {
173     EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 46.0f);
174   }
175 }
176 
177 // These two tests assume memcpy() (no delay and no filtering) for input
178 // freq == output freq && same channels.  RemixAndResample uses 'Fixed'
179 // resamplers to enable this behavior
TEST_F(UtilityTest,RemixAndResampleCopyFrameSucceeds)180 TEST_F(UtilityTest, RemixAndResampleCopyFrameSucceeds) {
181   // Stereo -> stereo.
182   SetStereoFrame(&src_frame_, 10, 10);
183   SetStereoFrame(&dst_frame_, 0, 0);
184   RemixAndResample(src_frame_, &resampler_, &dst_frame_);
185   VerifyFramesAreEqual(src_frame_, dst_frame_);
186 
187   // Mono -> mono.
188   SetMonoFrame(&src_frame_, 20);
189   SetMonoFrame(&dst_frame_, 0);
190   RemixAndResample(src_frame_, &resampler_, &dst_frame_);
191   VerifyFramesAreEqual(src_frame_, dst_frame_);
192 }
193 
TEST_F(UtilityTest,RemixAndResampleMixingOnlySucceeds)194 TEST_F(UtilityTest, RemixAndResampleMixingOnlySucceeds) {
195   // Stereo -> mono.
196   SetStereoFrame(&dst_frame_, 0, 0);
197   SetMonoFrame(&src_frame_, 10);
198   SetStereoFrame(&golden_frame_, 10, 10);
199   RemixAndResample(src_frame_, &resampler_, &dst_frame_);
200   VerifyFramesAreEqual(dst_frame_, golden_frame_);
201 
202   // Mono -> stereo.
203   SetMonoFrame(&dst_frame_, 0);
204   SetStereoFrame(&src_frame_, 10, 20);
205   SetMonoFrame(&golden_frame_, 15);
206   RemixAndResample(src_frame_, &resampler_, &dst_frame_);
207   VerifyFramesAreEqual(golden_frame_, dst_frame_);
208 }
209 
TEST_F(UtilityTest,RemixAndResampleSucceeds)210 TEST_F(UtilityTest, RemixAndResampleSucceeds) {
211   const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000, 96000};
212   const int kSampleRatesSize = sizeof(kSampleRates) / sizeof(*kSampleRates);
213   const int kChannels[] = {1, 2};
214   const int kChannelsSize = sizeof(kChannels) / sizeof(*kChannels);
215   for (int src_rate = 0; src_rate < kSampleRatesSize; src_rate++) {
216     for (int dst_rate = 0; dst_rate < kSampleRatesSize; dst_rate++) {
217       for (int src_channel = 0; src_channel < kChannelsSize; src_channel++) {
218         for (int dst_channel = 0; dst_channel < kChannelsSize; dst_channel++) {
219           RunResampleTest(kChannels[src_channel], kSampleRates[src_rate],
220                           kChannels[dst_channel], kSampleRates[dst_rate]);
221         }
222       }
223     }
224   }
225 }
226 
227 }  // namespace
228 }  // namespace voe
229 }  // namespace webrtc
230