1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "media/base/multi_channel_resampler.h"
6 
7 #include <cmath>
8 #include <memory>
9 
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "base/logging.h"
13 #include "base/macros.h"
14 #include "media/base/audio_bus.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 
17 namespace media {
18 
19 // Just test a basic resampling case.  The SincResampler unit test will take
20 // care of accuracy testing; we just need to check that multichannel works as
21 // expected within some tolerance.
22 static const float kScaleFactor = 192000.0f / 44100.0f;
23 
24 // Simulate large and small sample requests used by the different audio paths.
25 static const int kHighLatencySize = 8192;
26 // Low latency buffers show a larger error than high latency ones.  Which makes
27 // sense since each error represents a larger portion of the total request.
28 static const int kLowLatencySize = 128;
29 
30 // Test fill value.
31 static const float kFillValue = 0.1f;
32 
33 // Chosen arbitrarily based on what each resampler reported during testing.
34 static const double kLowLatencyMaxRMSError = 0.0036;
35 static const double kLowLatencyMaxError = 0.04;
36 static const double kHighLatencyMaxRMSError = 0.0036;
37 static const double kHighLatencyMaxError = 0.04;
38 
39 class MultiChannelResamplerTest
40     : public testing::TestWithParam<int> {
41  public:
MultiChannelResamplerTest()42   MultiChannelResamplerTest()
43       : last_frame_delay_(-1) {
44   }
45   virtual ~MultiChannelResamplerTest() = default;
46 
InitializeAudioData(int channels,int frames)47   void InitializeAudioData(int channels, int frames) {
48     frames_ = frames;
49     audio_bus_ = AudioBus::Create(channels, frames);
50   }
51 
52   // MultiChannelResampler::MultiChannelAudioSourceProvider implementation, just
53   // fills the provided audio_data with |kFillValue|.
ProvideInput(int frame_delay,AudioBus * audio_bus)54   virtual void ProvideInput(int frame_delay, AudioBus* audio_bus) {
55     EXPECT_GE(frame_delay, last_frame_delay_);
56     last_frame_delay_ = frame_delay;
57 
58     float fill_value = fill_junk_values_ ? (1 / kFillValue) : kFillValue;
59     EXPECT_EQ(audio_bus->channels(), audio_bus_->channels());
60     for (int i = 0; i < audio_bus->channels(); ++i)
61       for (int j = 0; j < audio_bus->frames(); ++j)
62         audio_bus->channel(i)[j] = fill_value;
63   }
64 
MultiChannelTest(int channels,int frames,double expected_max_rms_error,double expected_max_error)65   void MultiChannelTest(int channels, int frames, double expected_max_rms_error,
66                         double expected_max_error) {
67     InitializeAudioData(channels, frames);
68     MultiChannelResampler resampler(
69         channels, kScaleFactor, SincResampler::kDefaultRequestSize,
70         base::BindRepeating(&MultiChannelResamplerTest::ProvideInput,
71                             base::Unretained(this)));
72 
73     // First prime the resampler with some junk data, so we can verify Flush().
74     fill_junk_values_ = true;
75     resampler.Resample(1, audio_bus_.get());
76     resampler.Flush();
77     fill_junk_values_ = false;
78 
79     // The last frame delay should be strictly less than the total frame count.
80     EXPECT_LT(last_frame_delay_, audio_bus_->frames());
81     last_frame_delay_ = -1;
82 
83     // If Flush() didn't work, the rest of the tests will fail.
84     resampler.Resample(frames, audio_bus_.get());
85     TestValues(expected_max_rms_error, expected_max_error);
86   }
87 
HighLatencyTest(int channels)88   void HighLatencyTest(int channels) {
89     MultiChannelTest(channels, kHighLatencySize, kHighLatencyMaxRMSError,
90                      kHighLatencyMaxError);
91   }
92 
LowLatencyTest(int channels)93   void LowLatencyTest(int channels) {
94     MultiChannelTest(channels, kLowLatencySize, kLowLatencyMaxRMSError,
95                      kLowLatencyMaxError);
96   }
97 
TestValues(double expected_max_rms_error,double expected_max_error)98   void TestValues(double expected_max_rms_error, double expected_max_error ) {
99     // Calculate Root-Mean-Square-Error for the resampling.
100     double max_error = 0.0;
101     double sum_of_squares = 0.0;
102     for (int i = 0; i < audio_bus_->channels(); ++i) {
103       for (int j = 0; j < frames_; ++j) {
104         // Ensure all values are accounted for.
105         ASSERT_NE(audio_bus_->channel(i)[j], 0);
106 
107         double error = fabs(audio_bus_->channel(i)[j] - kFillValue);
108         max_error = std::max(max_error, error);
109         sum_of_squares += error * error;
110       }
111     }
112 
113     double rms_error = sqrt(
114         sum_of_squares / (frames_ * audio_bus_->channels()));
115 
116     EXPECT_LE(rms_error, expected_max_rms_error);
117     EXPECT_LE(max_error, expected_max_error);
118   }
119 
120  protected:
121   int frames_;
122   bool fill_junk_values_;
123   std::unique_ptr<AudioBus> audio_bus_;
124   int last_frame_delay_;
125 
126   DISALLOW_COPY_AND_ASSIGN(MultiChannelResamplerTest);
127 };
128 
TEST_P(MultiChannelResamplerTest,HighLatency)129 TEST_P(MultiChannelResamplerTest, HighLatency) {
130   HighLatencyTest(GetParam());
131 }
132 
TEST_P(MultiChannelResamplerTest,LowLatency)133 TEST_P(MultiChannelResamplerTest, LowLatency) {
134   LowLatencyTest(GetParam());
135 }
136 
137 // Test common channel layouts: mono, stereo, 5.1, 7.1.
138 INSTANTIATE_TEST_SUITE_P(MultiChannelResamplerTest,
139                          MultiChannelResamplerTest,
140                          testing::Values(1, 2, 6, 8));
141 
142 }  // namespace media
143