• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "audio/remix_resample.h"
12 
13 #include <cmath>
14 
15 #include "common_audio/resampler/include/push_resampler.h"
16 #include "rtc_base/arraysize.h"
17 #include "rtc_base/checks.h"
18 #include "rtc_base/format_macros.h"
19 #include "test/gtest.h"
20 
21 namespace webrtc {
22 namespace voe {
23 namespace {
24 
25 class UtilityTest : public ::testing::Test {
26  protected:
UtilityTest()27   UtilityTest() {
28     src_frame_.sample_rate_hz_ = 16000;
29     src_frame_.samples_per_channel_ = src_frame_.sample_rate_hz_ / 100;
30     src_frame_.num_channels_ = 1;
31     dst_frame_.CopyFrom(src_frame_);
32     golden_frame_.CopyFrom(src_frame_);
33   }
34 
35   void RunResampleTest(int src_channels,
36                        int src_sample_rate_hz,
37                        int dst_channels,
38                        int dst_sample_rate_hz);
39 
40   PushResampler<int16_t> resampler_;
41   AudioFrame src_frame_;
42   AudioFrame dst_frame_;
43   AudioFrame golden_frame_;
44 };
45 
46 // Sets the signal value to increase by |data| with every sample. Floats are
47 // used so non-integer values result in rounding error, but not an accumulating
48 // error.
SetMonoFrame(float data,int sample_rate_hz,AudioFrame * frame)49 void SetMonoFrame(float data, int sample_rate_hz, AudioFrame* frame) {
50   frame->Mute();
51   frame->num_channels_ = 1;
52   frame->sample_rate_hz_ = sample_rate_hz;
53   frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100);
54   int16_t* frame_data = frame->mutable_data();
55   for (size_t i = 0; i < frame->samples_per_channel_; i++) {
56     frame_data[i] = static_cast<int16_t>(data * i);
57   }
58 }
59 
60 // Keep the existing sample rate.
SetMonoFrame(float data,AudioFrame * frame)61 void SetMonoFrame(float data, AudioFrame* frame) {
62   SetMonoFrame(data, frame->sample_rate_hz_, frame);
63 }
64 
65 // Sets the signal value to increase by |left| and |right| with every sample in
66 // each channel respectively.
SetStereoFrame(float left,float right,int sample_rate_hz,AudioFrame * frame)67 void SetStereoFrame(float left,
68                     float right,
69                     int sample_rate_hz,
70                     AudioFrame* frame) {
71   frame->Mute();
72   frame->num_channels_ = 2;
73   frame->sample_rate_hz_ = sample_rate_hz;
74   frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100);
75   int16_t* frame_data = frame->mutable_data();
76   for (size_t i = 0; i < frame->samples_per_channel_; i++) {
77     frame_data[i * 2] = static_cast<int16_t>(left * i);
78     frame_data[i * 2 + 1] = static_cast<int16_t>(right * i);
79   }
80 }
81 
82 // Keep the existing sample rate.
SetStereoFrame(float left,float right,AudioFrame * frame)83 void SetStereoFrame(float left, float right, AudioFrame* frame) {
84   SetStereoFrame(left, right, frame->sample_rate_hz_, frame);
85 }
86 
87 // Sets the signal value to increase by |ch1|, |ch2|, |ch3|, |ch4| with every
88 // sample in each channel respectively.
SetQuadFrame(float ch1,float ch2,float ch3,float ch4,int sample_rate_hz,AudioFrame * frame)89 void SetQuadFrame(float ch1,
90                   float ch2,
91                   float ch3,
92                   float ch4,
93                   int sample_rate_hz,
94                   AudioFrame* frame) {
95   frame->Mute();
96   frame->num_channels_ = 4;
97   frame->sample_rate_hz_ = sample_rate_hz;
98   frame->samples_per_channel_ = rtc::CheckedDivExact(sample_rate_hz, 100);
99   int16_t* frame_data = frame->mutable_data();
100   for (size_t i = 0; i < frame->samples_per_channel_; i++) {
101     frame_data[i * 4] = static_cast<int16_t>(ch1 * i);
102     frame_data[i * 4 + 1] = static_cast<int16_t>(ch2 * i);
103     frame_data[i * 4 + 2] = static_cast<int16_t>(ch3 * i);
104     frame_data[i * 4 + 3] = static_cast<int16_t>(ch4 * i);
105   }
106 }
107 
VerifyParams(const AudioFrame & ref_frame,const AudioFrame & test_frame)108 void VerifyParams(const AudioFrame& ref_frame, const AudioFrame& test_frame) {
109   EXPECT_EQ(ref_frame.num_channels_, test_frame.num_channels_);
110   EXPECT_EQ(ref_frame.samples_per_channel_, test_frame.samples_per_channel_);
111   EXPECT_EQ(ref_frame.sample_rate_hz_, test_frame.sample_rate_hz_);
112 }
113 
114 // Computes the best SNR based on the error between |ref_frame| and
115 // |test_frame|. It allows for up to a |max_delay| in samples between the
116 // signals to compensate for the resampling delay.
ComputeSNR(const AudioFrame & ref_frame,const AudioFrame & test_frame,size_t max_delay)117 float ComputeSNR(const AudioFrame& ref_frame,
118                  const AudioFrame& test_frame,
119                  size_t max_delay) {
120   VerifyParams(ref_frame, test_frame);
121   float best_snr = 0;
122   size_t best_delay = 0;
123   for (size_t delay = 0; delay <= max_delay; delay++) {
124     float mse = 0;
125     float variance = 0;
126     const int16_t* ref_frame_data = ref_frame.data();
127     const int16_t* test_frame_data = test_frame.data();
128     for (size_t i = 0;
129          i < ref_frame.samples_per_channel_ * ref_frame.num_channels_ - delay;
130          i++) {
131       int error = ref_frame_data[i] - test_frame_data[i + delay];
132       mse += error * error;
133       variance += ref_frame_data[i] * ref_frame_data[i];
134     }
135     float snr = 100;  // We assign 100 dB to the zero-error case.
136     if (mse > 0)
137       snr = 10 * std::log10(variance / mse);
138     if (snr > best_snr) {
139       best_snr = snr;
140       best_delay = delay;
141     }
142   }
143   printf("SNR=%.1f dB at delay=%" RTC_PRIuS "\n", best_snr, best_delay);
144   return best_snr;
145 }
146 
VerifyFramesAreEqual(const AudioFrame & ref_frame,const AudioFrame & test_frame)147 void VerifyFramesAreEqual(const AudioFrame& ref_frame,
148                           const AudioFrame& test_frame) {
149   VerifyParams(ref_frame, test_frame);
150   const int16_t* ref_frame_data = ref_frame.data();
151   const int16_t* test_frame_data = test_frame.data();
152   for (size_t i = 0;
153        i < ref_frame.samples_per_channel_ * ref_frame.num_channels_; i++) {
154     EXPECT_EQ(ref_frame_data[i], test_frame_data[i]);
155   }
156 }
157 
RunResampleTest(int src_channels,int src_sample_rate_hz,int dst_channels,int dst_sample_rate_hz)158 void UtilityTest::RunResampleTest(int src_channels,
159                                   int src_sample_rate_hz,
160                                   int dst_channels,
161                                   int dst_sample_rate_hz) {
162   PushResampler<int16_t> resampler;  // Create a new one with every test.
163   const int16_t kSrcCh1 = 30;  // Shouldn't overflow for any used sample rate.
164   const int16_t kSrcCh2 = 15;
165   const int16_t kSrcCh3 = 22;
166   const int16_t kSrcCh4 = 8;
167   const float resampling_factor =
168       (1.0 * src_sample_rate_hz) / dst_sample_rate_hz;
169   const float dst_ch1 = resampling_factor * kSrcCh1;
170   const float dst_ch2 = resampling_factor * kSrcCh2;
171   const float dst_ch3 = resampling_factor * kSrcCh3;
172   const float dst_ch4 = resampling_factor * kSrcCh4;
173   const float dst_stereo_to_mono = (dst_ch1 + dst_ch2) / 2;
174   const float dst_quad_to_mono = (dst_ch1 + dst_ch2 + dst_ch3 + dst_ch4) / 4;
175   const float dst_quad_to_stereo_ch1 = (dst_ch1 + dst_ch2) / 2;
176   const float dst_quad_to_stereo_ch2 = (dst_ch3 + dst_ch4) / 2;
177   if (src_channels == 1)
178     SetMonoFrame(kSrcCh1, src_sample_rate_hz, &src_frame_);
179   else if (src_channels == 2)
180     SetStereoFrame(kSrcCh1, kSrcCh2, src_sample_rate_hz, &src_frame_);
181   else
182     SetQuadFrame(kSrcCh1, kSrcCh2, kSrcCh3, kSrcCh4, src_sample_rate_hz,
183                  &src_frame_);
184 
185   if (dst_channels == 1) {
186     SetMonoFrame(0, dst_sample_rate_hz, &dst_frame_);
187     if (src_channels == 1)
188       SetMonoFrame(dst_ch1, dst_sample_rate_hz, &golden_frame_);
189     else if (src_channels == 2)
190       SetMonoFrame(dst_stereo_to_mono, dst_sample_rate_hz, &golden_frame_);
191     else
192       SetMonoFrame(dst_quad_to_mono, dst_sample_rate_hz, &golden_frame_);
193   } else {
194     SetStereoFrame(0, 0, dst_sample_rate_hz, &dst_frame_);
195     if (src_channels == 1)
196       SetStereoFrame(dst_ch1, dst_ch1, dst_sample_rate_hz, &golden_frame_);
197     else if (src_channels == 2)
198       SetStereoFrame(dst_ch1, dst_ch2, dst_sample_rate_hz, &golden_frame_);
199     else
200       SetStereoFrame(dst_quad_to_stereo_ch1, dst_quad_to_stereo_ch2,
201                      dst_sample_rate_hz, &golden_frame_);
202   }
203 
204   // The sinc resampler has a known delay, which we compute here. Multiplying by
205   // two gives us a crude maximum for any resampling, as the old resampler
206   // typically (but not always) has lower delay.
207   static const size_t kInputKernelDelaySamples = 16;
208   const size_t max_delay = static_cast<size_t>(
209       static_cast<double>(dst_sample_rate_hz) / src_sample_rate_hz *
210       kInputKernelDelaySamples * dst_channels * 2);
211   printf("(%d, %d Hz) -> (%d, %d Hz) ",  // SNR reported on the same line later.
212          src_channels, src_sample_rate_hz, dst_channels, dst_sample_rate_hz);
213   RemixAndResample(src_frame_, &resampler, &dst_frame_);
214 
215   if (src_sample_rate_hz == 96000 && dst_sample_rate_hz == 8000) {
216     // The sinc resampler gives poor SNR at this extreme conversion, but we
217     // expect to see this rarely in practice.
218     EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 14.0f);
219   } else {
220     EXPECT_GT(ComputeSNR(golden_frame_, dst_frame_, max_delay), 46.0f);
221   }
222 }
223 
TEST_F(UtilityTest,RemixAndResampleCopyFrameSucceeds)224 TEST_F(UtilityTest, RemixAndResampleCopyFrameSucceeds) {
225   // Stereo -> stereo.
226   SetStereoFrame(10, 10, &src_frame_);
227   SetStereoFrame(0, 0, &dst_frame_);
228   RemixAndResample(src_frame_, &resampler_, &dst_frame_);
229   VerifyFramesAreEqual(src_frame_, dst_frame_);
230 
231   // Mono -> mono.
232   SetMonoFrame(20, &src_frame_);
233   SetMonoFrame(0, &dst_frame_);
234   RemixAndResample(src_frame_, &resampler_, &dst_frame_);
235   VerifyFramesAreEqual(src_frame_, dst_frame_);
236 }
237 
TEST_F(UtilityTest,RemixAndResampleMixingOnlySucceeds)238 TEST_F(UtilityTest, RemixAndResampleMixingOnlySucceeds) {
239   // Stereo -> mono.
240   SetStereoFrame(0, 0, &dst_frame_);
241   SetMonoFrame(10, &src_frame_);
242   SetStereoFrame(10, 10, &golden_frame_);
243   RemixAndResample(src_frame_, &resampler_, &dst_frame_);
244   VerifyFramesAreEqual(dst_frame_, golden_frame_);
245 
246   // Mono -> stereo.
247   SetMonoFrame(0, &dst_frame_);
248   SetStereoFrame(10, 20, &src_frame_);
249   SetMonoFrame(15, &golden_frame_);
250   RemixAndResample(src_frame_, &resampler_, &dst_frame_);
251   VerifyFramesAreEqual(golden_frame_, dst_frame_);
252 }
253 
TEST_F(UtilityTest,RemixAndResampleSucceeds)254 TEST_F(UtilityTest, RemixAndResampleSucceeds) {
255   const int kSampleRates[] = {8000, 16000, 32000, 44100, 48000, 96000};
256   const int kSampleRatesSize = arraysize(kSampleRates);
257   const int kSrcChannels[] = {1, 2, 4};
258   const int kSrcChannelsSize = arraysize(kSrcChannels);
259   const int kDstChannels[] = {1, 2};
260   const int kDstChannelsSize = arraysize(kDstChannels);
261 
262   for (int src_rate = 0; src_rate < kSampleRatesSize; src_rate++) {
263     for (int dst_rate = 0; dst_rate < kSampleRatesSize; dst_rate++) {
264       for (int src_channel = 0; src_channel < kSrcChannelsSize; src_channel++) {
265         for (int dst_channel = 0; dst_channel < kDstChannelsSize;
266              dst_channel++) {
267           RunResampleTest(kSrcChannels[src_channel], kSampleRates[src_rate],
268                           kDstChannels[dst_channel], kSampleRates[dst_rate]);
269         }
270       }
271     }
272   }
273 }
274 
275 }  // namespace
276 }  // namespace voe
277 }  // namespace webrtc
278