• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <audio_utils/Balance.h>
18 #include <gtest/gtest.h>
19 #include <vector>
20 
TEST(audio_utils_balance,stereo)21 TEST(audio_utils_balance, stereo) {
22   // disable ramping so we can check single frame processing.
23   android::audio_utils::Balance balance(false /* ramp */);
24   ASSERT_EQ(false, balance.getRamp());
25 
26   balance.setChannelMask(AUDIO_CHANNEL_OUT_STEREO);
27   std::vector<float> buffer = {1.f, -1.f};
28 
29   // balance of 0 is no change.
30   ASSERT_EQ(0.f, balance.getBalance());
31   balance.process(buffer.data(), 1 /* frames */);
32   ASSERT_EQ((std::vector<float>{1.f, -1.f}), buffer);
33 
34   // balance of 1.f is right.
35   balance.setBalance(1.f);
36   ASSERT_EQ(1.f, balance.getBalance());
37   balance.process(buffer.data(), 1 /* frames */);
38   ASSERT_EQ((std::vector<float>{0.f, -1.f}), buffer);
39 
40   // balance of -1.f is left.
41   buffer = {1.f, -1.f};
42   balance.setBalance(-1.f); // to left
43   ASSERT_EQ(-1.f, balance.getBalance());
44   balance.process(buffer.data(), 1 /* frames */);
45   ASSERT_EQ((std::vector<float>{1.f, 0.f}), buffer);
46 }
47 
48 TEST(audio_utils_balance, 7point1) {
49   // disable ramping so we can check single frame processing.
50   android::audio_utils::Balance balance(false /* ramp */);
51   ASSERT_EQ(false, balance.getRamp());
52 
53   balance.setChannelMask(AUDIO_CHANNEL_OUT_7POINT1);
54   // FL, FR, FC, LFE, BL, BR, SL, SR
55   std::vector<float> buffer = {1.f, -1.f, 0.5f, 0.25f, 0.75f, 0.75f, 0.125f, 0.125f};
56 
57   // balance of 0 is no change.
58   ASSERT_EQ(0.f, balance.getBalance());
59   balance.process(buffer.data(), 1 /* frames */);
60   ASSERT_EQ((std::vector<float>{1.f, -1.f, 0.5f, 0.25f, 0.75f, 0.75f, 0.125f, 0.125f}), buffer);
61 
62   // balance of 1.f is right.
63   balance.setBalance(1.f);
64   balance.process(buffer.data(), 1 /* frames */);
65   ASSERT_EQ((std::vector<float>{0.f, -1.f, 0.5f, 0.25f, 0.f, 0.75f, 0.f, 0.125f}), buffer);
66 
67   // balance of -1.f is left.
68   buffer = {1.f, -1.f, 0.5f, 0.25f, 0.75f, 0.75f, 0.125f, 0.125f};
69   balance.setBalance(-1.f); // to left
70   ASSERT_EQ(-1.f, balance.getBalance());
71   balance.process(buffer.data(), 1 /* frames */);
72   ASSERT_EQ((std::vector<float>{1.f, 0.f, 0.5f, 0.25f, 0.75f, 0.f, 0.125f, 0.f}), buffer);
73 }
74 
TEST(audio_utils_balance,lfe)75 TEST(audio_utils_balance, lfe) {
76   // disable ramping so we can check single frame processing.
77   android::audio_utils::Balance balance(false /* ramp */);
78   ASSERT_EQ(false, balance.getRamp());
79   std::vector<float> buffer = {1.f, -1.f};
80 
81   // NOTE: single channel falls under mono exception (we ignore balance)
82   // so we pair with another "center" channel.
83   // LFE by itself is considered "center".
84   for (auto channelMask : {
85         (AUDIO_CHANNEL_OUT_FRONT_CENTER | AUDIO_CHANNEL_OUT_LOW_FREQUENCY),
86         (AUDIO_CHANNEL_OUT_FRONT_CENTER | AUDIO_CHANNEL_OUT_LOW_FREQUENCY_2),
87       }) {
88     balance.setChannelMask((audio_channel_mask_t)channelMask);
89 
90       // balance of 0 is no change.
91       balance.setBalance(0.f);
92       balance.process(buffer.data(), 1 /* frames */);
93       ASSERT_EQ((std::vector<float>{1.f, -1.f}), buffer);
94 
95       // balance of 1.f is right. (center unaffected)
96       balance.setBalance(1.f);
97       balance.process(buffer.data(), 1 /* frames */);
98       ASSERT_EQ((std::vector<float>{1.f, -1.f}), buffer);
99 
100       // balance of -1.f is left. (center unaffected)
101       balance.setBalance(-1.f);
102       balance.process(buffer.data(), 1 /* frames */);
103       ASSERT_EQ((std::vector<float>{1.f, -1.f}), buffer);
104   }
105 
106   // If both LFE and LFE2 are present, we assume L/R.
107   balance.setChannelMask((audio_channel_mask_t)
108       (AUDIO_CHANNEL_OUT_LOW_FREQUENCY | AUDIO_CHANNEL_OUT_LOW_FREQUENCY_2));
109   // balance of 0 is no change.
110   balance.setBalance(0.f); // to left
111   balance.process(buffer.data(), 1 /* frames */);
112   ASSERT_EQ((std::vector<float>{1.f, -1.f}), buffer);
113 
114   // balance of 1.f is right.
115   balance.setBalance(1.f);
116   balance.process(buffer.data(), 1 /* frames */);
117   ASSERT_EQ((std::vector<float>{0.f, -1.f}), buffer);
118 
119   // balance of -1.f is left.
120   buffer = {1.f, -1.f};
121   balance.setBalance(-1.f); // to left
122   balance.process(buffer.data(), 1 /* frames */);
123   ASSERT_EQ((std::vector<float>{1.f, 0.f}), buffer);
124 }
125